8311825: Duplicate qualified enum constants not detected
Reviewed-by: vromero
This commit is contained in:
parent
4676b40f17
commit
d1fa1a8686
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/switchextra
@ -1734,6 +1734,8 @@ public class Attr extends JCTree.Visitor {
|
||||
Symbol enumSym = TreeInfo.symbol(expr);
|
||||
if (enumSym == null || !enumSym.isEnum() || enumSym.kind != VAR) {
|
||||
log.error(expr.pos(), Errors.EnumLabelMustBeEnumConstant);
|
||||
} else if (!constants.add(enumSym)) {
|
||||
log.error(label.pos(), Errors.DuplicateCaseLabel);
|
||||
}
|
||||
} else {
|
||||
log.error(expr.pos(), Errors.EnumLabelMustBeUnqualifiedEnum);
|
||||
@ -1765,6 +1767,8 @@ public class Attr extends JCTree.Visitor {
|
||||
(stringSwitch ? Errors.StringConstReq
|
||||
: intSwitch ? Errors.ConstExprReq
|
||||
: Errors.PatternOrEnumReq));
|
||||
} else if (!constants.add(s)) {
|
||||
log.error(label.pos(), Errors.DuplicateCaseLabel);
|
||||
}
|
||||
} else if (!stringSwitch && !intSwitch) {
|
||||
log.error(label.pos(), Errors.ConstantLabelNotCompatible(pattype, seltype));
|
||||
|
@ -23,7 +23,7 @@
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @bug 8300543 8309336
|
||||
* @bug 8300543 8309336 8311825
|
||||
* @summary Check switches work correctly with qualified enum constants
|
||||
* @compile/fail/ref=EnumSwitchQualifiedErrors.out -XDrawDiagnostics EnumSwitchQualifiedErrors.java
|
||||
*/
|
||||
@ -69,6 +69,30 @@ public class EnumSwitchQualifiedErrors {
|
||||
};
|
||||
}
|
||||
|
||||
int testQualifiedDuplicate1(Object o) {
|
||||
return switch(o) {
|
||||
case E1.A -> 1;
|
||||
case E1.A -> 2;
|
||||
default -> -1;
|
||||
};
|
||||
}
|
||||
|
||||
int testQualifiedDuplicate2(E1 e) {
|
||||
return switch(e) {
|
||||
case A -> 1;
|
||||
case E1.A -> 2;
|
||||
default -> -1;
|
||||
};
|
||||
}
|
||||
|
||||
int testQualifiedDuplicate3(E1 e) {
|
||||
return switch(e) {
|
||||
case E1.A -> 1;
|
||||
case A -> 2;
|
||||
default -> -1;
|
||||
};
|
||||
}
|
||||
|
||||
sealed interface I {}
|
||||
enum E1 implements I { A; }
|
||||
enum E2 { A; }
|
||||
|
@ -8,4 +8,7 @@ EnumSwitchQualifiedErrors.java:58:18: compiler.err.enum.label.must.be.enum.const
|
||||
EnumSwitchQualifiedErrors.java:65:18: compiler.err.pattern.or.enum.req
|
||||
EnumSwitchQualifiedErrors.java:66:18: compiler.err.pattern.or.enum.req
|
||||
EnumSwitchQualifiedErrors.java:67:18: compiler.err.pattern.expected
|
||||
10 errors
|
||||
EnumSwitchQualifiedErrors.java:75:18: compiler.err.duplicate.case.label
|
||||
EnumSwitchQualifiedErrors.java:83:18: compiler.err.duplicate.case.label
|
||||
EnumSwitchQualifiedErrors.java:91:18: compiler.err.duplicate.case.label
|
||||
13 errors
|
||||
|
Loading…
x
Reference in New Issue
Block a user