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

Commit b6827ff

Browse files
committed
8311825: Duplicate qualified enum constants not detected
Reviewed-by: vromero Backport-of: d1fa1a868636dc15e96d1b4bf4acf28257c9551f
1 parent a08c6b9 commit b6827ff

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,8 @@ private void handleSwitch(JCTree switchTree,
17341734
Symbol enumSym = TreeInfo.symbol(expr);
17351735
if (enumSym == null || !enumSym.isEnum() || enumSym.kind != VAR) {
17361736
log.error(expr.pos(), Errors.EnumLabelMustBeEnumConstant);
1737+
} else if (!constants.add(enumSym)) {
1738+
log.error(label.pos(), Errors.DuplicateCaseLabel);
17371739
}
17381740
} else {
17391741
log.error(expr.pos(), Errors.EnumLabelMustBeUnqualifiedEnum);
@@ -1765,6 +1767,8 @@ private void handleSwitch(JCTree switchTree,
17651767
(stringSwitch ? Errors.StringConstReq
17661768
: intSwitch ? Errors.ConstExprReq
17671769
: Errors.PatternOrEnumReq));
1770+
} else if (!constants.add(s)) {
1771+
log.error(label.pos(), Errors.DuplicateCaseLabel);
17681772
}
17691773
} else if (!stringSwitch && !intSwitch) {
17701774
log.error(label.pos(), Errors.ConstantLabelNotCompatible(pattype, seltype));

test/langtools/tools/javac/switchextra/EnumSwitchQualifiedErrors.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/**
2525
* @test
26-
* @bug 8300543 8309336
26+
* @bug 8300543 8309336 8311825
2727
* @summary Check switches work correctly with qualified enum constants
2828
* @compile/fail/ref=EnumSwitchQualifiedErrors.out -XDrawDiagnostics EnumSwitchQualifiedErrors.java
2929
*/
@@ -69,6 +69,30 @@ int testPatternMatchingSwitch5(Object e) {
6969
};
7070
}
7171

72+
int testQualifiedDuplicate1(Object o) {
73+
return switch(o) {
74+
case E1.A -> 1;
75+
case E1.A -> 2;
76+
default -> -1;
77+
};
78+
}
79+
80+
int testQualifiedDuplicate2(E1 e) {
81+
return switch(e) {
82+
case A -> 1;
83+
case E1.A -> 2;
84+
default -> -1;
85+
};
86+
}
87+
88+
int testQualifiedDuplicate3(E1 e) {
89+
return switch(e) {
90+
case E1.A -> 1;
91+
case A -> 2;
92+
default -> -1;
93+
};
94+
}
95+
7296
sealed interface I {}
7397
enum E1 implements I { A; }
7498
enum E2 { A; }

test/langtools/tools/javac/switchextra/EnumSwitchQualifiedErrors.out

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ EnumSwitchQualifiedErrors.java:58:18: compiler.err.enum.label.must.be.enum.const
88
EnumSwitchQualifiedErrors.java:65:18: compiler.err.pattern.or.enum.req
99
EnumSwitchQualifiedErrors.java:66:18: compiler.err.pattern.or.enum.req
1010
EnumSwitchQualifiedErrors.java:67:18: compiler.err.pattern.expected
11-
10 errors
11+
EnumSwitchQualifiedErrors.java:75:18: compiler.err.duplicate.case.label
12+
EnumSwitchQualifiedErrors.java:83:18: compiler.err.duplicate.case.label
13+
EnumSwitchQualifiedErrors.java:91:18: compiler.err.duplicate.case.label
14+
13 errors

0 commit comments

Comments
 (0)