8332230: jshell throws AssertionError when processing annotations

Reviewed-by: asotona
This commit is contained in:
Jan Lahoda 2024-05-30 06:02:31 +00:00
parent 66d9bfce29
commit 9a72068ef0
5 changed files with 197 additions and 4 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools
jdk/jshell
tools/javac
annotations/typeAnnotations
recovery

@ -1166,6 +1166,15 @@ public class Annotate {
scan(tree.args);
// the anonymous class instantiation if any will be visited separately.
}
@Override
public void visitErroneous(JCErroneous tree) {
if (tree.errs != null) {
for (JCTree err : tree.errs) {
scan(err);
}
}
}
}
/* *******************

@ -5279,6 +5279,8 @@ public class Attr extends JCTree.Visitor {
default:
attribClass(env.tree.pos(), env.enclClass.sym);
}
annotate.flush();
}
public void attribPackage(DiagnosticPosition pos, PackageSymbol p) {

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 8270139 8273039 8286895
* @bug 8270139 8273039 8286895 8332230
* @summary Verify error recovery in JShell
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@ -65,4 +65,12 @@ public class ErrorRecoveryTest extends KullaTesting {
DiagCheck.DIAG_IGNORE,
ste(MAIN_SNIPPET, NONEXISTENT, REJECTED, false, null));
}
//JDK-8332230:
public void testAnnotationsd() {
assertEval("k=aa:a.@a",
DiagCheck.DIAG_ERROR,
DiagCheck.DIAG_IGNORE,
ste(MAIN_SNIPPET, NONEXISTENT, REJECTED, false, null));
}
}

@ -0,0 +1,141 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8332230
* @summary Verify that the last type in the last method has correct type annotations
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.compiler/com.sun.tools.javac.util
* @build toolbox.ToolBox toolbox.JavacTask
* @run main QueuesAreFlushed
*/
import com.sun.source.tree.AnnotatedTypeTree;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskEvent.Kind;
import com.sun.source.util.TaskListener;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.type.TypeMirror;
import toolbox.TestRunner;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.ToolBox;
public class QueuesAreFlushed extends TestRunner {
private ToolBox tb;
public static void main(String... args) throws Exception {
new QueuesAreFlushed().runTests();
}
QueuesAreFlushed() {
super(System.err);
tb = new ToolBox();
}
public void runTests() throws Exception {
runTests(m -> new Object[] { Paths.get(m.getName()) });
}
@Test
public void testTypeAnnotations(Path base) throws Exception {
Path current = base.resolve(".");
Path src = current.resolve("src");
Path classes = current.resolve("classes");
tb.writeJavaFiles(src,
"""
package test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
public class Test {
@Target(ElementType.TYPE_USE)
@interface Ann {}
public static void main(Object o) {
boolean b;
b = o instanceof @Ann String;
}
}
""");
Files.createDirectories(classes);
AtomicBoolean seenAnnotationMirror = new AtomicBoolean();
new JavacTask(tb)
.outdir(classes)
.callback(task -> {
task.addTaskListener(new TaskListener() {
@Override
public void finished(TaskEvent e) {
if (e.getKind() != Kind.ANALYZE) {
return ;
}
new TreePathScanner<Void, Void>() {
@Override
public Void visitAnnotatedType(AnnotatedTypeTree node, Void p) {
TypeMirror type = Trees.instance(task).getTypeMirror(getCurrentPath());
List<? extends AnnotationMirror> ams = type.getAnnotationMirrors();
if (ams.size() != 1) {
throw new AssertionError("Expected a single annotation, but got: " + ams);
}
String expectedMirror = "@test.Test.Ann";
String actualMirror = ams.get(0).toString();
if (!Objects.equals(expectedMirror, actualMirror)) {
throw new AssertionError("Expected: " + expectedMirror +
", but got: " + actualMirror);
}
seenAnnotationMirror.set(true);
return super.visitAnnotatedType(node, p);
}
}.scan(e.getCompilationUnit(), null);
}
});
})
.files(tb.findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll();
if (!seenAnnotationMirror.get()) {
throw new AssertionError("Didn't see the AnnotatedTypeTree!");
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 8301580 8322159
* @bug 8301580 8322159 8332230
* @summary Verify error recovery w.r.t. Attr
* @library /tools/lib
* @enablePreview
@ -124,4 +124,37 @@ public class AttrRecovery extends TestRunner {
}
}
@Test //JDK-8332230
public void testAnnotationsInErroneousTree1() throws Exception {
String code = """
package p;
public class C {
static int v;
public void t() {
//not a statement expression,
//will be wrapped in an erroneous tree:
p.@Ann C.v;
}
@interface Ann {}
}
""";
Path curPath = Path.of(".");
List<String> actual = new JavacTask(tb)
.options("-XDrawDiagnostics", "-XDdev", "-XDshould-stop.at=FLOW")
.sources(code)
.outdir(curPath)
.run(Expect.FAIL)
.writeAll()
.getOutputLines(OutputKind.DIRECT);
List<String> expected = List.of(
"C.java:7:17: compiler.err.not.stmt",
"1 error"
);
if (!Objects.equals(actual, expected)) {
error("Expected: " + expected + ", but got: " + actual);
}
}
}