Skip to content

Commit

Permalink
8318144: Match on enum constants with body compiles but fails with Ma…
Browse files Browse the repository at this point in the history
…tchException

Reviewed-by: liach, vromero
  • Loading branch information
lahodaj committed Nov 7, 2023
1 parent 42f43c5 commit 541ff71
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,13 @@ private static boolean enumEqCheck(Object value, EnumDesc<?> label, MethodHandle
Object resolved;

try {
if (!(value instanceof Enum<?> enumValue)) {
return false;
}

Class<?> clazz = label.constantType().resolveConstantDesc(lookup);

if (value.getClass() != clazz) {
if (enumValue.getDeclaringClass() != clazz) {
return false;
}

Expand Down
32 changes: 31 additions & 1 deletion test/jdk/java/lang/runtime/SwitchBootstrapsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

/**
* @test
* @bug 8318144
* @enablePreview
* @compile SwitchBootstrapsTest.java
* @run testng/othervm SwitchBootstrapsTest
Expand Down Expand Up @@ -70,7 +71,11 @@ private void testType(Object target, int start, int result, Object... labels) th
}

private void testEnum(Enum<?> target, int start, int result, Object... labels) throws Throwable {
MethodType switchType = MethodType.methodType(int.class, target.getClass(), int.class);
testEnum(target.getClass(), target, start, result, labels);
}

private void testEnum(Class<?> targetClass, Enum<?> target, int start, int result, Object... labels) throws Throwable {
MethodType switchType = MethodType.methodType(int.class, targetClass, int.class);
MethodHandle indy = ((CallSite) BSM_ENUM_SWITCH.invoke(MethodHandles.lookup(), "", switchType, labels)).dynamicInvoker();
assertEquals((int) indy.invoke(target, start), result);
assertEquals(-1, (int) indy.invoke(null, start));
Expand Down Expand Up @@ -138,6 +143,31 @@ public void testEnums() throws Throwable {
testEnum(E1.A, 0, 0);
}

public void testEnumsWithConstants() throws Throwable {
enum E {
A {},
B {},
C {}
}
ClassDesc eDesc = E.class.describeConstable().get();
Object[] typeParams = new Object[] {
EnumDesc.of(eDesc, "A"),
EnumDesc.of(eDesc, "B"),
EnumDesc.of(eDesc, "C"),
"a",
String.class
};
testType(E.A, 0, 0, typeParams);
testType(E.B, 0, 1, typeParams);
testType(E.C, 0, 2, typeParams);
testType("a", 0, 3, typeParams);
testType("x", 0, 4, typeParams);
testType('a', 0, 5, typeParams);
testEnum(E.class, E.A, 0, 0, "A", "B", "C");
testEnum(E.class, E.B, 0, 1, "A", "B", "C");
testEnum(E.class, E.C, 0, 2, "A", "B", "C");
}

public void testWrongSwitchTypes() throws Throwable {
MethodType[] switchTypes = new MethodType[] {
MethodType.methodType(int.class, Object.class),
Expand Down
86 changes: 86 additions & 0 deletions test/langtools/tools/javac/patterns/SwitchEnumConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2023, 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 8318144
* @summary Verify switches work in presence of enum constants that have bodies
* @compile SwitchEnumConstants.java
* @run main SwitchEnumConstants
*/

import java.util.function.ToIntFunction;

public class SwitchEnumConstants {

public static void main(String... args) throws Exception {
new SwitchEnumConstants().run();
}

void run() throws Exception {
doRun(this::typeSwitch);
doRun(this::enumSwitch);
}

void doRun(ToIntFunction<Object> c) throws Exception {
assertEquals(0, c.applyAsInt(E.A));
assertEquals(1, c.applyAsInt(E.B));
assertEquals(2, c.applyAsInt(E.C));
assertEquals(3, c.applyAsInt(""));
}

int typeSwitch(Object o) {
return switch (o) {
case E.A -> 0;
case E.B -> 1;
case E.C -> 2;
case String s -> 3;
default -> throw new IllegalStateException();
};
}

int enumSwitch(Object o) {
if (!(o instanceof E e)) {
return 3;
}
return switch (e) {
case A -> 0;
case B -> 1;
case C -> 2;
};
}


private static void assertEquals(int expected, int actual) {
if (expected != actual) {
throw new AssertionError("expected: " + expected +
", actual: " + actual);
}
}

enum E {
A {},
B {},
C {}
}
}

1 comment on commit 541ff71

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