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

8294670: Enhanced switch statements have an implicit default which does not complete normally #10540

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 @@ -694,7 +694,7 @@ public void visitSwitch(JCSwitch tree) {
log.error(tree, Errors.NotExhaustiveStatement);
}
}
if (!tree.hasUnconditionalPattern) {
if (!tree.hasUnconditionalPattern && !exhaustiveSwitch) {
alive = Liveness.ALIVE;
}
alive = alive.or(resolveBreaks(tree, prevPendingExits));
Expand Down
1 change: 0 additions & 1 deletion test/langtools/tools/javac/patterns/EnumTypeChanges.java
Expand Up @@ -84,7 +84,6 @@ String statementEnumExhaustive(EnumTypeChangesEnum e) {
case B -> { return "B"; }
case EnumTypeChangesEnum x when e == EnumTypeChangesEnum.A -> throw new AssertionError();
}
return "";
}

String expressionEnumExhaustive(EnumTypeChangesEnum e) {
Expand Down
118 changes: 116 additions & 2 deletions test/langtools/tools/javac/patterns/Exhaustiveness.java
Expand Up @@ -23,7 +23,7 @@

/**
* @test
* @bug 8262891 8268871 8274363 8281100
* @bug 8262891 8268871 8274363 8281100 8294670
* @summary Check exhaustiveness of switches over sealed types.
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
Expand Down Expand Up @@ -402,7 +402,7 @@ public class Test {
private void test(Object obj) {
switch (obj) {
case String s: return;
};
}
}
}
""",
Expand Down Expand Up @@ -1010,6 +1010,7 @@ private void testStatement(R obj) {
""");
}

@Test
public void testNonPrimitiveBooleanGuard(Path base) throws Exception {
doTest(base,
new String[0],
Expand Down Expand Up @@ -1056,6 +1057,119 @@ void test(A arg) {
"2 errors");
}

@Test //JDK-8294670
public void testImplicitDefaultCannotCompleteNormally(Path base) throws Exception {
doTest(base,
new String[0],
"""
package test;
public class Test {
sealed interface A {}
final class B implements A {}

int test(A arg) {
switch (arg) {
case B b: return 1;
}
}
}
""");
doTest(base,
new String[0],
"""
package test;
public class Test {
sealed interface A {}
final class B implements A {}

int test(A arg) {
switch (arg) {
case B b: return 1;
default: return 1;
}
}
}
""");
doTest(base,
new String[0],
"""
package test;
public class Test {
sealed interface A {}
final class B implements A {}

int test(A arg) {
switch (arg) {
case B b: break;
}
}
}
""",
"Test.java:10:5: compiler.err.missing.ret.stmt",
"- compiler.note.preview.filename: Test.java, DEFAULT",
"- compiler.note.preview.recompile",
"1 error");
doTest(base,
new String[0],
"""
package test;
public class Test {
sealed interface A {}
final class B implements A {}

int test(A arg) {
switch (arg) {
case B b: return 1;
default: break;
}
}
}
""",
"Test.java:11:5: compiler.err.missing.ret.stmt",
"- compiler.note.preview.filename: Test.java, DEFAULT",
"- compiler.note.preview.recompile",
"1 error");
doTest(base,
new String[0],
"""
package test;
public class Test {
sealed interface A {}
final class B implements A {}

int test(A arg) {
switch (arg) {
case B b:
}
}
}
""",
"Test.java:10:5: compiler.err.missing.ret.stmt",
"- compiler.note.preview.filename: Test.java, DEFAULT",
"- compiler.note.preview.recompile",
"1 error");
doTest(base,
new String[0],
"""
package test;
public class Test {
sealed interface A {}
final class B implements A {}

int test(A arg) {
switch (arg) {
case B b: return 1;
default:
}
}
}
""",
"Test.java:11:5: compiler.err.missing.ret.stmt",
"- compiler.note.preview.filename: Test.java, DEFAULT",
"- compiler.note.preview.recompile",
"1 error");
}

private void doTest(Path base, String[] libraryCode, String testCode, String... expectedErrors) throws IOException {
Path current = base.resolve(".");
Path libClasses = current.resolve("libClasses");
Expand Down
1 change: 0 additions & 1 deletion test/langtools/tools/javac/switchnull/SwitchNull.java
Expand Up @@ -93,7 +93,6 @@ private int switchEnum(E e) {
case C: return 2;
case null: return -1;
}
throw new AssertionError(String.valueOf(e));
}

private int switchEnumWithDefault(E e) {
Expand Down