diff --git a/src/passes/GlobalTypeOptimization.cpp b/src/passes/GlobalTypeOptimization.cpp index 6316dc7e57e..c038a7a0a06 100644 --- a/src/passes/GlobalTypeOptimization.cpp +++ b/src/passes/GlobalTypeOptimization.cpp @@ -444,11 +444,13 @@ struct GlobalTypeOptimization : public Pass { // Clear the old names and write the new ones. nameInfo.fieldNames.clear(); - for (Index i = 0; i < oldFieldNames.size(); i++) { + for (Index i = 0; i < indexesAfterRemoval.size(); i++) { auto newIndex = indexesAfterRemoval[i]; - if (newIndex != RemovedField && oldFieldNames.count(i)) { - assert(oldFieldNames[i].is()); - nameInfo.fieldNames[newIndex] = oldFieldNames[i]; + if (newIndex != RemovedField) { + auto iter = oldFieldNames.find(i); + if (iter != oldFieldNames.end()) { + nameInfo.fieldNames[newIndex] = iter->second; + } } } } diff --git a/test/lit/passes/gto-removals.wast b/test/lit/passes/gto-removals.wast index 9dbb712bd28..814712bccc4 100644 --- a/test/lit/passes/gto-removals.wast +++ b/test/lit/passes/gto-removals.wast @@ -1675,3 +1675,45 @@ ) ) ) + +(module + ;; A struct with partial names: only some fields are named. We should still + ;; update names properly when removing and reordering. + + ;; CHECK: (rec + ;; CHECK-NEXT: (type $struct (sub (struct (field (mut i32)) (field $named (mut i32))))) + (type $struct (sub (struct (field (mut i32)) (field (mut i32)) (field $named (mut i32)) (field (mut i32)) (field (mut i32))))) + + ;; CHECK: (type $1 (func (param (ref $struct)))) + + ;; CHECK: (func $func (type $1) (param $x (ref $struct)) + ;; CHECK-NEXT: (struct.set $struct 0 + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: (struct.get $struct 0 + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (struct.set $struct $named + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: (struct.get $struct $named + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $func (param $x (ref $struct)) + ;; Use fields 1 and 2. + (struct.set $struct 1 + (local.get $x) + (struct.get $struct 1 + (local.get $x) + ) + ) + (struct.set $struct 2 + (local.get $x) + (struct.get $struct 2 + (local.get $x) + ) + ) + ) +) +