Skip to content

Commit 79c4490

Browse files
committed
More robust constant folding
Can now also convert a constant to a type that's lower-bounded by a primitive numeric type.
1 parent 1682983 commit 79c4490

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,15 @@ object Constants {
167167
/** Convert constant value to conform to given type.
168168
*/
169169
def convertTo(pt: Type)(implicit ctx: Context): Constant = {
170-
val target = pt.typeSymbol
170+
def lowerBound(pt: Type): Type = pt.dealias.stripTypeVar match {
171+
case tref: TypeRef if !tref.symbol.isClass => lowerBound(tref.info.bounds.lo)
172+
case param: PolyParam => lowerBound(ctx.typeComparer.bounds(param).lo)
173+
case pt => pt
174+
}
175+
val target = lowerBound(pt).typeSymbol
171176
if (target == tpe.typeSymbol)
172177
this
173-
else if (target == defn.ByteClass && isByteRange)
178+
else if ((target == defn.ByteClass) && isByteRange)
174179
Constant(byteValue)
175180
else if (target == defn.ShortClass && isShortRange)
176181
Constant(shortValue)

0 commit comments

Comments
 (0)