Skip to content

Commit f27ae12

Browse files
committed
Fix: descending sequences were not properly handled in match
1 parent 26cf625 commit f27ae12

File tree

8 files changed

+42
-9
lines changed

8 files changed

+42
-9
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 16
2+
3+
Bug fixes:
4+
5+
* `match` worked incorrectly with descending sequences (e.g., `3:1`)
6+
17
# 1.0 RC 15
28

39
* `ActiveBinding` objects do not support the `UNBOX` message anymore

com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/fastpaths/IsElementFastPath.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -85,7 +85,7 @@ protected Byte iselementOne(RAbstractDoubleVector el, RAbstractDoubleVector set,
8585
return RRuntime.LOGICAL_FALSE;
8686
}
8787

88-
@Specialization(guards = "el.getLength() == 1")
88+
@Specialization(guards = {"el.getLength() == 1", "set.getStride() >= 0"})
8989
protected Byte isElementOneSequence(RAbstractDoubleVector el, RIntSequence set,
9090
@Cached("createBinaryProfile()") ConditionProfile profile) {
9191
double element = el.getDataAt(0);

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RDoubleSequence.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -69,7 +69,8 @@ public Object getStrideObject() {
6969
return getStride();
7070
}
7171

72-
public double getEnd() {
72+
// NOTE: it does not hold that getStart() <= getEnd()!
73+
private double getEnd() {
7374
return start + (getLength() - 1) * stride;
7475
}
7576

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RIntSequence.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -88,7 +88,9 @@ public int getStride() {
8888
}
8989

9090
public int getIndexFor(int element) {
91-
if (element < getStart() || element > getEnd()) {
91+
int first = Math.min(getStart(), getEnd());
92+
int last = Math.max(getStart(), getEnd());
93+
if (element < first || element > last) {
9294
return -1;
9395
}
9496
if ((element - getStart()) % getStride() == 0) {
@@ -117,6 +119,7 @@ public String toString() {
117119
return "[" + start + " - " + getEnd() + "]";
118120
}
119121

122+
// NOTE: it does not hold that getStart() <= getEnd()!
120123
public int getEnd() {
121124
return start + (getLength() - 1) * stride;
122125
}

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RStringSequence.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -127,7 +127,9 @@ public int getIndexFor(String element) {
127127
String c = element.substring(prefix.length(), element.length() - suffix.length());
128128
try {
129129
int current = Integer.parseInt(c);
130-
if (current < getStart() || current > getEnd()) {
130+
int first = Math.min(getStart(), getEnd());
131+
int last = Math.max(getStart(), getEnd());
132+
if (current < first || current > last) {
131133
return -1;
132134
}
133135
if ((current - getStart()) % getStride() == 0) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43791,6 +43791,18 @@ integer(0)
4379143791
#{ match(c(T,F), c('TRUE', 'FALSE')) }
4379243792
[1] 1 2
4379343793

43794+
##com.oracle.truffle.r.test.builtins.TestBuiltin_match.testMatchInSequence#
43795+
#match(1:3, 3:1)
43796+
[1] 3 2 1
43797+
43798+
##com.oracle.truffle.r.test.builtins.TestBuiltin_match.testMatchInSequence#
43799+
#match(1:3, numeric(0))
43800+
[1] NA NA NA
43801+
43802+
##com.oracle.truffle.r.test.builtins.TestBuiltin_match.testMatchInSequence#
43803+
#match(as.character(1:3), as.character(3:1))
43804+
[1] 3 2 1
43805+
4379443806
##com.oracle.truffle.r.test.builtins.TestBuiltin_match.testMatchInSequence#
4379543807
#{ match(c( 1L, 2L, 3L), seq.int(from=1L, to=10L, by=2L)) }
4379643808
[1] 1 NA 2
@@ -78708,6 +78720,9 @@ Warning messages:
7870878720
#{z <- 1 ; f <- function(d) { b <- matrix(1:4,nrow=2,ncol=2) ; invisible(b[{z<<-z+1;1},{z<<-z*2;2},drop=z<<-z*10]) }; f(0) ; z; f(1L) ; z}
7870978721
[1] 820
7871078722

78723+
##com.oracle.truffle.r.test.builtins.TestBuiltin_subset.testSubsetAssign#
78724+
#{z <- c(3,4,2); z[3:1] <- 3:1;}
78725+
7871178726
##com.oracle.truffle.r.test.builtins.TestBuiltin_subset.testSubsetMissing#
7871278727
#l <- as.pairlist(list(x='a', y='b')); l[[]]
7871378728
NULL

com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_match.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
*/
@@ -421,6 +421,10 @@ public void testMatchInSequence() {
421421
testMatchStringSequence("a", "b");
422422
testMatchStringSequence("", "b");
423423
testMatchStringSequence("", "");
424+
425+
assertEval("match(1:3, 3:1)");
426+
assertEval("match(as.character(1:3), as.character(3:1))");
427+
assertEval("match(1:3, numeric(0))");
424428
}
425429

426430
private void testMatchStringSequence(String preffix, String suffix) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ public void testSubsetAssign() {
210210

211211
assertEval("{z <- 1 ; f <- function(d) { b <- matrix(1:4,nrow=2,ncol=2) ; invisible(b[[{z<<-z+1;1},{z<<-z*2;2},drop=z<<-z*10]]) }; f(0) ; z; f(1L) ; z}");
212212
assertEval(Output.IgnoreWarningContext, "{z <- 1 ; f <- function(d) { b <- data.frame(x='a', y='b') ; invisible(b[[{z<<-z+1;1},{z<<-z*2;2},drop=z<<-z*10]]) }; f(0) ; z; f(1L) ; z}");
213+
214+
assertEval("{z <- c(3,4,2); z[3:1] <- 3:1;}");
213215
}
214216

215217
@Test

0 commit comments

Comments
 (0)