Skip to content
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

Deprecate numeric conversions that lose precision (e.g., Long to Double) #8679

Merged
merged 2 commits into from Feb 3, 2020

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Feb 1, 2020

Int to Float, Long to Float and Long to Double are dangerous conversions and should never be done automatically.

Example:

scala> (33554435: Float).toInt
res0: Int = 33554436

Rebase #7405 and use ordinary warning system.

Int to Float, Long to Float and Long to Double are dangerous conversions
and should never be done automatically.
@scala-jenkins scala-jenkins added this to the 2.13.3 milestone Feb 1, 2020
A deprecation warning is emitted eagerly, even during
silent typechecking.
@som-snytt
Copy link
Contributor Author

Maybe escalate to error under -Xsource:3.

Copy link
Member

@lrytz lrytz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @smarter & @som-snytt

@lrytz lrytz merged commit 2ce4baa into scala:2.13.x Feb 3, 2020
@lrytz lrytz changed the title Warn on inharmonic conversions [ci: last-only] Deprecate numeric conversions that lose precision Feb 3, 2020
@lrytz lrytz added the release-notes worth highlighting in next release notes label Feb 3, 2020
@som-snytt
Copy link
Contributor Author

@lrytz thanks for improving the comment. #lazy

@lrytz
Copy link
Member

lrytz commented Feb 3, 2020

Except that it actually doesn't warn on the example I put in the comment 😄 (logged at scala/bug#10773 (comment))

@som-snytt som-snytt deleted the review/10773 branch February 3, 2020 14:25
@som-snytt
Copy link
Contributor Author

[warn] /home/jenkins/workspace/scala-2.13.x-validate-main@2/src/library/scala/jdk/DoubleAccumulator.scala:110:27: Automatic conversion from Long to Double is deprecated (since 2.13.1) because it loses precision. Write `.toDouble` instead.
[warn]         x(x.length - 1) = pv
[warn]                           ^
[warn] /home/jenkins/workspace/scala-2.13.x-validate-main@2/src/library/scala/jdk/DoubleAccumulator.scala:119:27: Automatic conversion from Long to Double is deprecated (since 2.13.1) because it loses precision. Write `.toDouble` instead.
[warn]         x(x.length - 1) = pv
[warn]                           ^

@lrytz it's a long getting stuck in the data array.

Also demonstrates that, like the equals warning in RefChecks, it's in your face.

@SethTisue SethTisue modified the milestones: 2.13.3, 2.13.2 Feb 4, 2020
@SethTisue SethTisue changed the title Deprecate numeric conversions that lose precision Deprecate numeric conversions that lose precision (e.g., Long to Double) Feb 11, 2020
@Sciss
Copy link

Sciss commented Jun 16, 2020

Is there a compiler switch to turn the warning off?

@som-snytt
Copy link
Contributor Author

@Sciss

scala> def f(i: Int): Float = i
                              ^
       warning: Widening conversion from Int to Float is deprecated because it loses precision. Write `.toFloat` instead.
def f(i: Int): Float

scala> :replay -Wconf:cat=deprecation&msg=conversion:s
replay> def f(i: Int): Float = i
def f(i: Int): Float


scala> 

@SethTisue
Copy link
Member

@Sciss see #8373 for full details on -Wconf

@som-snytt
Copy link
Contributor Author

som-snytt commented Jun 16, 2020

I recommend your favorite beverage and armchair and scalac -Wconf:help.

@lrytz
Copy link
Member

lrytz commented Jun 16, 2020

@som-snytt @SethTisue it's heart-warming to see you both get up in the morning and be thinking about -Wconf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes worth highlighting in next release notes
Projects
None yet
6 participants