Add destructuring support to dead-def elimination

Introduce `collect_pattern_addrs` to gather all bound addresses from a
destructuring pattern. This enables the optimizer to remove unused
destructuring definitions, similar to how simple unused definitions are
handled. Added integration tests to verify this behavior and other
optimizer robustness scenarios.
This commit is contained in:
2026-03-21 15:35:28 +01:00
parent 6264e21f24
commit bc287d27dd
3 changed files with 144 additions and 4 deletions
+26 -1
View File
@@ -199,7 +199,7 @@ impl UsageInfo {
}
}
fn collect_assigned_from_target(&mut self, node: &AnalyzedNode) {
pub fn collect_assigned_from_target(&mut self, node: &AnalyzedNode) {
match &node.kind {
NodeKind::Identifier { binding, .. } => {
let addr = match binding {
@@ -217,3 +217,28 @@ impl UsageInfo {
}
}
}
/// Collects all addresses declared in a binding pattern (Identifier Declaration or nested Tuple).
///
/// Used by the optimizer to enumerate all slots bound by a destructuring `def` pattern,
/// enabling dead-def elimination for patterns that `extract_def_addr` cannot handle
/// (i.e. anything other than a plain `Identifier`).
pub fn collect_pattern_addrs(pattern: &AnalyzedNode, out: &mut Vec<Address<VirtualId>>) {
match &pattern.kind {
NodeKind::Identifier {
binding: IdentifierBinding::Declaration { addr, .. },
..
} => {
out.push(*addr);
}
NodeKind::Def { pattern: inner, .. } => {
collect_pattern_addrs(inner, out);
}
NodeKind::Tuple { elements } => {
for el in elements {
collect_pattern_addrs(el, out);
}
}
_ => {}
}
}