8332463: Byte conditional pattern case element dominates short constant case element

Reviewed-by: vromero
This commit is contained in:
Aggelos Biboudis 2024-05-22 19:48:23 +00:00
parent d59c12fe10
commit c4557a7b0d
5 changed files with 97 additions and 10 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/patterns

@ -4818,7 +4818,6 @@ public class Check {
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:
@ -4833,11 +4832,6 @@ public class Check {
}
}
// 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);
}

@ -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
@ -59,7 +59,7 @@ public class PrimitivePatternsSwitchErrors {
int i = 42;
return switch (i) {
case Integer ib -> ib;
case byte ip -> ip; // Error - dominated!
case byte ip -> ip; // OK - not dominated!
};
}

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

@ -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;
};
}
}

@ -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;
};
}
}