Skip to content

Commit d220558

Browse files
committed
Add checkPostCondition to Literalize
1 parent a098fd3 commit d220558

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/dotty/tools/dotc/transform/Literalize.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import core.Flags._
1010
import core.Decorators._
1111
import core.StdNames.nme
1212
import ast.Trees._
13+
import dotty.tools.dotc.ast.tpd
14+
import dotty.tools.dotc.core.Constants._
1315

1416
/** This phase rewrites idempotent expressions with constant types to Literals.
1517
* The constant types are eliminated by erasure, so we need to keep
@@ -70,4 +72,19 @@ class Literalize extends MiniPhaseTransform { thisTransform =>
7072
case ConstantType(const) if tree.const.value != const.value => Literal(const)
7173
case _ => tree
7274
}
75+
76+
/** Check that all literals have types matchin underlying constants
77+
*/
78+
override def checkPostCondition(tree: Tree)(implicit ctx: Context): Unit = {
79+
tree match {
80+
case Literal(c @ Constant(treeValue)) =>
81+
tree.tpe match {
82+
case ConstantType(typeValue) =>
83+
assert(treeValue == typeValue.value, i"Type of Literal $tree is inconsistent with underlying constant")
84+
case tpe =>
85+
assert(c.tpe =:= tpe, i"Type of Literal $tree is inconsistent with underlying constant type ${c.tpe}")
86+
}
87+
case _ =>
88+
}
89+
}
7390
}

0 commit comments

Comments
 (0)