Skip to content

Commit 46809b3

Browse files
archiecobbsVicente Romero
authored and
Vicente Romero
committed
8324736: Invalid end positions for EMPTY_STATEMENT
Reviewed-by: vromero
1 parent f7f291c commit 46809b3

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -3965,8 +3965,9 @@ public JCTree.JCCompilationUnit parseCompilationUnit() {
39653965
// comes after before deciding how best to handle them.
39663966
ListBuffer<JCTree> semiList = new ListBuffer<>();
39673967
while (firstTypeDecl && mods == null && token.kind == SEMI) {
3968-
semiList.append(toP(F.at(token.pos).Skip()));
3968+
int pos = token.pos;
39693969
nextToken();
3970+
semiList.append(toP(F.at(pos).Skip()));
39703971
if (token.kind == EOF)
39713972
break OUTER;
39723973
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8324736
27+
* @summary Verify starting and ending source positions are not backwards
28+
* @modules jdk.compiler/com.sun.tools.javac.tree
29+
* @run main ReversedSourcePositions
30+
*/
31+
32+
import com.sun.source.tree.CompilationUnitTree;
33+
import com.sun.source.tree.Tree;
34+
import com.sun.source.util.JavacTask;
35+
import com.sun.source.util.TreeScanner;
36+
import com.sun.tools.javac.tree.JCTree;
37+
38+
import java.io.IOException;
39+
import java.net.URI;
40+
import java.util.List;
41+
42+
import javax.tools.JavaCompiler;
43+
import javax.tools.JavaFileObject;
44+
import javax.tools.SimpleJavaFileObject;
45+
import javax.tools.ToolProvider;
46+
47+
public class ReversedSourcePositions {
48+
49+
public static void main(String... args) throws Exception {
50+
51+
// Create test case source
52+
var source = new SimpleJavaFileObject(URI.create("file://T.java"), JavaFileObject.Kind.SOURCE) {
53+
@Override
54+
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
55+
return """
56+
package errorpronecrash;
57+
;
58+
public class ReproFile {}
59+
""";
60+
}
61+
};
62+
63+
// Parse source
64+
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
65+
JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, List.of(), List.of(), List.of(source));
66+
Iterable<? extends CompilationUnitTree> units = ((JavacTask)task).parse();
67+
68+
// Look for reversed source positions
69+
JCTree.JCCompilationUnit unit = (JCTree.JCCompilationUnit)units.iterator().next();
70+
unit.accept(new TreeScanner<Void, Void>() {
71+
@Override
72+
public Void scan(Tree node, Void aVoid) {
73+
if (node instanceof JCTree tree) {
74+
int start = tree.getStartPosition();
75+
int end = tree.getEndPosition(unit.endPositions);
76+
if (start >= end) {
77+
throw new AssertionError(
78+
String.format("[%d, %d] %s %s\n", start, end, tree.getKind(), tree));
79+
}
80+
}
81+
return super.scan(node, aVoid);
82+
}
83+
}, null);
84+
}
85+
}

0 commit comments

Comments
 (0)