RFR: 8043226: Better diagnostics for non-applicable type annotations [v2]

Liam Miller-Cushon cushon at openjdk.org
Mon Jan 8 19:12:34 UTC 2024


> Hi,
> 
> Please consider this improvement to diagnostics on inadmissable type annotations.
> 
> [JDK-8057683: clarify error messages trying to annotate scoping](https://bugs.openjdk.org/browse/JDK-8057683) is closely related to JDK-8043226 and I think could be made a duplicate of that bug. [JDK-8057683: improve ordering of errors with type annotations](https://bugs.openjdk.org/browse/JDK-8057683) is also closely related, and this PR partially fixes the issues described in that bug.
> 
> I have some notes on details of the proposed changes below.
> 
> ---
> 
> Currently javac reports 'scoping construct cannot be annotated' diagnostics for type annotations at locations where they are not admissable. 
> 
> As discussed in [JDK-8043226](https://bugs.openjdk.org/browse/JDK-8043226) and [JDK-8057683](https://bugs.openjdk.org/browse/JDK-8057683), the current language is unclear. The 'scoping construct' language was used in JSR-308 discussions but didn't end up in the final specification, JLS §9.7.4 talks about where annotations are 'admissable', and that language is mirrored in JVMS §4.7.20.2.
> 
> This change updates the diagnostics to state that type annotations 'not admissible at this location' and removed the reference to 'scoping constructs'. Additionally, the diagnostic now includes an explanation of a location in the type where annotations would be admissible, to make it easier to understand how to annotate qualified type names.
> 
> Before:
> 
> 
> test/langtools/tools/javac/annotations/typeAnnotations/failures/CantAnnotateScoping.java:38: error: scoping construct cannot be annotated with type-use annotation: @TA
>     @TA Outer.SInner osi;
>              ^
> 
> 
> After:
> 
> 
> test/langtools/tools/javac/annotations/typeAnnotations/failures/CantAnnotateScoping.java:38: error: type annotations are not admissible at this location: @TA
>     @TA Outer.SInner osi;
>              ^
>   (to annotate a qualified type, write Outer. at TA SInner)
> 
> 
> ---
> 
> Attribution currently assumes `@TA java.lang.Object` in `List<@TA java.lang.Object>` will be a type (not a package), and reports a resolution failure when it can't find a class named `java`.
> 
> This change modifies attribution to search for packages as well as types when resolving annotated types, and relies on the subsequent error checking for annotated types to report that type annotations are not admissible at that location.
> 
> This also improves the ordering of the diagnostic in the output, since attribution errors are report before type annotation validation errors in the l...

Liam Miller-Cushon has updated the pull request incrementally with one additional commit since the last revision:

  Update src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
  
  Co-authored-by: Werner Dietl <wdietl at gmail.com>

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/16592/files
  - new: https://git.openjdk.org/jdk/pull/16592/files/55c15c9b..568eb387

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=16592&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=16592&range=00-01

  Stats: 1 line in 1 file changed: 0 ins; 0 del; 1 mod
  Patch: https://git.openjdk.org/jdk/pull/16592.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/16592/head:pull/16592

PR: https://git.openjdk.org/jdk/pull/16592


More information about the compiler-dev mailing list