Skip to content

Warn on lossy conversion of literals & constants #13792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID]:
OverrideTypeMismatchErrorID,
OverrideErrorID,
MatchableWarningID,
CannotExtendFunctionID
CannotExtendFunctionID,
LossyWideningConstantConversionID

def errorNumber = ordinal - 2

Expand Down
7 changes: 7 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,13 @@ import transform.SymUtils._
|"""
}

class LossyWideningConstantConversion(sourceType: Type, targetType: Type)(using Context)
extends Message(LossyWideningConstantConversionID):
def kind = "Lossy Conversion"
def msg = em"""|Widening conversion from $sourceType to $targetType loses precision.
|Write `.to$targetType` instead.""".stripMargin
def explain = ""

class PatternMatchExhaustivity(uncoveredFn: => String, hasMore: Boolean)(using Context)
extends Message(PatternMatchExhaustivityID) {
def kind = "Pattern Match Exhaustivity"
Expand Down
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,11 @@ class Typer extends Namer
else if (target.isRef(defn.FloatClass))
tree.kind match {
case Whole(16) => // cant parse hex literal as float
case _ => return lit(floatFromDigits(digits))
case _ =>
val float = floatFromDigits(digits)
if digits.toIntOption.exists(_ != float.toInt) then
report.warning(LossyWideningConstantConversion(defn.IntType, target), tree.srcPos)
return lit(float)
}
else if (target.isRef(defn.DoubleClass))
tree.kind match {
Expand Down Expand Up @@ -3733,6 +3737,12 @@ class Typer extends Namer
case ConstantType(x) =>
val converted = x.convertTo(pt)
if converted != null && (converted ne x) then
val cls = pt.classSymbol
if x.tag == IntTag && cls == defn.FloatClass && x.intValue.toFloat.toInt != x.intValue
|| x.tag == LongTag && cls == defn.FloatClass && x.longValue.toFloat.toLong != x.longValue
|| x.tag == LongTag && cls == defn.DoubleClass && x.longValue.toDouble.toLong != x.longValue
then
report.warning(LossyWideningConstantConversion(x.tpe, pt), tree.srcPos)
return adaptConstant(tree, ConstantType(converted))
case _ =>

Expand Down
30 changes: 30 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i11333.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- [E167] Lossy Conversion Error: tests/neg-custom-args/fatal-warnings/i11333.scala:2:19 -------------------------------
2 | val f1: Float = 123456789 // error
| ^^^^^^^^^
| Widening conversion from Int to Float loses precision.
| Write `.toFloat` instead.
-- [E167] Lossy Conversion Error: tests/neg-custom-args/fatal-warnings/i11333.scala:3:19 -------------------------------
3 | val d1: Double = 1234567890123456789L // error
| ^^^^^^^^^^^^^^^^^^^^
| Widening conversion from Long to Double loses precision.
| Write `.toDouble` instead.
-- [E167] Lossy Conversion Error: tests/neg-custom-args/fatal-warnings/i11333.scala:4:19 -------------------------------
4 | val f2: Float = 123456789L // error
| ^^^^^^^^^^
| Widening conversion from Long to Float loses precision.
| Write `.toFloat` instead.
-- [E167] Lossy Conversion Error: tests/neg-custom-args/fatal-warnings/i11333.scala:10:21 ------------------------------
10 | val f1_b: Float = i1 // error
| ^^
| Widening conversion from Int to Float loses precision.
| Write `.toFloat` instead.
-- [E167] Lossy Conversion Error: tests/neg-custom-args/fatal-warnings/i11333.scala:11:21 ------------------------------
11 | val d1_b: Double = l1 // error
| ^^
| Widening conversion from Long to Double loses precision.
| Write `.toDouble` instead.
-- [E167] Lossy Conversion Error: tests/neg-custom-args/fatal-warnings/i11333.scala:12:21 ------------------------------
12 | val f2_b: Float = l2 // error
| ^^
| Widening conversion from Long to Float loses precision.
| Write `.toFloat` instead.
12 changes: 12 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i11333.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class C:
val f1: Float = 123456789 // error
val d1: Double = 1234567890123456789L // error
val f2: Float = 123456789L // error

inline val i1 = 123456789
inline val l1 = 1234567890123456789L
inline val l2 = 123456789L

val f1_b: Float = i1 // error
val d1_b: Double = l1 // error
val f2_b: Float = l2 // error