Skip to content

Commit ea93259

Browse files
committed
Only allow constant type vals to be inlined.
1 parent 2324be5 commit ea93259

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,12 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
427427
def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = {
428428
val tree1 = ConstFold(tree)
429429
def canInlineConstant(value: Constant): Boolean = {
430+
val sym = tree1.symbol
430431
isIdempotentExpr(tree1) && // see note in documentation
431432
// lazy value must be initialized (would not be needed with isPureExpr)
432-
!tree1.symbol.is(Lazy) &&
433+
!sym.is(Lazy) &&
433434
// could hide initialization order issues (ex. val with constant type read before initialized)
434-
(!ctx.owner.isLocalDummy || (tree1.symbol.is(Method) || value.isZero) ||
435+
(!ctx.owner.isLocalDummy || (!sym.is(Method) && !sym.is(Lazy) && value.isZero) ||
435436
ctx.scala2Mode // ignore in Scala 2 because of inlined `final val` values
436437
)
437438
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
assert
2+
r2
3+
s
4+
r init
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
abstract class A {
4+
def s: Boolean = { println("s"); r }
5+
def r: Boolean
6+
}
7+
8+
object Test extends A {
9+
assert({ println("assert"); r2 != s }) // s not initialized yet
10+
def r2: true = {
11+
println("r2")
12+
true
13+
}
14+
override val r: true = {
15+
println("r init")
16+
true
17+
}
18+
def main(args: Array[String]): Unit = {}
19+
}

0 commit comments

Comments
 (0)