Skip to content

Soundness bug in structural types #17581

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
Florian3k opened this issue May 25, 2023 · 4 comments · Fixed by #17977
Closed

Soundness bug in structural types #17581

Florian3k opened this issue May 25, 2023 · 4 comments · Fixed by #17977
Assignees
Labels
itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)
Milestone

Comments

@Florian3k
Copy link
Contributor

Compiler version

3.2.2, 3.3.0

Minimized code

import scala.reflect.Selectable.reflectiveSelectable

class Test

def foo[A <: {def bar: Any}](ob: A) = ob.bar

@main def main = foo(new Test)

Output

Code compiles and fails at runtime with:

Exception in thread "main" java.lang.NoSuchMethodException: Test.bar()
	at java.lang.Class.getMethod(Class.java:1786)
	at scala.reflect.Selectable.applyDynamic(Selectable.scala:38)
	at scala.reflect.Selectable.applyDynamic$(Selectable.scala:11)
	at scala.reflect.Selectable$DefaultSelectable.applyDynamic(Selectable.scala:51)
	at scala.reflect.Selectable.selectDynamic(Selectable.scala:28)
	at scala.reflect.Selectable.selectDynamic$(Selectable.scala:11)
	at scala.reflect.Selectable$DefaultSelectable.selectDynamic(Selectable.scala:51)
	at v1$package$.foo(v1.scala:5)
	at v1$package$.main(v1.scala:7)
	at main.main(v1.scala:7)

Expectation

Shouldn't compile

More information

The following variants don't compile (as expected):

import scala.reflect.Selectable.reflectiveSelectable

class Test

def foo[A <: {def bar: Int}](ob: A) = ob.bar // Int instead of Any

@main def main = foo(new Test)

Result:

[error] ./v2.scala:7:22
[error] Found:    Test
[error] Required: Object{def bar: Int}
[error] @main def main = foo(new Test)
[error]                      ^^^^^^^^
import scala.reflect.Selectable.reflectiveSelectable

class Test

def foo[A <: {def bar(): Any}](ob: A) = ob.bar() // bar() instead of bar

@main def main = foo(new Test)

Result:

[error] ./v3.scala:7:22
[error] Found:    Test
[error] Required: Object{def bar(): Any}
[error] @main def main = foo(new Test)
[error]                      ^^^^^^^^
@Florian3k Florian3k added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 25, 2023
@prolativ
Copy link
Contributor

The problem seems independent of Selectable:

class Test
val test: { def bar: Any } = new Test

This compiles although it shouldn't

@scala-center-bot
Copy link

This issue was picked for the Issue Spree No. 32 of 20 June 2023 which takes place in 7 days. @dwijnand, @eugenefle, @hamzaremmal, @iusildra will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

@SethTisue
Copy link
Member

SethTisue commented Jun 14, 2023

severity level: pants on fire 🙀

well, to be fair, note that the bug is specific to the return type of bar being Any, so that's.... somewhat reassuring

as expected, Scala 2 rejects it

@mbovel
Copy link
Member

mbovel commented Jun 19, 2023

I see now that this has been fixed. Should we find another issue for tomorrow's spree?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants