Add object-oriented records example
This commit is contained in:
@@ -472,9 +472,12 @@ impl Optimizer {
|
||||
|
||||
for (old_idx, capture_addr) in original_upvalues.iter().enumerate() {
|
||||
let mut inlined_val = None;
|
||||
if let Some(val) = sub.get_value(capture_addr) {
|
||||
if !sub.assigned.contains(capture_addr)
|
||||
&& let Some(val) = sub.get_value(capture_addr)
|
||||
{
|
||||
inlined_val = Some(val.clone());
|
||||
}
|
||||
|
||||
if let Address::Local(slot) = capture_addr {
|
||||
sub.captured_slots.insert(*slot);
|
||||
}
|
||||
|
||||
@@ -159,9 +159,7 @@ impl UsageInfo {
|
||||
|
||||
pub fn collect_pattern(&mut self, node: &AnalyzedNode) {
|
||||
match &node.kind {
|
||||
BoundKind::Define { addr, .. } => {
|
||||
self.assigned.insert(*addr);
|
||||
}
|
||||
BoundKind::Define { .. } => {}
|
||||
BoundKind::Set { addr, .. } => {
|
||||
self.assigned.insert(*addr);
|
||||
}
|
||||
|
||||
@@ -427,6 +427,47 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optimizer_upvalue_inlining_bug_repro() {
|
||||
let env = Environment::new();
|
||||
let source = r#"
|
||||
(do
|
||||
(def make-counter (fn [init]
|
||||
(do
|
||||
(def val init)
|
||||
{
|
||||
:inc (fn [] (assign val (+ val 1)))
|
||||
:get (fn [] val)
|
||||
})))
|
||||
(def c (make-counter 10))
|
||||
((.inc c))
|
||||
((.get c)))
|
||||
"#;
|
||||
let res = env.run_script(source);
|
||||
assert!(res.is_ok(), "Optimizer bug triggered: {:?}", res.err());
|
||||
assert_eq!(format!("{}", res.unwrap()), "11");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optimizer_destructuring_inlining_and_mutation() {
|
||||
let env = Environment::new();
|
||||
// 1. Test: Destructuring definition should allow inlining if not mutated
|
||||
let source_inline = "(do (def [x y] [10 20]) (+ x y))";
|
||||
let res_inline = env.run_script(source_inline).unwrap();
|
||||
assert_eq!(format!("{}", res_inline), "30");
|
||||
|
||||
// 2. Test: Destructuring definition should NOT be inlined if mutated (Regression Test)
|
||||
let source_mutation = r#"
|
||||
(do
|
||||
(def [a b] [1 2])
|
||||
(def f (fn [] (assign a (+ a b))))
|
||||
(f)
|
||||
a)
|
||||
"#;
|
||||
let res_mutation = env.run_script(source_mutation).unwrap();
|
||||
assert_eq!(format!("{}", res_mutation), "3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_record_basics() {
|
||||
let env = Environment::new();
|
||||
|
||||
Reference in New Issue
Block a user