Skip to content

Revert "Fix #2266: Do not replace constant type lazy vals with constant." #2308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
14 changes: 0 additions & 14 deletions compiler/src/dotty/tools/dotc/core/Constants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 5 additions & 12 deletions compiler/src/dotty/tools/dotc/transform/FirstTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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._
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions compiler/test/dotc/scala-collections.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions tests/run/if-with-constant-cond.check

This file was deleted.

9 changes: 0 additions & 9 deletions tests/run/if-with-constant-cond.scala

This file was deleted.

3 changes: 0 additions & 3 deletions tests/run/inline-constant-in-constructor-1.check

This file was deleted.

15 changes: 0 additions & 15 deletions tests/run/inline-constant-in-constructor-1.scala

This file was deleted.

3 changes: 0 additions & 3 deletions tests/run/inline-constant-in-constructor-2.check

This file was deleted.

14 changes: 0 additions & 14 deletions tests/run/inline-constant-in-constructor-2.scala

This file was deleted.

4 changes: 0 additions & 4 deletions tests/run/inline-constant-in-constructor-3.check

This file was deleted.

19 changes: 0 additions & 19 deletions tests/run/inline-constant-in-constructor-3.scala

This file was deleted.

2 changes: 0 additions & 2 deletions tests/run/inline-constant-lazy-val.check

This file was deleted.

10 changes: 0 additions & 10 deletions tests/run/inline-constant-lazy-val.scala

This file was deleted.