-
Notifications
You must be signed in to change notification settings - Fork 357
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
In Java 21, case labels can be null
.
#6174
Conversation
} | ||
} | ||
// Could also check that the values match. | ||
boolean result = enumConstants.size() == caseLabels.size(); | ||
boolean result = | ||
(enumConstants.size() == caseLabels.size() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks suspicious to me. One of the labels could be null, in which case I think casesAreExhaustive
should return false. Could you write a test to assuage my concern? Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good catch. I don't know how to write a test case that would have caught this bug, because I can't see how a checker would behave differently when the cases are not exhaustive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could a test be something like:
x = null;
switch (*) {
case a: x = nonnull; break;
case b: x = nonnull; break;
...
}
x.toString();
The key is to know whether fallthrough is possible.
caseLabels.add(((IdentifierTree) caseEnumConstant).getName()); | ||
if (caseEnumConstant.getKind() != Kind.NULL_LITERAL) { | ||
caseLabels.add(((IdentifierTree) caseEnumConstant).getName()); | ||
} | ||
} | ||
} | ||
// Could also check that the values match. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the line below, now that "null" is a possible value, I think that the cases are exhaustive only if they also include "null". Otherwise, fallthrough is possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null
doesn't have to appear for the switch block to be exhaustive. If a case null
does not appear and null is the value of the switch selector expressions, then a NPE is thrown.
Here's the definition of exhaustive switch blocks: https://docs.oracle.com/javase/specs/jls/se20/preview/specs/patterns-switch-record-patterns-jls.html#jls-14.11.1.1.
I read the JLS, and if |
We may just want to hold off on merging this pull request until we have implement support for Pattern Matching for switch and Record Patterns https://openjdk.org/jeps/440). There are more test cases that crash. |
Just curious, are the crashes in the dataflow library or elsewhere? From the NullAway perspective, it'd be cool if we could test out a version of dataflow with crashes fixed before all the checkers fully support these constructs. But I agree it might be best to consolidate fixes for related issues. |
I tried this test case (copied from https://openjdk.org/jeps/441):
The crash is not in dataflow, but the crash is because dataflow does not have a node for
|
Ah, makes sense. I can see how fixing that issue might be intertwined with fixing the crash. |
@smillst CI is failing |
@msridhar I'm going to close this pull request because this in not the correct implementation for |
Thanks @smillst! |
Fixes #6173.