-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compute the type of switch expressions and check them. (#4978)
- Loading branch information
Showing
8 changed files
with
449 additions
and
6 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
checker/tests/nullness/java17/SwitchExpressionInvariant.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,27 @@ | ||
// @below-java17-jdk-skip-test | ||
import java.util.List; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
public class SwitchExpressionInvariant { | ||
public static boolean flag = false; | ||
|
||
void method( | ||
List<@NonNull String> nonnullStrings, List<@Nullable String> nullableStrings, int fenum) { | ||
|
||
List<@NonNull String> list = | ||
// :: error: (assignment) | ||
switch (fenum) { | ||
// :: error: (switch.expression) | ||
case 1 -> nonnullStrings; | ||
default -> nullableStrings; | ||
}; | ||
|
||
List<@Nullable String> list2 = | ||
switch (fenum) { | ||
// :: error: (switch.expression) | ||
case 1 -> nonnullStrings; | ||
default -> nullableStrings; | ||
}; | ||
} | ||
} |
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
100 changes: 100 additions & 0 deletions
100
framework/tests/value/java17/SwitchExpressionTyping.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,100 @@ | ||
// @below-java17-jdk-skip-test | ||
import org.checkerframework.common.value.qual.IntVal; | ||
|
||
public class SwitchExpressionTyping { | ||
public static boolean flag = false; | ||
|
||
void method0(String s) { | ||
@IntVal({0, 1, 2, 3}) int o = | ||
switch (s) { | ||
case "Hello?" -> { | ||
throw new RuntimeException(); | ||
} | ||
case "Hello" -> 0; | ||
case "Bye" -> 1; | ||
case "Later" -> 2; | ||
case "What?" -> throw new RuntimeException(); | ||
default -> 3; | ||
}; | ||
} | ||
|
||
void method1(String s) { | ||
@IntVal({1, 2, 3}) int o = | ||
switch (s) { | ||
case "Hello?" -> 1; | ||
case "Hello" -> 1; | ||
case "Bye" -> 1; | ||
case "Later" -> 1; | ||
case "What?" -> { | ||
if (flag) { | ||
yield 2; | ||
} | ||
yield 3; | ||
} | ||
default -> 1; | ||
}; | ||
|
||
@IntVal(1) int o2 = | ||
// :: error: (assignment) | ||
switch (s) { | ||
case "Hello?" -> 1; | ||
case "Hello" -> 1; | ||
case "Bye" -> 1; | ||
case "Later" -> 1; | ||
case "What?" -> { | ||
if (flag) { | ||
yield 2; | ||
} | ||
yield 3; | ||
} | ||
default -> 1; | ||
}; | ||
} | ||
|
||
void method2(String s, String r) { | ||
@IntVal({0, 1, 2, 3}) int o = | ||
switch (s) { | ||
case "Hello?" -> { | ||
if (flag) { | ||
throw new RuntimeException(); | ||
} | ||
yield 2; | ||
} | ||
case "Hello" -> { | ||
int i = | ||
switch (r) { | ||
case "Hello" -> 4; | ||
case "Bye" -> 5; | ||
case "Later" -> 6; | ||
default -> 42; | ||
}; | ||
yield 0; | ||
} | ||
case "Bye" -> 1; | ||
case "Later" -> { | ||
int i = | ||
switch (r) { | ||
case "Hello": | ||
{ | ||
yield 4; | ||
} | ||
case "Bye": | ||
{ | ||
yield 5; | ||
} | ||
case "Later": | ||
{ | ||
yield 6; | ||
} | ||
default: | ||
{ | ||
yield 42; | ||
} | ||
}; | ||
yield 2; | ||
} | ||
case "What?" -> throw new RuntimeException(); | ||
default -> 3; | ||
}; | ||
} | ||
} |
Oops, something went wrong.