Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
blindpirate committed Nov 28, 2022
1 parent 8bb111b commit 0e1d803
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/langtools/tools/javac/attr/AttrRecoveryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ErroneousTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener;
import com.sun.source.util.TreePath;
Expand All @@ -45,7 +46,9 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;

import toolbox.TestRunner;
import toolbox.JavacTask;
Expand Down Expand Up @@ -162,4 +165,61 @@ public Void visitErroneous(ErroneousTree node, Void p) {
throw new AssertionError();
}
}

@Test
public void testVarAssignment2Self(Path base) throws Exception {
Path current = base;
Path src = current.resolve("src");
Path classes = current.resolve("classes");
tb.writeJavaFiles(src,
"""
public class Test {
void t() {
var v = v;
}
}
""");

Files.createDirectories(classes);

AtomicInteger seenVariables = new AtomicInteger();
TreePathScanner<Void, Trees> checkTypes = new TreePathScanner<>() {
@Override
public Void visitVariable(VariableTree node, Trees trees) {
if (node.getName().contentEquals("v")) {
TypeMirror type = trees.getTypeMirror(getCurrentPath());
if (type == null) {
throw new AssertionError("Unexpected null type!");
}
seenVariables.incrementAndGet();
}
return super.visitVariable(node, trees);
}
};

new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(classes)
.files(tb.findJavaFiles(src))
.callback(t -> {
t.addTaskListener(new TaskListener() {
CompilationUnitTree parsed;
@Override
public void finished(TaskEvent e) {
switch (e.getKind()) {
case PARSE -> parsed = e.getCompilationUnit();
case COMPILATION ->
checkTypes.scan(parsed, Trees.instance(t));
}
}
});
})
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);

if (seenVariables.get() != 1) {
throw new AssertionError("Didn't see enough variables: " + seenVariables);
}
}
}

0 comments on commit 0e1d803

Please sign in to comment.