8335766: Switch case with pattern matching and guard clause compiles inconsistently

Reviewed-by: abimpoudis
This commit is contained in:
Jan Lahoda 2024-07-10 09:55:56 +00:00
parent a44b60c8c1
commit 537d20afbf
2 changed files with 14 additions and 2 deletions

View File

@ -3436,7 +3436,15 @@ public class JavacParser implements Parser {
: PatternResult.PATTERN;
}
parenDepth++; break;
case RPAREN: parenDepth--; break;
case RPAREN:
parenDepth--;
if (parenDepth == 0 &&
typeDepth == 0 &&
peekToken(lookahead, TokenKind.IDENTIFIER) &&
S.token(lookahead + 1).name() == names.when) {
return PatternResult.PATTERN;
}
break;
case ARROW: return parenDepth > 0 ? PatternResult.EXPRESSION
: pendingResult;
case FINAL:

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -109,6 +109,10 @@ public class DisambiguatePatterns {
ExpressionType.EXPRESSION);
test.disambiguationTest("a & b",
ExpressionType.EXPRESSION);
test.disambiguationTest("R r when (x > 0)",
ExpressionType.PATTERN);
test.disambiguationTest("R(int x) when (x > 0)",
ExpressionType.PATTERN);
}
private final ParserFactory factory;