8310326: Incorrect position of the synthetic unnamed class

Reviewed-by: jlaskey
This commit is contained in:
Jan Lahoda 2023-06-20 10:34:52 +00:00
parent a0595761ef
commit 4ca548fe74
2 changed files with 28 additions and 3 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/parser
test/langtools/tools/javac/parser

@ -4039,7 +4039,7 @@ public class JavacParser implements Parser {
}
}
int primaryPos = defs.first().pos;
int primaryPos = getStartPos(defs.first());
String simplename = PathFileObject.getSimpleName(log.currentSourceFile());
if (simplename.endsWith(".java")) {
@ -4050,7 +4050,7 @@ public class JavacParser implements Parser {
}
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(),

@ -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
@ -2395,6 +2395,31 @@ public class JavacParserTest extends TestCase {
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)