Skip to content

Commit 537d20a

Browse files
committed
8335766: Switch case with pattern matching and guard clause compiles inconsistently
Reviewed-by: abimpoudis
1 parent a44b60c commit 537d20a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3436,7 +3436,15 @@ PatternResult analyzePattern(int lookahead) {
34363436
: PatternResult.PATTERN;
34373437
}
34383438
parenDepth++; break;
3439-
case RPAREN: parenDepth--; break;
3439+
case RPAREN:
3440+
parenDepth--;
3441+
if (parenDepth == 0 &&
3442+
typeDepth == 0 &&
3443+
peekToken(lookahead, TokenKind.IDENTIFIER) &&
3444+
S.token(lookahead + 1).name() == names.when) {
3445+
return PatternResult.PATTERN;
3446+
}
3447+
break;
34403448
case ARROW: return parenDepth > 0 ? PatternResult.EXPRESSION
34413449
: pendingResult;
34423450
case FINAL:

test/langtools/tools/javac/patterns/DisambiguatePatterns.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -109,6 +109,10 @@ public static void main(String... args) throws Throwable {
109109
ExpressionType.EXPRESSION);
110110
test.disambiguationTest("a & b",
111111
ExpressionType.EXPRESSION);
112+
test.disambiguationTest("R r when (x > 0)",
113+
ExpressionType.PATTERN);
114+
test.disambiguationTest("R(int x) when (x > 0)",
115+
ExpressionType.PATTERN);
112116
}
113117

114118
private final ParserFactory factory;

0 commit comments

Comments
 (0)