Skip to content

Commit

Permalink
8312204: unexpected else with statement causes compiler crash
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Aug 8, 2023
1 parent 87a6acb commit a1115a7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ public Token split() {

@Override
public int errPos() {
throw new AssertionError();
return S.errPos();
}

@Override
public void errPos(int pos) {
throw new AssertionError();
S.errPos(pos);
}

@Override
Expand Down
40 changes: 39 additions & 1 deletion test/langtools/tools/javac/parser/JavacParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326 8312093
* @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
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
Expand Down Expand Up @@ -2448,6 +2448,44 @@ public Void visitMethod(MethodTree node, Void p) {
}.scan(cut, null);
}

@Test //JDK-8312204
void testDanglingElse() throws IOException {
String code = """
void main() {
else ;
}
""";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll,
List.of("--enable-preview", "--source", SOURCE_VERSION),
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();

String result = cut.toString().replaceAll("\\R", "\n");
System.out.println("RESULT\n" + result);
assertEquals("incorrect AST",
result,
"""
\n\
/*synthetic*/ final class Test {
\n\
void main() {
(ERROR);
}
}""");

List<String> codes = new LinkedList<>();

for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getLineNumber() + ":" + d.getColumnNumber() + ":" + d.getCode());
}

assertEquals("testDanglingElse: " + codes,
List.of("2:5:compiler.err.else.without.if"),
codes);
}

void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)
Expand Down

0 comments on commit a1115a7

Please sign in to comment.