From a370eed7bebbc534e19de7dadefd72a70349854f Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Sat, 15 Apr 2017 19:29:44 +0200 Subject: Fix #2266: Do not replace constant type lazy vals with constant. --- compiler/src/dotty/tools/dotc/ast/TreeInfo.scala | 2 +- tests/run/inline-constant-lazy-val.check | 2 ++ tests/run/inline-constant-lazy-val.scala | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/run/inline-constant-lazy-val.check create mode 100644 tests/run/inline-constant-lazy-val.scala diff --git a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala index f3bce4000..cac0e4b91 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala @@ -427,7 +427,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] => def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = { val tree1 = ConstFold(tree) tree1.tpe.widenTermRefExpr match { - case ConstantType(value) if isIdempotentExpr(tree1) => Literal(value) + case ConstantType(value) if isIdempotentExpr(tree1) && !tree1.symbol.is(Lazy) => Literal(value) case _ => tree1 } } diff --git a/tests/run/inline-constant-lazy-val.check b/tests/run/inline-constant-lazy-val.check new file mode 100644 index 000000000..16858db7a --- /dev/null +++ b/tests/run/inline-constant-lazy-val.check @@ -0,0 +1,2 @@ +X +Y diff --git a/tests/run/inline-constant-lazy-val.scala b/tests/run/inline-constant-lazy-val.scala new file mode 100644 index 000000000..5f4aaa12b --- /dev/null +++ b/tests/run/inline-constant-lazy-val.scala @@ -0,0 +1,10 @@ + +object Test { + lazy val x: true = { println("X"); true } + + def main(args: Array[String]): Unit = { + lazy val y: true = { println("Y"); true } + x + y + } +} \ No newline at end of file -- cgit v1.2.3