Skip to content

Commit 7564949

Browse files
committed
8291643: Consider omitting type annotations from type error diagnostics
Reviewed-by: vromero
1 parent 4c09d9f commit 7564949

File tree

10 files changed

+35
-6
lines changed

10 files changed

+35
-6
lines changed

make/langtools/tools/propertiesparser/parser/MessageType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public enum SimpleType implements MessageType {
9797
TOKEN("token", "TokenKind", "com.sun.tools.javac.parser.Tokens"),
9898
TREE_TAG("tree tag", "Tag", "com.sun.tools.javac.tree.JCTree"),
9999
TYPE("type", "Type", "com.sun.tools.javac.code"),
100+
ANNOTATED_TYPE("annotated-type", "AnnotatedType", "com.sun.tools.javac.util.JCDiagnostic"),
100101
URL("url", "URL", "java.net"),
101102
SET("set", "Set", "java.util"),
102103
LIST("list", "List", "java.util"),

src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ private Type typeWithAnnotations(final JCTree typetree, final Type type,
510510
Fragments.TypeAnnotation1(onlyTypeAnnotations.head) :
511511
Fragments.TypeAnnotation(onlyTypeAnnotations);
512512
log.error(typetree.pos(), Errors.TypeAnnotationInadmissible(
513-
annotationFragment, annotated.tsym.owner, annotated));
513+
annotationFragment, annotated.tsym.owner, new JCDiagnostic.AnnotatedType(annotated)));
514514
return type;
515515
}
516516

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5868,8 +5868,10 @@ private void validateAnnotatedType(final JCTree errtree, final Type type) {
58685868
Fragment annotationFragment = onlyTypeAnnotations.size() == 1 ?
58695869
Fragments.TypeAnnotation1(onlyTypeAnnotations.head) :
58705870
Fragments.TypeAnnotation(onlyTypeAnnotations);
5871+
JCDiagnostic.AnnotatedType annotatedType = new JCDiagnostic.AnnotatedType(
5872+
type.stripMetadata().annotatedType(onlyTypeAnnotations));
58715873
log.error(at.underlyingType.pos(), Errors.TypeAnnotationInadmissible(annotationFragment,
5872-
type.tsym.owner, type.stripMetadata().annotatedType(onlyTypeAnnotations)));
5874+
type.tsym.owner, annotatedType));
58735875
}
58745876
repeat = false;
58755877
}

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3258,7 +3258,7 @@ compiler.err.this.as.identifier=\
32583258
compiler.err.receiver.parameter.not.applicable.constructor.toplevel.class=\
32593259
receiver parameter not applicable for constructor of top-level class
32603260

3261-
# 0: fragment, 1: symbol, 2: type
3261+
# 0: fragment, 1: symbol, 2: annotated-type
32623262
compiler.err.type.annotation.inadmissible=\
32633263
{0} not expected here\n\
32643264
(to annotate a qualified type, write {1}.{2})

src/jdk.compiler/share/classes/com/sun/tools/javac/util/AbstractDiagnosticFormatter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ else if (arg instanceof Iterable<?> iterable && !(arg instanceof Path)) {
200200
return formatIterable(d, iterable, l);
201201
}
202202
else if (arg instanceof Type type) {
203-
return printer.visit(type, l);
203+
return printer.visit(type.stripMetadata(), l);
204+
}
205+
else if (arg instanceof JCDiagnostic.AnnotatedType type) {
206+
return printer.visit(type.type(), l);
204207
}
205208
else if (arg instanceof Symbol symbol) {
206209
return printer.visit(symbol, l);

src/jdk.compiler/share/classes/com/sun/tools/javac/util/JCDiagnostic.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import com.sun.tools.javac.api.DiagnosticFormatter;
3838
import com.sun.tools.javac.code.Lint.LintCategory;
39+
import com.sun.tools.javac.code.Type;
3940
import com.sun.tools.javac.tree.EndPosTable;
4041
import com.sun.tools.javac.tree.JCTree;
4142
import com.sun.tools.javac.util.DefinedBy.Api;
@@ -606,6 +607,9 @@ public Fragment(String prefix, String key, Object... args) {
606607
}
607608
}
608609

610+
/** A diagnostic argument that is a type, which will be printed with type annotations. */
611+
public record AnnotatedType(Type type) {}
612+
609613
/**
610614
* Create a diagnostic object.
611615
* @param formatter the formatter to use for the diagnostic
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8291643
4+
* @summary Consider omitting type annotations from type error diagnostics
5+
* @compile/fail/ref=IncompatibleTypes.out -XDrawDiagnostics IncompatibleTypes.java
6+
*/
7+
import java.lang.annotation.*;
8+
import java.util.List;
9+
10+
class IncompatibleTypes {
11+
List<@A Number> f(List<String> xs) {
12+
return xs;
13+
}
14+
}
15+
16+
@Target(ElementType.TYPE_USE)
17+
@interface A { }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
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>)
2+
1 error
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
LambdaConv25.java:19:15: compiler.err.prob.found.req: (compiler.misc.invalid.generic.lambda.target: <X>()void, kindname.interface, LambdaConv25.C)
1+
LambdaConv25.java:19:15: compiler.err.prob.found.req: (compiler.misc.invalid.generic.lambda.target: ()void, kindname.interface, LambdaConv25.C)
22
1 error

test/langtools/tools/javac/warnings/6747671/T6747671.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ T6747671.java:29:28: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
77
T6747671.java:32:9: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
88
T6747671.java:32:20: compiler.warn.raw.class.use: T6747671.A, T6747671<E>.A<X>
99
T6747671.java:33:16: compiler.warn.raw.class.use: T6747671.A.Z, T6747671<E>.A<X>.Z<Y>
10-
T6747671.java:36:9: compiler.warn.raw.class.use: @T6747671.TA T6747671.B, T6747671.B<X>
10+
T6747671.java:36:9: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
1111
T6747671.java:36:27: compiler.warn.raw.class.use: T6747671.B, T6747671.B<X>
1212
11 warnings

0 commit comments

Comments
 (0)