Skip to content

Commit 73f099b

Browse files
committed
Fix double evaluation of scrutinee with side-effects, add test
1 parent fa81e54 commit 73f099b

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
object Test extends dotty.runtime.LegacyApp {
2+
lazy val any = {
3+
println(1)
4+
1: Any
5+
}
6+
7+
any.isInstanceOf[Int]
8+
9+
lazy val int = {
10+
println(2)
11+
2
12+
}
13+
14+
int.isInstanceOf[Int]
15+
}

0 commit comments

Comments
 (0)