Skip to content

Commit ed63f6d

Browse files
committed
[GR-14535] GitHub issue #61: rep.int with zero length value argument.
PullRequest: fastr/1965
2 parents 60c5ec1 + 2b17f53 commit ed63f6d

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.0 RC 15
2+
3+
Bug fixes:
4+
5+
* `rep.int` with value argument of length 0 just returns the value argument
6+
17
# 1.0 RC 14
28

39
* all FastR specific options (NOT those GNU-R compatible like `--save`) are experimental except for `--R.PrintErrorStacktracesToFile`,

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
package com.oracle.truffle.r.nodes.builtin.base;
2424

2525
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.abstractVectorValue;
26-
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.notEmpty;
2726
import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.typeName;
2827
import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE;
2928
import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.INTERNAL;
@@ -48,19 +47,24 @@
4847
public abstract class RepeatInternal extends RBuiltinNode.Arg2 {
4948

5049
private final ConditionProfile timesOneProfile = ConditionProfile.createBinaryProfile();
50+
private final ConditionProfile valueLen0Profile = ConditionProfile.createBinaryProfile();
5151

5252
static {
5353
Casts casts = new Casts(RepeatInternal.class);
5454
casts.arg("x").mustBe(abstractVectorValue(), RError.Message.ATTEMPT_TO_REPLICATE, typeName());
55-
casts.arg("times").defaultError(RError.Message.INVALID_TYPE, typeName(), "times", "vector").mustBe(abstractVectorValue()).asIntegerVector().mustBe(notEmpty(),
56-
RError.Message.INVALID_VALUE, "times");
55+
casts.arg("times").defaultError(RError.Message.INVALID_TYPE, typeName(), "times", "vector").mustBe(abstractVectorValue()).asIntegerVector();
5756
}
5857

5958
private RAbstractVector performRep(RAbstractVector value, RAbstractIntVector times, VectorFactory factory, VectorAccess valueAccess, VectorAccess timesAccess, VectorAccess resultAccess) {
6059
try (SequentialIterator valueIter = valueAccess.access(value); SequentialIterator timesIter = timesAccess.access(times)) {
6160
int valueLength = valueAccess.getLength(valueIter);
6261
int timesLength = timesAccess.getLength(timesIter);
63-
62+
if (valueLen0Profile.profile(valueLength == 0)) {
63+
return factory.createVector(valueAccess.getType(), 0, false);
64+
}
65+
if (timesLength == 0) {
66+
throw error(RError.Message.INVALID_VALUE, "times");
67+
}
6468
RVector<?> result;
6569
if (timesOneProfile.profile(timesLength == 1)) {
6670
timesAccess.next(timesIter);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59547,6 +59547,18 @@ Error in rep_len(function() 42, 7) : attempt to replicate non-vector
5954759547
#x<-as.raw(16); y<-as.raw(5); rep_len(c(x, y), 5)
5954859548
[1] 10 05 10 05 10
5954959549

59550+
##com.oracle.truffle.r.test.builtins.TestBuiltin_repint.testRepInt#
59551+
#rep.int(character(0), numeric(0))
59552+
character(0)
59553+
59554+
##com.oracle.truffle.r.test.builtins.TestBuiltin_repint.testRepInt#
59555+
#rep.int(integer(0), character(0))
59556+
integer(0)
59557+
59558+
##com.oracle.truffle.r.test.builtins.TestBuiltin_repint.testRepInt#
59559+
#rep.int(integer(0), numeric(0))
59560+
integer(0)
59561+
5955059562
##com.oracle.truffle.r.test.builtins.TestBuiltin_repint.testRepInt#
5955159563
#{ rep.int("a",3) }
5955259564
[1] "a" "a" "a"

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1515
*
1616
* Copyright (c) 2012-2014, Purdue University
17-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates
17+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates
1818
*
1919
* All rights reserved.
2020
*/
@@ -188,6 +188,10 @@ public void testRepInt() {
188188
assertEval("{ rep.int(7, -4) }");
189189
assertEval("{ rep.int(c(7,1), c(1,-4)) }");
190190
assertEval("{ rep.int(c(7,1), c(1,4,5)) }");
191+
192+
assertEval("rep.int(integer(0), character(0))");
193+
assertEval("rep.int(integer(0), numeric(0))");
194+
assertEval("rep.int(character(0), numeric(0))");
191195
}
192196

193197
@Test

0 commit comments

Comments
 (0)