Skip to content

Commit

Permalink
8290709: Incorrect dominance error for unconditional pattern vs. null
Browse files Browse the repository at this point in the history
Reviewed-by: jlahoda
  • Loading branch information
biboudis authored and lahodaj committed Sep 1, 2022
1 parent 6a1b0b5 commit 9444a08
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1720,8 +1720,6 @@ private void handleSwitch(JCTree switchTree,
preview.checkSourceLevel(expr.pos(), Feature.CASE_NULL);
if (hasNullPattern) {
log.error(label.pos(), Errors.DuplicateCaseLabel);
} else if (wasUnconditionalPattern) {
log.error(label.pos(), Errors.PatternDominated);
}
hasNullPattern = true;
attribExpr(expr, switchEnv, seltype);
Expand Down
12 changes: 1 addition & 11 deletions test/langtools/tools/javac/patterns/CaseStructureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8269146
* @bug 8269146 8290709
* @summary Check compilation outcomes for various combinations of case label element.
* @library /tools/lib /tools/javac/lib
* @modules
Expand Down Expand Up @@ -120,16 +120,6 @@ protected void doWork() throws Throwable {
if (patternCases > 0 && defaultCases > 0) {
shouldPass &= false;
}
if (!asCaseLabelElements) {
//as an edge case, `case <total-pattern>: case null:` is prohibited:
boolean seenPattern = false;
for (CaseLabel label : caseLabels) {
switch (label) {
case NULL: if (seenPattern) shouldPass = false; break;
case PARENTHESIZED_PATTERN, TYPE_PATTERN: seenPattern = true; break;
}
}
}
if (!(shouldPass ^ result.hasErrors())) {
throw new AssertionError("Unexpected result: shouldPass=" + shouldPass + ", actual: " + !result.hasErrors() + ", info: " + result.compilationInfo());
}
Expand Down
9 changes: 8 additions & 1 deletion test/langtools/tools/javac/patterns/Domination.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8262891
* @bug 8262891 8290709
* @summary Check the pattern domination error are reported correctly.
* @compile/fail/ref=Domination.out -XDrawDiagnostics --enable-preview -source ${jdk.version} Domination.java
*/
Expand Down Expand Up @@ -211,4 +211,11 @@ record R(int a) {}
case R(int a): return -1;
}
}

