aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Stucki <nicolas.stucki@gmail.com>2017-04-17 16:54:44 +0200
committerNicolas Stucki <nicolas.stucki@gmail.com>2017-04-17 16:54:44 +0200
commitea9325965a82610083e75e0d111fe3d70c46b3d2 (patch)
tree3db8c18cd41cb4f9ab51537b466957b4bfa5428e
parent2324be56376aed8cd168e712001485f2202cdda6 (diff)
downloaddotty-ea9325965a82610083e75e0d111fe3d70c46b3d2.zip
dotty-ea9325965a82610083e75e0d111fe3d70c46b3d2.tar.gz
dotty-ea9325965a82610083e75e0d111fe3d70c46b3d2.tar.bz2
Only allow constant type vals to be inlined.
-rw-r--r--compiler/src/dotty/tools/dotc/ast/TreeInfo.scala5
-rw-r--r--tests/run/inline-constant-in-constructor-3.check4
-rw-r--r--tests/run/inline-constant-in-constructor-3.scala19
3 files changed, 26 insertions, 2 deletions
diff --git a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
index 46af1f1..4918749 100644
--- a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
+++ b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
@@ -427,11 +427,12 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = {
val tree1 = ConstFold(tree)
def canInlineConstant(value: Constant): Boolean = {
+ val sym = tree1.symbol
isIdempotentExpr(tree1) && // see note in documentation
// lazy value must be initialized (would not be needed with isPureExpr)
- !tree1.symbol.is(Lazy) &&
+ !sym.is(Lazy) &&
// could hide initialization order issues (ex. val with constant type read before initialized)
- (!ctx.owner.isLocalDummy || (tree1.symbol.is(Method) || value.isZero) ||
+ (!ctx.owner.isLocalDummy || (!sym.is(Method) && !sym.is(Lazy) && value.isZero) ||
ctx.scala2Mode // ignore in Scala 2 because of inlined `final val` values
)
}
diff --git a/tests/run/inline-constant-in-constructor-3.check b/tests/run/inline-constant-in-constructor-3.check
new file mode 100644
index 0000000..b0171e3
--- /dev/null
+++ b/tests/run/inline-constant-in-constructor-3.check
@@ -0,0 +1,4 @@
+assert
+r2
+s
+r init
diff --git a/tests/run/inline-constant-in-constructor-3.scala b/tests/run/inline-constant-in-constructor-3.scala
new file mode 100644
index 0000000..621ace2
--- /dev/null
+++ b/tests/run/inline-constant-in-constructor-3.scala
@@ -0,0 +1,19 @@
+
+
+abstract class A {
+ def s: Boolean = { println("s"); r }
+ def r: Boolean
+}
+
+object Test extends A {
+ assert({ println("assert"); r2 != s }) // s not initialized yet
+ def r2: true = {
+ println("r2")
+ true
+ }
+ override val r: true = {
+ println("r init")
+ true
+ }
+ def main(args: Array[String]): Unit = {}
+}