Skip to content
Permalink
Browse files
8253584: Redunant errors for partial member selects
Reviewed-by: vromero
  • Loading branch information
Jan Lahoda committed Sep 29, 2020
1 parent ebf443a commit 86491a5f6b2958480a73183b9a4a29ef55d3a569
Showing with 51 additions and 3 deletions.
  1. +3 −2 src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
  2. +48 −1 test/langtools/tools/javac/parser/JavacParserTest.java
@@ -567,7 +567,7 @@ public Name ident() {
return ident(false);
}

protected Name ident(boolean advanceOnErrors) {
protected Name ident(boolean allowClass) {
if (token.kind == IDENTIFIER) {
Name name = token.name();
nextToken();
@@ -603,8 +603,9 @@ protected Name ident(boolean advanceOnErrors) {
return name;
} else {
accept(IDENTIFIER);
if (advanceOnErrors) {
if (allowClass && token.kind == CLASS) {
nextToken();
return names._class;
}
return names.error;
}
@@ -23,7 +23,7 @@

/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -1564,6 +1564,53 @@ void testErrorRecoveryClassNotBrace() throws IOException {
assertEquals("Unexpected AST, got:\n" + ast, expected, ast);
}

@Test //JDK-8253584
void testElseRecovery() throws IOException {
//verify the errors and AST form produced for member selects which are
//missing the selected member name:
String code = """
package t;
class Test {
void t() {
if (true) {
s().
} else {
}
}
String s() {
return null;
}
}
""";
StringWriter out = new StringWriter();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(out, fm, null, List.of("-XDrawDiagnostics"),
null, Arrays.asList(new MyFileObject(code)));
String ast = ct.parse().iterator().next().toString().replaceAll("\\R", "\n");
String expected = """
package t;
\n\
class Test {
\n\
void t() {
if (true) {
(ERROR);
} else {
}
}
\n\
String s() {
return null;
}
} """;
assertEquals("Unexpected AST, got:\n" + ast, expected, ast);
assertEquals("Unexpected errors, got:\n" + out.toString(),
out.toString(),
"""
Test.java:5:17: compiler.err.expected: token.identifier
Test.java:5:16: compiler.err.not.stmt
""");
}

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

1 comment on commit 86491a5

@bridgekeeper

This comment has been minimized.

Copy link

@bridgekeeper bridgekeeper bot commented on 86491a5 Sep 29, 2020

Please sign in to comment.