RFR: 8371155: Type annotations on local variables are classified after the local var initializer has been type checked [v2]
Jan Lahoda
jlahoda at openjdk.org
Fri Feb 27 09:21:18 UTC 2026
> 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.
Jan Lahoda has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains four commits:
- Merge remote-tracking branch 'upstream-ssh/master' into JDK-8371155
- Cleanup.
- Fixing test.
- 8371155: Type annotations on local variables are classified after the local var initializer has been type checked
-------------
Changes: https://git.openjdk.org/jdk/pull/29817/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=29817&range=01
Stats: 286 lines in 3 files changed: 259 ins; 3 del; 24 mod
Patch: https://git.openjdk.org/jdk/pull/29817.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/29817/head:pull/29817
PR: https://git.openjdk.org/jdk/pull/29817
More information about the compiler-dev
mailing list