RFR: 8371155: Type annotations on local variables are classified after the local var initializer has been type checked
Vicente Romero
vromero at openjdk.org
Thu Feb 26 19:08:51 UTC 2026
On Thu, 19 Feb 2026 13:34:54 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:
> Consider a type-annotated field like:
>
> @TypeAnno Supplier<String> v = () -> "";
>
>
> For this field, the type for both the VariableTree and the lambda is `java.util.function. at TypeAnno Supplier<String>`. The type can be accessed e.g. using `Trees.getTypeMirror`.
>
> For local variables, however:
>
> {
> @TypeAnno Supplier<String> v = () -> "";
> }
>
>
> the type is merely `java.util.function.Supplier<String>`. This is because for fields, the annotations are processed before the main variable type is assigned and before the initializer type is inferred. For local variable, the annotations are processed when the whole class is attributed, and at that point the main variable type is assigned, and the initializer type is already derived, so neither of these reflects the annotation.
>
> The proposal here is to organize annotation for variable types earlier, immediately after they are entered. A limitation here is that at that point, some local-variable (sub-)kinds, like exception "parameters" and resource variables, are not yet separated out of the local variables, so `TypeAnnotations` cannot rely solely on `getKind()`, and must also look at other aspects.
lgtm
-------------
Marked as reviewed by vromero (Reviewer).
PR Review: https://git.openjdk.org/jdk/pull/29817#pullrequestreview-3863021718
More information about the compiler-dev
mailing list