Skip to content

Commit 94bcf7c

Browse files
committed
Follow TermRefs when constant folding
A TermRef representing a constant value needs to be considered a constant when folding.
1 parent 5c62c0b commit 94bcf7c

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/dotty/tools/dotc/core/Types.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,14 @@ object Types {
715715
case _ => this
716716
}
717717

718+
/** Widen from TermRef to its underlying non-termref
719+
* base type, while also skipping Expr types.
720+
*/
721+
final def widenTermRefExpr(implicit ctx: Context): Type = stripTypeVar match {
722+
case tp: TermRef if !tp.isOverloaded => tp.underlying.widenExpr.widenTermRefExpr
723+
case _ => this
724+
}
725+
718726
/** Widen from ExprType type to its result type.
719727
* (Note: no stripTypeVar needed because TypeVar's can't refer to ExprTypes.)
720728
*/

src/dotty/tools/dotc/typer/ConstFold.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ object ConstFold {
2020
def apply(tree: Tree)(implicit ctx: Context): Tree = finish(tree) {
2121
tree match {
2222
case Apply(Select(xt, op), yt :: Nil) =>
23-
xt.tpe match {
23+
xt.tpe.widenTermRefExpr match {
2424
case ConstantType(x) =>
25-
yt.tpe match {
25+
yt.tpe.widenTermRefExpr match {
2626
case ConstantType(y) => foldBinop(op, x, y)
2727
case _ => null
2828
}
2929
case _ => null
3030
}
3131
case Select(xt, op) =>
32-
xt.tpe match {
32+
xt.tpe.widenTermRefExpr match {
3333
case ConstantType(x) => foldUnop(op, x)
3434
case _ => null
3535
}
@@ -42,7 +42,7 @@ object ConstFold {
4242
*/
4343
def apply(tree: Tree, pt: Type)(implicit ctx: Context): Tree =
4444
finish(apply(tree)) {
45-
tree.tpe match {
45+
tree.tpe.widenTermRefExpr match {
4646
case ConstantType(x) => x convertTo pt
4747
case _ => null
4848
}

0 commit comments

Comments
 (0)