RFR: 8294943: Implement record patterns in enhanced for [v15]

Maurizio Cimadamore mcimadamore at openjdk.org
Thu Nov 24 18:23:25 UTC 2022


On Thu, 24 Nov 2022 17:12:05 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java line 3016:
>> 
>>> 3014:                             return ForInitResult.LocalVarDecl;
>>> 3015:                         }
>>> 3016:                         else if(peekToken(lookahead, COLON)) {
>> 
>> Does this logic works with nested record patterns? You don't seem to "count" the parenthesis... I do see tests with it though...
>
> This is an example I found where classification goes wrong:
> 
> 
> for (m(a ? b() : c) ; ;) {}
> 
> This used to parse correctly, but now parsing fails because the classification sees ")" followed by a ":".
> 
> This a nasty one: we can't just bail out on "?" as that is useful for generic types. But once you accept "?" then it could be a conditional as well, and conditionals also feature a ":" which means here we have a problem.
> 
> I think "?" should only conditionally be accepted if we're inside generic brackets. But then again, you could still have `a < b ? c() : d` which is not a type.

Ok, after some offline discussion, I put together a patch which addresses these (and other) issues:

https://github.com/biboudis/jdk/pull/3

The core idea is to detect whether a `?` is part of a generic type or not. See the above PR for more details.

Sadly, one of the tests I've added seems to crash the compiler, because a type annotation is found in an unexpected place. So that needs more work.

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

PR: https://git.openjdk.org/jdk/pull/10798


More information about the compiler-dev mailing list