Skip to content

Commit 33df851

Browse files
committed
Fix double evaluation of scrutinee with side-effects, add test
1 parent bf85091 commit 33df851

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,13 @@ class IsInstanceOfEvaluator extends MiniPhaseTransform { thisTransformer =>
9191
*
9292
* `scrutinee.isInstanceOf[Selector]` if `scrutinee eq null`
9393
*/
94-
def rewrite(tree: Select, to: Boolean): Tree = {
95-
val expr =
96-
if (!to || !tree.qualifier.tpe.widen.derivesFrom(defn.AnyRefAlias))
97-
Literal(Constant(to))
98-
else
99-
Apply(tree.qualifier.select(defn.Object_ne), List(Literal(Constant(null))))
100-
101-
if (!isPureExpr(tree.qualifier)) Block(List(tree.qualifier), expr)
102-
else expr
103-
}
94+
def rewrite(tree: Select, to: Boolean): Tree =
95+
if (!to || !tree.qualifier.tpe.widen.derivesFrom(defn.AnyRefAlias)) {
96+
val literal = Literal(Constant(to))
97+
if (!isPureExpr(tree.qualifier)) Block(List(tree.qualifier), literal)
98+
else literal
99+
} else
100+
Apply(tree.qualifier.select(defn.Object_ne), List(Literal(Constant(null))))
104101

105102
/** Attempts to rewrite TypeApply to either `scrutinee ne null` or a
106103
* constant

tests/run/isInstanceOf-eval.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1
2+
2

tests/run/isInstanceOf-eval.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object Foo {
2+
def main(args: Array[String]) = {
3+
lazy val any = {
4+
println(1)
5+
1: Any
6+
}
7+
8+
any.isInstanceOf[Int]
9+
10+
lazy val int = {
11+
println(2)
12+
2
13+
}
14+
15+
int.isInstanceOf[Int]
16+
}
17+
}

0 commit comments

Comments
 (0)