From 1ff1cf5393778b7dc21f765f5c8f372c05877862 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 8 Sep 2015 10:34:48 +0200 Subject: Follow TermRefs when constant folding A TermRef representing a constant value needs to be considered a constant when folding. --- src/dotty/tools/dotc/core/Types.scala | 8 ++++++++ src/dotty/tools/dotc/typer/ConstFold.scala | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index 5fd3b81d0..d04da30c0 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -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.) */ diff --git a/src/dotty/tools/dotc/typer/ConstFold.scala b/src/dotty/tools/dotc/typer/ConstFold.scala index ac1c7260b..68a5d05f5 100644 --- a/src/dotty/tools/dotc/typer/ConstFold.scala +++ b/src/dotty/tools/dotc/typer/ConstFold.scala @@ -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 } @@ -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 } -- cgit v1.2.3