Skip to content

Commit

Permalink
issue #2939 - NPE in Switch (#2942)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalat authored Sep 11, 2024
1 parent 206c8e4 commit 83ebf78
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ private boolean checkAndSetEnhanced(BlockScope upperScope, TypeBinding expressio
return true;
}
}
if (JavaFeature.PRIMITIVES_IN_PATTERNS.isSupported(upperScope.compilerOptions())) {
if (expressionType != null && JavaFeature.PRIMITIVES_IN_PATTERNS.isSupported(upperScope.compilerOptions())) {
switch (expressionType.id) {
case TypeIds.T_float:
case TypeIds.T_double:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class PrimitiveInPatternsTest extends AbstractRegressionTest9 {
static {
// TESTS_NUMBERS = new int [] { 1 };
// TESTS_RANGE = new int[] { 1, -1 };
// TESTS_NAMES = new String[] { "testEnhancedPrimitiveSwitch_001" };
// TESTS_NAMES = new String[] { "testEnhancedPrimitiveSwitchNPE_001" };
}
private String extraLibPath;
public static Class<?> testClass() {
Expand Down Expand Up @@ -6936,6 +6936,30 @@ public static void main(String[] args) {
"----------\n");
}

public void testEnhancedPrimitiveSwitchNPE_001() {
runNegativeTest(new String[] {
"X.java",
"""
class X {
// Y is not defined/visible
<T extends Y> void foo(T single) {
switch (single) {
case int i -> System.out.print(i);
default -> System.out.print('-');
}
System.out.println("hello");
}
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 3)\n" +
" <T extends Y> void foo(T single) {\n" +
" ^\n" +
"Y cannot be resolved to a type\n" +
"----------\n");
}

// test from spec
public void _testSpec001() {
runConformTest(new String[] {
Expand Down

0 comments on commit 83ebf78

Please sign in to comment.