Skip to content

Commit

Permalink
added also checks for double
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianpage committed Feb 26, 2020
1 parent fcfc47d commit c8cb24b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/reflect/scala/reflect/internal/Constants.scala
Expand Up @@ -64,7 +64,9 @@ trait Constants extends api.Constants {
def isIntRange: Boolean = ByteTag <= tag && tag <= IntTag
def isLongRange: Boolean = ByteTag <= tag && tag <= LongTag
// Float has a Fraction of 23 Bits + 1 implicit leading Bit so the total precision is 24 Bit (see https://en.wikipedia.org/wiki/Single-precision_floating-point_format)
def isFloatRange: Boolean = ByteTag <= tag && tag <= FloatTag && longValue <= (1L << 24) && longValue >= -(1L << 24)
def isFloatRepresentable: Boolean = ByteTag <= tag && tag <= FloatTag && (tag != IntTag && tag != LongTag || longValue <= (1L << 24) && longValue >= -(1L << 24))
// Double has a Fraction of 53 Bits + 1 implicit leading Bit => 54 Bit
def isDoubleRepresentable: Boolean = ByteTag <= tag && tag <= DoubleTag && (tag != LongTag || longValue <= (1L << 54) && longValue >= -(1L << 54))
def isNumeric: Boolean = ByteTag <= tag && tag <= DoubleTag
def isNonUnitAnyVal = BooleanTag <= tag && tag <= DoubleTag
def isSuitableLiteralType = BooleanTag <= tag && tag <= NullTag
Expand Down Expand Up @@ -219,9 +221,9 @@ trait Constants extends api.Constants {
Constant(intValue)
else if (target == LongClass && isLongRange)
Constant(longValue)
else if (target == FloatClass && isFloatRange)
else if (target == FloatClass && isFloatRepresentable)
Constant(floatValue)
else if (target == DoubleClass && isNumeric)
else if (target == DoubleClass && isDoubleRepresentable)
Constant(doubleValue)
else
null
Expand Down
14 changes: 13 additions & 1 deletion test/files/neg/deprecated_widening.check
Expand Up @@ -13,6 +13,18 @@ deprecated_widening.scala:23: warning: Automatic conversion from Long to Float i
deprecated_widening.scala:25: warning: Automatic conversion from Long to Float is deprecated (since 2.13.1) because it loses precision. Write `.toFloat` instead.
val truncatedNegFloat: Float = - 16777217L // deprecated
^
deprecated_widening.scala:28: warning: Automatic conversion from Int to Float is deprecated (since 2.13.1) because it loses precision. Write `.toFloat` instead.
val truncatedPosFloatI:Float = 16777217 // deprecated
^
deprecated_widening.scala:30: warning: Automatic conversion from Int to Float is deprecated (since 2.13.1) because it loses precision. Write `.toFloat` instead.
val truncatedNegFloatI: Float = - 16777217 // deprecated
^
deprecated_widening.scala:33: warning: Automatic conversion from Long to Double is deprecated (since 2.13.1) because it loses precision. Write `.toDouble` instead.
val truncatedPosDouble:Double = 18014398509481985L // deprecated
^
deprecated_widening.scala:35: warning: Automatic conversion from Long to Double is deprecated (since 2.13.1) because it loses precision. Write `.toDouble` instead.
val truncatedNegDouble: Double = - 18014398509481985L // deprecated
^
deprecated_widening.scala:12: warning: method int2float in object Int is deprecated (since 2.13.1): Implicit conversion from Int to Float is dangerous because it loses precision. Write `.toFloat` instead.
implicitly[Int => Float] // deprecated
^
Expand All @@ -23,5 +35,5 @@ deprecated_widening.scala:15: warning: method long2double in object Long is depr
implicitly[Long => Double] // deprecated
^
error: No warnings can be incurred under -Werror.
8 warnings
12 warnings
1 error
10 changes: 10 additions & 0 deletions test/files/neg/deprecated_widening.scala
Expand Up @@ -23,4 +23,14 @@ object Test {
val truncatedPosFloat:Float = 16777217L // deprecated
val negFloat: Float = - 16777216L // OK
val truncatedNegFloat: Float = - 16777217L // deprecated

val posFloatI:Float = 16777216 // OK
val truncatedPosFloatI:Float = 16777217 // deprecated
val negFloatI: Float = - 16777216 // OK
val truncatedNegFloatI: Float = - 16777217 // deprecated

val posDouble:Double = 18014398509481984L// OK
val truncatedPosDouble:Double = 18014398509481985L // deprecated
val negDouble: Double = - 18014398509481984L // OK
val truncatedNegDouble: Double = - 18014398509481985L // deprecated
}

0 comments on commit c8cb24b

Please sign in to comment.