Skip to content

Commit 2324be5

Browse files
committed
Fix constant type val value inline in constructors.
1 parent a370eed commit 2324be5

7 files changed

+62
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33
package ast
44

55
import core._
6-
import Flags._, Trees._, Types._, Contexts._
6+
import Flags._, Trees._, Types._, Contexts._, Constants._
77
import Names._, StdNames._, NameOps._, Decorators._, Symbols._
88
import util.HashSet
99
import typer.ConstFold
@@ -426,8 +426,17 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
426426
*/
427427
def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = {
428428
val tree1 = ConstFold(tree)
429+
def canInlineConstant(value: Constant): Boolean = {
430+
isIdempotentExpr(tree1) && // see note in documentation
431+
// lazy value must be initialized (would not be needed with isPureExpr)
432+
!tree1.symbol.is(Lazy) &&
433+
// 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.scala2Mode // ignore in Scala 2 because of inlined `final val` values
436+
)
437+
}
429438
tree1.tpe.widenTermRefExpr match {
430-
case ConstantType(value) if isIdempotentExpr(tree1) && !tree1.symbol.is(Lazy) => Literal(value)
439+
case ConstantType(value) if canInlineConstant(value) => Literal(value)
431440
case _ => tree1
432441
}
433442
}

compiler/src/dotty/tools/dotc/core/Constants.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ object Constants {
5353
def isNonUnitAnyVal = BooleanTag <= tag && tag <= DoubleTag
5454
def isAnyVal = UnitTag <= tag && tag <= DoubleTag
5555

56+
/** Is the zero or un-initialized value of the type */
57+
def isZero(implicit ctx: Context): Boolean = tag match {
58+
case BooleanTag => !value.asInstanceOf[Boolean]
59+
case ByteTag => value.asInstanceOf[Byte] == 0
60+
case ShortTag => value.asInstanceOf[Short] == 0
61+
case CharTag => value.asInstanceOf[Char] == 0
62+
case IntTag => value.asInstanceOf[Int] == 0
63+
case LongTag => value.asInstanceOf[Long] == 0L
64+
case FloatTag => value.asInstanceOf[Float] == 0.0
65+
case DoubleTag => value.asInstanceOf[Double] == 0.0
66+
case NullTag => true
67+
case _ => false
68+
}
69+
5670
def tpe(implicit ctx: Context): Type = tag match {
5771
case UnitTag => defn.UnitType
5872
case BooleanTag => defn.BooleanType

compiler/test/dotc/scala-collections.blacklist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,5 @@ scala/util/control/Exception.scala
8181
# 51 | implicit def throwableSubtypeToCatcher[Ex <: Throwable: ClassTag, T](pf: PartialFunction[Ex, T]) =
8282
# | ^
8383
# | cyclic reference involving method mkCatcher
84+
85+
scala/concurrent/duration/Duration.scala
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
assert
2+
s
3+
r init
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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"); r == s }) // r constant type not replaced by true, r not initialized yet
10+
override val r: true = {
11+
println("r init")
12+
true
13+
}
14+
def main(args: Array[String]): Unit = {}
15+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
assert
2+
s
3+
r init
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
abstract class A {
3+
def s: Boolean = { println("s"); r }
4+
def r: Boolean
5+
}
6+
7+
object Test extends A {
8+
assert({ println("assert"); r == s }) // r constant type replaced by false
9+
override val r: false = {
10+
println("r init")
11+
false
12+
}
13+
def main(args: Array[String]): Unit = {}
14+
}

0 commit comments

Comments
 (0)