Skip to content

Commit a745f06

Browse files
committed
Merge pull request scala#4670 from retronym/ticket/9422
SI-9422 Fix incorrect constant propagation
2 parents 65fa73d + ec95e53 commit a745f06

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/compiler/scala/tools/nsc/backend/opt/ConstantOptimization.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ abstract class ConstantOptimization extends SubComponent {
170170
// out all the possibilities
171171
case Impossible(possible2) => (possible -- possible2).nonEmpty
172172
})
173-
def mightNotEqual(other: Contents): Boolean = (this ne other) && (other match {
174-
// two Possibles might not be equal if either has possible members that the other doesn't
175-
case Possible(possible2) => (possible -- possible2).nonEmpty || (possible2 -- possible).nonEmpty
173+
def mightNotEqual(other: Contents): Boolean = (other match {
174+
case Possible(possible2) =>
175+
// two Possibles must equal if each is known to be of the same, single value
176+
val mustEqual = possible.size == 1 && possible == possible2
177+
!mustEqual
176178
case Impossible(_) => true
177179
})
178180
}

test/files/run/t9422.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Test(val x: Long) {
2+
def sameDirection(y: Long): Boolean =
3+
(y == 0 || x == 0 || ((y > 0) == (x > 0)))
4+
}
5+
6+
object Test {
7+
def main(args: Array[String]) {
8+
val b = new Test(1L)
9+
assert(!b.sameDirection(-1L))
10+
}
11+
}

0 commit comments

Comments
 (0)