From 991b9af3853cd22f6406c44367c7b239e7918803 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 26 Apr 2017 12:12:51 +0200 Subject: [PATCH] Revert "Fix #2266: Do not replace constant type lazy vals with constant." --- .../src/dotty/tools/dotc/ast/TreeInfo.scala | 14 ++------------ .../src/dotty/tools/dotc/core/Constants.scala | 14 -------------- .../tools/dotc/transform/FirstTransform.scala | 17 +++++------------ .../test/dotc/scala-collections.blacklist | 2 -- tests/run/if-with-constant-cond.check | 4 ---- tests/run/if-with-constant-cond.scala | 9 --------- .../inline-constant-in-constructor-1.check | 3 --- .../inline-constant-in-constructor-1.scala | 15 --------------- .../inline-constant-in-constructor-2.check | 3 --- .../inline-constant-in-constructor-2.scala | 14 -------------- .../inline-constant-in-constructor-3.check | 4 ---- .../inline-constant-in-constructor-3.scala | 19 ------------------- tests/run/inline-constant-lazy-val.check | 2 -- tests/run/inline-constant-lazy-val.scala | 10 ---------- 14 files changed, 7 insertions(+), 123 deletions(-) delete mode 100644 tests/run/if-with-constant-cond.check delete mode 100644 tests/run/if-with-constant-cond.scala delete mode 100644 tests/run/inline-constant-in-constructor-1.check delete mode 100644 tests/run/inline-constant-in-constructor-1.scala delete mode 100644 tests/run/inline-constant-in-constructor-2.check delete mode 100644 tests/run/inline-constant-in-constructor-2.scala delete mode 100644 tests/run/inline-constant-in-constructor-3.check delete mode 100644 tests/run/inline-constant-in-constructor-3.scala delete mode 100644 tests/run/inline-constant-lazy-val.check delete 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 49187492e00d..f3bce4000079 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala @@ -3,7 +3,7 @@ package dotc package ast import core._ -import Flags._, Trees._, Types._, Contexts._, Constants._ +import Flags._, Trees._, Types._, Contexts._ import Names._, StdNames._, NameOps._, Decorators._, Symbols._ import util.HashSet import typer.ConstFold @@ -426,18 +426,8 @@ 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) - !sym.is(Lazy) && - // could hide initialization order issues (ex. val with constant type read before initialized) - (!ctx.owner.isLocalDummy || (!sym.is(Method) && !sym.is(Lazy) && value.isZero) || - ctx.scala2Mode // ignore in Scala 2 because of inlined `final val` values - ) - } tree1.tpe.widenTermRefExpr match { - case ConstantType(value) if canInlineConstant(value) => Literal(value) + case ConstantType(value) if isIdempotentExpr(tree1) => Literal(value) case _ => tree1 } } diff --git a/compiler/src/dotty/tools/dotc/core/Constants.scala b/compiler/src/dotty/tools/dotc/core/Constants.scala index 8ea285c8d45b..ed388b7ecaea 100644 --- a/compiler/src/dotty/tools/dotc/core/Constants.scala +++ b/compiler/src/dotty/tools/dotc/core/Constants.scala @@ -53,20 +53,6 @@ object Constants { def isNonUnitAnyVal = BooleanTag <= tag && tag <= DoubleTag def isAnyVal = UnitTag <= tag && tag <= DoubleTag - /** Is the zero or un-initialized value of the type */ - def isZero(implicit ctx: Context): Boolean = tag match { - case BooleanTag => !value.asInstanceOf[Boolean] - case ByteTag => value.asInstanceOf[Byte] == 0 - case ShortTag => value.asInstanceOf[Short] == 0 - case CharTag => value.asInstanceOf[Char] == 0 - case IntTag => value.asInstanceOf[Int] == 0 - case LongTag => value.asInstanceOf[Long] == 0L - case FloatTag => value.asInstanceOf[Float] == 0.0 - case DoubleTag => value.asInstanceOf[Double] == 0.0 - case NullTag => true - case _ => false - } - def tpe(implicit ctx: Context): Type = tag match { case UnitTag => defn.UnitType case BooleanTag => defn.BooleanType diff --git a/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala b/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala index 767ee0901f74..a3cf71ef2203 100644 --- a/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala +++ b/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala @@ -9,7 +9,7 @@ import dotty.tools.dotc.transform.TreeTransforms._ import ast.Trees._ import Flags._ import Types._ -import Constants._ +import Constants.Constant import Contexts.Context import Symbols._ import SymDenotations._ @@ -34,8 +34,6 @@ import StdNames._ * - drops branches of ifs using the rules * if (true) A else B --> A * if (false) A else B --> B - * if (C: true) A else B --> C; A - * if (C: false) A else B --> C; B */ class FirstTransform extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer => import ast.tpd._ @@ -192,16 +190,11 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota override def transformBlock(tree: Block)(implicit ctx: Context, info: TransformerInfo) = constToLiteral(tree) - override def transformIf(tree: If)(implicit ctx: Context, info: TransformerInfo) = { - tree.cond.tpe.widenTermRefExpr match { - case ConstantType(Constant(condVal: Boolean)) => - val selected = if (condVal) tree.thenp else tree.elsep - if (isPureExpr(tree.cond)) selected - else Block(tree.cond :: Nil, selected) - case _ => - tree + override def transformIf(tree: If)(implicit ctx: Context, info: TransformerInfo) = + tree.cond match { + case Literal(Constant(c: Boolean)) => if (c) tree.thenp else tree.elsep + case _ => tree } - } // invariants: all modules have companion objects // all types are TypeTrees diff --git a/compiler/test/dotc/scala-collections.blacklist b/compiler/test/dotc/scala-collections.blacklist index ae43e6eb4fa6..d6972a645ed3 100644 --- a/compiler/test/dotc/scala-collections.blacklist +++ b/compiler/test/dotc/scala-collections.blacklist @@ -81,5 +81,3 @@ scala/util/control/Exception.scala # 51 | implicit def throwableSubtypeToCatcher[Ex <: Throwable: ClassTag, T](pf: PartialFunction[Ex, T]) = # | ^ # | cyclic reference involving method mkCatcher - -scala/concurrent/duration/Duration.scala diff --git a/tests/run/if-with-constant-cond.check b/tests/run/if-with-constant-cond.check deleted file mode 100644 index 2e31d4f236a2..000000000000 --- a/tests/run/if-with-constant-cond.check +++ /dev/null @@ -1,4 +0,0 @@ -cond1 -then1 -cond2 -else2 diff --git a/tests/run/if-with-constant-cond.scala b/tests/run/if-with-constant-cond.scala deleted file mode 100644 index db5959c840e2..000000000000 --- a/tests/run/if-with-constant-cond.scala +++ /dev/null @@ -1,9 +0,0 @@ - -object Test { - - def main(args: Array[String]): Unit = { - if ({ println("cond1"); true }: true) println("then1") else println("else1") - if ({ println("cond2"); false }: false) println("then2") else println("else2") - } - -} \ No newline at end of file diff --git a/tests/run/inline-constant-in-constructor-1.check b/tests/run/inline-constant-in-constructor-1.check deleted file mode 100644 index 05035c26dcc6..000000000000 --- a/tests/run/inline-constant-in-constructor-1.check +++ /dev/null @@ -1,3 +0,0 @@ -assert -s -r init diff --git a/tests/run/inline-constant-in-constructor-1.scala b/tests/run/inline-constant-in-constructor-1.scala deleted file mode 100644 index 11a727fd6abe..000000000000 --- a/tests/run/inline-constant-in-constructor-1.scala +++ /dev/null @@ -1,15 +0,0 @@ - - -abstract class A { - def s: Boolean = { println("s"); r } - def r: Boolean -} - -object Test extends A { - assert({ println("assert"); r == s }) // r constant type not replaced by true, r not initialized yet - override val r: true = { - println("r init") - true - } - def main(args: Array[String]): Unit = {} -} diff --git a/tests/run/inline-constant-in-constructor-2.check b/tests/run/inline-constant-in-constructor-2.check deleted file mode 100644 index 05035c26dcc6..000000000000 --- a/tests/run/inline-constant-in-constructor-2.check +++ /dev/null @@ -1,3 +0,0 @@ -assert -s -r init diff --git a/tests/run/inline-constant-in-constructor-2.scala b/tests/run/inline-constant-in-constructor-2.scala deleted file mode 100644 index a8d351ab634a..000000000000 --- a/tests/run/inline-constant-in-constructor-2.scala +++ /dev/null @@ -1,14 +0,0 @@ - -abstract class A { - def s: Boolean = { println("s"); r } - def r: Boolean -} - -object Test extends A { - assert({ println("assert"); r == s }) // r constant type replaced by false - override val r: false = { - println("r init") - false - } - def main(args: Array[String]): Unit = {} -} diff --git a/tests/run/inline-constant-in-constructor-3.check b/tests/run/inline-constant-in-constructor-3.check deleted file mode 100644 index b0171e3d9adf..000000000000 --- a/tests/run/inline-constant-in-constructor-3.check +++ /dev/null @@ -1,4 +0,0 @@ -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 deleted file mode 100644 index 621ace231386..000000000000 --- a/tests/run/inline-constant-in-constructor-3.scala +++ /dev/null @@ -1,19 +0,0 @@ - - -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 = {} -} diff --git a/tests/run/inline-constant-lazy-val.check b/tests/run/inline-constant-lazy-val.check deleted file mode 100644 index 16858db7afb6..000000000000 --- a/tests/run/inline-constant-lazy-val.check +++ /dev/null @@ -1,2 +0,0 @@ -X -Y diff --git a/tests/run/inline-constant-lazy-val.scala b/tests/run/inline-constant-lazy-val.scala deleted file mode 100644 index 5f4aaa12bd21..000000000000 --- a/tests/run/inline-constant-lazy-val.scala +++ /dev/null @@ -1,10 +0,0 @@ - -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