expression type A is a subtype of pattern type B diagnostics
Liam Miller-Cushon
cushon at google.com
Tue Apr 1 16:48:31 UTC 2025
Hi,
I had a question about a change that was made to instanceof patterns as
part of JEP 440.
javac emits diagnostics for instanceof patterns where the expression type
is a subtype of the pattern type in JDK 20, but the diagnostics were
removed in 21 as part of the implementation of JEP 440.
The spec diff
<https://cr.openjdk.org/~gbierman/jep440%2B441/jep440%2B441-20230509/specs/patterns-switch-record-patterns-jls.html#jls-15.20>
for those changes removed language from 15.20 about
If the type of the RelationalExpression is a subtype of the type of the
> Pattern, then a compile-time error occurs.
The implementation also updated
test/langtools/tools/javac/patterns/NoSubtypeCheck.java to test for those
diagnostics when --release 20 is passed.
So all of this seems deliberate and working as intended, I was just curious
what the rationale for the change was. Does anyone remember the history or
have pointers to discussions about it on list?
public class Z {
class A {}
class B extends A {}
void f(B b) {
if (b instanceof A a) {}
}
}
Z.java:5: error: expression type Z.B is a subtype of pattern type Z.A
if (b instanceof A a) {}
^
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20250401/6352bc31/attachment.htm>
More information about the compiler-dev
mailing list