Skip to content

Commit

Permalink
Use ordinary warning to respect silent treatment
Browse files Browse the repository at this point in the history
A deprecation warning is emitted eagerly, even during
silent typechecking.
  • Loading branch information
som-snytt committed Feb 1, 2020
1 parent c8ba3c4 commit fdcd611
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
11 changes: 6 additions & 5 deletions src/compiler/scala/tools/nsc/typechecker/Typers.scala
Expand Up @@ -1138,11 +1138,12 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
}
@inline def warnNumericWiden(tpSym: Symbol, ptSym: Symbol): Unit =
if (!isPastTyper) {
if (tpSym == IntClass && ptSym == FloatClass ||
tpSym == LongClass && (ptSym == FloatClass || ptSym == DoubleClass))
context.deprecationWarning(tree.pos, NoSymbol,
s"Automatic conversion from ${tpSym.name} to ${ptSym.name} is deprecated (since 2.13.1) because it loses precision. " +
s"Write `.to${ptSym.name}` instead.".stripMargin, "2.13.1")
val isInharmonic = (
tpSym == IntClass && ptSym == FloatClass ||
tpSym == LongClass && (ptSym == FloatClass || ptSym == DoubleClass)
)
if (isInharmonic)
context.warning(tree.pos, s"Automatic conversion from ${tpSym.name} to ${ptSym.name} is deprecated (since 2.13.1) because it loses precision. Write `.to${ptSym.name}` instead.")
else if (settings.warnNumericWiden) context.warning(tree.pos, "implicit numeric widening")
}

Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/Function0.scala
Expand Up @@ -11,7 +11,7 @@
*/

// GENERATED CODE: DO NOT EDIT.
// genprod generated these sources at: 2019-06-18T20:49:11.955Z
// genprod generated these sources at: 2020-02-01T04:13:22.795Z

package scala

Expand Down
16 changes: 8 additions & 8 deletions test/files/neg/deprecated_widening.check
@@ -1,21 +1,21 @@
deprecated_widening.scala:3: warning: Automatic conversion from Int to Float is deprecated (since 2.13.1) because it loses precision. Write `.toFloat` instead.
deprecated_widening.scala:5: warning: Automatic conversion from Int to Float is deprecated (since 2.13.1) because it loses precision. Write `.toFloat` instead.
val i_f: Float = i // deprecated
^
deprecated_widening.scala:5: warning: Automatic conversion from Long to Float is deprecated (since 2.13.1) because it loses precision. Write `.toFloat` instead.
deprecated_widening.scala:7: warning: Automatic conversion from Long to Float is deprecated (since 2.13.1) because it loses precision. Write `.toFloat` instead.
val l_f: Float = l // deprecated
^
deprecated_widening.scala:6: warning: Automatic conversion from Long to Double is deprecated (since 2.13.1) because it loses precision. Write `.toDouble` instead.
deprecated_widening.scala:8: warning: Automatic conversion from Long to Double is deprecated (since 2.13.1) because it loses precision. Write `.toDouble` instead.
val l_d: Double = l // deprecated
^
deprecated_widening.scala:10: 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.
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
^
deprecated_widening.scala:12: warning: method long2float in object Long is deprecated (since 2.13.1): Implicit conversion from Long to Float is dangerous because it loses precision. Write `.toFloat` instead.
deprecated_widening.scala:14: warning: method long2float in object Long is deprecated (since 2.13.1): Implicit conversion from Long to Float is dangerous because it loses precision. Write `.toFloat` instead.
implicitly[Long => Float] // deprecated
^
deprecated_widening.scala:13: warning: method long2double in object Long is deprecated (since 2.13.1): Implicit conversion from Long to Double is dangerous because it loses precision. Write `.toDouble` instead.
deprecated_widening.scala:15: warning: method long2double in object Long is deprecated (since 2.13.1): Implicit conversion from Long to Double is dangerous because it loses precision. Write `.toDouble` instead.
implicitly[Long => Double] // deprecated
^
error: No warnings can be incurred under -Werror.
6 warnings found
one error found
6 warnings
1 error
1 change: 0 additions & 1 deletion test/files/neg/deprecated_widening.flags

This file was deleted.

6 changes: 6 additions & 0 deletions test/files/neg/deprecated_widening.scala
@@ -1,3 +1,5 @@
// scalac: -deprecation -Werror
//
object Test {
def foo(i: Int, l: Long): Unit = {
val i_f: Float = i // deprecated
Expand All @@ -12,4 +14,8 @@ object Test {
implicitly[Long => Float] // deprecated
implicitly[Long => Double] // deprecated
}

// don't leak silent warning from float conversion
val n = 42
def clean = n max 27
}
12 changes: 6 additions & 6 deletions test/files/run/number-parsing.scala
Expand Up @@ -8,12 +8,12 @@ object Test {
assert((MinusZero: scala.Float) == (PlusZero: scala.Float))
assert(!(MinusZero equals PlusZero))

List(
-5f.max(2) ,
-5f max 2 ,
-5.max(2) ,
-5 max 2
) foreach (num => assert(num == 2))
assert(List[AnyVal](
-5f.max(2),
-5f max 2,
-5.max(2),
-5 max 2,
).forall(_ == 2))
}

case class Foo(val x: Double) {
Expand Down

0 comments on commit fdcd611

Please sign in to comment.