int testNotDominates2(Integer x) {
switch (x) {
case Integer i: return i;
case null : return -1;
}
}
}
6 changes: 0 additions & 6 deletions test/langtools/tools/javac/patterns/SwitchErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,6 @@ void patternAndDefault4(Object o) {
case String s: break;
}
}
void nullAfterTotal(Object o) {
switch (o) {
case Object obj: break;
case null: break;
}
}
void sealedNonAbstract(SealedNonAbstract obj) {
switch (obj) {//does not cover SealedNonAbstract
case A a -> {}
Expand Down
38 changes: 18 additions & 20 deletions test/langtools/tools/javac/patterns/SwitchErrors.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ SwitchErrors.java:28:18: compiler.err.prob.found.req: (compiler.misc.inconvertib
SwitchErrors.java:29:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, int)
SwitchErrors.java:30:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.CharSequence)
SwitchErrors.java:36:13: compiler.err.unconditional.pattern.and.default
SwitchErrors.java:42:18: compiler.err.pattern.dominated
SwitchErrors.java:42:24: compiler.err.unconditional.pattern.and.default
SwitchErrors.java:48:18: compiler.err.unconditional.pattern.and.default
SwitchErrors.java:54:18: compiler.err.duplicate.unconditional.pattern
Expand All @@ -27,22 +26,21 @@ SwitchErrors.java:137:28: compiler.err.flows.through.from.pattern
SwitchErrors.java:143:18: compiler.err.flows.through.from.pattern
SwitchErrors.java:148:27: compiler.err.flows.through.to.pattern
SwitchErrors.java:154:18: compiler.err.flows.through.to.pattern
SwitchErrors.java:160:18: compiler.err.pattern.dominated
SwitchErrors.java:172:18: compiler.err.pattern.expected
SwitchErrors.java:178:78: compiler.err.cant.resolve.location: kindname.variable, n, , , (compiler.misc.location: kindname.class, SwitchErrors, null)
SwitchErrors.java:184:73: compiler.err.cant.resolve.location: kindname.variable, n, , , (compiler.misc.location: kindname.class, SwitchErrors, null)
SwitchErrors.java:191:21: compiler.err.flows.through.to.pattern
SwitchErrors.java:200:44: compiler.err.flows.through.from.pattern
SwitchErrors.java:218:29: compiler.err.unconditional.pattern.and.default
SwitchErrors.java:225:21: compiler.err.flows.through.to.pattern
SwitchErrors.java:225:47: compiler.err.flows.through.from.pattern
SwitchErrors.java:232:44: compiler.err.flows.through.from.pattern
SwitchErrors.java:232:47: compiler.err.flows.through.from.pattern
SwitchErrors.java:244:18: compiler.err.duplicate.unconditional.pattern
SwitchErrors.java:249:18: compiler.err.prob.found.req: (compiler.misc.not.applicable.types: int, java.lang.Integer)
SwitchErrors.java:254:18: compiler.err.type.found.req: int, (compiler.misc.type.req.class.array)
SwitchErrors.java:267:24: compiler.err.flows.through.to.pattern
SwitchErrors.java:281:37: compiler.err.flows.through.from.pattern
SwitchErrors.java:166:18: compiler.err.pattern.expected
SwitchErrors.java:172:78: compiler.err.cant.resolve.location: kindname.variable, n, , , (compiler.misc.location: kindname.class, SwitchErrors, null)
SwitchErrors.java:178:73: compiler.err.cant.resolve.location: kindname.variable, n, , , (compiler.misc.location: kindname.class, SwitchErrors, null)
SwitchErrors.java:185:21: compiler.err.flows.through.to.pattern
SwitchErrors.java:194:44: compiler.err.flows.through.from.pattern
SwitchErrors.java:212:29: compiler.err.unconditional.pattern.and.default
SwitchErrors.java:219:21: compiler.err.flows.through.to.pattern
SwitchErrors.java:219:47: compiler.err.flows.through.from.pattern
SwitchErrors.java:226:44: compiler.err.flows.through.from.pattern
SwitchErrors.java:226:47: compiler.err.flows.through.from.pattern
SwitchErrors.java:238:18: compiler.err.duplicate.unconditional.pattern
SwitchErrors.java:243:18: compiler.err.prob.found.req: (compiler.misc.not.applicable.types: int, java.lang.Integer)
SwitchErrors.java:248:18: compiler.err.type.found.req: int, (compiler.misc.type.req.class.array)
SwitchErrors.java:261:24: compiler.err.flows.through.to.pattern
SwitchErrors.java:275:37: compiler.err.flows.through.from.pattern
SwitchErrors.java:9:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:15:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:21:9: compiler.err.not.exhaustive.statement
Expand All @@ -53,8 +51,8 @@ SwitchErrors.java:86:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:91:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:97:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:104:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:164:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:237:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:158:9: compiler.err.not.exhaustive.statement
SwitchErrors.java:231:9: compiler.err.not.exhaustive.statement
- compiler.note.preview.filename: SwitchErrors.java, DEFAULT
- compiler.note.preview.recompile
57 errors
55 errors
20 changes: 19 additions & 1 deletion test/langtools/tools/javac/patterns/Switches.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

/*
* @test
* @bug 8262891 8268333 8268896 8269802 8269808 8270151 8269113 8277864
* @bug 8262891 8268333 8268896 8269802 8269808 8270151 8269113 8277864 8290709
* @summary Check behavior of pattern switches.
* @compile --enable-preview -source ${jdk.version} Switches.java
* @run main/othervm --enable-preview Switches
Expand Down Expand Up @@ -94,6 +94,10 @@ void run() {
assertEquals("a", deconstructExpression(new R("a")));
assertEquals("1", deconstructExpression(new R(1)));
assertEquals("other", deconstructExpression(""));
assertEquals("OK", totalPatternAndNull(Integer.valueOf(42)));
assertEquals("OK", totalPatternAndNull(null));
assertEquals("1", nullAfterTotal(Integer.valueOf(42)));
assertEquals("OK", nullAfterTotal(null));
}

void run(Function<Object, Integer> mapper) {
Expand Down Expand Up @@ -633,6 +637,20 @@ String deconstructExpression(Object o) {
};
}

String totalPatternAndNull(Integer in) {
return switch (in) {
case -1: { yield "";}
case Integer i: case null: { yield "OK";}
};
}

String nullAfterTotal(Object o) {
return switch (o) {
case Object obj: { yield "1";}
case null: { yield "OK";}
};
}

//verify that for cases like:
//case ConstantClassClash ->
//ConstantClassClash is interpreted as a field, not as a class
Expand Down

1 comment on commit 9444a08

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.