Skip to content

Overflow in Literal Types #11041

Closed
Closed
@jackkoenig

Description

@jackkoenig

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions