Skip to content

Commit 7e399e5

Browse files
committed
UpdateStorageMode: support NULL and show proper error messages
1 parent 3d34138 commit 7e399e5

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/UpdateStorageMode.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.oracle.truffle.api.dsl.Cached;
2929
import com.oracle.truffle.api.dsl.Specialization;
3030
import com.oracle.truffle.api.object.DynamicObject;
31+
import com.oracle.truffle.api.profiles.ValueProfile;
3132
import com.oracle.truffle.r.nodes.attributes.ArrayAttributeNode;
3233
import com.oracle.truffle.r.nodes.attributes.SetAttributeNode;
3334
import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctions.SetClassAttributeNode;
@@ -62,13 +63,23 @@ public abstract class UpdateStorageMode extends RBuiltinNode.Arg2 {
6263
}
6364

6465
@Specialization
66+
protected Object updateNull(@SuppressWarnings("unused") RNull x, String value,
67+
@Cached("createEqualityProfile()") ValueProfile valueProfile) {
68+
RType mode = typeFromMode.execute(valueProfile.profile(value));
69+
checkMode(mode);
70+
return mode.create(0, false);
71+
}
72+
73+
protected static boolean notNull(Object x) {
74+
return RNull.instance != x;
75+
}
76+
77+
@Specialization(guards = "notNull(x)")
6578
protected Object update(Object x, String value,
6679
@Cached("create()") ArrayAttributeNode attrAttrAccess,
6780
@Cached("create()") SetAttributeNode setAttrNode) {
6881
RType mode = typeFromMode.execute(value);
69-
if (mode == RType.DefunctReal || mode == RType.DefunctSingle) {
70-
throw error(RError.Message.USE_DEFUNCT, mode.getName(), mode == RType.DefunctSingle ? "mode<-" : "double");
71-
}
82+
checkMode(mode);
7283
initTypeOfNode();
7384
RType typeX = typeof.execute(x);
7485
if (typeX == mode) {
@@ -115,6 +126,12 @@ protected Object update(Object x, String value,
115126
throw error(RError.Message.INVALID_UNNAMED_VALUE);
116127
}
117128

129+
private void checkMode(RType mode) {
130+
if (mode == RType.DefunctReal || mode == RType.DefunctSingle) {
131+
throw error(RError.Message.USE_DEFUNCT, mode.getName(), mode == RType.DefunctSingle ? "mode<-" : "double");
132+
}
133+
}
134+
118135
private void initCastTypeNode() {
119136
if (castTypeNode == null) {
120137
CompilerDirectives.transferToInterpreterAndInvalidate();

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/RError.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ public enum Message {
574574
ARGUMENT_NOT_VECTOR("argument %d is not a vector"),
575575
CANNOT_COERCE("cannot coerce type '%s' to vector of type '%s'"),
576576
CANNOT_COERCE_RFFI("(%s) object cannot be coerced to type '%s'"),
577+
CANNOT_COERCE_QUOTED("'%s' object cannot be coerced to type '%s'"),
577578
ARGUMENT_ONLY_FIRST("argument '%s' has length > 1 and only the first element will be used"),
578579
ARGUMENT_ONLY_FIRST_1("only the first element of '%s' argument used"),
579580
ARGUMENT_WRONG_LENGTH("wrong length for argument"),

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74529,6 +74529,24 @@ can write "no"
7452974529
#argv <- structure(list(x = structure(c(1, 0.666666666666667, 0.333333333333333, 0, -0.333333333333333, -0.666666666666667, -1, -1.33333333333333, -1.66666666666667, 1.5, 1, 0.5, 0, -0.5, -1, -1.5, -2, -2.5, 3, 2, 1, 0, -1, -2, -3, -4, -5, -Inf, -Inf, -Inf, NaN, Inf, Inf, Inf, Inf, Inf, -3, -2, -1, 0, 1, 2, 3, 4, 5, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, -1, -0.666666666666667, -0.333333333333333, 0, 0.333333333333333, 0.666666666666667, 1, 1.33333333333333, 1.66666666666667, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1, 1.25, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1), .Dim = c(9L, 9L))), .Names = 'x');do.call('storage.mode', argv)
7453074530
[1] "double"
7453174531

74532+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testErrors#
74533+
#{ x <- 1; storage.mode(x) <- 42.5; }
74534+
Error in storage.mode(x) <- 42.5 :
74535+
'value' must be non-null character string
74536+
74537+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testErrors#Output.IgnoreErrorMessage#
74538+
#{ x <- as.pairlist(1); storage.mode(x) <- 'integer'; }
74539+
74540+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testErrors#Output.IgnoreErrorMessage#
74541+
#{ x <- new.env(); storage.mode(x) <- 'integer'; }
74542+
Error in storage.mode(x) <- "integer" :
74543+
environments cannot be coerced to other types
74544+
74545+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testErrors#Output.IgnoreErrorMessage#
74546+
#{ x <- quote(a+b); storage.mode(x) <- 'integer'; }
74547+
Error in storage.mode(x) <- "integer" :
74548+
'pairlist' object cannot be coerced to type 'integer'
74549+
7453274550
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testUpdateStorageMode#
7453374551
#{ x <- c(1L, 2L); dim(x)<-c(1,2); storage.mode(x) <- "double"; x}
7453474552
[,1] [,2]
@@ -74541,6 +74559,30 @@ can write "no"
7454174559
#{ x <- c(1L, 2L); storage.mode(x) <- "not.double"}
7454274560
Error in storage.mode(x) <- "not.double" : invalid value
7454374561

74562+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testUpdateStorageModeWithNull#
74563+
#{ x <- NULL; storage.mode(x) <- 'character'; x }
74564+
character(0)
74565+
74566+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testUpdateStorageModeWithNull#
74567+
#{ x <- NULL; storage.mode(x) <- 'double'; x }
74568+
numeric(0)
74569+
74570+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testUpdateStorageModeWithNull#
74571+
#{ x <- NULL; storage.mode(x) <- 'integer'; x }
74572+
integer(0)
74573+
74574+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testUpdateStorageModeWithNull#
74575+
#{ x <- NULL; storage.mode(x) <- 'logical'; x }
74576+
logical(0)
74577+
74578+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testUpdateStorageModeWithNull#
74579+
#{ x <- NULL; storage.mode(x) <- 'numeric'; x }
74580+
numeric(0)
74581+
74582+
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.testUpdateStorageModeWithNull#
74583+
#{ x <- NULL; storage.mode(x) <- 'raw'; x }
74584+
raw(0)
74585+
7454474586
##com.oracle.truffle.r.test.builtins.TestBuiltin_storagemodeassign.teststoragemodeassign1#
7454574587
#argv <- list(structure(c(4L, 5L, 10L, 9L, 13L, 13L, 12L, 15L, 18L, 19L, 22L, 27L, 28L, 24L, 27L, 28L, 30L, 31L, 32L, 36L, 28L, 32L, 35L, 33L, 38L, 41L, 38L, 38L, 32L, 34L, 44L, 44L, 44L, 46L, 47L, 49L, 50L, 53L, 52L, 55L, 54L, 60L, 63L, 86L, 85L, 85L, 78L, 74L, 97L, 98L, 98L, 99L, 99L, 101L, 108L, 110L, 108L, 111L, 115L, 117L, 70L, 77L, 83L, 61L, 69L, 78L, 66L, 58L, 64L, 69L, 66L, 61L, 76L, 72L, 64L, 53L, 63L, 59L, 77L, 49L, 69L, 88L, 75L, 61L, 65L, 74L, 72L, 76L, 58L, 55L, 60L, 52L, 60L, 61L, 72L, 147L, 149L, 153L, 154L, 151L, 150L, 145L, 143L, 143L, 141L, 156L, 149L, 143L, 142L, 149L, 152L, 142L, 144L, 152L, 155L, 124L, 136L, 139L, 132L, 115L, 96L, 94L, 96L, 122L, 116L, 124L, 119L, 128L, 115L, 111L, 111L, 116L, 126L, 117L, 115L, 4L, 12L, 21L, 15L, 15L, 16L, 18L, 13L, 20L, 21L, 23L, 25L, 27L, 31L, 30L), .Dim = c(75L, 2L), .Dimnames = list(c('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75'), c('x', 'y'))), value = 'double');`storage.mode<-`(argv[[1]],argv[[2]]);
7454674588
x y

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_storagemodeassign.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,16 @@ public void testUpdateStorageMode() {
5858
assertEval("{ x <- c(1L, 2L); storage.mode(x) <- \"not.double\"}");
5959
assertEval("{ x <- c(1L, 2L); dim(x)<-c(1,2); storage.mode(x) <- \"double\"; x}");
6060
}
61+
62+
@Test
63+
public void testUpdateStorageModeWithNull() {
64+
assertEval(template("{ x <- NULL; storage.mode(x) <- '%0'; x }", new String[]{"integer", "character", "logical", "double", "numeric", "raw"}));
65+
}
66+
67+
@Test
68+
public void testErrors() {
69+
// TODO: need to fix cast pipeline and specializations in UpdateStorageMode
70+
assertEval(Ignored.ImplementationError, template("{ x <- %0; storage.mode(x) <- 'integer'; }", new String[]{"new.env()", "quote(a+b)", "as.pairlist(1)"}));
71+
assertEval("{ x <- 1; storage.mode(x) <- 42.5; }");
72+
}
6173
}

0 commit comments

Comments
 (0)