Skip to content

Commit 522fa13

Browse files
committed
8301580: Error recovery does not clear returnResult
Reviewed-by: vromero
1 parent 7ac2079 commit 522fa13

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -5219,7 +5219,7 @@ public void visitAnnotatedType(JCAnnotatedType tree) {
52195219

52205220
public void visitErroneous(JCErroneous tree) {
52215221
if (tree.errs != null) {
5222-
Env<AttrContext> errEnv = env.dup(env.tree);
5222+
Env<AttrContext> errEnv = env.dup(env.tree, env.info.dup());
52235223
errEnv.info.returnResult = unknownExprInfo;
52245224
for (JCTree err : tree.errs)
52255225
attribTree(err, errEnv, new ResultInfo(KindSelector.ERR, pt()));

test/langtools/jdk/jshell/SnippetHighlightTest.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8274148
26+
* @bug 8274148 8301580
2727
* @summary Check snippet highlighting
2828
* @library /tools/lib
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -99,6 +99,21 @@ public void testMemberExpr() {
9999
"Highlight[start=5, end=11, attributes=[DECLARATION]]");
100100
}
101101

102+
public void testClassErrorRecovery() { //JDK-8301580
103+
assertHighlights("""
104+
class C {
105+
void m
106+
{
107+
return ;
108+
}
109+
}
110+
""",
111+
"Highlight[start=0, end=5, attributes=[KEYWORD]]",
112+
"Highlight[start=6, end=7, attributes=[DECLARATION]]",
113+
"Highlight[start=13, end=17, attributes=[KEYWORD]]",
114+
"Highlight[start=32, end=38, attributes=[KEYWORD]]");
115+
}
116+
102117
private void assertHighlights(String code, String... expected) {
103118
List<String> completions = computeHighlights(code);
104119
assertEquals(completions, Arrays.asList(expected), "Input: " + code + ", " + completions.toString());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2023, 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 8301580
27+
* @summary Verify error recovery w.r.t. Attr
28+
* @library /tools/lib
29+
* @modules jdk.compiler/com.sun.tools.javac.api
30+
* jdk.compiler/com.sun.tools.javac.main
31+
* jdk.jdeps/com.sun.tools.classfile
32+
* @build toolbox.ToolBox toolbox.JavacTask
33+
* @run main AttrRecovery
34+
*/
35+
36+
import java.nio.file.Path;
37+
import java.util.List;
38+
import java.util.Objects;
39+
40+
import toolbox.JavacTask;
41+
import toolbox.Task.Expect;
42+
import toolbox.Task.OutputKind;
43+
import toolbox.TestRunner;
44+
import toolbox.ToolBox;
45+
46+
public class AttrRecovery extends TestRunner {
47+
48+
ToolBox tb;
49+
50+
public AttrRecovery() {
51+
super(System.err);
52+
tb = new ToolBox();
53+
}
54+
55+
public static void main(String[] args) throws Exception {
56+
AttrRecovery t = new AttrRecovery();
57+
t.runTests();
58+
}
59+
60+
@Test
61+
public void testFlowExits() throws Exception {
62+
String code = """
63+
class C {
64+
void build
65+
{
66+
return ;
67+
}
68+
}
69+
""";
70+
Path curPath = Path.of(".");
71+
List<String> actual = new JavacTask(tb)
72+
.options("-XDrawDiagnostics", "-XDdev", "-XDshould-stop.at=FLOW")
73+
.sources(code)
74+
.outdir(curPath)
75+
.run(Expect.FAIL)
76+
.getOutputLines(OutputKind.DIRECT);
77+
78+
List<String> expected = List.of(
79+
"C.java:3:5: compiler.err.expected: '('",
80+
"C.java:4:9: compiler.err.ret.outside.meth",
81+
"2 errors"
82+
);
83+
84+
if (!Objects.equals(actual, expected)) {
85+
error("Expected: " + expected + ", but got: " + actual);
86+
}
87+
}
88+
89+
}

0 commit comments

Comments
 (0)