diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/RecordPattern.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/RecordPattern.java index 883202e4099..b824369cdfa 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/RecordPattern.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/RecordPattern.java @@ -244,6 +244,7 @@ original record instance as receiver - leaving the stack drained. labels.add(exceptionLabel); TypeBinding componentType = p.accessorMethod.returnType; + checkForPrimitiveType(currentScope, p, componentType); if (TypeBinding.notEquals(p.accessorMethod.original().returnType.erasure(), componentType.erasure())) codeStream.checkcast(componentType); // lastComponent ? [C] : [R, C] @@ -291,6 +292,18 @@ original record instance as receiver - leaving the stack drained. } } + private void checkForPrimitiveType(BlockScope currentScope, Pattern p, TypeBinding componentType) { + if (p.isTotalTypeNode && !componentType.isPrimitiveType() && p instanceof TypePattern tp) { + TypeBinding providedType = tp.resolvedType; + if (providedType != null && providedType.isPrimitiveType()) { + PrimitiveConversionRoute route = Pattern.findPrimitiveConversionRoute(componentType, providedType, currentScope); + if (route != PrimitiveConversionRoute.NO_CONVERSION_ROUTE) { + p.isTotalTypeNode = false; + } + } + } + } + @Override public void traverse(ASTVisitor visitor, BlockScope scope) { if (visitor.visit(this, scope)) { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java index 93c368e72d9..8670e433d6d 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java @@ -25,7 +25,7 @@ public class PrimitiveInPatternsTest extends AbstractRegressionTest9 { static { // TESTS_NUMBERS = new int [] { 1 }; // TESTS_RANGE = new int[] { 1, -1 }; -// TESTS_NAMES = new String[] { "testIssue2936" }; +// TESTS_NAMES = new String[] { "testIssuePrimitivesWithNull" }; } private String extraLibPath; public static Class testClass() { @@ -7331,6 +7331,24 @@ public static void main(String[] args) { "43.0"); } + public void testIssuePrimitivesWithNull() { + runConformTest(new String[] { + "X.java", + """ + public class X { + record R(Integer i) {} + public static int foo(R r) { + if (r instanceof R(int i)) { return i; } + return -1; + } + public static void main(String argv[]) { + System.out.println(foo(new R(null))); + } + } + """ + }, + "-1"); + } public void _testSpec00X() { runNegativeTest(new String[] { "X.java",