-
Notifications
You must be signed in to change notification settings - Fork 356
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
Don't crash on switch expressions or switch statements with arrow cases. #4976
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1d4d989
Don't crash on SwitchExpressionTrees.
smillst 2a35402
Fix skip test.
smillst 71332ad
Tweak.
smillst d574ce8
Tweak.
smillst 49bb8fd
Don't format Java 17 unless using java 17.
smillst d87dbc5
Checkpoint.
smillst 2adde5f
Add comment.
smillst e311609
Revert some changes.
smillst 4ce5c66
Revert more changes.
smillst 2ae98c3
Delete more code.
smillst 11fc31b
Add some test cases.
smillst f3f069f
Merge remote-tracking branch 'origin/master' into switch-expressions-…
smillst eb8ad78
Better work around.
smillst dcdab5b
Clarify changelog.
smillst f68c82a
Fix typos.
smillst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// @below-java17-jdk-skip-test | ||
public class NullnessSwitchArrows { | ||
public enum Day { | ||
SUNDAY, | ||
MONDAY, | ||
TUESDAY, | ||
WEDNESDAY, | ||
THURSDAY, | ||
FRIDAY, | ||
SATURDAY; | ||
} | ||
|
||
void method1() { | ||
Object o; | ||
Day day = Day.WEDNESDAY; | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> o = "hello"; | ||
case TUESDAY -> o = null; | ||
case THURSDAY, SATURDAY -> o = "hello"; | ||
case WEDNESDAY -> o = "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
} | ||
|
||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
|
||
void method2() { | ||
Object o; | ||
Day day = Day.WEDNESDAY; | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> o = "hello"; | ||
case TUESDAY -> o = "hello"; | ||
case THURSDAY, SATURDAY -> o = "hello"; | ||
case WEDNESDAY -> o = "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
} | ||
|
||
// TODO: this is a false positive. It works for case: statements; see below. | ||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
|
||
void method2b() { | ||
Object o; | ||
Day day = Day.WEDNESDAY; | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY: | ||
o = "hello"; | ||
break; | ||
case TUESDAY: | ||
o = "hello"; | ||
break; | ||
case THURSDAY, SATURDAY: | ||
o = "hello"; | ||
break; | ||
case WEDNESDAY: | ||
o = "hello"; | ||
break; | ||
default: | ||
throw new IllegalStateException("Invalid day: " + day); | ||
} | ||
|
||
o.toString(); | ||
} | ||
|
||
void method3() { | ||
Object o; | ||
Day day = Day.WEDNESDAY; | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> o = "hello"; | ||
case TUESDAY -> o = "hello"; | ||
case THURSDAY, SATURDAY -> o = "hello"; | ||
case WEDNESDAY -> o = "hello"; | ||
default -> o = "hello"; | ||
} | ||
|
||
o.toString(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
checker/tests/nullness/java17/NullnessSwitchExpressions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// @below-java17-jdk-skip-test | ||
public class NullnessSwitchExpressions { | ||
public enum Day { | ||
SUNDAY, | ||
MONDAY, | ||
TUESDAY, | ||
WEDNESDAY, | ||
THURSDAY, | ||
FRIDAY, | ||
SATURDAY; | ||
} | ||
|
||
void method1() { | ||
Day day = Day.WEDNESDAY; | ||
Object o = | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> "hello"; | ||
case TUESDAY -> null; | ||
case THURSDAY, SATURDAY -> "hello"; | ||
case WEDNESDAY -> "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
}; | ||
|
||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
|
||
void method2() { | ||
Day day = Day.WEDNESDAY; | ||
Object o = | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> "hello"; | ||
case TUESDAY -> "hello"; | ||
case THURSDAY, SATURDAY -> "hello"; | ||
case WEDNESDAY -> "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
}; | ||
|
||
// TODO: This is a false positive. | ||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
|
||
void method3() { | ||
Day day = Day.WEDNESDAY; | ||
Object o = | ||
switch (day) { | ||
case MONDAY, FRIDAY, SUNDAY -> "hello"; | ||
case TUESDAY -> "hello"; | ||
case THURSDAY, SATURDAY -> { | ||
String s = null; | ||
if (day == Day.THURSDAY) { | ||
s = "hello"; | ||
// TODO: This is a false positive. | ||
// :: error: (dereference.of.nullable) | ||
s.toString(); | ||
} | ||
yield s; | ||
} | ||
case WEDNESDAY -> "hello"; | ||
default -> throw new IllegalStateException("Invalid day: " + day); | ||
}; | ||
|
||
// :: error: (dereference.of.nullable) | ||
o.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I decided not to add a SwitchExpressionNode, because I wasn't sure what fields it would need when we properly implement switch expressions.