8291643: Consider omitting type annotations from type error diagnostics

Reviewed-by: vromero
This commit is contained in:
Liam Miller-Cushon 2024-06-05 19:10:13 +00:00
parent 4c09d9f828
commit 7564949a56
10 changed files with 35 additions and 6 deletions

View File

@ -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"),

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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})

View File

@ -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);

View File

@ -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<JavaFileObject> {
}
}
/** 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

View File

@ -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<String> xs) {
return xs;
}
}
@Target(ElementType.TYPE_USE)
@interface A { }

View File

@ -0,0 +1,2 @@
IncompatibleTypes.java:12:12: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.util.List<java.lang.String>, java.util.List<java.lang.Number>)
1 error

View File

@ -1,2 +1,2 @@
LambdaConv25.java:19:15: compiler.err.prob.found.req: (compiler.misc.invalid.generic.lambda.target: <X>()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

View File

@ -7,6 +7,6 @@ T6747671.java:29:28: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
T6747671.java:32:9: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
T6747671.java:32:20: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
T6747671.java:33:16: compiler.warn.raw.class.use: T6747671.A.Z, T6747671<E>.A<X>.Z<Y>
T6747671.java:36:9: compiler.warn.raw.class.use: @T6747671.TA T6747671.B, T6747671.B<X>
T6747671.java:36:9: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
T6747671.java:36:27: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
11 warnings