Defer attaching type annotations to completion

This commit is contained in:
Liam Miller-Cushon 2025-11-04 08:57:17 +01:00
commit e85cb0315d
4 changed files with 16 additions and 18 deletions

View File

@ -56,6 +56,7 @@ import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
import com.sun.tools.javac.comp.Check;
import com.sun.tools.javac.file.BaseFileManager;
import com.sun.tools.javac.file.PathFileObject;
import com.sun.tools.javac.jvm.ClassFile.Version;
@ -2309,7 +2310,8 @@ public class ClassReader {
currentClassFile = classFile;
List<Attribute.TypeCompound> newList = deproxyTypeCompoundList(proxies);
sym.setTypeAttributes(newList.prependList(sym.getRawTypeAttributes()));
addTypeAnnotationsToSymbol(sym, newList);
Assert.check(sym.completer == Completer.NULL_COMPLETER);
sym.completer = sym -> addTypeAnnotationsToSymbol(sym, newList);
} finally {
currentClassFile = previousClassFile;
}
@ -2331,7 +2333,7 @@ public class ClassReader {
} catch (CompletionFailure ex) {
JavaFileObject prev = log.useSource(currentClassFile);
try {
log.warning(Warnings.CantAttachTypeAnnotations(attributes, s.owner, s.name, ex.getDetailValue()));
log.error(Errors.CantAttachTypeAnnotations(attributes, s.owner, s.name, ex.getDetailValue()));
} finally {
log.useSource(prev);
}

View File

@ -2465,7 +2465,7 @@ compiler.warn.annotation.method.not.found.reason=\
Cannot find annotation method ''{1}()'' in type ''{0}'': {2}
# 0: list of annotation, 1: symbol, 2: name, 3: message segment
compiler.warn.cant.attach.type.annotations=\
compiler.err.cant.attach.type.annotations=\
Cannot attach type annotations {0} to {1}.{2}:\n\
{3}

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 8337998 8370800
* @bug 8337998
* @summary CompletionFailure in getEnclosingType attaching type annotations
* @library /tools/javac/lib /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
@ -56,16 +56,13 @@ public class CompletionErrorOnEnclosingType {
class A<E> {}
class B {
public @Anno A<String> a;
private @Anno A<String> a;
}
""";
String cSrc =
"""
class C {
B b;
public void test() {
b.a.toString();
}
}
""";
@ -77,26 +74,25 @@ public class CompletionErrorOnEnclosingType {
tb.createDirectories(out);
new JavacTask(tb).outdir(out).files(tb.findJavaFiles(src)).run();
// now if we remove A.class javac should not crash
// now if we remove A.class there will be an error but javac should not crash
tb.deleteFiles(out.resolve("A.class"));
List<String> log =
new JavacTask(tb)
.outdir(out)
.classpath(out)
// DO NOT SUBMIT - this is a draft, see discussion in PR
.options(/*"-Werror",*/ "-XDrawDiagnostics")
.options("-XDrawDiagnostics")
.files(src.resolve("C.java"))
.run(Expect.FAIL)
.run(Expect.SUCCESS)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
var expectedOutput =
List.of(
"B.class:-:-: compiler.warn.cant.attach.type.annotations: @Anno, B, a, (compiler.misc.class.file.not.found: A)",
"C.java:4:8: compiler.err.cant.access: A, (compiler.misc.class.file.not.found: A)",
"1 error",
"1 warning");
"B.class:-:-: compiler.err.cant.attach.type.annotations: @Anno, B, a,"
+ " (compiler.misc.class.file.not.found: A)",
"1 error");
// FIXME
expectedOutput = List.of("");
if (!expectedOutput.equals(log)) {
throw new Exception("expected output not found: " + log);
}

View File

@ -41,7 +41,7 @@ compiler.err.signature.doesnt.match.intf # UNUSED
compiler.err.signature.doesnt.match.supertype # UNUSED
compiler.err.source.cant.overwrite.input.file
compiler.err.stack.sim.error
compiler.warn.cant.attach.type.annotations # bad class file
compiler.err.cant.attach.type.annotations # bad class file
compiler.err.type.var.more.than.once # UNUSED
compiler.err.type.var.more.than.once.in.result # UNUSED
compiler.err.unexpected.type