Skip to content

Commit cf63f2e

Browse files
committed
8298184: Incorrect record component type in record patterns
Reviewed-by: vromero
1 parent 58170f6 commit cf63f2e

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Diff for: src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -4204,13 +4204,15 @@ public void visitRecordPattern(JCRecordPattern tree) {
42044204
}
42054205
}
42064206
tree.type = tree.deconstructor.type = type;
4207-
Type site = types.removeWildcards(tree.type);
4207+
Type site = types.capture(tree.type);
42084208
List<Type> expectedRecordTypes;
42094209
if (site.tsym.kind == Kind.TYP && ((ClassSymbol) site.tsym).isRecord()) {
42104210
ClassSymbol record = (ClassSymbol) site.tsym;
42114211
expectedRecordTypes = record.getRecordComponents()
42124212
.stream()
4213-
.map(rc -> types.memberType(site, rc)).collect(List.collector());
4213+
.map(rc -> types.memberType(site, rc))
4214+
.map(t -> types.upward(t, types.captures(t)).baseType())
4215+
.collect(List.collector());
42144216
tree.record = record;
42154217
} else {
42164218
log.error(tree.pos(), Errors.DeconstructionPatternOnlyRecords(site.tsym));

Diff for: test/langtools/tools/javac/patterns/GenericRecordDeconstructionPattern.java

+10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323

2424
/**
2525
* @test
26+
* @bug 8298184
2627
* @enablePreview
28+
* @compile GenericRecordDeconstructionPattern.java
29+
* @run main GenericRecordDeconstructionPattern
2730
*/
2831
import java.util.List;
2932
import java.util.Objects;
@@ -46,6 +49,8 @@ void run() {
4649
testInference3();
4750
assertEquals(0, forEachInference(List.of(new Box(""))));
4851
assertEquals(1, forEachInference(List.of(new Box(null))));
52+
assertEquals(1, runIfSuperBound(new Box<>(new StringBuilder())));
53+
assertEquals(1, runIfSuperBound(new Box<>(0)));
4954
}
5055

5156
void runTest(Function<Box<String>, Integer> test) {
@@ -120,6 +125,11 @@ int runSwitchInferenceNested(I<I<String>> b) {
120125
}
121126
}
122127

128+
int runIfSuperBound(I<? super String> b) {
129+
if (b instanceof Box(var v)) return 1;
130+
return -1;
131+
}
132+
123133
sealed interface I<T> {}
124134
record Box<V>(V v) implements I<V> {
125135
}

0 commit comments

Comments
 (0)