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:
@@ -1,6 +1,6 @@
|
||||
use crate::ast::compiler::bound_nodes::{Address, AnalyzedNode, BoundKind, NodeMetrics};
|
||||
use crate::ast::nodes::Node;
|
||||
use crate::ast::types::{Purity, StaticType, Value};
|
||||
use crate::ast::types::{Purity, StaticType, Value, RecordLayout};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
@@ -48,6 +48,32 @@ impl<'a> Folder<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_fold_record(
|
||||
&self,
|
||||
fields: &[(AnalyzedNode, AnalyzedNode)],
|
||||
template: &AnalyzedNode,
|
||||
) -> Option<AnalyzedNode> {
|
||||
let mut layout_fields = Vec::with_capacity(fields.len());
|
||||
let mut values = Vec::with_capacity(fields.len());
|
||||
|
||||
for (k_node, v_node) in fields {
|
||||
if let BoundKind::Constant(Value::Keyword(kw)) = &k_node.kind {
|
||||
if let BoundKind::Constant(val) = &v_node.kind {
|
||||
layout_fields.push((*kw, val.static_type()));
|
||||
values.push(val.clone());
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
let layout = RecordLayout::get_or_create(layout_fields);
|
||||
let record_val = Value::Record(layout, Rc::new(values));
|
||||
Some(self.make_constant_node(record_val, template))
|
||||
}
|
||||
|
||||
pub fn try_fold_pure(
|
||||
&self,
|
||||
callee: &AnalyzedNode,
|
||||
|
||||
Reference in New Issue
Block a user