-
Notifications
You must be signed in to change notification settings - Fork 11
Handle enum constants in switch expressions correctly #463
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
14695df
better tests
kelloggm f868c01
adapt based on changes later in #411
kelloggm 40e895d
adapt to updated junit
kelloggm 3d1aed1
Merge branch 'main' of github.com:njit-jerse/specimin into switch-enum-2
kelloggm 9f82260
partial progress porting this, but it doesn't seem to work as I'd expect
kelloggm d335754
port the fix
kelloggm a6638d1
suppress interning checker
kelloggm 63cccd8
address code rabbit comments
kelloggm fb4fb4b
avoid duplicates
kelloggm 511a884
remove CF-3025
kelloggm 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
19 changes: 19 additions & 0 deletions
19
src/test/java/org/checkerframework/specimin/SwitchEnumConstant2Test.java
This file contains hidden or 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,19 @@ | ||
| package org.checkerframework.specimin; | ||
|
|
||
| import java.io.IOException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * This test checks that Specimin properly handles the scoping of enum constants used in switch | ||
| * statements and expressions, which have special (weird) rules. This variant checks that only | ||
| * constants used in the case part of the switch go into the enum. | ||
| */ | ||
| public class SwitchEnumConstant2Test { | ||
| @Test | ||
| public void runTest() throws IOException { | ||
| SpeciminTestExecutor.runTestWithoutJarPaths( | ||
| "switchenumconstant2", | ||
| new String[] {"com/example/Simple.java"}, | ||
| new String[] {"com.example.Simple#bar(MyEnum)"}); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/test/java/org/checkerframework/specimin/SwitchEnumConstantTest.java
This file contains hidden or 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,18 @@ | ||
| package org.checkerframework.specimin; | ||
|
|
||
| import java.io.IOException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * This test checks that Specimin properly handles the scoping of enum constants used in switch | ||
| * statements and expressions, which have special (weird) rules. | ||
| */ | ||
| public class SwitchEnumConstantTest { | ||
| @Test | ||
| public void runTest() throws IOException { | ||
| SpeciminTestExecutor.runTestWithoutJarPaths( | ||
| "switchenumconstant", | ||
| new String[] {"com/example/Simple.java"}, | ||
| new String[] {"com.example.Simple#bar(MyEnum)"}); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/test/resources/switchenumconstant/expected/com/example/Simple.java
This file contains hidden or 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,24 @@ | ||
| package com.example; | ||
|
|
||
| import org.example.MyEnum; | ||
|
|
||
| class Simple { | ||
| void bar(MyEnum m) { | ||
| switch(m) { | ||
| case CONSTANT1: | ||
| int x = 5 + 8; | ||
| break; | ||
| case CONSTANT2: | ||
| int y = 3 * 2; | ||
| break; | ||
| } | ||
| int z = switch (m) { | ||
| case CONSTANT3 -> | ||
| 5; | ||
| case CONSTANT4 -> | ||
| 6; | ||
| default -> | ||
| 0; | ||
| }; | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
src/test/resources/switchenumconstant/expected/org/example/MyEnum.java
This file contains hidden or 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,5 @@ | ||
| package org.example; | ||
|
|
||
| public enum MyEnum { | ||
| CONSTANT4, CONSTANT3, CONSTANT2, CONSTANT1; | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/test/resources/switchenumconstant/input/com/example/Simple.java
This file contains hidden or 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,35 @@ | ||
| package com.example; | ||
|
|
||
| import org.example.MyEnum; | ||
|
|
||
| // The Java scoping rules for switches over enum constants | ||
| // are surprisingly complicated and poorly documented by the JLS. | ||
| // Specifically, Java permits an unqualified enum constant that | ||
| // is a member of the type used as the selector expression in | ||
| // a switch statement to be used either as a rule or as a case, | ||
| // without an explicit import. Specimin needs to correctly | ||
| // identify those cases as enum constants, because otherwise | ||
| // there usually is not _anything_ in scope that matches, which | ||
| // causes us to produce non-sensical output. However, I don't | ||
| // _think_ that the unqualified enum constant names can be used | ||
| // in the bodies of the expressions in the rest of the switch, | ||
| // and have implemented Specimin's logic assuming that is so. | ||
| // This test case checks that we get this right. | ||
| class Simple { | ||
| // Target method. | ||
| void bar(MyEnum m) { | ||
| switch(m) { | ||
| case CONSTANT1: | ||
| int x = 5 + 8; // nonsense code | ||
| break; | ||
| case CONSTANT2: | ||
| int y = 3 * 2; | ||
| break; | ||
| } | ||
| int z = switch (m) { | ||
| case CONSTANT3 -> 5; | ||
| case CONSTANT4 -> 6; | ||
| default -> 0; | ||
| }; | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/test/resources/switchenumconstant2/expected/com/example/Foo.java
This file contains hidden or 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,7 @@ | ||
| package com.example; | ||
|
|
||
| public class Foo { | ||
| public int CONSTANT6; | ||
|
|
||
| public int CONSTANT5; | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.