RFR: 8268850: AST model for 'var' variables should more closely adhere to the source code

Vicente Romero vromero at openjdk.org
Thu Feb 19 16:16:43 UTC 2026


On Thu, 22 Jan 2026 17:16:06 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> Currently, if a local variable is declared using `var`, or if a lambda parameter has an inferred (missing) type, the AST model is a bit weird. The variable starts with `null` type, and the type is then replaced with a synthetic AST representing the real type. This (draft) PR is attempting to clean this up a little:
> - variables declared with `var` have `VarTypeTree` as the type. This is a new AST node that represents `var`. Positions are expected to be exact w.r.t. what is in the source code.
> - lambda parameters with inferred type would have `null` as the type. This is consistent with majority of the places in the AST where something is not part of the source code
> - for explicit parameters, the type is the real type, as before
> 
> This is not a compatible change, but I tried to partially write two clients, and it feels it wasn't too bad, and (hopefully) the new state is cleaner/more consistent.
> 
> There are some consistency comments:
> 
> - when the inferred types are inaccessible, javac (currently) sometimes produces a compile-time error, and sometimes does not; after reading the JLS, the error is indeed mandated in some cases, and not in others. `test/langtools/tools/javac/lvti/VarAccessibility.java` shows the cases
> - when the inferred types have potential for various types of warnings, javac (currently) sometimes produces those warnings, and sometimes not. The decision here is mostly implementation driven. I looked at the possibility to always produce the warnings, but there's https://bugs.openjdk.org/browse/JDK-8013222, from which it seems the original intent was to not produce the warnings. So, this PR currently will not produce warnings on the inferred types.
> 
> I was considering adding something similar to get JCVarDecl.DeclKind, but (based on the experience from the clients), it didn't feel strongly needed, as the type is determinable from `getType()`.
> 
> Any insights?
> 
> CC @cushon
> 
> Please also review:
> - CSR: https://bugs.openjdk.org/browse/JDK-8376736
> - release note: https://bugs.openjdk.org/browse/JDK-8377951

looks sensible to me

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

Marked as reviewed by vromero (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/29369#pullrequestreview-3826910025


More information about the compiler-dev mailing list