Skip to content

Commit 612b689

Browse files
committed
8043226: Better diagnostics for non-applicable type annotations
Reviewed-by: vromero
1 parent dce9703 commit 612b689

File tree

17 files changed

+241
-255
lines changed

17 files changed

+241
-255
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,10 @@ private String printAnnotations(Type t, boolean prefix) {
199199
List<Attribute.TypeCompound> annos = t.getAnnotationMirrors();
200200
if (!annos.isEmpty()) {
201201
if (prefix) sb.append(' ');
202-
sb.append(annos);
203-
sb.append(' ');
202+
for (Attribute.TypeCompound anno : annos) {
203+
sb.append(anno);
204+
sb.append(' ');
205+
}
204206
}
205207
return sb.toString();
206208
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.sun.tools.javac.comp.AttrContext;
5656
import com.sun.tools.javac.comp.Env;
5757
import com.sun.tools.javac.resources.CompilerProperties.Errors;
58+
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
5859
import com.sun.tools.javac.tree.JCTree;
5960
import com.sun.tools.javac.tree.JCTree.JCAnnotatedType;
6061
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
@@ -79,6 +80,7 @@
7980
import com.sun.tools.javac.tree.TreeScanner;
8081
import com.sun.tools.javac.util.Assert;
8182
import com.sun.tools.javac.util.Context;
83+
import com.sun.tools.javac.util.JCDiagnostic;
8284
import com.sun.tools.javac.util.List;
8385
import com.sun.tools.javac.util.ListBuffer;
8486
import com.sun.tools.javac.util.Log;
@@ -495,22 +497,20 @@ private Type typeWithAnnotations(final JCTree typetree, final Type type,
495497
*/
496498
if (enclTy != null &&
497499
enclTy.hasTag(TypeTag.NONE)) {
498-
switch (onlyTypeAnnotations.size()) {
499-
case 0:
500-
// Don't issue an error if all type annotations are
501-
// also declaration annotations.
502-
// If the annotations are also declaration annotations, they are
503-
// illegal as type annotations but might be legal as declaration annotations.
504-
// The normal declaration annotation checks make sure that the use is valid.
505-
break;
506-
case 1:
507-
log.error(typetree.pos(),
508-
Errors.CantTypeAnnotateScoping1(onlyTypeAnnotations.head));
509-
break;
510-
default:
511-
log.error(typetree.pos(),
512-
Errors.CantTypeAnnotateScoping(onlyTypeAnnotations));
500+
if (onlyTypeAnnotations.isEmpty()) {
501+
// Don't issue an error if all type annotations are
502+
// also declaration annotations.
503+
// If the annotations are also declaration annotations, they are
504+
// illegal as type annotations but might be legal as declaration annotations.
505+
// The normal declaration annotation checks make sure that the use is valid.
506+
return type;
513507
}
508+
Type annotated = typeWithAnnotations(type.stripMetadata(), enclTy, annotations);
509+
JCDiagnostic.Fragment annotationFragment = onlyTypeAnnotations.size() == 1 ?
510+
Fragments.TypeAnnotation1(onlyTypeAnnotations.head) :
511+
Fragments.TypeAnnotation(onlyTypeAnnotations);
512+
log.error(typetree.pos(), Errors.TypeAnnotationInadmissible(
513+
annotationFragment, annotated.tsym.owner, annotated));
514514
return type;
515515
}
516516

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5241,7 +5241,14 @@ public void visitModifiers(JCModifiers tree) {
52415241

52425242
public void visitAnnotatedType(JCAnnotatedType tree) {
52435243
attribAnnotationTypes(tree.annotations, env);
5244-
Type underlyingType = attribType(tree.underlyingType, env);
5244+
Type underlyingType =
5245+
attribTree(tree.underlyingType, env, new ResultInfo(KindSelector.TYP_PCK, Type.noType));
5246+
if (underlyingType.hasTag(PACKAGE)) {
5247+
// Type annotations are not admissible on packages, but we handle packages here to
5248+
// report better diagnostics later in validateAnnotatedType.
5249+
result = tree.type = underlyingType;
5250+
return;
5251+
}
52455252
Type annotatedType = underlyingType.preannotatedType();
52465253

52475254
if (!env.info.isNewClass)
@@ -5850,14 +5857,19 @@ private void validateAnnotatedType(final JCTree errtree, final Type type) {
58505857
} else if (enclTr.hasTag(ANNOTATED_TYPE)) {
58515858
JCAnnotatedType at = (JCTree.JCAnnotatedType) enclTr;
58525859
if (enclTy == null || enclTy.hasTag(NONE)) {
5853-
if (at.getAnnotations().size() == 1) {
5854-
log.error(at.underlyingType.pos(), Errors.CantTypeAnnotateScoping1(at.getAnnotations().head.attribute));
5855-
} else {
5856-
ListBuffer<Attribute.Compound> comps = new ListBuffer<>();
5857-
for (JCAnnotation an : at.getAnnotations()) {
5858-
comps.add(an.attribute);
5860+
ListBuffer<Attribute.TypeCompound> onlyTypeAnnotationsBuf = new ListBuffer<>();
5861+
for (JCAnnotation an : at.getAnnotations()) {
5862+
if (chk.isTypeAnnotation(an, false)) {
5863+
onlyTypeAnnotationsBuf.add((Attribute.TypeCompound) an.attribute);
58595864
}
5860-
log.error(at.underlyingType.pos(), Errors.CantTypeAnnotateScoping(comps.toList()));
5865+
}
5866+
List<Attribute.TypeCompound> onlyTypeAnnotations = onlyTypeAnnotationsBuf.toList();
5867+
if (!onlyTypeAnnotations.isEmpty()) {
5868+
Fragment annotationFragment = onlyTypeAnnotations.size() == 1 ?
5869+
Fragments.TypeAnnotation1(onlyTypeAnnotations.head) :
5870+
Fragments.TypeAnnotation(onlyTypeAnnotations);
5871+
log.error(at.underlyingType.pos(), Errors.TypeAnnotationInadmissible(annotationFragment,
5872+
type.tsym.owner, type.stripMetadata().annotatedType(onlyTypeAnnotations)));
58615873
}
58625874
repeat = false;
58635875
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,15 +3258,18 @@ 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-
# TODO 308: make a better error message
3261+
# 0: fragment, 1: symbol, 2: type
3262+
compiler.err.type.annotation.inadmissible=\
3263+
{0} not expected here\n\
3264+
(to annotate a qualified type, write {1}.{2})
3265+
32623266
# 0: annotation
3263-
compiler.err.cant.type.annotate.scoping.1=\
3264-
scoping construct cannot be annotated with type-use annotation: {0}
3267+
compiler.misc.type.annotation.1=\
3268+
type annotation {0} is
32653269

3266-
# TODO 308: make a better error message
32673270
# 0: list of annotation
3268-
compiler.err.cant.type.annotate.scoping=\
3269-
scoping construct cannot be annotated with type-use annotations: {0}
3271+
compiler.misc.type.annotation=\
3272+
type annotations {0} are
32703273

32713274
# 0: type, 1: type
32723275
compiler.err.incorrect.receiver.name=\

test/langtools/ProblemList.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ jdk/jshell/HighlightUITest.java
5757
#
5858
# javac
5959

60-
tools/javac/annotations/typeAnnotations/failures/CantAnnotatePackages.java 8057679 generic-all clarify error messages trying to annotate scoping
61-
tools/javac/annotations/typeAnnotations/failures/CantAnnotateScoping.java 8057679 generic-all clarify error messages trying to annotate scoping
62-
tools/javac/annotations/typeAnnotations/failures/CantAnnotateStaticClass2.java 8057679,8057683 generic-all clarify error messages and improve ordering of errors with type annotations
63-
tools/javac/annotations/typeAnnotations/failures/CantAnnotateStaticClass3.java 8057679,8057683 generic-all clarify error messages and improve ordering of errors with type annotations
64-
tools/javac/annotations/typeAnnotations/newlocations/RepeatingTypeAnnotations.java 8057683 generic-all improve ordering of errors with type annotations
6560
tools/javac/annotations/typeAnnotations/referenceinfos/Lambda.java 8057687 generic-all emit correct byte code an attributes for type annotations
6661
tools/javac/annotations/typeAnnotations/referenceinfos/NestedTypes.java 8057687 generic-all emit correct byte code an attributes for type annotations
6762
tools/javac/warnings/suppress/TypeAnnotations.java 8057683 generic-all improve ordering of errors with type annotations

test/langtools/tools/javac/annotations/typeAnnotations/failures/CantAnnotatePackages.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8026564
3+
* @bug 8026564 8043226
44
* @summary The parts of a fully-qualified type can't be annotated.
55
* @author Werner Dietl
6-
* @ignore 8057679 clarify error messages trying to annotate scoping
76
* @compile/fail/ref=CantAnnotatePackages.out -XDrawDiagnostics CantAnnotatePackages.java
87
*/
98

@@ -14,14 +13,9 @@ class CantAnnotatePackages {
1413
// Before a package component:
1514
@TA java.lang.Object of1;
1615

17-
// These result in a different error.
18-
// TODO: should this be unified?
19-
2016
List<@TA java.lang.Object> of2;
2117
java. @TA lang.Object of3;
2218
List<java. @TA lang.Object> of4;
23-
24-
// TODO: also note the order of error messages.
2519
}
2620

2721
@Target(ElementType.TYPE_USE)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
CantAnnotatePackages.java:14:13: compiler.err.cant.type.annotate.scoping.1: @TA
2-
CantAnnotatePackages.java:19:18: compiler.err.cant.type.annotate.scoping.1: @TA
3-
CantAnnotatePackages.java:20:19: compiler.err.cant.type.annotate.scoping.1: @TA
4-
CantAnnotatePackages.java:21:24: compiler.err.cant.type.annotate.scoping.1: @TA
1+
CantAnnotatePackages.java:14:18: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation.1: @TA), java.lang, @TA java.lang.Object
2+
CantAnnotatePackages.java:16:14: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation.1: @TA), java.lang, @TA java.lang.Object
3+
CantAnnotatePackages.java:17:9: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation.1: @TA), java.lang, @TA java.lang.Object
4+
CantAnnotatePackages.java:18:14: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation.1: @TA), java.lang, @TA java.lang.Object
55
4 errors

test/langtools/tools/javac/annotations/typeAnnotations/failures/CantAnnotateScoping.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8006733 8006775
3+
* @bug 8006733 8006775 8043226
44
* @summary Ensure behavior for nested types is correct.
55
* @author Werner Dietl
6-
* @ignore 8057679 clarify error messages trying to annotate scoping
76
* @compile/fail/ref=CantAnnotateScoping.out -XDrawDiagnostics CantAnnotateScoping.java
87
*/
98

@@ -35,16 +34,20 @@ static class SInner {}
3534
// Legal
3635
List<Outer. @TA SInner> li;
3736

38-
// Illegal
37+
// Illegal: inadmissible location for type-use annotations: @TA
3938
@TA Outer.SInner osi;
40-
// Illegal
39+
// Illegal: inadmissible location for type-use annotations: @TA
4140
List<@TA Outer.SInner> aloi;
4241
// Illegal
42+
// 1: inadmissible location for type-use annotations: @TA,@TA2
43+
// 2: annotation @DA not applicable in this type context
4344
Object o1 = new @TA @DA @TA2 Outer.SInner();
4445
// Illegal
46+
// 1: inadmissible location for type-use annotations: @TA
47+
// 2: annotation @DA not applicable in this type context
4548
Object o = new ArrayList<@TA @DA Outer.SInner>();
4649

47-
// Illegal: @TA is only a type-use annotation
50+
// Illegal: inadmissible location for type-use annotations: @TA
4851
@TA java.lang.Object f1;
4952

5053
// Legal: @DA is only a declaration annotation
@@ -53,20 +56,17 @@ static class SInner {}
5356
// Legal: @DTA is both a type-use and declaration annotation
5457
@DTA java.lang.Object f3;
5558

56-
// Illegal: @TA and @TA2 are only type-use annotations
59+
// Illegal: inadmissible location for type-use annotations: @TA,@TA2
5760
@DTA @DA @TA @DA2 @TA2 java.lang.Object f4;
5861

59-
// Illegal: Do we want one or two messages?
60-
// 1: @DA in invalid location
61-
// 2: Not finding class "lang"
62+
// Illegal: annotation @DA not applicable in this type context
6263
java. @DA lang.Object f5;
6364

64-
// Illegal: Do we want one or two messages?
65-
// 1: @DA in invalid location
66-
// 2: Not finding class "XXX"
65+
// Illegal: two messages:
66+
// 1: package java.XXX does not exist
67+
// 2: annotation @DA not applicable in this type context
6768
java. @DA XXX.Object f6;
6869

69-
// Illegal: Can't find class "lang".
70-
// Would a different error message be desirable?
70+
// Illegal: inadmissible location for type-use annotations: @TA
7171
java. @TA lang.Object f7;
7272
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
CantAnnotateScoping.java:66:18: compiler.err.doesnt.exist: java.XXX
2-
CantAnnotateScoping.java:38:14: compiler.err.cant.type.annotate.scoping.1: @TA
3-
CantAnnotateScoping.java:40:19: compiler.err.cant.type.annotate.scoping.1: @TA
4-
CantAnnotateScoping.java:47:13: compiler.err.cant.type.annotate.scoping.1: @TA
5-
CantAnnotateScoping.java:56:32: compiler.err.cant.type.annotate.scoping: @TA,@TA2
6-
CantAnnotateScoping.java:61:19: compiler.err.cant.type.annotate.scoping.1: @DA
7-
CantAnnotateScoping.java:70:19: compiler.err.cant.type.annotate.scoping.1: @TA
8-
CantAnnotateScoping.java:61:11: compiler.err.annotation.type.not.applicable
9-
CantAnnotateScoping.java:66:11: compiler.err.annotation.type.not.applicable
10-
CantAnnotateScoping.java:42:39: compiler.err.cant.type.annotate.scoping: @TA,@DA,@TA2
11-
CantAnnotateScoping.java:42:25: compiler.err.annotation.type.not.applicable
12-
CantAnnotateScoping.java:44:43: compiler.err.cant.type.annotate.scoping: @TA,@DA
13-
CantAnnotateScoping.java:44:34: compiler.err.annotation.type.not.applicable
14-
13 errors
1+
CantAnnotateScoping.java:68:18: compiler.err.doesnt.exist: java.XXX
2+
CantAnnotateScoping.java:38:14: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation.1: @TA), Test.Outer, @TA Test.Outer.SInner
3+
CantAnnotateScoping.java:51:18: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation.1: @TA), java.lang, @TA java.lang.Object
4+
CantAnnotateScoping.java:60:37: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation: @TA,@TA2), java.lang, @DTA @TA @TA2 java.lang.Object
5+
CantAnnotateScoping.java:40:14: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation.1: @TA), Test.Outer, @TA Test.Outer.SInner
6+
CantAnnotateScoping.java:63:11: compiler.err.annotation.type.not.applicable.to.type: DA
7+
CantAnnotateScoping.java:68:11: compiler.err.annotation.type.not.applicable.to.type: DA
8+
CantAnnotateScoping.java:71:9: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation.1: @TA), java.lang, @TA java.lang.Object
9+
CantAnnotateScoping.java:44:34: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation: @TA,@TA2), Test.Outer, @TA @TA2 Test.Outer.SInner
10+
CantAnnotateScoping.java:44:25: compiler.err.annotation.type.not.applicable.to.type: DA
11+
CantAnnotateScoping.java:48:38: compiler.err.type.annotation.inadmissible: (compiler.misc.type.annotation.1: @TA), Test.Outer, @TA Test.Outer.SInner
12+
CantAnnotateScoping.java:48:34: compiler.err.annotation.type.not.applicable.to.type: DA
13+
12 errors

test/langtools/tools/javac/annotations/typeAnnotations/failures/CantAnnotateStaticClass2.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8006733 8006775
3+
* @bug 8006733 8006775 8043226
44
* @summary Ensure behavior for nested types is correct.
55
* @author Werner Dietl
6-
* @ignore 8057679 clarify error messages trying to annotate scoping
7-
* @ignore 8057683 improve ordering of errors with type annotations
86
* @compile/fail/ref=CantAnnotateStaticClass2.out -XDrawDiagnostics CantAnnotateStaticClass2.java
97
*/
108

0 commit comments

Comments
 (0)