8292625: jshell crash on "var a = a"

Reviewed-by: jlaskey, jlahoda
This commit is contained in:
Bo Zhang 2022-11-29 13:57:56 +00:00 committed by Jan Lahoda
parent 2deb318c9f
commit 33587ffd35
3 changed files with 73 additions and 2 deletions

View File

@ -4653,7 +4653,7 @@ public class Attr extends JCTree.Visitor {
&& v.type.hasTag(NONE)) {
//self reference to implicitly typed variable declaration
log.error(TreeInfo.positionFor(v, env.enclClass), Errors.CantInferLocalVarType(v.name, Fragments.LocalSelfRef));
return v.type = types.createErrorType(v.type);
return tree.type = v.type = types.createErrorType(v.type);
}
// Test (4): if symbol is an instance field of a raw type,

View File

@ -23,7 +23,11 @@
/*
* @test
* @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513 8170015 8170368 8172102 8172103 8165405 8173073 8173848 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466 8172154 8192979 8191842 8198573 8198801 8210596 8210959 8215099 8199623 8236715 8239536 8247456 8246774 8238173
* @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897
* 8167128 8154513 8170015 8170368 8172102 8172103 8165405 8173073 8173848
* 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466 8172154
* 8192979 8191842 8198573 8198801 8210596 8210959 8215099 8199623 8236715
* 8239536 8247456 8246774 8238173 8292625
* @summary Simple jshell tool tests
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@ -957,4 +961,11 @@ public class ToolSimpleTest extends ReplToolTesting {
(a) -> assertCommand(a, "i", "i ==> 1")
);
}
@Test
public void testSelfReference() {
test(
(a) -> assertCommandOutputContains(a, "var a = a;", "cannot use 'var' on self-referencing variable")
);
}
}

View File

@ -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;
@ -45,7 +46,9 @@ import java.nio.file.Files;
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;
@ -162,4 +165,61 @@ public class AttrRecoveryTest extends TestRunner {
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);
}
}
}