Skip to content

Commit

Permalink
Constant fold all the number conversion methods
Browse files Browse the repository at this point in the history
Co-authored-by: Eugene Flesselle <[email protected]>
Co-authored-by: Martin Kucera <[email protected]>
  • Loading branch information
3 people committed May 9, 2023
1 parent ce65296 commit 7be3311
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
11 changes: 10 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/ConstFold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ object ConstFold:
nme.ADD, nme.SUB, nme.MUL, nme.DIV, nme.MOD)

val foldedUnops = Set[Name](
nme.UNARY_!, nme.UNARY_~, nme.UNARY_+, nme.UNARY_-)
nme.UNARY_!, nme.UNARY_~, nme.UNARY_+, nme.UNARY_-,
nme.toByte, nme.toChar, nme.toShort, nme.toInt, nme.toFloat, nme.toLong, nme.toDouble)

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

case (nme.toByte , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.byteValue)
case (nme.toChar , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.charValue)
case (nme.toShort , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.shortValue)
case (nme.toInt , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.intValue)
case (nme.toLong , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.longValue)
case (nme.toFloat , ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.floatValue)
case (nme.toDouble, ByteTag | CharTag | ShortTag | IntTag | LongTag | FloatTag | DoubleTag ) => Constant(x.doubleValue)

case _ => null
}

Expand Down
31 changes: 31 additions & 0 deletions tests/pos/13990.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

object Test:

inline val myInt = 1 << 4

// toLong
inline val byte2Long = 0.toByte.toLong
inline val char2Long = 'c'.toLong
inline val short2Long = 0.toShort.toLong
inline val int2Long = 0.toLong
inline val long2Long = 0L.toLong
inline val int2LongPropagated = myInt.toLong

// toInt
inline val byte2Int = 0.toByte.toInt
inline val char2Int = 'c'.toInt
inline val short2Int = 0.toShort.toInt
inline val int2Int = 0.toInt
inline val long2Int = 0L.toInt
inline val int2IntPropagated = myInt.toInt

// toByte
inline val byte2Byte = 0.toByte.toByte
inline val char2Byte = 'c'.toByte
inline val short2Byte = 0.toShort.toByte
inline val int2Byte = 0.toByte
inline val long2Byte = 0L.toByte
inline val int2BytePropagated = myInt.toByte

// chain everything
inline val wow: Double = 1.toByte.toShort.toChar.toInt.toLong.toFloat.toDouble

0 comments on commit 7be3311

Please sign in to comment.