From a663434be7679d5d40ea2c6f69f95399cc960800 Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Tue, 24 Oct 2017 12:07:27 +0200 Subject: [PATCH] 8189796: Incorrect end position for missing statement Recording end positions for error trees representing missing statements. Co-authored-by: Dusan Balek Reviewed-by: mcimadamore --- .../sun/tools/javac/parser/JavacParser.java | 5 +-- .../tools/javac/parser/JavacParserTest.java | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java index 0d1a14799a2..354f3433c7f 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java @@ -2396,9 +2396,8 @@ public class JavacParser implements Parser { int pos = token.pos; List stats = blockStatement(); if (stats.isEmpty()) { - JCErroneous e = F.at(pos).Erroneous(); - error(e, "illegal.start.of.stmt"); - return F.at(pos).Exec(e); + JCErroneous e = syntaxError(pos, "illegal.start.of.stmt"); + return toP(F.at(pos).Exec(e)); } else { JCStatement first = stats.head; String error = null; diff --git a/test/langtools/tools/javac/parser/JavacParserTest.java b/test/langtools/tools/javac/parser/JavacParserTest.java index 43e8d31d00e..7f84dc8b7c0 100644 --- a/test/langtools/tools/javac/parser/JavacParserTest.java +++ b/test/langtools/tools/javac/parser/JavacParserTest.java @@ -38,6 +38,7 @@ import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.ErroneousTree; import com.sun.source.tree.ExpressionStatementTree; import com.sun.source.tree.ExpressionTree; +import com.sun.source.tree.IfTree; import com.sun.source.tree.LambdaExpressionTree; import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodTree; @@ -284,6 +285,48 @@ public class JavacParserTest extends TestCase { 1, t.getSourcePositions().getStartPosition(cut, clazz)); } + @Test + void testPositionMissingStatement() throws IOException { + String code = "class C { void t() { if (true) } }"; + DiagnosticCollector dc = new DiagnosticCollector<>(); + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, dc, null, + null, Arrays.asList(new MyFileObject(code))); + + CompilationUnitTree cut = ct.parse().iterator().next(); + Trees trees = Trees.instance(ct); + SourcePositions positions = trees.getSourcePositions(); + + new TreeScanner() { + @Override + public Void visitIf(IfTree it, Void v) { + StatementTree st = it.getThenStatement(); + int startpos = (int) positions.getStartPosition(cut, st); + int endpos = (int) positions.getEndPosition(cut, st); + assertEquals("testPositionMissingStatement.execpos", startpos, endpos); + assertEquals("testPositionMissingStatement.execkind", + Kind.EXPRESSION_STATEMENT, + st.getKind()); + Tree err = ((ExpressionStatementTree) st).getExpression(); + startpos = (int) positions.getStartPosition(cut, err); + endpos = (int) positions.getEndPosition(cut, err); + assertEquals("testPositionMissingStatement.errpos", startpos, endpos); + assertEquals("testPositionMissingStatement.errkind", + Kind.ERRONEOUS, + err.getKind()); + return super.visitIf(it, v); + } + }.scan(cut, null); + + assertEquals("testPositionMissingStatement.diags", 1, dc.getDiagnostics().size()); + Diagnostic d = dc.getDiagnostics().get(0); + int startpos = (int) d.getStartPosition(); + int pos = (int) d.getPosition(); + int endpos = (int) d.getEndPosition(); + assertEquals("testPositionMissingStatement.diagspan", startpos, endpos); + assertEquals("testPositionMissingStatement.diagpref", startpos, pos); + } + @Test void testPositionsSane1() throws IOException { performPositionsSanityTest("package test; class Test { " +