From 4dfcc6df173d91b9b0b73d1ac3008e7cbb283871 Mon Sep 17 00:00:00 2001 From: Aggelos Biboudis Date: Wed, 27 Mar 2024 09:38:25 +0000 Subject: [PATCH] 8329115: Crash involving return from inner switch Reviewed-by: jlahoda --- .../com/sun/tools/javac/comp/Attr.java | 3 +++ .../tools/javac/patterns/T8329115.java | 20 +++++++++++++++++++ .../tools/javac/patterns/T8329115.out | 3 +++ 3 files changed, 26 insertions(+) create mode 100644 test/langtools/tools/javac/patterns/T8329115.java create mode 100644 test/langtools/tools/javac/patterns/T8329115.out 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