diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index 42e1accb7f4..c55bd27d7d9 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -2473,6 +2473,9 @@ public class Attr extends JCTree.Visitor { log.error(tree.pos(), Errors.RetOutsideMeth); } else if (env.info.yieldResult != null) { log.error(tree.pos(), Errors.ReturnOutsideSwitchExpression); + if (tree.expr != null) { + attribExpr(tree.expr, env, env.info.yieldResult.pt); + } } else if (!env.info.isLambda && !env.info.isNewClass && env.enclMethod != null && diff --git a/test/langtools/tools/javac/patterns/T8329115.java b/test/langtools/tools/javac/patterns/T8329115.java new file mode 100644 index 00000000000..90d7dfab813 --- /dev/null +++ b/test/langtools/tools/javac/patterns/T8329115.java @@ -0,0 +1,20 @@ +/** + * @test /nodynamiccopyright/ + * @bug 8329115 + * @summary Crash involving return from inner switch + * @compile/fail/ref=T8329115.out -XDrawDiagnostics -XDdev T8329115.java + */ +public class T8329115 { + record R1() {} + record R2() {} + + int test() { + return switch (new R1()) { + case R1() -> { + return switch (new R2()) { // crashes, instead it should just be the error: attempt to return out of a switch expression + case R2() -> 1; + }; + } + }; + } +} diff --git a/test/langtools/tools/javac/patterns/T8329115.out b/test/langtools/tools/javac/patterns/T8329115.out new file mode 100644 index 00000000000..b0976c6512e --- /dev/null +++ b/test/langtools/tools/javac/patterns/T8329115.out @@ -0,0 +1,3 @@ +T8329115.java:14:17: compiler.err.return.outside.switch.expression +T8329115.java:12:16: compiler.err.switch.expression.no.result.expressions +2 errors