diff options
author | Nicolas Stucki <nicolas.stucki@gmail.com> | 2017-04-16 18:48:53 +0200 |
---|---|---|
committer | Nicolas Stucki <nicolas.stucki@gmail.com> | 2017-04-16 18:48:53 +0200 |
commit | 2324be56376aed8cd168e712001485f2202cdda6 (patch) | |
tree | 46ccb7fdd77e94f235a5defb93b13f0c38c37015 /compiler/src/dotty/tools/dotc/ast/TreeInfo.scala | |
parent | a370eed7bebbc534e19de7dadefd72a70349854f (diff) | |
download | dotty-2324be56376aed8cd168e712001485f2202cdda6.tar.gz dotty-2324be56376aed8cd168e712001485f2202cdda6.tar.bz2 dotty-2324be56376aed8cd168e712001485f2202cdda6.zip |
Fix constant type val value inline in constructors.
Diffstat (limited to 'compiler/src/dotty/tools/dotc/ast/TreeInfo.scala')
-rw-r--r-- | compiler/src/dotty/tools/dotc/ast/TreeInfo.scala | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala index cac0e4b91..46af1f1b4 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._ +import Flags._, Trees._, Types._, Contexts._, Constants._ import Names._, StdNames._, NameOps._, Decorators._, Symbols._ import util.HashSet import typer.ConstFold @@ -426,8 +426,17 @@ 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 = { + isIdempotentExpr(tree1) && // see note in documentation + // lazy value must be initialized (would not be needed with isPureExpr) + !tree1.symbol.is(Lazy) && + // could hide initialization order issues (ex. val with constant type read before initialized) + (!ctx.owner.isLocalDummy || (tree1.symbol.is(Method) || value.isZero) || + ctx.scala2Mode // ignore in Scala 2 because of inlined `final val` values + ) + } tree1.tpe.widenTermRefExpr match { - case ConstantType(value) if isIdempotentExpr(tree1) && !tree1.symbol.is(Lazy) => Literal(value) + case ConstantType(value) if canInlineConstant(value) => Literal(value) case _ => tree1 } } |