Skip to content

Commit 4ca548f

Browse files
committedJun 20, 2023
8310326: Incorrect position of the synthetic unnamed class
Reviewed-by: jlaskey
1 parent a059576 commit 4ca548f

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed
 

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4039,7 +4039,7 @@ private List<JCTree> constructUnnamedClass(List<JCTree> origDefs) {
40394039
}
40404040
}
40414041

4042-
int primaryPos = defs.first().pos;
4042+
int primaryPos = getStartPos(defs.first());
40434043
String simplename = PathFileObject.getSimpleName(log.currentSourceFile());
40444044

40454045
if (simplename.endsWith(".java")) {
@@ -4050,7 +4050,7 @@ private List<JCTree> constructUnnamedClass(List<JCTree> origDefs) {
40504050
}
40514051

40524052
Name name = names.fromString(simplename);
4053-
JCModifiers unnamedMods = F.at(primaryPos)
4053+
JCModifiers unnamedMods = F.at(Position.NOPOS)
40544054
.Modifiers(Flags.FINAL|Flags.SYNTHETIC|Flags.UNNAMED_CLASS, List.nil());
40554055
JCClassDecl unnamed = F.at(primaryPos).ClassDef(
40564056
unnamedMods, name, List.nil(), null, List.nil(), List.nil(),

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

+26-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 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671
26+
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326
2727
* @summary tests error and diagnostics positions
2828
* @author Jan Lahoda
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -2395,6 +2395,31 @@ public Void visitCase(CaseTree node, Void p) {
23952395
codes);
23962396
}
23972397

2398+
@Test //JDK-8310326
2399+
void testUnnamedClassPositions() throws IOException {
2400+
String code = """
2401+
void main() {
2402+
}
2403+
""";
2404+
DiagnosticCollector<JavaFileObject> coll =
2405+
new DiagnosticCollector<>();
2406+
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, List.of("--enable-preview", "--source", System.getProperty("java.specification.version")),
2407+
null, Arrays.asList(new MyFileObject(code)));
2408+
Trees trees = Trees.instance(ct);
2409+
SourcePositions sp = trees.getSourcePositions();
2410+
CompilationUnitTree cut = ct.parse().iterator().next();
2411+
new TreeScanner<Void, Void>() {
2412+
@Override
2413+
public Void visitClass(ClassTree node, Void p) {
2414+
assertEquals("Wrong start position", 0, sp.getStartPosition(cut, node));
2415+
assertEquals("Wrong end position", -1, sp.getEndPosition(cut, node));
2416+
assertEquals("Wrong modifiers start position", -1, sp.getStartPosition(cut, node.getModifiers()));
2417+
assertEquals("Wrong modifiers end position", -1, sp.getEndPosition(cut, node.getModifiers()));
2418+
return super.visitClass(node, p);
2419+
}
2420+
}.scan(cut, null);
2421+
}
2422+
23982423
void run(String[] args) throws Exception {
23992424
int passed = 0, failed = 0;
24002425
final Pattern p = (args != null && args.length > 0)

0 commit comments

Comments
 (0)
Please sign in to comment.