Skip to content

Commit e86400e

Browse files
committed
Fix panic in remove_node for Definition nodes
Definition nodes store their name in the `export` field, not the `name` field (which is None). The log::debug! in remove_node incorrectly used `node.name.as_ref().unwrap()`, which panics for Definition nodes. Changed to `node.export.as_ref().unwrap()`, matching the pattern used elsewhere (e.g., the Display impl at line 1324). Added a unit test that verifies remove_node works correctly for type definition nodes.
1 parent e1e684a commit e86400e

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

crates/wac-graph/src/graph.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ impl CompositionGraph {
10291029
if let NodeKind::Definition = node.kind {
10301030
log::debug!(
10311031
"removing type definition `{name}`",
1032-
name = node.name.as_ref().unwrap()
1032+
name = node.export.as_ref().unwrap()
10331033
);
10341034
let removed = self.defined.remove(&node.item_kind.ty());
10351035
assert!(removed.is_some());
@@ -1959,4 +1959,22 @@ mod test {
19591959
UnexportError::MustExportDefinition
19601960
));
19611961
}
1962+
1963+
#[test]
1964+
fn it_can_remove_a_type_definition() {
1965+
let mut graph = CompositionGraph::new();
1966+
let ty_id = graph
1967+
.types_mut()
1968+
.add_defined_type(DefinedType::Alias(ValueType::Primitive(PrimitiveType::S32)));
1969+
let node_id = graph
1970+
.define_type("foo", Type::Value(ValueType::Defined(ty_id)))
1971+
.unwrap();
1972+
1973+
// Definition nodes store their name in `export`, not `name`.
1974+
// Removing a definition node should not panic.
1975+
graph.remove_node(node_id);
1976+
1977+
// Verify the definition and export were cleaned up
1978+
assert!(graph.get_export("foo").is_none());
1979+
}
19621980
}

0 commit comments

Comments
 (0)