Skip to content

Commit 86491a5

Browse files
committedSep 29, 2020
8253584: Redunant errors for partial member selects
Reviewed-by: vromero
1 parent ebf443a commit 86491a5

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed
 

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ public Name ident() {
567567
return ident(false);
568568
}
569569

570-
protected Name ident(boolean advanceOnErrors) {
570+
protected Name ident(boolean allowClass) {
571571
if (token.kind == IDENTIFIER) {
572572
Name name = token.name();
573573
nextToken();
@@ -603,8 +603,9 @@ protected Name ident(boolean advanceOnErrors) {
603603
return name;
604604
} else {
605605
accept(IDENTIFIER);
606-
if (advanceOnErrors) {
606+
if (allowClass && token.kind == CLASS) {
607607
nextToken();
608+
return names._class;
608609
}
609610
return names.error;
610611
}

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

+48-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
26+
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584
2727
* @summary tests error and diagnostics positions
2828
* @author Jan Lahoda
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -1564,6 +1564,53 @@ public class B {
15641564
assertEquals("Unexpected AST, got:\n" + ast, expected, ast);
15651565
}
15661566

1567+
@Test //JDK-8253584
1568+
void testElseRecovery() throws IOException {
1569+
//verify the errors and AST form produced for member selects which are
1570+
//missing the selected member name:
1571+
String code = """
1572+
package t;
1573+
class Test {
1574+
void t() {
1575+
if (true) {
1576+
s().
1577+
} else {
1578+
}
1579+
}
1580+
String s() {
1581+
return null;
1582+
}
1583+
}
1584+
""";
1585+
StringWriter out = new StringWriter();
1586+
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(out, fm, null, List.of("-XDrawDiagnostics"),
1587+
null, Arrays.asList(new MyFileObject(code)));
1588+
String ast = ct.parse().iterator().next().toString().replaceAll("\\R", "\n");
1589+
String expected = """
1590+
package t;
1591+
\n\
1592+
class Test {
1593+
\n\
1594+
void t() {
1595+
if (true) {
1596+
(ERROR);
1597+
} else {
1598+
}
1599+
}
1600+
\n\
1601+
String s() {
1602+
return null;
1603+
}
1604+
} """;
1605+
assertEquals("Unexpected AST, got:\n" + ast, expected, ast);
1606+
assertEquals("Unexpected errors, got:\n" + out.toString(),
1607+
out.toString(),
1608+
"""
1609+
Test.java:5:17: compiler.err.expected: token.identifier
1610+
Test.java:5:16: compiler.err.not.stmt
1611+
""");
1612+
}
1613+
15671614
void run(String[] args) throws Exception {
15681615
int passed = 0, failed = 0;
15691616
final Pattern p = (args != null && args.length > 0)

0 commit comments

Comments
 (0)
Please sign in to comment.