Skip to content
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

instanceof pattern matching should reject supertypes #1356

Closed
DasBrain opened this issue Sep 6, 2023 · 4 comments
Closed

instanceof pattern matching should reject supertypes #1356

DasBrain opened this issue Sep 6, 2023 · 4 comments
Assignees

Comments

@DasBrain
Copy link

DasBrain commented Sep 6, 2023

The following snippet should be rejected by the compler:

String str = "foo";
if (str instanceof String s) {}

because String is a the same (or a supertype) of the type of the variable str, so this check would always succeed (if str != null).

This is mandated by JLS §15.20.2:

If the type of the RelationalExpression is a subtype of the type of the Pattern, then a compile-time error occurs.

@srikanth-sankaran srikanth-sankaran self-assigned this Sep 7, 2023
@srikanth-sankaran
Copy link
Contributor

Reproduced. I'll take a look. Thanks

@srikanth-sankaran
Copy link
Contributor

JDK21 javac accepts the code above while upto JDK20, it was rejected.

@srikanth-sankaran
Copy link
Contributor

JLS21 15.20.2 is missing the said clause - however there are phantom bullets: See https://bugs.openjdk.org/browse/JDK-8317695

@srikanth-sankaran
Copy link
Contributor

Closing with no change for reasons cited - latest JLS and latest javac don't call for or enforce this. Should there be a change in future, we will revisit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants