From 79c4490a823f779251f4f2b4a332e639e6ad00d6 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 3 Feb 2014 22:34:15 +0100 Subject: More robust constant folding Can now also convert a constant to a type that's lower-bounded by a primitive numeric type. --- src/dotty/tools/dotc/core/Constants.scala | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/dotty/tools') diff --git a/src/dotty/tools/dotc/core/Constants.scala b/src/dotty/tools/dotc/core/Constants.scala index 50bebefe9..b04ec0e89 100644 --- a/src/dotty/tools/dotc/core/Constants.scala +++ b/src/dotty/tools/dotc/core/Constants.scala @@ -167,10 +167,15 @@ object Constants { /** Convert constant value to conform to given type. */ def convertTo(pt: Type)(implicit ctx: Context): Constant = { - val target = pt.typeSymbol + def lowerBound(pt: Type): Type = pt.dealias.stripTypeVar match { + case tref: TypeRef if !tref.symbol.isClass => lowerBound(tref.info.bounds.lo) + case param: PolyParam => lowerBound(ctx.typeComparer.bounds(param).lo) + case pt => pt + } + val target = lowerBound(pt).typeSymbol if (target == tpe.typeSymbol) this - else if (target == defn.ByteClass && isByteRange) + else if ((target == defn.ByteClass) && isByteRange) Constant(byteValue) else if (target == defn.ShortClass && isShortRange) Constant(shortValue) -- cgit v1.2.3