diff --git a/make/langtools/tools/propertiesparser/parser/MessageType.java b/make/langtools/tools/propertiesparser/parser/MessageType.java index 05da11d2992..cba9263567d 100644 --- a/make/langtools/tools/propertiesparser/parser/MessageType.java +++ b/make/langtools/tools/propertiesparser/parser/MessageType.java @@ -97,6 +97,7 @@ public interface MessageType { TOKEN("token", "TokenKind", "com.sun.tools.javac.parser.Tokens"), TREE_TAG("tree tag", "Tag", "com.sun.tools.javac.tree.JCTree"), TYPE("type", "Type", "com.sun.tools.javac.code"), + ANNOTATED_TYPE("annotated-type", "AnnotatedType", "com.sun.tools.javac.util.JCDiagnostic"), URL("url", "URL", "java.net"), SET("set", "Set", "java.util"), LIST("list", "List", "java.util"), diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java index 865853c27dc..36ab4a145f9 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java @@ -510,7 +510,7 @@ public class TypeAnnotations { Fragments.TypeAnnotation1(onlyTypeAnnotations.head) : Fragments.TypeAnnotation(onlyTypeAnnotations); log.error(typetree.pos(), Errors.TypeAnnotationInadmissible( - annotationFragment, annotated.tsym.owner, annotated)); + annotationFragment, annotated.tsym.owner, new JCDiagnostic.AnnotatedType(annotated))); return type; } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index 7595444b358..533541bd108 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -5868,8 +5868,10 @@ public class Attr extends JCTree.Visitor { Fragment annotationFragment = onlyTypeAnnotations.size() == 1 ? Fragments.TypeAnnotation1(onlyTypeAnnotations.head) : Fragments.TypeAnnotation(onlyTypeAnnotations); + JCDiagnostic.AnnotatedType annotatedType = new JCDiagnostic.AnnotatedType( + type.stripMetadata().annotatedType(onlyTypeAnnotations)); log.error(at.underlyingType.pos(), Errors.TypeAnnotationInadmissible(annotationFragment, - type.tsym.owner, type.stripMetadata().annotatedType(onlyTypeAnnotations))); + type.tsym.owner, annotatedType)); } repeat = false; } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties index 2a64ee1ba03..8576106e15f 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties @@ -3258,7 +3258,7 @@ compiler.err.this.as.identifier=\ compiler.err.receiver.parameter.not.applicable.constructor.toplevel.class=\ receiver parameter not applicable for constructor of top-level class -# 0: fragment, 1: symbol, 2: type +# 0: fragment, 1: symbol, 2: annotated-type compiler.err.type.annotation.inadmissible=\ {0} not expected here\n\ (to annotate a qualified type, write {1}.{2}) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java index fc33c895fb2..d9d55e16528 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java @@ -200,7 +200,10 @@ public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter return formatIterable(d, iterable, l); } else if (arg instanceof Type type) { - return printer.visit(type, l); + return printer.visit(type.stripMetadata(), l); + } + else if (arg instanceof JCDiagnostic.AnnotatedType type) { + return printer.visit(type.type(), l); } else if (arg instanceof Symbol symbol) { return printer.visit(symbol, l); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JCDiagnostic.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JCDiagnostic.java index 2e9394c0bd4..3f779175540 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JCDiagnostic.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/JCDiagnostic.java @@ -36,6 +36,7 @@ import javax.tools.JavaFileObject; import com.sun.tools.javac.api.DiagnosticFormatter; import com.sun.tools.javac.code.Lint.LintCategory; +import com.sun.tools.javac.code.Type; import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.util.DefinedBy.Api; @@ -606,6 +607,9 @@ public class JCDiagnostic implements Diagnostic { } } + /** A diagnostic argument that is a type, which will be printed with type annotations. */ + public record AnnotatedType(Type type) {} + /** * Create a diagnostic object. * @param formatter the formatter to use for the diagnostic diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/failures/IncompatibleTypes.java b/test/langtools/tools/javac/annotations/typeAnnotations/failures/IncompatibleTypes.java new file mode 100644 index 00000000000..06e1e5ba6c1 --- /dev/null +++ b/test/langtools/tools/javac/annotations/typeAnnotations/failures/IncompatibleTypes.java @@ -0,0 +1,17 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8291643 + * @summary Consider omitting type annotations from type error diagnostics + * @compile/fail/ref=IncompatibleTypes.out -XDrawDiagnostics IncompatibleTypes.java + */ +import java.lang.annotation.*; +import java.util.List; + +class IncompatibleTypes { + List<@A Number> f(List xs) { + return xs; + } +} + +@Target(ElementType.TYPE_USE) +@interface A { } diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/failures/IncompatibleTypes.out b/test/langtools/tools/javac/annotations/typeAnnotations/failures/IncompatibleTypes.out new file mode 100644 index 00000000000..db97e6ae235 --- /dev/null +++ b/test/langtools/tools/javac/annotations/typeAnnotations/failures/IncompatibleTypes.out @@ -0,0 +1,2 @@ +IncompatibleTypes.java:12:12: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.util.List, java.util.List) +1 error diff --git a/test/langtools/tools/javac/lambda/LambdaConv25.out b/test/langtools/tools/javac/lambda/LambdaConv25.out index 8f544da42be..905ce521b96 100644 --- a/test/langtools/tools/javac/lambda/LambdaConv25.out +++ b/test/langtools/tools/javac/lambda/LambdaConv25.out @@ -1,2 +1,2 @@ -LambdaConv25.java:19:15: compiler.err.prob.found.req: (compiler.misc.invalid.generic.lambda.target: ()void, kindname.interface, LambdaConv25.C) +LambdaConv25.java:19:15: compiler.err.prob.found.req: (compiler.misc.invalid.generic.lambda.target: ()void, kindname.interface, LambdaConv25.C) 1 error diff --git a/test/langtools/tools/javac/warnings/6747671/T6747671.out b/test/langtools/tools/javac/warnings/6747671/T6747671.out index 518804d68b5..1176234cd39 100644 --- a/test/langtools/tools/javac/warnings/6747671/T6747671.out +++ b/test/langtools/tools/javac/warnings/6747671/T6747671.out @@ -7,6 +7,6 @@ T6747671.java:29:28: compiler.warn.raw.class.use: T6747671.B, T6747671.B T6747671.java:32:9: compiler.warn.raw.class.use: T6747671.A, T6747671.A T6747671.java:32:20: compiler.warn.raw.class.use: T6747671.A, T6747671.A T6747671.java:33:16: compiler.warn.raw.class.use: T6747671.A.Z, T6747671.A.Z -T6747671.java:36:9: compiler.warn.raw.class.use: @T6747671.TA T6747671.B, T6747671.B +T6747671.java:36:9: compiler.warn.raw.class.use: T6747671.B, T6747671.B T6747671.java:36:27: compiler.warn.raw.class.use: T6747671.B, T6747671.B 11 warnings