Skip to content

Commit 44dc850

Browse files
committed
8331212: Error recovery for broken switch expressions could be improved
Reviewed-by: asotona
1 parent b2fb5ea commit 44dc850

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ public void visitPackageDef(JCPackageDecl tree) {
470470

471471
protected void scanSyntheticBreak(TreeMaker make, JCTree swtch) {
472472
if (swtch.hasTag(SWITCH_EXPRESSION)) {
473-
JCYield brk = make.at(Position.NOPOS).Yield(null);
473+
JCYield brk = make.at(Position.NOPOS).Yield(make.Erroneous()
474+
.setType(swtch.type));
474475
brk.target = swtch;
475476
scan(brk);
476477
} else {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
/*
25+
* @test
26+
* @bug 8331212
27+
* @summary Verify error recovery w.r.t. Flow
28+
* @library /tools/lib
29+
* @enablePreview
30+
* @modules jdk.compiler/com.sun.tools.javac.api
31+
* jdk.compiler/com.sun.tools.javac.main
32+
* java.base/jdk.internal.classfile.impl
33+
* @build toolbox.ToolBox toolbox.JavacTask
34+
* @run main FlowRecovery
35+
*/
36+
37+
import java.nio.file.Path;
38+
import java.util.List;
39+
import java.util.Objects;
40+
41+
import toolbox.JavacTask;
42+
import toolbox.Task.Expect;
43+
import toolbox.Task.OutputKind;
44+
import toolbox.TestRunner;
45+
import toolbox.ToolBox;
46+
47+
public class FlowRecovery extends TestRunner {
48+
49+
ToolBox tb;
50+
51+
public FlowRecovery() {
52+
super(System.err);
53+
tb = new ToolBox();
54+
}
55+
56+
public static void main(String[] args) throws Exception {
57+
FlowRecovery t = new FlowRecovery();
58+
t.runTests();
59+
}
60+
61+
@Test //8331212
62+
public void testYieldErrors() throws Exception {
63+
String code = """
64+
class Test {
65+
public boolean test() {
66+
return switch (0) {
67+
case 0 -> true;
68+
default -> {}
69+
};
70+
}
71+
}
72+
""";
73+
Path curPath = Path.of(".");
74+
List<String> actual = new JavacTask(tb)
75+
.options("-XDrawDiagnostics", "-XDdev")
76+
.sources(code)
77+
.outdir(curPath)
78+
.run(Expect.FAIL)
79+
.writeAll()
80+
.getOutputLines(OutputKind.DIRECT);
81+
82+
List<String> expected = List.of(
83+
"Test.java:5:25: compiler.err.rule.completes.normally",
84+
"1 error"
85+
);
86+
87+
if (!Objects.equals(actual, expected)) {
88+
error("Expected: " + expected + ", but got: " + actual);
89+
}
90+
}
91+
92+
}

0 commit comments

Comments
 (0)