From 619d6d49dc791b7a561a623255ad30d37d4a56bb Mon Sep 17 00:00:00 2001 From: arty Date: Tue, 24 Oct 2023 09:03:49 -0700 Subject: [PATCH] Add renames that were needed but missed downstream of assign --- resources/tests/cse-complex-21.clsp | 28 ++++++++++++++++++++++++++++ src/compiler/rename.rs | 9 ++++++--- src/tests/classic/run.rs | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 resources/tests/cse-complex-21.clsp diff --git a/resources/tests/cse-complex-21.clsp b/resources/tests/cse-complex-21.clsp new file mode 100644 index 000000000..531bb5c41 --- /dev/null +++ b/resources/tests/cse-complex-21.clsp @@ -0,0 +1,28 @@ +(mod (X) + (include *standard-cl-21*) + + (defun mess (X) ;; 11 41 + (assign + Y (+ X 1) ;; 12 42 + Z (+ Y 2) ;; 14 44 + + (if (= X 11) + (assign + Y (* X 2) ;; 22 82 + Z (+ Y 1) ;; 23 83 + + (* Y Z) ;; 22 * 23 = 506 + ) + + (assign + Y (* X 3) ;; 33 123 + Z (+ Y 2) ;; 35 125 + + (* Y Z) ;; 123 * 125 = 15375 + ) + ) + ) + ) + + (mess X) + ) diff --git a/src/compiler/rename.rs b/src/compiler/rename.rs index 40ad2e0b8..30557c681 100644 --- a/src/compiler/rename.rs +++ b/src/compiler/rename.rs @@ -164,7 +164,7 @@ pub fn rename_assign_bindings( ) -> Result<(BodyForm, Vec>), CompileErr> { // Order the bindings. let sorted_bindings = toposort_assign_bindings(l, bindings)?; - let mut renames = HashMap::new(); + let mut renames: HashMap, Vec> = HashMap::new(); // Process in reverse order so we rename from inner to outer. let bindings_to_rename: Vec> = sorted_bindings.to_vec(); let renamed_bindings = map_m_reverse( @@ -175,9 +175,11 @@ pub fn rename_assign_bindings( for (name, renamed) in new_names.iter() { renames.insert(name.clone(), renamed.clone()); } + + let renamed_in_body = Rc::new(rename_args_bodyform(b.body.borrow())?); Ok(Rc::new(Binding { pattern: BindingPattern::Complex(rename_in_cons(&renames, p.clone(), false)), - body: Rc::new(rename_in_bodyform(&renames, b.body.clone())?), + body: Rc::new(rename_in_bodyform(&renames, renamed_in_body)?), ..b.clone() })) } else { @@ -186,7 +188,8 @@ pub fn rename_assign_bindings( }, &bindings_to_rename, )?; - Ok((rename_in_bodyform(&renames, body)?, renamed_bindings)) + let new_body = Rc::new(rename_args_bodyform(body.borrow())?); + Ok((rename_in_bodyform(&renames, new_body)?, renamed_bindings)) } fn rename_in_bodyform( diff --git a/src/tests/classic/run.rs b/src/tests/classic/run.rs index b7f038fc1..69e8f297e 100644 --- a/src/tests/classic/run.rs +++ b/src/tests/classic/run.rs @@ -1343,3 +1343,25 @@ fn test_classic_obeys_operator_choice_at_compile_time_version_0() { .to_string(); assert_eq!(compiled, "FAIL: unimplemented operator 48"); } + +#[test] +fn test_assign_rename_tricky() { + let filename = "resources/tests/cse-complex-21.clsp"; + let program = do_basic_run(&vec!["run".to_string(), filename.to_string()]) + .trim() + .to_string(); + + let run_result_11 = do_basic_brun(&vec![ + "brun".to_string(), + program.clone(), + "(11)".to_string(), + ]) + .trim() + .to_string(); + assert_eq!(run_result_11, "506"); + + let run_result_41 = do_basic_brun(&vec!["brun".to_string(), program, "(41)".to_string()]) + .trim() + .to_string(); + assert_eq!(run_result_41, "15375"); +}