Skip to content

Commit

Permalink
Follow TermRefs when constant folding
Browse files Browse the repository at this point in the history
A TermRef representing a constant value needs to be
considered a constant when folding.
  • Loading branch information
odersky authored and DarkDimius committed Sep 14, 2015
1 parent 869bdb0 commit 1ff1cf5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,14 @@ object Types {
case _ => this
}

/** Widen from TermRef to its underlying non-termref
* base type, while also skipping Expr types.
*/
final def widenTermRefExpr(implicit ctx: Context): Type = stripTypeVar match {
case tp: TermRef if !tp.isOverloaded => tp.underlying.widenExpr.widenTermRefExpr
case _ => this
}

/** Widen from ExprType type to its result type.
* (Note: no stripTypeVar needed because TypeVar's can't refer to ExprTypes.)
*/
Expand Down
8 changes: 4 additions & 4 deletions src/dotty/tools/dotc/typer/ConstFold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ object ConstFold {
def apply(tree: Tree)(implicit ctx: Context): Tree = finish(tree) {
tree match {
case Apply(Select(xt, op), yt :: Nil) =>
xt.tpe match {
xt.tpe.widenTermRefExpr match {
case ConstantType(x) =>
yt.tpe match {
yt.tpe.widenTermRefExpr match {
case ConstantType(y) => foldBinop(op, x, y)
case _ => null
}
case _ => null
}
case Select(xt, op) =>
xt.tpe match {
xt.tpe.widenTermRefExpr match {
case ConstantType(x) => foldUnop(op, x)
case _ => null
}
Expand All @@ -42,7 +42,7 @@ object ConstFold {
*/
def apply(tree: Tree, pt: Type)(implicit ctx: Context): Tree =
finish(apply(tree)) {
tree.tpe match {
tree.tpe.widenTermRefExpr match {
case ConstantType(x) => x convertTo pt
case _ => null
}
Expand Down

0 comments on commit 1ff1cf5

Please sign in to comment.