Skip to content

Commit

Permalink
8298184: Incorrect record component type in record patterns
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Dec 7, 2022
1 parent 58170f6 commit cf63f2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Expand Up @@ -4204,13 +4204,15 @@ public void visitRecordPattern(JCRecordPattern tree) {
}
}
tree.type = tree.deconstructor.type = type;
Type site = types.removeWildcards(tree.type);
Type site = types.capture(tree.type);
List<Type> expectedRecordTypes;
if (site.tsym.kind == Kind.TYP && ((ClassSymbol) site.tsym).isRecord()) {
ClassSymbol record = (ClassSymbol) site.tsym;
expectedRecordTypes = record.getRecordComponents()
.stream()
.map(rc -> types.memberType(site, rc)).collect(List.collector());
.map(rc -> types.memberType(site, rc))
.map(t -> types.upward(t, types.captures(t)).baseType())
.collect(List.collector());
tree.record = record;
} else {
log.error(tree.pos(), Errors.DeconstructionPatternOnlyRecords(site.tsym));
Expand Down
Expand Up @@ -23,7 +23,10 @@

/**
* @test
* @bug 8298184
* @enablePreview
* @compile GenericRecordDeconstructionPattern.java
* @run main GenericRecordDeconstructionPattern
*/
import java.util.List;
import java.util.Objects;
Expand All @@ -46,6 +49,8 @@ void run() {
testInference3();
assertEquals(0, forEachInference(List.of(new Box(""))));
assertEquals(1, forEachInference(List.of(new Box(null))));
assertEquals(1, runIfSuperBound(new Box<>(new StringBuilder())));
assertEquals(1, runIfSuperBound(new Box<>(0)));
}

void runTest(Function<Box<String>, Integer> test) {
Expand Down Expand Up @@ -120,6 +125,11 @@ int runSwitchInferenceNested(I<I<String>> b) {
}
}

int runIfSuperBound(I<? super String> b) {
if (b instanceof Box(var v)) return 1;
return -1;
}

sealed interface I<T> {}
record Box<V>(V v) implements I<V> {
}
Expand Down

1 comment on commit cf63f2e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.