RFR: 8302344: Compiler Implementation for Unnamed patterns and variables (Preview) [v12]

Maurizio Cimadamore mcimadamore at openjdk.org
Thu May 4 17:23:26 UTC 2023


On Thu, 4 May 2023 15:08:21 GMT, Aggelos Biboudis <abimpoudis at openjdk.org> wrote:

>> This PR implements [JEP 443](https://openjdk.org/jeps/443), the preview feature for Unnamed Patterns and Variables in Java.
>> 
>> Draft Spec: https://cr.openjdk.org/~abimpoudis/unnamed/latest/
>
> Aggelos Biboudis has updated the pull request incrementally with one additional commit since the last revision:
> 
>   8307444: java.lang.AssertionError when using unnamed patterns

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 4165:

> 4163:         tree.type = tree.var.type = type;
> 4164:         Name name = tree.var.name;
> 4165:         if (name == names.underscore) name = names.empty;

Why isn't the parser doing this?

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MemberEnter.java line 295:

> 293:                 : tree.vartype.type;
> 294:         Name name = tree.name;
> 295:         if (Feature.UNNAMED_VARIABLES.allowedInSource(source) && name == names.underscore) name = names.empty;

Same, I believe the parser should have set the right name on this

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java line 3591:

> 3589:                 }
> 3590:             }
> 3591:             result = toP(F.at(pos).VarDef(mods, name, type, init, declaredUsingVar));

I think this should check whether name is underscore (and feature is enabled) and, if that's the case, replace the name with an empty name, so that the rest of the compiler is already correct.

Also, related, no matter what, I think javac's Pretty class should be updated to render variables with empty name as "_" when the tree is turned back into source.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/13528#discussion_r1185294832
PR Review Comment: https://git.openjdk.org/jdk/pull/13528#discussion_r1185295283
PR Review Comment: https://git.openjdk.org/jdk/pull/13528#discussion_r1185299587


More information about the kulla-dev mailing list