8271928: ErroneousTree with start position -1

Reviewed-by: jlaskey, vromero
This commit is contained in:
Jan Lahoda 2021-08-11 08:54:20 +00:00
parent adba09b91d
commit 3215dbc8b8
2 changed files with 33 additions and 3 deletions

View File

@ -548,8 +548,13 @@ public class TreeInfo {
}
case ERRONEOUS: {
JCErroneous node = (JCErroneous)tree;
if (node.errs != null && node.errs.nonEmpty())
return getStartPos(node.errs.head);
if (node.errs != null && node.errs.nonEmpty()) {
int pos = getStartPos(node.errs.head);
if (pos != Position.NOPOS) {
return pos;
}
}
break;
}
}
return tree.pos;

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
@ -1795,6 +1795,31 @@ public class JavacParserTest extends TestCase {
assertEquals("correct parameter type span", code.substring(typeStart, typeEnd), "int[]...");
}
@Test //JDK-8271928
void testX() throws IOException {
String code = """
package test;
public static void test() {
return test;
}
""";
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null,
null, null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
SourcePositions sp = Trees.instance(ct).getSourcePositions();
new TreePathScanner<Void, Void>() {
@Override
public Void visitErroneous(ErroneousTree tree, Void p) {
int pos = (int) sp.getStartPosition(cut, tree);
if (pos == (-1)) {
fail("Invalid source position for an ErroneousTree");
}
return scan(tree.getErrorTrees(), p);
}
}.scan(cut, null);
}
void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)