Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk21 Public archive

Commit 83ea293

Browse files
committedJun 13, 2023
8309467: Pattern dominance should be adjusted
Reviewed-by: vromero Backport-of: 408cadb
1 parent b743405 commit 83ea293

File tree

4 files changed

+70
-18
lines changed

4 files changed

+70
-18
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4695,7 +4695,8 @@ void checkSwitchCaseLabelDominated(List<JCCase> cases) {
46954695
//the current label is potentially dominated by the existing (test) label, check:
46964696
boolean dominated = false;
46974697
if (label instanceof JCConstantCaseLabel) {
4698-
dominated |= !(testCaseLabel instanceof JCConstantCaseLabel);
4698+
dominated |= !(testCaseLabel instanceof JCConstantCaseLabel) &&
4699+
TreeInfo.unguardedCase(testCase);
46994700
} else if (label instanceof JCPatternCaseLabel patternCL &&
47004701
testCaseLabel instanceof JCPatternCaseLabel testPatternCaseLabel &&
47014702
TreeInfo.unguardedCase(testCase)) {

‎test/langtools/tools/javac/patterns/Domination.out

+1-7
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ Domination.java:35:18: compiler.err.pattern.dominated
22
Domination.java:43:18: compiler.err.pattern.dominated
33
Domination.java:51:18: compiler.err.pattern.dominated
44
Domination.java:67:18: compiler.err.pattern.dominated
5-
Domination.java:74:18: compiler.err.pattern.dominated
6-
Domination.java:81:18: compiler.err.pattern.dominated
75
Domination.java:88:18: compiler.err.pattern.dominated
8-
Domination.java:95:18: compiler.err.pattern.dominated
9-
Domination.java:102:18: compiler.err.pattern.dominated
106
Domination.java:113:18: compiler.err.pattern.dominated
11-
Domination.java:124:18: compiler.err.pattern.dominated
12-
Domination.java:135:18: compiler.err.pattern.dominated
137
Domination.java:144:18: compiler.err.pattern.dominated
148
Domination.java:153:18: compiler.err.pattern.dominated
159
Domination.java:184:18: compiler.err.pattern.dominated
1610
Domination.java:193:18: compiler.err.pattern.dominated
1711
Domination.java:202:18: compiler.err.pattern.dominated
1812
Domination.java:211:18: compiler.err.pattern.dominated
19-
18 errors
13+
12 errors

‎test/langtools/tools/javac/patterns/NewCaseStructureTest.java

+31-10
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ private int test(Integer o) {
269269
@Test
270270
public void testDominance(Path base) throws Exception {
271271
//A case label with a case pattern p (guarded or unguarded) dominates another case label with a case constant c if p dominates c, which is defined as follows:
272-
// A type pattern that declares a pattern variable of type T dominates a constant c of a primitive type P if the wrapper class of P ([5.1.7]) is a subtype of the erasure of T.
272+
// (unguarded case with) A type pattern that declares a pattern variable of type T dominates a constant c of a primitive type P if the wrapper class of P ([5.1.7]) is a subtype of the erasure of T.
273273
doTest(base,
274274
"""
275275
package test;
@@ -282,10 +282,22 @@ private int test(Integer o) {
282282
};
283283
}
284284
}
285+
""");
286+
doTest(base,
287+
"""
288+
package test;
289+
public class Test {
290+
private int test(Integer o) {
291+
return switch (o) {
292+
case Integer i -> 0;
293+
case 0 -> 0;
294+
};
295+
}
296+
}
285297
""",
286298
"Test.java:6:18: compiler.err.pattern.dominated",
287299
"1 error");
288-
// A type pattern that declares a pattern variable of type T dominates an enum constant c of type E if E is a subtype of the erasure of the type of T.
300+
// (unguarded case with) A type pattern that declares a pattern variable of type T dominates an enum constant c of type E if E is a subtype of the erasure of the type of T.
289301
doTest(base,
290302
"""
291303
package test;
@@ -299,6 +311,19 @@ private int test(E o) {
299311
}
300312
}
301313
enum E {A, B;}
314+
""");
315+
doTest(base,
316+
"""
317+
package test;
318+
public class Test {
319+
private int test(E o) {
320+
return switch (o) {
321+
case E e -> 0;
322+
case B -> 0;
323+
};
324+
}
325+
}
326+
enum E {A, B;}
302327
""",
303328
"Test.java:6:18: compiler.err.pattern.dominated",
304329
"1 error");
@@ -315,19 +340,15 @@ private int test(String o) {
315340
};
316341
}
317342
}
318-
""",
319-
"Test.java:6:18: compiler.err.pattern.dominated",
320-
"1 error");
321-
// A parenthesized pattern dominates a constant c if its contained pattern dominates c.
343+
""");
322344
doTest(base,
323345
"""
324346
package test;
325347
public class Test {
326-
private int test(Integer o) {
348+
private int test(String o) {
327349
return switch (o) {
328-
case Integer i when i > 0 -> 0;
329-
case 0 -> 0;
330-
case Integer i -> 0;
350+
case String s -> 0;
351+
case "a" -> 0;
331352
};
332353
}
333354
}

‎test/langtools/tools/javac/patterns/Switches.java

+36
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ void run() {
105105
emptyFallThrough(1.0);
106106
testSimpleSwitch();
107107
testSimpleSwitchExpression();
108+
assertEquals(0, constantAndPatternGuardInteger(0, true));
109+
assertEquals(0, constantAndPatternGuardInteger(1, true));
110+
assertEquals(1, constantAndPatternGuardInteger(1, false));
111+
assertEquals(2, constantAndPatternGuardInteger(0, false));
112+
assertEquals(0, constantAndPatternGuardString("", true));
113+
assertEquals(0, constantAndPatternGuardString("a", true));
114+
assertEquals(1, constantAndPatternGuardString("a", false));
115+
assertEquals(2, constantAndPatternGuardString("", false));
116+
assertEquals(0, constantAndPatternGuardEnum(E.A, true));
117+
assertEquals(0, constantAndPatternGuardEnum(E.B, true));
118+
assertEquals(1, constantAndPatternGuardEnum(E.B, false));
119+
assertEquals(2, constantAndPatternGuardEnum(E.A, false));
108120
}
109121

110122
void run(Function<Object, Integer> mapper) {
@@ -713,6 +725,30 @@ void testSimpleSwitchExpression() {
713725
assertEquals(1, res);
714726
}
715727

728+
int constantAndPatternGuardInteger(Integer i, boolean g) {
729+
return switch (i) {
730+
case Integer j when g -> 0;
731+
case 1 -> 1;
732+
case Integer j -> 2;
733+
};
734+
}
735+
736+
int constantAndPatternGuardString(String s, boolean g) {
737+
return switch (s) {
738+
case String t when g -> 0;
739+
case "a" -> 1;
740+
case String t -> 2;
741+
};
742+
}
743+
744+
int constantAndPatternGuardEnum(E e, boolean g) {
745+
return switch (e) {
746+
case E f when g -> 0;
747+
case E.B -> 1;
748+
case E f -> 2;
749+
};
750+
}
751+
716752
//verify that for cases like:
717753
//case ConstantClassClash ->
718754
//ConstantClassClash is interpreted as a field, not as a class

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jun 13, 2023

@openjdk-notifier[bot]
This repository has been archived.