Skip to content

Commit 9f5cce9

Browse files
committed
fixes the record type inference issue #4643
1 parent fff6740 commit 9f5cce9

File tree

2 files changed

+47
-2
lines changed
  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast
  • org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression

2 files changed

+47
-2
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/RecordPattern.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,13 @@ public TypeBinding resolveType(BlockScope scope) {
128128
}
129129
}
130130

131+
RecordComponentBinding[] componentBindings = this.resolvedType.components();
131132
LocalVariableBinding [] bindings = NO_VARIABLES;
132133
for (int i = 0, l = this.patterns.length; i < l; ++i) {
133134
Pattern p = this.patterns[i];
135+
p.setOuterExpressionType(componentBindings[i].type);
134136
p.resolveTypeWithBindings(bindings, scope);
135137
bindings = LocalVariableBinding.merge(bindings, p.bindingsWhenTrue());
136-
p.setOuterExpressionType(this.resolvedType.components()[i].type);
137138
}
138139

139140
if (this.resolvedType == null || !this.resolvedType.isValidBinding()) {

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordPatternTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class RecordPatternTest extends AbstractRegressionTest9 {
2828
static {
2929
// TESTS_NUMBERS = new int [] { 40 };
3030
// TESTS_RANGE = new int[] { 1, -1 };
31-
// TESTS_NAMES = new String[] { "testRecPatExhaust018" };
31+
// TESTS_NAMES = new String[] { "testRecordTypeInfer_4643" };
3232
}
3333
private String extraLibPath;
3434
public static Class<?> testClass() {
@@ -5046,4 +5046,48 @@ record Wildcard<T>(Class<T> bound) implements ComponentType<List<? extends T>> {
50465046
"""
50475047
});
50485048
}
5049+
public void testRecordTypeInfer_4643_001() {
5050+
runConformTest(new String[] { "X.java", """
5051+
public class X {
5052+
5053+
private static void foo() {
5054+
record Box<T>(T t) {}
5055+
5056+
Box<Box<String>> bo = new Box<>(new Box<>("str"));
5057+
if (bo instanceof Box(Box(var sString))) {
5058+
System.out.println(sString.length());
5059+
}
5060+
}
5061+
5062+
public static void main(String[] args) {
5063+
foo();
5064+
}
5065+
}
5066+
""" }, "3");
5067+
}
5068+
public void testRecordTypeInfer_4643_002() {
5069+
runNegativeTest(new String[] {
5070+
"X.java",
5071+
"""
5072+
public class X {
5073+
5074+
private static void foo() {
5075+
record Box<T>(T t) {}
5076+
Box<Box<Integer>> bo = new Box<>(new Box<>(1));
5077+
if (bo instanceof Box(Box(String sString))) {}
5078+
}
5079+
public static void main(String[] args) {
5080+
foo();
5081+
}
5082+
}
5083+
"""
5084+
},
5085+
"----------\n" +
5086+
"1. ERROR in X.java (at line 6)\n" +
5087+
" if (bo instanceof Box(Box(String sString))) {}\n" +
5088+
" ^^^^^^^^^^^^^^\n" +
5089+
"Record component with type Integer is not compatible with type String\n" +
5090+
"----------\n");
5091+
}
5092+
50495093
}

0 commit comments

Comments
 (0)