Skip to content

Commit 93214fb

Browse files
authored
Propagate exactness when canonicalizing types (#7519)
The part of type canonicalization that replaces referenced heap types from previous recursion groups with their canonical versions was accidentally dropping exactness from the updated types. Fix it.
1 parent 38e0f4e commit 93214fb

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/wasm/wasm-type.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2518,7 +2518,7 @@ void updateReferencedHeapTypes(
25182518
if (type->isRef()) {
25192519
auto ht = type->getHeapType();
25202520
if (auto it = canonicalized.find(ht); it != canonicalized.end()) {
2521-
*type = Type(it->second, type->getNullability());
2521+
*type = type->with(it->second);
25222522
}
25232523
} else if (type->isTuple()) {
25242524
TypeGraphWalkerBase<ChildUpdater>::scanType(type);

test/gtest/type-builder.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ TEST_F(TypeTest, CanonicalizeUses) {
429429
}
430430

431431
TEST_F(TypeTest, CanonicalizeExactRefs) {
432-
TypeBuilder builder(4);
432+
TypeBuilder builder(10);
433433

434434
// Types that vary in exactness or nullability of references are different.
435435
Type a = builder.getTempRefType(builder[0], Nullable, Inexact);
@@ -442,10 +442,38 @@ TEST_F(TypeTest, CanonicalizeExactRefs) {
442442
builder[2] = Struct({Field(c, Mutable)});
443443
builder[3] = Struct({Field(d, Mutable)});
444444

445+
auto translate = [&](HeapType t) -> HeapType {
446+
for (int i = 0; i < 4; ++i) {
447+
if (t == builder[i]) {
448+
return builder[4 + i];
449+
}
450+
}
451+
WASM_UNREACHABLE("unexpected type");
452+
};
453+
454+
builder[4].copy(builder[0], translate);
455+
builder[5].copy(builder[1], translate);
456+
builder[6].copy(builder[2], translate);
457+
builder[7].copy(builder[3], translate);
458+
459+
// Test with references to previous types as well.
460+
Type ref0 = builder.getTempRefType(builder[0], Nullable, Exact);
461+
Type ref4 = builder.getTempRefType(builder[4], Nullable, Exact);
462+
463+
builder[8] = Struct({Field(ref0, Mutable)});
464+
builder[9] = Struct({Field(ref4, Mutable)});
465+
445466
auto result = builder.build();
446467
ASSERT_TRUE(result);
447468
auto built = *result;
448469

470+
EXPECT_EQ(built[0], built[4]);
471+
EXPECT_EQ(built[1], built[5]);
472+
EXPECT_EQ(built[2], built[6]);
473+
EXPECT_EQ(built[3], built[7]);
474+
475+
EXPECT_EQ(built[8], built[9]);
476+
449477
EXPECT_NE(built[0], built[1]);
450478
EXPECT_NE(built[0], built[2]);
451479
EXPECT_NE(built[0], built[3]);

0 commit comments

Comments
 (0)