Skip to content

Commit

Permalink
Merge pull request #13792 from dwijnand/lint-too-big-constants-pr
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Nov 3, 2021
2 parents fa56a4e + a42588d commit 83effc3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
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

0 comments on commit 83effc3

Please sign in to comment.