Skip to content

Commit 16a28ac

Browse files
authored
Merge pull request #2308 from lampepfl/revert-2267-fix-constant-lazy-vals
Revert "Fix #2266: Do not replace constant type lazy vals with constant."
2 parents 47c52ad + 991b9af commit 16a28ac

14 files changed

+7
-123
lines changed

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

Lines changed: 2 additions & 12 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._, Constants._
6+
import Flags._, Trees._, Types._, Contexts._
77
import Names._, StdNames._, NameOps._, Decorators._, Symbols._
88
import util.HashSet
99
import typer.ConstFold
@@ -426,18 +426,8 @@ 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-
val sym = tree1.symbol
431-
isIdempotentExpr(tree1) && // see note in documentation
432-
// lazy value must be initialized (would not be needed with isPureExpr)
433-
!sym.is(Lazy) &&
434-
// could hide initialization order issues (ex. val with constant type read before initialized)
435-
(!ctx.owner.isLocalDummy || (!sym.is(Method) && !sym.is(Lazy) && value.isZero) ||
436-
ctx.scala2Mode // ignore in Scala 2 because of inlined `final val` values
437-
)
438-
}
439429
tree1.tpe.widenTermRefExpr match {
440-
case ConstantType(value) if canInlineConstant(value) => Literal(value)
430+
case ConstantType(value) if isIdempotentExpr(tree1) => Literal(value)
441431
case _ => tree1
442432
}
443433
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,6 @@ 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-
7056
def tpe(implicit ctx: Context): Type = tag match {
7157
case UnitTag => defn.UnitType
7258
case BooleanTag => defn.BooleanType

compiler/src/dotty/tools/dotc/transform/FirstTransform.scala

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import dotty.tools.dotc.transform.TreeTransforms._
99
import ast.Trees._
1010
import Flags._
1111
import Types._
12-
import Constants._
12+
import Constants.Constant
1313
import Contexts.Context
1414
import Symbols._
1515
import SymDenotations._
@@ -34,8 +34,6 @@ import StdNames._
3434
* - drops branches of ifs using the rules
3535
* if (true) A else B --> A
3636
* if (false) A else B --> B
37-
* if (C: true) A else B --> C; A
38-
* if (C: false) A else B --> C; B
3937
*/
4038
class FirstTransform extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer =>
4139
import ast.tpd._
@@ -192,16 +190,11 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
192190
override def transformBlock(tree: Block)(implicit ctx: Context, info: TransformerInfo) =
193191
constToLiteral(tree)
194192

195-
override def transformIf(tree: If)(implicit ctx: Context, info: TransformerInfo) = {
196-
tree.cond.tpe.widenTermRefExpr match {
197-
case ConstantType(Constant(condVal: Boolean)) =>
198-
val selected = if (condVal) tree.thenp else tree.elsep
199-
if (isPureExpr(tree.cond)) selected
200-
else Block(tree.cond :: Nil, selected)
201-
case _ =>
202-
tree
193+
override def transformIf(tree: If)(implicit ctx: Context, info: TransformerInfo) =
194+
tree.cond match {
195+
case Literal(Constant(c: Boolean)) => if (c) tree.thenp else tree.elsep
196+
case _ => tree
203197
}
204-
}
205198

206199
// invariants: all modules have companion objects
207200
// all types are TypeTrees

compiler/test/dotc/scala-collections.blacklist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,3 @@ 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

tests/run/if-with-constant-cond.check

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/run/if-with-constant-cond.scala

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/run/inline-constant-in-constructor-1.check

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/run/inline-constant-in-constructor-1.scala

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/run/inline-constant-in-constructor-2.check

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/run/inline-constant-in-constructor-2.scala

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/run/inline-constant-in-constructor-3.check

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/run/inline-constant-in-constructor-3.scala

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/run/inline-constant-lazy-val.check

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/run/inline-constant-lazy-val.scala

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)