Skip to content

Commit df35462

Browse files
biboudislahodaj
andcommittedFeb 5, 2024
8323502: javac crash with wrongly typed method block in Flow
Co-authored-by: Jan Lahoda <jlahoda@openjdk.org> Reviewed-by: jlahoda
1 parent af32262 commit df35462

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1610,14 +1610,16 @@ public void visitSwitch(JCSwitch tree) {
16101610
}
16111611

16121612
public void visitSwitchExpression(JCSwitchExpression tree) {
1613+
boolean wrongContext = false;
1614+
16131615
tree.polyKind = (pt().hasTag(NONE) && pt() != Type.recoveryType && pt() != Infer.anyPoly) ?
16141616
PolyKind.STANDALONE : PolyKind.POLY;
16151617

16161618
if (tree.polyKind == PolyKind.POLY && resultInfo.pt.hasTag(VOID)) {
16171619
//this means we are returning a poly conditional from void-compatible lambda expression
16181620
resultInfo.checkContext.report(tree, diags.fragment(Fragments.SwitchExpressionTargetCantBeVoid));
1619-
result = tree.type = types.createErrorType(resultInfo.pt);
1620-
return;
1621+
resultInfo = recoveryInfo;
1622+
wrongContext = true;
16211623
}
16221624

16231625
ResultInfo condInfo = tree.polyKind == PolyKind.STANDALONE ?
@@ -1655,7 +1657,7 @@ public void visitYield(JCYield brk) {
16551657

16561658
Type owntype = (tree.polyKind == PolyKind.STANDALONE) ? condType(caseTypePositions.toList(), caseTypes.toList()) : pt();
16571659

1658-
result = tree.type = check(tree, owntype, KindSelector.VAL, resultInfo);
1660+
result = tree.type = wrongContext? types.createErrorType(pt()) : check(tree, owntype, KindSelector.VAL, resultInfo);
16591661
}
16601662
//where:
16611663
CheckContext switchExpressionContext(CheckContext checkContext) {
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/*
24+
* @test
25+
* @bug 8323502
26+
* @summary javac crash with wrongly typed method block in Flow
27+
* @compile/fail/ref=T8323502.out -XDrawDiagnostics --should-stop=at=FLOW -XDdev T8323502.java
28+
*/
29+
public class T8323502 {
30+
public void m(Object o) {
31+
return switch(o) {
32+
default -> System.out.println("boom");
33+
};
34+
}
35+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
T8323502.java:31:16: compiler.err.prob.found.req: (compiler.misc.unexpected.ret.val)
2+
1 error

0 commit comments

Comments
 (0)
Please sign in to comment.