Refactor: Use new_inner for substitution maps
The `SubstitutionMap::new_inner()` method provides a more robust way to create nested substitution maps, ensuring that only global substitutions are inherited. This prevents potential issues with overlapping local and upvalue addresses in nested scopes, such as during lambda inlining. This change also includes: - A new `try_fold_record` method in `folder.rs` to optimize record literals into constant values. - Updates to `inliner.rs` to recognize `Value::Record` for inlining. - Various test cases to verify record optimization and constant folding.
This commit is contained in:
@@ -229,7 +229,7 @@ impl Optimizer {
|
||||
&& path.enter_lambda(&callee.identity)
|
||||
{
|
||||
path.inlining_depth += 1;
|
||||
let mut inner_sub = SubstitutionMap::new();
|
||||
let mut inner_sub = sub.new_inner();
|
||||
let collapsed = if inliner
|
||||
.prepare_beta_reduction(params, &arg_nodes, body, &mut inner_sub)
|
||||
.is_some()
|
||||
@@ -266,7 +266,7 @@ impl Optimizer {
|
||||
&& positional_count.is_some()
|
||||
&& !lambda_node.ty.is_recursive
|
||||
{
|
||||
let mut inner_sub = SubstitutionMap::new();
|
||||
let mut inner_sub = sub.new_inner();
|
||||
path.inlining_stack.insert(*idx);
|
||||
path.inlining_depth += 1;
|
||||
let collapsed = if inliner
|
||||
@@ -467,7 +467,7 @@ impl Optimizer {
|
||||
|
||||
let mut new_upvalues = Vec::new();
|
||||
let mut mapping = Vec::new();
|
||||
let mut next_inner_subs = SubstitutionMap::new();
|
||||
let mut next_inner_subs = sub.new_inner();
|
||||
next_inner_subs.assigned = info.assigned;
|
||||
|
||||
for (old_idx, capture_addr) in original_upvalues.iter().enumerate() {
|
||||
@@ -543,12 +543,17 @@ impl Optimizer {
|
||||
.collect();
|
||||
(BoundKind::Tuple { elements }, node.ty.clone())
|
||||
}
|
||||
BoundKind::Record { fields } => {
|
||||
let fields = fields
|
||||
.into_iter()
|
||||
.map(|(k, v)| (self.visit_node(k, sub, path), self.visit_node(v, sub, path)))
|
||||
BoundKind::Record { ref fields } => {
|
||||
let mapped_fields: Vec<_> = fields
|
||||
.iter()
|
||||
.map(|(k, v)| (self.visit_node(k.clone(), sub, path), self.visit_node(v.clone(), sub, path)))
|
||||
.collect();
|
||||
(BoundKind::Record { fields }, node.ty.clone())
|
||||
|
||||
if self.enabled && let Some(folded) = folder.try_fold_record(&mapped_fields, &node) {
|
||||
return folded;
|
||||
}
|
||||
|
||||
(BoundKind::Record { fields: mapped_fields }, node.ty.clone())
|
||||
}
|
||||
BoundKind::Expansion {
|
||||
ref original_call,
|
||||
|
||||
Reference in New Issue
Block a user