Skip to content
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

8269113: Javac throws when compiling switch (null) #4679

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -370,7 +370,7 @@ private void handleSwitch(JCTree tree,
ListBuffer<JCStatement> statements = new ListBuffer<>();
VarSymbol temp = new VarSymbol(Flags.SYNTHETIC,
names.fromString("selector" + tree.pos + target.syntheticNameChar() + "temp"),
seltype,
seltype.hasTag(BOT) ? syms.objectType : seltype,
currentMethodSym);
boolean hasNullCase = cases.stream()
.flatMap(c -> c.labels.stream())
Expand Down
17 changes: 16 additions & 1 deletion test/langtools/tools/javac/patterns/Switches.java
Expand Up @@ -27,7 +27,7 @@

/*
* @test
* @bug 8262891 8268333 8268896
* @bug 8262891 8268333 8268896 8269113
* @summary Check behavior of pattern switches.
* @compile --enable-preview -source ${jdk.version} Switches.java
* @run main/othervm --enable-preview Switches
Expand All @@ -44,6 +44,7 @@ void run() {
run(this::testBooleanSwitchExpression);
assertFalse(testNullSwitch(null));
assertTrue(testNullSwitch(""));
assertFalse(testNullSelector());
runArrayTypeTest(this::testArrayTypeStatement);
runArrayTypeTest(this::testArrayTypeExpression);
runDefaultTest(this::testDefaultDoesNotDominateStatement);
Expand All @@ -64,6 +65,7 @@ void run() {
runFallThrough(this::testFallThroughExpression);
npeTest(this::npeTestStatement);
npeTest(this::npeTestExpression);
npeTest(this::npeTestNullSelector);
exhaustiveStatementSane("");
exhaustiveStatementSane(null);
}
Expand Down Expand Up @@ -167,6 +169,13 @@ boolean testNullSwitch(Object o) {
};
}

boolean testNullSelector() {
return switch (null) {
case null -> false;
default -> true;
};
}

String testArrayTypeStatement(Object o) {
String res;
switch (o) {
Expand Down Expand Up @@ -354,6 +363,12 @@ void npeTestExpression(I i) {
};
}

void npeTestNullSelector(Object o) {
switch (null) {
default:
}
}

void exhaustiveStatementSane(Object o) {
switch (o) {
case Object obj:; //no break intentionally - should not fall through to any possible default
Expand Down