Skip to content

Commit a1115a7

Browse files
committedAug 8, 2023
8312204: unexpected else with statement causes compiler crash
Reviewed-by: vromero
1 parent 87a6acb commit a1115a7

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/parser/VirtualParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ public Token split() {
148148

149149
@Override
150150
public int errPos() {
151-
throw new AssertionError();
151+
return S.errPos();
152152
}
153153

154154
@Override
155155
public void errPos(int pos) {
156-
throw new AssertionError();
156+
S.errPos(pos);
157157
}
158158

159159
@Override

‎test/langtools/tools/javac/parser/JavacParserTest.java

+39-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326 8312093
26+
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326 8312093 8312204
2727
* @summary tests error and diagnostics positions
2828
* @author Jan Lahoda
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -2448,6 +2448,44 @@ public Void visitMethod(MethodTree node, Void p) {
24482448
}.scan(cut, null);
24492449
}
24502450

2451+
@Test //JDK-8312204
2452+
void testDanglingElse() throws IOException {
2453+
String code = """
2454+
void main() {
2455+
else ;
2456+
}
2457+
""";
2458+
DiagnosticCollector<JavaFileObject> coll =
2459+
new DiagnosticCollector<>();
2460+
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll,
2461+
List.of("--enable-preview", "--source", SOURCE_VERSION),
2462+
null, Arrays.asList(new MyFileObject(code)));
2463+
CompilationUnitTree cut = ct.parse().iterator().next();
2464+
2465+
String result = cut.toString().replaceAll("\\R", "\n");
2466+
System.out.println("RESULT\n" + result);
2467+
assertEquals("incorrect AST",
2468+
result,
2469+
"""
2470+
\n\
2471+
/*synthetic*/ final class Test {
2472+
\n\
2473+
void main() {
2474+
(ERROR);
2475+
}
2476+
}""");
2477+
2478+
List<String> codes = new LinkedList<>();
2479+
2480+
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
2481+
codes.add(d.getLineNumber() + ":" + d.getColumnNumber() + ":" + d.getCode());
2482+
}
2483+
2484+
assertEquals("testDanglingElse: " + codes,
2485+
List.of("2:5:compiler.err.else.without.if"),
2486+
codes);
2487+
}
2488+
24512489
void run(String[] args) throws Exception {
24522490
int passed = 0, failed = 0;
24532491
final Pattern p = (args != null && args.length > 0)

0 commit comments

Comments
 (0)
Please sign in to comment.