Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3434,9 +3434,17 @@ PatternResult analyzePattern(int lookahead) {
case GT:
typeDepth--;
if (typeDepth == 0 && !peekToken(lookahead, DOT)) {
return peekToken(lookahead, LAX_IDENTIFIER) ||
peekToken(lookahead, tk -> tk == LPAREN) ? PatternResult.PATTERN
: PatternResult.EXPRESSION;
if(peekToken(lookahead, LAX_IDENTIFIER) ||
peekToken(lookahead, tk -> tk == LPAREN)) {
return PatternResult.PATTERN;
}
else if (peekToken(lookahead, LBRACKET) ||
peekToken(lookahead, MONKEYS_AT)) {
break;
}
else {
return PatternResult.EXPRESSION;
}
} else if (typeDepth < 0) return PatternResult.EXPRESSION;
break;
case MONKEYS_AT:
Expand Down
22 changes: 21 additions & 1 deletion test/langtools/tools/javac/patterns/DisambiguatePatterns.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2026, 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
Expand Down Expand Up @@ -113,6 +113,26 @@ public static void main(String... args) throws Throwable {
ExpressionType.PATTERN);
test.disambiguationTest("R(int x) when (x > 0)",
ExpressionType.PATTERN);
test.disambiguationTest("java.util.List<?>[] p",
ExpressionType.PATTERN);
test.disambiguationTest("java.util.List<?>[][] p",
ExpressionType.PATTERN);
test.disambiguationTest("a<b<c>>[] d",
ExpressionType.PATTERN);
test.disambiguationTest("java.util.List<?>[] p when true",
ExpressionType.PATTERN);
test.disambiguationTest("java.util.List<?> @Ann [] p",
ExpressionType.PATTERN);
test.disambiguationTest("a<b<c>> @Ann [] d",
ExpressionType.PATTERN);
test.disambiguationTest("(java.util.List<?>[]) o",
ExpressionType.EXPRESSION);
test.disambiguationTest("String[].class",
ExpressionType.EXPRESSION);
test.disambiguationTest("new int[1][]",
ExpressionType.EXPRESSION);
test.disambiguationTest("new java.util.List<?>[1][]",
ExpressionType.EXPRESSION);
}

private final ParserFactory factory;
Expand Down