-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Output type mismatch in SAM #18315
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
Comments
It looks like it's not really only about the output type. The following code also stopped compiling because of the regression: trait Foo {
type XXX
def foo(x: Int): Int
}
def makeFoo: Foo { type XXX = String } =
x => x Compilation error (with [error] Foo.scala:7:3
[error] Found: <notype>
[error] Required: Foo{type XXX = String}
[error]
[error] Explanation
[error] ===========
[error]
[error] Tree: {
[error] def $anonfun(x: Int): Int =
[error] {
[error] x
[error] }
[error] closure($anonfun:<notype>)
[error] }
[error] I tried to show that
[error] <notype>
[error] conforms to
[error] Foo{type XXX = String}
[error] but the comparison trace ended with `false`:
[error]
[error] ==> <notype> <: Foo{type XXX = String}
[error] ==> <notype> <: Foo
[error] <== <notype> <: Foo = false
[error] <== <notype> <: Foo{type XXX = String} = false
[error]
[error] The tests were made under the empty constraint
[error] x => x
[error] ^^^^^^ The error disappears when |
It looks like this only worked by chance. Scala 2 doesn't support refinements in SAM types, and supporting them properly when a SAM type closure is expanded into an anonymous class is non-trivial (the refinement has to be moved inside the class). For example, Scala 3.3.0 compiles the following code, but it crashes with trait Foo {
var x: Int = 1
type XXX
def foo(x: Int): Int
}
object Test {
def makeFoo: Foo { type XXX = String } =
x => x
} java.lang.AssertionError: assertion failed:
Found: Object with Foo {...}
Required: Foo{type XXX = String} I see you've put this in the 3.3.2 milestone, but there is currently no release-3.3.2 branch and main is now on 3.4.x, so I don't know if #18201 is in what will become 3.3.2 /cc @Kordyjan . |
If we can't make this work, we should at least make the error message clear, warning users that SAM conversions don't work with refinements |
This was dropped in scala#18201 which restricted SAM types to valid parent types, but it turns out that there is code in the wild that relies on refinements being allowed here. To support this properly, we had to enhance ExpandSAMs to move refinements into type members to pass Ycheck (previous Scala 3 releases would accept the code in tests/run/i18315.scala but fail Ycheck). Fixes scala#18315.
This was dropped in scala#18201 which restricted SAM types to valid parent types, but it turns out that there is code in the wild that relies on refinements being allowed here. To support this properly, we had to enhance ExpandSAMs to move refinements into type members to pass Ycheck (previous Scala 3 releases would accept the code in tests/run/i18315.scala but fail Ycheck). Fixes scala#18315.
This was dropped in #18201 which restricted SAM types to valid parent types, but it turns out that there is code in the wild that relies on refinements being allowed here. To support this properly, we had to enhance ExpandSAMs to move refinements into type members to pass Ycheck (previous Scala 3 releases would accept the code in tests/run/i18315.scala but fail Ycheck). Fixes #18315. This should be backported to any branch where #18201 is backported.
This was dropped in scala#18201 which restricted SAM types to valid parent types, but it turns out that there is code in the wild that relies on refinements being allowed here. To support this properly, we had to enhance ExpandSAMs to move refinements into type members to pass Ycheck (previous Scala 3 releases would accept the code in tests/run/i18315.scala but fail Ycheck). Fixes scala#18315.
Compiler version
3.4.0-RC1-bin-20230729-a73316a-NIGHTLY - failing, current nightly
3.3.2-RC1-bin-20230715-4851278-NIGHTLY - first bad release (first bad commit: 18f90d9)
3.3.2-RC1-bin-20230714-04eae14-NIGHTLY - last good release
Minimized code
Output
With
-explain
enabled:Expectation
The code should compile successfully
The text was updated successfully, but these errors were encountered: