JDK-8334055 : Unhelpful 'required: reference' diagnostics after JDK-8043226
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 23
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2024-06-12
  • Updated: 2024-06-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 24
24Unresolved
Related Reports
Relates :  
Description
JDK-8043226 improved 'scoping construct cannot be annotated' diagnostics to more clearly describe the error.

Given:

import java.util.*;
import java.lang.annotation.*;

@Target(ElementType.TYPE_USE) @interface TA {}

class T {
  @TA Map.Entry<?, ?> x;
  List<@TA NoSuch> y;
  List<@TA java.lang.Object> z;
}

The error on 'x' was updated from:

T.java:7: error: scoping construct cannot be annotated with type-use annotation: @TA
  @TA Map.Entry<?, ?> x;
               ^

to:

T.java:7: error: type annotation @TA is not expected here
  @TA Map.Entry<?, ?> x;
               ^
  (to annotate a qualified type, write Map.@TA Entry<?,?>)

Additionally, that change tried to improve diagnostics for type annotations package names, changing the diagnostic on 'z' from:

T.java:9: error: cannot find symbol
  List<@TA java.lang.Object> z;
           ^
  symbol:   class java
  location: class T

to:

T.java:9: error: type annotation @TA is not expected here
  List<@TA java.lang.Object> z;
           ^
  (to annotate a qualified type, write java.lang.@TA Object)

To support the second case, attribution of annotated types was updated to handle package names: https://github.com/openjdk/jdk/blob/612b6896d28cebf61ef024709ff3afb5e3ee0dde/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java#L5246-L5251

The change to support package names had unintended side effects for missing symbols, e.g. before:

T.java:8: error: cannot find symbol
  List<@TA NoSuch> y;
           ^
  symbol:   class NoSuch
  location: class T

after:

T.java:8: error: unexpected type
  List<@TA NoSuch> y;
       ^
  required: reference
  found:    NoSuch

The 'required: reference' / 'found:    NoSuch' details are confusing. They're happening because NoSuch is being resolved as a package that doesn't exist, and then the error for an unexpected package type is reported before type annotation validation happens.

I'd like to back out the change to the handling of package names for now.
Comments
A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/19667 Date: 2024-06-12 01:15:09 +0000
12-06-2024