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

Overflow in Literal Types #11041

Closed
jackkoenig opened this issue Jan 9, 2021 · 2 comments
Closed

Overflow in Literal Types #11041

jackkoenig opened this issue Jan 9, 2021 · 2 comments

Comments

@jackkoenig
Copy link

Minimized code

Scala 2 and 3 both warn when left-shifting an Int by a Long:

val z = (1: Int) << 33L
method << in class Int is deprecated since 2.12.7: shifting a value by a `Long` argument is deprecated (except when the value is a `Long`).
Call `toInt` on the argument to maintain the current behavior and avoid the deprecation warning.

https://scastie.scala-lang.org/cdcgyKBlRCWY5K3kGbG4Tg

But there is no warning in Scala 3 when we don't upcast to Int:

val z = 1 << 33L
println(z.getClass) // int

https://scastie.scala-lang.org/nfhS9dZbQ1yyKhZsGmx2Mg

Using : Nothing to see what the compiler knows about the types shows an overflow in the Literal type of z:

val z: Nothing = 1 << 33L
Found:    (2 : Int)
Required: Nothing

https://scastie.scala-lang.org/CLAqsDtHSlKu05yvvKJhmw

Using literal types in Scala 2.13.4 at least still prints the warning:

val z: 2 = 1 << 33L
method << in class Int is deprecated (since 2.12.7): shifting a value by a `Long` argument is deprecated (except when the value is a `Long`).
Call `toInt` on the argument to maintain the current behavior and avoid the deprecation warning.

https://scastie.scala-lang.org/5oZ4UEsNSzi02gxjh3n1sw

Expectation

I would expect a warning in Scala 3 like there is in Scala 2 (better yet would be an error if possible).

@griggt
Copy link
Collaborator

griggt commented Jan 9, 2021

See also #2968, which contains the same 1 << 33L example, although the behavior has since changed (perhaps as a result of #6366).

The lack of the deprecation warning when constant folded is also noted in that ticket: #2968 (comment)

@odersky
Copy link
Contributor

odersky commented Jan 12, 2021

Closing this one; keeping #2968, which has more context.

@odersky odersky closed this as completed Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants