8291643: Consider omitting type annotations from type error diagnostics
Reviewed-by: vromero
This commit is contained in:
parent
4c09d9f828
commit
7564949a56
@ -97,6 +97,7 @@ public interface MessageType {
|
|||||||
TOKEN("token", "TokenKind", "com.sun.tools.javac.parser.Tokens"),
|
TOKEN("token", "TokenKind", "com.sun.tools.javac.parser.Tokens"),
|
||||||
TREE_TAG("tree tag", "Tag", "com.sun.tools.javac.tree.JCTree"),
|
TREE_TAG("tree tag", "Tag", "com.sun.tools.javac.tree.JCTree"),
|
||||||
TYPE("type", "Type", "com.sun.tools.javac.code"),
|
TYPE("type", "Type", "com.sun.tools.javac.code"),
|
||||||
|
ANNOTATED_TYPE("annotated-type", "AnnotatedType", "com.sun.tools.javac.util.JCDiagnostic"),
|
||||||
URL("url", "URL", "java.net"),
|
URL("url", "URL", "java.net"),
|
||||||
SET("set", "Set", "java.util"),
|
SET("set", "Set", "java.util"),
|
||||||
LIST("list", "List", "java.util"),
|
LIST("list", "List", "java.util"),
|
||||||
|
@ -510,7 +510,7 @@ public class TypeAnnotations {
|
|||||||
Fragments.TypeAnnotation1(onlyTypeAnnotations.head) :
|
Fragments.TypeAnnotation1(onlyTypeAnnotations.head) :
|
||||||
Fragments.TypeAnnotation(onlyTypeAnnotations);
|
Fragments.TypeAnnotation(onlyTypeAnnotations);
|
||||||
log.error(typetree.pos(), Errors.TypeAnnotationInadmissible(
|
log.error(typetree.pos(), Errors.TypeAnnotationInadmissible(
|
||||||
annotationFragment, annotated.tsym.owner, annotated));
|
annotationFragment, annotated.tsym.owner, new JCDiagnostic.AnnotatedType(annotated)));
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5868,8 +5868,10 @@ public class Attr extends JCTree.Visitor {
|
|||||||
Fragment annotationFragment = onlyTypeAnnotations.size() == 1 ?
|
Fragment annotationFragment = onlyTypeAnnotations.size() == 1 ?
|
||||||
Fragments.TypeAnnotation1(onlyTypeAnnotations.head) :
|
Fragments.TypeAnnotation1(onlyTypeAnnotations.head) :
|
||||||
Fragments.TypeAnnotation(onlyTypeAnnotations);
|
Fragments.TypeAnnotation(onlyTypeAnnotations);
|
||||||
|
JCDiagnostic.AnnotatedType annotatedType = new JCDiagnostic.AnnotatedType(
|
||||||
|
type.stripMetadata().annotatedType(onlyTypeAnnotations));
|
||||||
log.error(at.underlyingType.pos(), Errors.TypeAnnotationInadmissible(annotationFragment,
|
log.error(at.underlyingType.pos(), Errors.TypeAnnotationInadmissible(annotationFragment,
|
||||||
type.tsym.owner, type.stripMetadata().annotatedType(onlyTypeAnnotations)));
|
type.tsym.owner, annotatedType));
|
||||||
}
|
}
|
||||||
repeat = false;
|
repeat = false;
|
||||||
}
|
}
|
||||||
|
@ -3258,7 +3258,7 @@ compiler.err.this.as.identifier=\
|
|||||||
compiler.err.receiver.parameter.not.applicable.constructor.toplevel.class=\
|
compiler.err.receiver.parameter.not.applicable.constructor.toplevel.class=\
|
||||||
receiver parameter not applicable for constructor of top-level 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=\
|
compiler.err.type.annotation.inadmissible=\
|
||||||
{0} not expected here\n\
|
{0} not expected here\n\
|
||||||
(to annotate a qualified type, write {1}.{2})
|
(to annotate a qualified type, write {1}.{2})
|
||||||
|
@ -200,7 +200,10 @@ public abstract class AbstractDiagnosticFormatter implements DiagnosticFormatter
|
|||||||
return formatIterable(d, iterable, l);
|
return formatIterable(d, iterable, l);
|
||||||
}
|
}
|
||||||
else if (arg instanceof Type type) {
|
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) {
|
else if (arg instanceof Symbol symbol) {
|
||||||
return printer.visit(symbol, l);
|
return printer.visit(symbol, l);
|
||||||
|
@ -36,6 +36,7 @@ import javax.tools.JavaFileObject;
|
|||||||
|
|
||||||
import com.sun.tools.javac.api.DiagnosticFormatter;
|
import com.sun.tools.javac.api.DiagnosticFormatter;
|
||||||
import com.sun.tools.javac.code.Lint.LintCategory;
|
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.EndPosTable;
|
||||||
import com.sun.tools.javac.tree.JCTree;
|
import com.sun.tools.javac.tree.JCTree;
|
||||||
import com.sun.tools.javac.util.DefinedBy.Api;
|
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.
|
* Create a diagnostic object.
|
||||||
* @param formatter the formatter to use for the diagnostic
|
* @param formatter the formatter to use for the diagnostic
|
||||||
|
@ -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 { }
|
@ -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
|
@ -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
|
1 error
|
||||||
|
@ -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: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: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: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>
|
T6747671.java:36:27: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
|
||||||
11 warnings
|
11 warnings
|
||||||
|
Loading…
Reference in New Issue
Block a user