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 febc1371256..e388e98087a 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 @@ -128,12 +128,13 @@ public TypeBinding resolveType(BlockScope scope) { } } + RecordComponentBinding[] componentBindings = this.resolvedType.components(); LocalVariableBinding [] bindings = NO_VARIABLES; for (int i = 0, l = this.patterns.length; i < l; ++i) { Pattern p = this.patterns[i]; + p.setOuterExpressionType(componentBindings[i].type); p.resolveTypeWithBindings(bindings, scope); bindings = LocalVariableBinding.merge(bindings, p.bindingsWhenTrue()); - p.setOuterExpressionType(this.resolvedType.components()[i].type); } if (this.resolvedType == null || !this.resolvedType.isValidBinding()) { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordPatternTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordPatternTest.java index 7eea16c45d2..f3f5a38adc0 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordPatternTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordPatternTest.java @@ -28,7 +28,7 @@ public class RecordPatternTest extends AbstractRegressionTest9 { static { // TESTS_NUMBERS = new int [] { 40 }; // TESTS_RANGE = new int[] { 1, -1 }; -// TESTS_NAMES = new String[] { "testRecPatExhaust018" }; +// TESTS_NAMES = new String[] { "testRecordTypeInfer_4643" }; } private String extraLibPath; public static Class testClass() { @@ -5046,4 +5046,48 @@ record Wildcard(Class bound) implements ComponentType> { """ }); } + public void testRecordTypeInfer_4643_001() { + runConformTest(new String[] { "X.java", """ + public class X { + + private static void foo() { + record Box(T t) {} + + Box> bo = new Box<>(new Box<>("str")); + if (bo instanceof Box(Box(var sString))) { + System.out.println(sString.length()); + } + } + + public static void main(String[] args) { + foo(); + } + } + """ }, "3"); + } + public void testRecordTypeInfer_4643_002() { + runNegativeTest(new String[] { + "X.java", + """ + public class X { + + private static void foo() { + record Box(T t) {} + Box> bo = new Box<>(new Box<>(1)); + if (bo instanceof Box(Box(String sString))) {} + } + public static void main(String[] args) { + foo(); + } + } + """ + }, + "----------\n" + + "1. ERROR in X.java (at line 6)\n" + + " if (bo instanceof Box(Box(String sString))) {}\n" + + " ^^^^^^^^^^^^^^\n" + + "Record component with type Integer is not compatible with type String\n" + + "----------\n"); + } + } \ No newline at end of file