-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Incorrect "the type test for A cannot be checked at runtime because it's a local class" warning #20741
Comments
The following with member class B:
class A {
override def equals(x: Any): Boolean = x match {
case a: A => true
case _ => false
}
}
def f(x: Any): Any = {
val y = A()
println(y == x)
y
}
@main def test() =
val b0 = B()
val f = b0.f(null)
val b1 = B()
b1.f(f) but with local Not sure if more words from the code comment would be elucidating:
|
Can I try my hand at this one? Sounds interesting |
Something related: there are no warnings when a local case class is defined. But a local case class |
I think the warning is in fact correct. Consider the following: def f: Any = {
class A {
override def equals(x: Any): Boolean = x match {
case _: A => true
case _ => false
}
}
A()
}
@main def foo(): Unit =
val x0 = f
val x1 = f
println(x0 == x1) The program output is What needs fixing is that the same warning should be emitted for case classes as well. Consider the following: trait C:
def ff: Int
def f(x: Int): C =
case class A() extends C:
def ff = x
A()
@main def foo(): Unit =
val x0 = f(0)
val x1 = f(1)
println(x0 == x1) The output is |
My previous comment was that the warning is warranted but the words could be improved. Good catch on case class, which is unchecked:
unchecked added in #4045 in transform/SyntheticMethods.scala I wanted to suggest
instead of
but the local class warning is not emitted for polymorphic case:
|
Interesting. Should it not be an error then, not just warning? To me, this behavior seems very counter-intuitive. |
The use of warning is more or less consistent with those cases when erased types appear in a A more ideal fix would be to implement the match of local classes correctly. This is done for inner classes (as shown by @som-snytt's example). My guess is that in the inner class case, the match not only check the runtime class, but also the captured outer object. For local classes, captured lexical closures (could be empty) can be checked to achieve the correct behavior. |
Compiler version
Tested 3.4.2, 3.4.1, 3.4.0, 3.3.3, all with the same issue.
2.13.14 works fine and does not emit warning.
Minimized example
Output Error/Warning message
Why this Error/Warning was not helpful
The code seems correct and runs correctly, i.e. the type test executes correctly at runtime, contradictory to the warning.
Suggested improvement
No warning should be emitted.
The text was updated successfully, but these errors were encountered: