8329115: Crash involving return from inner switch

Reviewed-by: jlahoda
This commit is contained in:
Aggelos Biboudis 2024-03-27 09:38:25 +00:00
parent 9e566d76d1
commit 4dfcc6df17
3 changed files with 26 additions and 0 deletions

View File

@ -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 &&

View File

@ -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;
};
}
};
}
}

View File

@ -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