Skip to content

Commit a098fd3

Browse files
committed
Fix #300: Make literals have the same constants as their types.
1 parent 3a68e50 commit a098fd3

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import ast.Trees._
1414
/** This phase rewrites idempotent expressions with constant types to Literals.
1515
* The constant types are eliminated by erasure, so we need to keep
1616
* the info about constantness in the trees.
17+
*
18+
* The phase also makes sure that the constant of a literal is the same as the constant
19+
* in the type of the literal.
1720
*/
1821
class Literalize extends MiniPhaseTransform { thisTransform =>
1922
import ast.tpd._
@@ -62,4 +65,9 @@ class Literalize extends MiniPhaseTransform { thisTransform =>
6265

6366
override def transformTypeApply(tree: TypeApply)(implicit ctx: Context, info: TransformerInfo): Tree =
6467
literalize(tree)
68+
69+
override def transformLiteral(tree: Literal)(implicit ctx: Context, info: TransformerInfo): Tree = tree.tpe match {
70+
case ConstantType(const) if tree.const.value != const.value => Literal(const)
71+
case _ => tree
72+
}
6573
}

tests/pos/constants.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
3+
val x: Double = 2
4+
val y: Byte = 3
5+
6+
}

0 commit comments

Comments
 (0)