8245786: Scope is wrong for ClassTree representing record

Listing RECORD as a class-like Tree Kind while computing Scope.

Reviewed-by: jjg
This commit is contained in:
Jan Lahoda 2020-05-28 13:37:39 +02:00
parent dfc7905a10
commit 7671f2716b
2 changed files with 59 additions and 7 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/api
test/langtools/tools/javac/api

@ -909,6 +909,7 @@ public class JavacTrees extends DocTrees {
case CLASS:
case ENUM:
case INTERFACE:
case RECORD:
// System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
env = enter.getClassEnv(((JCClassDecl)tree).sym);
if (env == null) return null;

@ -23,7 +23,7 @@
/*
* @test
* @bug 8205418 8207229 8207230 8230847
* @bug 8205418 8207229 8207230 8230847 8245786
* @summary Test the outcomes from Trees.getScope
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.comp
@ -43,6 +43,7 @@ import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ConditionalExpressionTree;
import com.sun.source.tree.IdentifierTree;
@ -78,6 +79,7 @@ public class TestGetScopeResult {
new TestGetScopeResult().testAnnotations();
new TestGetScopeResult().testAnnotationsLazy();
new TestGetScopeResult().testCircular();
new TestGetScopeResult().testRecord();
}
public void run() throws IOException {
@ -179,12 +181,7 @@ public class TestGetScopeResult {
@Override
public Void visitLambdaExpression(LambdaExpressionTree node, Void p) {
Scope scope = Trees.instance(t).getScope(new TreePath(getCurrentPath(), node.getBody()));
while (scope.getEnclosingClass() != null) {
for (Element el : scope.getLocalElements()) {
actual.add(el.getSimpleName() + ":" +el.asType().toString());
}
scope = scope.getEnclosingScope();
}
actual.addAll(dumpScope(scope));
return super.visitLambdaExpression(node, p);
}
}.scan(cut, null);
@ -493,4 +490,58 @@ public class TestGetScopeResult {
}
}
void testRecord() throws IOException {
JavacTool c = JavacTool.create();
try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) {
class MyFileObject extends SimpleJavaFileObject {
MyFileObject() {
super(URI.create("myfo:///Test.java"), SOURCE);
}
@Override
public String getCharContent(boolean ignoreEncodingErrors) {
return "record Test<T>(int mark) {}";
}
}
Context ctx = new Context();
TestAnalyzer.preRegister(ctx);
List<String> options = List.of("--enable-preview",
"-source", System.getProperty("java.specification.version"));
JavacTask t = (JavacTask) c.getTask(null, fm, null, options, null,
List.of(new MyFileObject()), ctx);
CompilationUnitTree cut = t.parse().iterator().next();
t.analyze();
List<String> actual = new ArrayList<>();
new TreePathScanner<Void, Void>() {
@Override
public Void visitClass(ClassTree node, Void p) {
Scope scope = Trees.instance(t).getScope(getCurrentPath());
actual.addAll(dumpScope(scope));
return super.visitClass(node, p);
}
}.scan(cut, null);
List<String> expected = List.of(
"super:java.lang.Record",
"this:Test<T>",
"T:T"
);
if (!expected.equals(actual)) {
throw new AssertionError("Unexpected Scope content: " + actual);
}
}
}
private List<String> dumpScope(Scope scope) {
List<String> content = new ArrayList<>();
while (scope.getEnclosingClass() != null) {
for (Element el : scope.getLocalElements()) {
content.add(el.getSimpleName() + ":" +el.asType().toString());
}
scope = scope.getEnclosingScope();
}
return content;
}
}