aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2015-09-08 10:34:48 +0200
committerDmitry Petrashko <dmitry.petrashko@gmail.com>2015-09-14 13:36:14 +0200
commit1ff1cf5393778b7dc21f765f5c8f372c05877862 (patch)
tree6e04edfe90f146b07897822755ca9619a19116c1
parent869bdb0503628e8665292073087b08b91399a6c9 (diff)
downloaddotty-1ff1cf5393778b7dc21f765f5c8f372c05877862.tar.gz
dotty-1ff1cf5393778b7dc21f765f5c8f372c05877862.tar.bz2
dotty-1ff1cf5393778b7dc21f765f5c8f372c05877862.zip
Follow TermRefs when constant folding
A TermRef representing a constant value needs to be considered a constant when folding.
-rw-r--r--src/dotty/tools/dotc/core/Types.scala8
-rw-r--r--src/dotty/tools/dotc/typer/ConstFold.scala8
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
}