Skip to content

Commit a72373c

Browse files
TheElectronWillEugene FlesselleKuceraMartin
committed
Constant fold all the number conversion methods
Co-authored-by: Eugene Flesselle <[email protected]> Co-authored-by: Martin Kucera <[email protected]>
1 parent ce65296 commit a72373c

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

compiler/src/dotty/tools/dotc/typer/ConstFold.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ 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.toByte, nme.toChar, nme.toShort, nme.toInt, nme.toFloat, nme.toLong, nme.toDouble)
2728

2829
def Apply[T <: Apply](tree: T)(using Context): T =
2930
tree.fun match
@@ -89,6 +90,14 @@ object ConstFold:
8990
case (nme.UNARY_- , FloatTag ) => Constant(-x.floatValue)
9091
case (nme.UNARY_- , DoubleTag ) => Constant(-x.doubleValue)
9192

93+
case (nme.toByte , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.byteValue)
94+
case (nme.toChar , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.charValue)
95+
case (nme.toShort , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.shortValue)
96+
case (nme.toInt , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.intValue)
97+
case (nme.toLong , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.longValue)
98+
case (nme.toFloat , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.floatValue)
99+
case (nme.toDouble, ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.doubleValue)
100+
92101
case _ => null
93102
}
94103

tests/pos/i13990.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
object Test:
3+
4+
inline val myInt = 1 << 4
5+
6+
// toLong
7+
inline val byte2Long = 0.toByte.toLong
8+
inline val char2Long = 'c'.toLong
9+
inline val short2Long = 0.toShort.toLong
10+
inline val int2Long = 0.toLong
11+
inline val long2Long = 0L.toLong
12+
inline val int2LongPropagated = myInt.toLong
13+
14+
// toInt
15+
inline val byte2Int = 0.toByte.toInt
16+
inline val char2Int = 'c'.toInt
17+
inline val short2Int = 0.toShort.toInt
18+
inline val int2Int = 0.toInt
19+
inline val long2Int = 0L.toInt
20+
inline val int2IntPropagated = myInt.toInt
21+
22+
// toByte
23+
inline val byte2Byte = 0.toByte.toByte
24+
inline val char2Byte = 'c'.toByte
25+
inline val short2Byte = 0.toShort.toByte
26+
inline val int2Byte = 0.toByte
27+
inline val long2Byte = 0L.toByte
28+
inline val int2BytePropagated = myInt.toByte
29+
30+
// chain everything
31+
inline val wow: Double = 1.toByte.toShort.toChar.toInt.toLong.toFloat.toDouble

0 commit comments

Comments
 (0)