Skip to content

Commit

Permalink
8310326: Incorrect position of the synthetic unnamed class
Browse files Browse the repository at this point in the history
Reviewed-by: jlaskey
  • Loading branch information
lahodaj committed Jun 20, 2023
1 parent a059576 commit 4ca548f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4039,7 +4039,7 @@ private List<JCTree> constructUnnamedClass(List<JCTree> origDefs) {
}
}

int primaryPos = defs.first().pos;
int primaryPos = getStartPos(defs.first());
String simplename = PathFileObject.getSimpleName(log.currentSourceFile());

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

Name name = names.fromString(simplename);
JCModifiers unnamedMods = F.at(primaryPos)
JCModifiers unnamedMods = F.at(Position.NOPOS)
.Modifiers(Flags.FINAL|Flags.SYNTHETIC|Flags.UNNAMED_CLASS, List.nil());
JCClassDecl unnamed = F.at(primaryPos).ClassDef(
unnamedMods, name, List.nil(), null, List.nil(), List.nil(),
Expand Down
27 changes: 26 additions & 1 deletion test/langtools/tools/javac/parser/JavacParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
Expand Down Expand Up @@ -2395,6 +2395,31 @@ public Void visitCase(CaseTree node, Void p) {
codes);
}

@Test //JDK-8310326
void testUnnamedClassPositions() throws IOException {
String code = """
void main() {
}
""";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, List.of("--enable-preview", "--source", System.getProperty("java.specification.version")),
null, Arrays.asList(new MyFileObject(code)));
Trees trees = Trees.instance(ct);
SourcePositions sp = trees.getSourcePositions();
CompilationUnitTree cut = ct.parse().iterator().next();
new TreeScanner<Void, Void>() {
@Override
public Void visitClass(ClassTree node, Void p) {
assertEquals("Wrong start position", 0, sp.getStartPosition(cut, node));
assertEquals("Wrong end position", -1, sp.getEndPosition(cut, node));
assertEquals("Wrong modifiers start position", -1, sp.getStartPosition(cut, node.getModifiers()));
assertEquals("Wrong modifiers end position", -1, sp.getEndPosition(cut, node.getModifiers()));
return super.visitClass(node, p);
}
}.scan(cut, null);
}

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

1 comment on commit 4ca548f

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.