Skip to content

Soundness bug in structural types #17581

@Florian3k

Description

@Florian3k

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]                      ^^^^^^^^

Metadata

Metadata

Assignees

Labels

itype:bugitype:soundnessSoundness bug (it lets us compile code that crashes at runtime with a ClassCastException)

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions