Skip to content

Commit

Permalink
8291643: Consider omitting type annotations from type error diagnostics
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
cushon committed Jun 5, 2024
1 parent 4c09d9f commit 7564949
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public enum SimpleType implements 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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private Type typeWithAnnotations(final JCTree typetree, final Type type,
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5868,8 +5868,10 @@ private void validateAnnotatedType(final JCTree errtree, final Type type) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ else if (arg instanceof Iterable<?> iterable && !(arg instanceof Path)) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

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;
Expand Down Expand Up @@ -606,6 +607,9 @@ public Fragment(String prefix, String key, Object... args) {
}
}

/** 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 { }
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/langtools/tools/javac/lambda/LambdaConv25.out
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/langtools/tools/javac/warnings/6747671/T6747671.out
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 comment on commit 7564949

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.