|
23 | 23 |
|
24 | 24 | /*
|
25 | 25 | * @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 |
27 | 27 | * @summary tests error and diagnostics positions
|
28 | 28 | * @author Jan Lahoda
|
29 | 29 | * @modules jdk.compiler/com.sun.tools.javac.api
|
@@ -2448,6 +2448,44 @@ public Void visitMethod(MethodTree node, Void p) {
|
2448 | 2448 | }.scan(cut, null);
|
2449 | 2449 | }
|
2450 | 2450 |
|
| 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 | + |
2451 | 2489 | void run(String[] args) throws Exception {
|
2452 | 2490 | int passed = 0, failed = 0;
|
2453 | 2491 | final Pattern p = (args != null && args.length > 0)
|
|
0 commit comments