From a1115a7a39438438ec247743718cdc1ec59823d6 Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Tue, 8 Aug 2023 09:28:21 +0000 Subject: [PATCH] 8312204: unexpected else with statement causes compiler crash Reviewed-by: vromero --- .../sun/tools/javac/parser/VirtualParser.java | 4 +- .../tools/javac/parser/JavacParserTest.java | 40 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/VirtualParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/VirtualParser.java index 6ad0a9353b9..1d716f75a90 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/VirtualParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/VirtualParser.java @@ -148,12 +148,12 @@ public class VirtualParser extends JavacParser { @Override public int errPos() { - throw new AssertionError(); + return S.errPos(); } @Override public void errPos(int pos) { - throw new AssertionError(); + S.errPos(pos); } @Override diff --git a/test/langtools/tools/javac/parser/JavacParserTest.java b/test/langtools/tools/javac/parser/JavacParserTest.java index b6c48070c81..0f4ff3cd2a5 100644 --- a/test/langtools/tools/javac/parser/JavacParserTest.java +++ b/test/langtools/tools/javac/parser/JavacParserTest.java @@ -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 @@ -2448,6 +2448,44 @@ public class JavacParserTest extends TestCase { }.scan(cut, null); } + @Test //JDK-8312204 + void testDanglingElse() throws IOException { + String code = """ + void main() { + else ; + } + """; + DiagnosticCollector 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 codes = new LinkedList<>(); + + for (Diagnostic 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)