aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2016-05-26 12:06:56 +0200
committerFelix Mulder <felix.mulder@gmail.com>2016-05-26 13:20:11 +0200
commit73f099ba506f6e877b676022a12cfdd31ab915b7 (patch)
tree0011f3add02b2b46fa8c6e894addbd7e74b22b44
parentfa81e54db9ebb36c7f76cbedfa98115e50a8f88c (diff)
downloaddotty-73f099ba506f6e877b676022a12cfdd31ab915b7.tar.gz
dotty-73f099ba506f6e877b676022a12cfdd31ab915b7.tar.bz2
dotty-73f099ba506f6e877b676022a12cfdd31ab915b7.zip
Fix double evaluation of scrutinee with side-effects, add test
-rw-r--r--src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala17
-rw-r--r--tests/run/isInstanceOf-eval.check2
-rw-r--r--tests/run/isInstanceOf-eval.scala15
3 files changed, 24 insertions, 10 deletions
diff --git a/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala b/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala
index 584628acd..6765604c8 100644
--- a/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala
+++ b/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala
@@ -91,16 +91,13 @@ class IsInstanceOfEvaluator extends MiniPhaseTransform { thisTransformer =>
*
* `scrutinee.isInstanceOf[Selector]` if `scrutinee eq null`
*/
- def rewrite(tree: Select, to: Boolean): Tree = {
- val expr =
- if (!to || !tree.qualifier.tpe.widen.derivesFrom(defn.AnyRefAlias))
- Literal(Constant(to))
- else
- Apply(tree.qualifier.select(defn.Object_ne), List(Literal(Constant(null))))
-
- if (!isPureExpr(tree.qualifier)) Block(List(tree.qualifier), expr)
- else expr
- }
+ def rewrite(tree: Select, to: Boolean): Tree =
+ if (!to || !tree.qualifier.tpe.widen.derivesFrom(defn.AnyRefAlias)) {
+ val literal = Literal(Constant(to))
+ if (!isPureExpr(tree.qualifier)) Block(List(tree.qualifier), literal)
+ else literal
+ } else
+ Apply(tree.qualifier.select(defn.Object_ne), List(Literal(Constant(null))))
/** Attempts to rewrite TypeApply to either `scrutinee ne null` or a
* constant
diff --git a/tests/run/isInstanceOf-eval.check b/tests/run/isInstanceOf-eval.check
new file mode 100644
index 000000000..1191247b6
--- /dev/null
+++ b/tests/run/isInstanceOf-eval.check
@@ -0,0 +1,2 @@
+1
+2
diff --git a/tests/run/isInstanceOf-eval.scala b/tests/run/isInstanceOf-eval.scala
new file mode 100644
index 000000000..aa907b697
--- /dev/null
+++ b/tests/run/isInstanceOf-eval.scala
@@ -0,0 +1,15 @@
+object Test extends dotty.runtime.LegacyApp {
+ lazy val any = {
+ println(1)
+ 1: Any
+ }
+
+ any.isInstanceOf[Int]
+
+ lazy val int = {
+ println(2)
+ 2
+ }
+
+ int.isInstanceOf[Int]
+}