Dotty: Overflow in Literal Types

Created on 9 Jan 2021  路  3Comments  路  Source: lampepfl/dotty

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.



md5-15e57723ec22f6d9c002037c1596ea01



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`:



md5-dad7bd01e811beb0ed249ebcc1ae2853



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:
```scala
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).

bug

All 3 comments

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: https://github.com/lampepfl/dotty/issues/2968#issuecomment-646957717

I wonder if this wouldn't work better as a Scalafix lint, since the gotcha is not gaping. It's not obvious to me that shift operators as inherited are terrible, so much as quirky. Is it likely that under 3.1, a misguided int-shift-long would trigger an error (an attempt to pass the int as long).

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Blaisorblade picture Blaisorblade  路  3Comments

smarter picture smarter  路  3Comments

andreaTP picture andreaTP  路  3Comments

adamgfraser picture adamgfraser  路  3Comments

travisbrown picture travisbrown  路  3Comments