Skip to content

Commit

Permalink
8332463: Byte conditional pattern case element dominates short consta…
Browse files Browse the repository at this point in the history
…nt case element

Reviewed-by: vromero
  • Loading branch information
biboudis committed May 22, 2024
1 parent d59c12f commit c4557a7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4818,7 +4818,6 @@ void checkSwitchCaseLabelDominated(JCCaseLabel unconditionalCaseLabel, List<JCCa
JCCaseLabel testCaseLabel = caseAndLabel.snd;
Type testType = labelType(testCaseLabel);
boolean dominated = false;
if (unconditionalCaseLabel == testCaseLabel) unconditionalFound = true;
if (types.isUnconditionallyExact(currentType, testType) &&
!currentType.hasTag(ERROR) && !testType.hasTag(ERROR)) {
//the current label is potentially dominated by the existing (test) label, check:
Expand All @@ -4833,11 +4832,6 @@ void checkSwitchCaseLabelDominated(JCCaseLabel unconditionalCaseLabel, List<JCCa
}
}

// Domination can occur even when we have not an unconditional pair between case labels.
if (unconditionalFound && unconditionalCaseLabel != label) {
dominated = true;
}

if (dominated) {
log.error(label.pos(), Errors.PatternDominated);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 8304487 8325653
* @bug 8304487 8325653 8332463
* @summary Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview)
* @enablePreview
* @compile/fail/ref=PrimitivePatternsSwitchErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW PrimitivePatternsSwitchErrors.java
Expand Down Expand Up @@ -59,7 +59,7 @@ public static int dominationBetweenBoxedAndPrimitive() {
int i = 42;
return switch (i) {
case Integer ib -> ib;
case byte ip -> ip; // Error - dominated!
case byte ip -> ip; // OK - not dominated!
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
PrimitivePatternsSwitchErrors.java:15:18: compiler.err.pattern.dominated
PrimitivePatternsSwitchErrors.java:24:18: compiler.err.pattern.dominated
PrimitivePatternsSwitchErrors.java:31:24: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.Long)
PrimitivePatternsSwitchErrors.java:62:18: compiler.err.pattern.dominated
PrimitivePatternsSwitchErrors.java:70:18: compiler.err.pattern.dominated
PrimitivePatternsSwitchErrors.java:78:18: compiler.err.pattern.dominated
PrimitivePatternsSwitchErrors.java:84:18: compiler.err.prob.found.req: (compiler.misc.possible.loss.of.precision: long, byte)
Expand Down Expand Up @@ -44,4 +43,4 @@ PrimitivePatternsSwitchErrors.java:254:16: compiler.err.not.exhaustive
PrimitivePatternsSwitchErrors.java:260:16: compiler.err.not.exhaustive
- compiler.note.preview.filename: PrimitivePatternsSwitchErrors.java, DEFAULT
- compiler.note.preview.recompile
44 errors
43 errors
54 changes: 54 additions & 0 deletions test/langtools/tools/javac/patterns/T8332463a.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2024, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8332463
* @summary Byte conditional pattern case element dominates short constant case element
* @compile --enable-preview --source ${jdk.version} T8332463a.java
*/
public class T8332463a {
public int test2() {
Byte i = (byte) 42;
return switch (i) {
case Byte ib -> 1;
case short s -> 2;
};
}

public int test4() {
int i = 42;
return switch (i) {
case Integer ib -> 1;
case byte ip -> 2;
};
}

public int test3() {
int i = 42;
return switch (i) {
case Integer ib -> 1;
case (byte) 0 -> 2;
};
}
}
40 changes: 40 additions & 0 deletions test/langtools/tools/javac/patterns/T8332463b.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8332463
* @summary Byte conditional pattern case element dominates short constant case element
* @enablePreview
* @compile T8332463b.java
* @compile --enable-preview --source ${jdk.version} T8332463b.java
*/
public class T8332463b {
public int test1() {
Byte i = (byte) 42;
return switch (i) {
case Byte ib -> 1;
case (short) 0 -> 2;
};
}
}

1 comment on commit c4557a7

@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.