diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java index 55eb421d262e7..00015976ca03a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java @@ -3501,7 +3501,7 @@ public PatternDescription makePatternDescription(Type selectorType, JCPattern pa } return new RecordPattern(record.type, componentTypes, nestedDescriptions); } else if (pattern instanceof JCAnyPattern) { - Type type = types.isSubtype(selectorType, syms.objectType) + Type type = types.isSubtype(selectorType, syms.objectType) || selectorType.isPrimitive() ? selectorType : syms.objectType; return new BindingPattern(type); } else { diff --git a/test/langtools/tools/javac/patterns/Unnamed.java b/test/langtools/tools/javac/patterns/Unnamed.java index b15902964a881..cbdb486e48c55 100644 --- a/test/langtools/tools/javac/patterns/Unnamed.java +++ b/test/langtools/tools/javac/patterns/Unnamed.java @@ -79,6 +79,8 @@ public void run() { assertEquals(2, testMixVarWithExplicit(new Box<>(new R2()))); assertEquals("binding", unnamedGuardAddsBindings("match1", "binding")); assertEquals("any", unnamedGuardAddsBindings(42, 42)); + assertEquals(true, testUnnamedPrimitiveAndExhaustiveness(new Prim1(4))); + assertEquals(false, testUnnamedPrimitiveAndExhaustiveness(new Prim2(5))); unnamedTest(); } @@ -270,6 +272,29 @@ String unnamedGuardAddsBindings(Object o1, Object o2) { }; } + boolean testUnnamedPrimitiveAndExhaustiveness(RecordWithPrimitive a) { + boolean r1 = switch (a) { + case Prim1(var _) -> true; + case Prim2(_) -> false; + }; + + boolean r2 = switch (a) { + case Prim1(var _) -> true; + case Prim2(var _) -> false; + }; + + boolean r3 = switch (a) { + case Prim1(_) -> true; + case Prim2(_) -> false; + }; + + return r1 && r2 && r3; + } + + sealed interface RecordWithPrimitive permits Prim1, Prim2 {}; + record Prim1(int n1) implements RecordWithPrimitive {}; + record Prim2(int n2) implements RecordWithPrimitive {}; + // JEP 443 examples record Point(int x, int y) { } enum Color { RED, GREEN, BLUE }