diff options
-rw-r--r-- | src/dotty/tools/dotc/transform/Literalize.scala | 8 | ||||
-rw-r--r-- | tests/pos/constants.scala | 6 |
2 files changed, 14 insertions, 0 deletions
diff --git a/src/dotty/tools/dotc/transform/Literalize.scala b/src/dotty/tools/dotc/transform/Literalize.scala index 99b4b173b..622ad7fc8 100644 --- a/src/dotty/tools/dotc/transform/Literalize.scala +++ b/src/dotty/tools/dotc/transform/Literalize.scala @@ -14,6 +14,9 @@ import ast.Trees._ /** This phase rewrites idempotent expressions with constant types to Literals. * The constant types are eliminated by erasure, so we need to keep * the info about constantness in the trees. + * + * The phase also makes sure that the constant of a literal is the same as the constant + * in the type of the literal. */ class Literalize extends MiniPhaseTransform { thisTransform => import ast.tpd._ @@ -62,4 +65,9 @@ class Literalize extends MiniPhaseTransform { thisTransform => override def transformTypeApply(tree: TypeApply)(implicit ctx: Context, info: TransformerInfo): Tree = literalize(tree) + + override def transformLiteral(tree: Literal)(implicit ctx: Context, info: TransformerInfo): Tree = tree.tpe match { + case ConstantType(const) if tree.const.value != const.value => Literal(const) + case _ => tree + } }
\ No newline at end of file diff --git a/tests/pos/constants.scala b/tests/pos/constants.scala new file mode 100644 index 000000000..ca767d27d --- /dev/null +++ b/tests/pos/constants.scala @@ -0,0 +1,6 @@ +object Test { + + val x: Double = 2 + val y: Byte = 3 + +} |