Skip to content

Commit b65c675

Browse files
Merge pull request #12212 from dotty-staging/fix-12208
Fix constant conversion to singleton types
2 parents 855f572 + 8699435 commit b65c675

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

compiler/src/dotty/tools/dotc/core/Constants.scala

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package core
34

45
import Types._, Symbols._, Contexts._
56
import printing.Printer
67
import printing.Texts.Text
8+
import Decorators._
79

810
object Constants {
911

@@ -161,25 +163,29 @@ object Constants {
161163
}
162164
case pt => pt
163165
}
164-
val target = classBound(pt).typeSymbol
165-
if (target == tpe.typeSymbol)
166-
this
167-
else if ((target == defn.ByteClass) && isByteRange)
168-
Constant(byteValue)
169-
else if (target == defn.ShortClass && isShortRange)
170-
Constant(shortValue)
171-
else if (target == defn.CharClass && isCharRange)
172-
Constant(charValue)
173-
else if (target == defn.IntClass && isIntRange)
174-
Constant(intValue)
175-
else if (target == defn.LongClass && isLongRange)
176-
Constant(longValue)
177-
else if (target == defn.FloatClass && isFloatRange)
178-
Constant(floatValue)
179-
else if (target == defn.DoubleClass && isNumeric)
180-
Constant(doubleValue)
181-
else
182-
null
166+
pt match
167+
case ConstantType(value) if value == this => this
168+
case _: SingletonType => null
169+
case _ =>
170+
val target = classBound(pt).typeSymbol
171+
if (target == tpe.typeSymbol)
172+
this
173+
else if ((target == defn.ByteClass) && isByteRange)
174+
Constant(byteValue)
175+
else if (target == defn.ShortClass && isShortRange)
176+
Constant(shortValue)
177+
else if (target == defn.CharClass && isCharRange)
178+
Constant(charValue)
179+
else if (target == defn.IntClass && isIntRange)
180+
Constant(intValue)
181+
else if (target == defn.LongClass && isLongRange)
182+
Constant(longValue)
183+
else if (target == defn.FloatClass && isFloatRange)
184+
Constant(floatValue)
185+
else if (target == defn.DoubleClass && isNumeric)
186+
Constant(doubleValue)
187+
else
188+
null
183189
}
184190

185191
def stringValue: String = value.toString

tests/neg/i12208.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val pi: 3.14 = 3 // error

0 commit comments

Comments
 (0)