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:
@@ -19,6 +19,24 @@ impl SubstitutionMap {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Creates a new SubstitutionMap for an inner scope (like an inlined Lambda).
|
||||
/// Safely inherits only Global substitutions, because Local and Upvalue
|
||||
/// addresses are relative to the specific function frame and would overlap.
|
||||
pub fn new_inner(&self) -> Self {
|
||||
let mut inner = Self::new();
|
||||
for (k, v) in &self.values {
|
||||
if matches!(k, Address::Global(_)) {
|
||||
inner.values.insert(*k, v.clone());
|
||||
}
|
||||
}
|
||||
for (k, v) in &self.ast_substitutions {
|
||||
if matches!(k, Address::Global(_)) {
|
||||
inner.ast_substitutions.insert(*k, v.clone());
|
||||
}
|
||||
}
|
||||
inner
|
||||
}
|
||||
|
||||
pub fn add_ast_substitution(&mut self, addr: Address, node: AnalyzedNode) {
|
||||
self.ast_substitutions.insert(addr, node);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user