@@ -23,7 +23,11 @@ object ConstFold:
2323 nme.ADD , nme.SUB , nme.MUL , nme.DIV , nme.MOD )
2424
2525 val foldedUnops = Set [Name ](
26- nme.UNARY_! , nme.UNARY_~ , nme.UNARY_+ , nme.UNARY_- )
26+ nme.UNARY_! , nme.UNARY_~ , nme.UNARY_+ , nme.UNARY_- ,
27+ nme.toChar, nme.toInt, nme.toFloat, nme.toLong, nme.toDouble,
28+ // toByte and toShort are NOT included because we cannot write
29+ // the type of a constant byte or short
30+ )
2731
2832 def Apply [T <: Apply ](tree : T )(using Context ): T =
2933 tree.fun match
@@ -89,6 +93,12 @@ object ConstFold:
8993 case (nme.UNARY_- , FloatTag ) => Constant (- x.floatValue)
9094 case (nme.UNARY_- , DoubleTag ) => Constant (- x.doubleValue)
9195
96+ case (nme.toChar , _ ) if x.isNumeric => Constant (x.charValue)
97+ case (nme.toInt , _ ) if x.isNumeric => Constant (x.intValue)
98+ case (nme.toLong , _ ) if x.isNumeric => Constant (x.longValue)
99+ case (nme.toFloat , _ ) if x.isNumeric => Constant (x.floatValue)
100+ case (nme.toDouble, _ ) if x.isNumeric => Constant (x.doubleValue)
101+
92102 case _ => null
93103 }
94104
0 commit comments