Skip to content

Commit afa96e3

Browse files
committed
Merge pull request scala#3887 from Ichoran/issue/8738
SI-8738 Regression in range equality
2 parents abdd570 + c7cc1a8 commit afa96e3

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/library/scala/collection/immutable/Range.scala

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,16 @@ extends scala.collection.AbstractSeq[Int]
364364
override def equals(other: Any) = other match {
365365
case x: Range =>
366366
// Note: this must succeed for overfull ranges (length > Int.MaxValue)
367-
(x canEqual this) && (
368-
isEmpty || // all empty sequences are equal
369-
(start == x.start && { // Otherwise, must have same start
370-
val l0 = last
371-
(l0 == x.last && ( // And same end
372-
start == l0 || step == x.step // And either the same step, or not take any steps
373-
))
374-
})
375-
)
367+
(x canEqual this) && {
368+
if (isEmpty) x.isEmpty // empty sequences are equal
369+
else // this is non-empty...
370+
x.nonEmpty && start == x.start && { // ...so other must contain something and have same start
371+
val l0 = last
372+
(l0 == x.last && ( // And same end
373+
start == l0 || step == x.step // And either the same step, or not take any steps
374+
))
375+
}
376+
}
376377
case _ =>
377378
super.equals(other)
378379
}

test/files/run/t8738.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Test {
2+
def check(a: Range, b: Range) = (a == b) == (a.toList == b.toList)
3+
def main(args: Array[String]) {
4+
val lo = -2 to 2
5+
val hi = lo
6+
val step = List(-6,-2,-1,1,2,6)
7+
for (i <- lo; j <- hi; n <- step; k <- lo; l <- hi; m <- step) {
8+
assert(
9+
check(i until j by n, k until l by m) &&
10+
check(i until j by n, k to l by m) &&
11+
check(i to j by n, k until l by m) &&
12+
check(i to j by n, k to l by m)
13+
)
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)