Skip to content

Commit

Permalink
Constant fold all the number conversion methods (#17446)
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]>

Fixes #13990. Made during the Scala Spree :)
  • Loading branch information
TheElectronWill authored May 24, 2023
2 parents 473e191 + d8832bf commit 5262680
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
12 changes: 11 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,11 @@ 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.toChar, nme.toInt, nme.toFloat, nme.toLong, nme.toDouble,
// toByte and toShort are NOT included because we cannot write
// the type of a constant byte or short
)

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

case (nme.toChar , _ ) if x.isNumeric => Constant(x.charValue)
case (nme.toInt , _ ) if x.isNumeric => Constant(x.intValue)
case (nme.toLong , _ ) if x.isNumeric => Constant(x.longValue)
case (nme.toFloat , _ ) if x.isNumeric => Constant(x.floatValue)
case (nme.toDouble, _ ) if x.isNumeric => Constant(x.doubleValue)

case _ => null
}

Expand Down
2 changes: 1 addition & 1 deletion tests/neg/serialversionuid-not-const.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@SerialVersionUID(13l.toLong) class C1 extends Serializable // error
@SerialVersionUID(13l.toLong) class C1 extends Serializable // OK because toLong is constant-folded
@SerialVersionUID(13l) class C2 extends Serializable // OK
@SerialVersionUID(13.asInstanceOf[Long]) class C3 extends Serializable // error
@SerialVersionUID(Test.bippy) class C4 extends Serializable // error
Expand Down
26 changes: 26 additions & 0 deletions tests/pos/i13990.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

object Test:

inline val myInt = 1 << 6

// toLong
inline val char2Long: 99L = 'c'.toLong
inline val int2Long: 0L = 0.toLong
inline val long2Long: 0L = 0L.toLong
inline val int2LongPropagated: 64L = myInt.toLong

// toInt
inline val char2Int: 99 = 'c'.toInt
inline val int2Int: 0 = 0.toInt
inline val long2Int: 0 = 0L.toInt
inline val long2IntWrapped: -2147483648 = 2147483648L.toInt
inline val int2IntPropagated: 64 = myInt.toInt

// toChar
inline val char2Char: 'c' = 'c'.toChar
inline val int2Char: 'c' = 99.toChar
inline val long2Char: 'c' = 99L.toChar
inline val int2CharPropagated: '@' = myInt.toChar

// chain everything
inline val wow: 1.0 = 1.toChar.toInt.toLong.toFloat.toDouble

0 comments on commit 5262680

Please sign in to comment.