Skip to content

Literal singleton type accepting wrong value #12208

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

Closed
kavedaa opened this issue Apr 23, 2021 · 1 comment · Fixed by #12212
Closed

Literal singleton type accepting wrong value #12208

kavedaa opened this issue Apr 23, 2021 · 1 comment · Fixed by #12212
Milestone

Comments

@kavedaa
Copy link

kavedaa commented Apr 23, 2021

Compiler version

3.0.0-RC3

Minimized code

scala> val pi: 3.14 = 3
val pi: 3.14 = 3.0

scala> pi
val res0: Double = 3.14

Expectation

2.13.4

scala> val pi: 3.14 = 3
                      ^
       error: type mismatch;
        found   : Double(3.0)
        required: 3.14
@som-snytt
Copy link
Contributor

Worth adding this was a good one. Earlier I checked List[3.14](3.14, 3). I also checked in the example that 3.0 is stored in the field, but I didn't check what REPL is reporting when it prints the RHS. I also wondered whether it matters what is stored in the field.

No one would complain about

scala> var x: 3.14 = null.asInstanceOf[3.14]
var x: 3.14 = 0.0

oh wait

scala> x                                                                                                            
val res1: Double = 0.0

which I guess is due to unstable x but still surprises me. x has to be 3.14 even if I lied in initializing it.

Scala 2 throws in the isInstanceOf case. I don't know why without consulting code or spec.

scala> trait T { inline val pi = 3.14 }
// defined trait T

scala> (null: T).pi
val res0: Double = 3.14

scala> null.asInstanceOf[T].pi
1 |null.asInstanceOf[T].pi
  |^^^^^^^^^^^^^^^^^^^^^^^
  |getter pi is declared as erased, but is in fact used

michelou pushed a commit to michelou/scala3 that referenced this issue Apr 26, 2021
@Kordyjan Kordyjan added this to the 3.0.1 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants