Skip to content

Commit bd86088

Browse files
committed
Fix compilation failure by refining adaptation of constants
Constants that are adapted to a different supertype need to do this explicitly (not just by changing the type). Otherwise tree checkers will compute the original type and fail. This caused a test failure in pos/harmonize. The mystery is why this was not caught in the checkin tests.
1 parent ccaf1c3 commit bd86088

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1337,10 +1337,22 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13371337
}
13381338
}
13391339

1340+
/** Adapt an expression of constant type to a different constant type `tpe`. */
1341+
def adaptConstant(tree: Tree, tpe: ConstantType): Tree = {
1342+
def lit = Literal(tpe.value).withPos(tree.pos)
1343+
tree match {
1344+
case Literal(c) => lit
1345+
case tree @ Block(stats, expr) => tpd.cpy.Block(tree)(stats, adaptConstant(expr, tpe))
1346+
case tree =>
1347+
if (isIdempotentExpr(tree)) lit // See discussion in phase Literalize why we demand isIdempotentExpr
1348+
else Block(tree :: Nil, lit)
1349+
}
1350+
}
1351+
13401352
def adaptToSubType(wtp: Type): Tree = {
13411353
// try converting a constant to the target type
13421354
val folded = ConstFold(tree, pt)
1343-
if (folded ne tree) return folded
1355+
if (folded ne tree) return adaptConstant(folded, folded.tpe.asInstanceOf[ConstantType])
13441356
// drop type if prototype is Unit
13451357
if (pt isRef defn.UnitClass)
13461358
return tpd.Block(tree :: Nil, Literal(Constant(())))

0 commit comments

Comments
 (0)