From 05c88b78262fe9afa9fa83159ece4a3efba83327 Mon Sep 17 00:00:00 2001 From: Mark <> Date: Thu, 22 Feb 2024 19:13:50 +0100 Subject: [PATCH] fix variable shadowing not working (how was this not noticed until now??) --- mers/Cargo.toml | 4 ++-- mers_lib/src/info/mod.rs | 4 ++-- mers_lib/src/program/run/assign_to.rs | 10 ++++++++-- mers_lib/src/program/run/mod.rs | 4 ++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mers/Cargo.toml b/mers/Cargo.toml index bcb5df7..f46204c 100644 --- a/mers/Cargo.toml +++ b/mers/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/Dummi26/mers" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -mers_lib = "0.6.0" -# mers_lib = { path = "../mers_lib" } +# mers_lib = "0.6.0" +mers_lib = { path = "../mers_lib" } clap = { version = "4.3.19", features = ["derive"] } colored = "2.1.0" diff --git a/mers_lib/src/info/mod.rs b/mers_lib/src/info/mod.rs index 0c71971..ac625d0 100755 --- a/mers_lib/src/info/mod.rs +++ b/mers_lib/src/info/mod.rs @@ -45,10 +45,10 @@ impl Local for Info { self.scopes.last_mut().unwrap().init_var(id, value) } fn get_var(&self, id: &Self::VariableIdentifier) -> Option<&Self::VariableData> { - self.scopes.iter().find_map(|l| l.get_var(id)) + self.scopes.iter().rev().find_map(|l| l.get_var(id)) } fn get_var_mut(&mut self, id: &Self::VariableIdentifier) -> Option<&mut Self::VariableData> { - self.scopes.iter_mut().find_map(|l| l.get_var_mut(id)) + self.scopes.iter_mut().rev().find_map(|l| l.get_var_mut(id)) } fn duplicate(&self) -> Self { Self { diff --git a/mers_lib/src/program/run/assign_to.rs b/mers_lib/src/program/run/assign_to.rs index a3fa8d1..1f737ce 100755 --- a/mers_lib/src/program/run/assign_to.rs +++ b/mers_lib/src/program/run/assign_to.rs @@ -25,9 +25,15 @@ impl MersStatement for AssignTo { return Err("can't init to statement type AssignTo".to_string().into()); } let source = self.source.check(info, None)?; - let target = match self.target.check(info, Some(&source)) { + let target = match self + .target + .check(info, if self.is_init { Some(&source) } else { None }) + { Ok(v) => v, Err(e) => { + if !self.is_init { + return Err(e); + } return Err(CheckError::new() .src(vec![ (self.pos_in_src.clone(), None), @@ -35,7 +41,7 @@ impl MersStatement for AssignTo { (self.source.source_range(), Some(error_colors::InitFrom)), ]) .msg(format!("Cannot initialize:")) - .err(e)) + .err(e)); } }; if !self.is_init { diff --git a/mers_lib/src/program/run/mod.rs b/mers_lib/src/program/run/mod.rs index 1483883..bc9e163 100755 --- a/mers_lib/src/program/run/mod.rs +++ b/mers_lib/src/program/run/mod.rs @@ -44,12 +44,12 @@ pub trait MersStatement: Debug + Send + Sync { fn run_custom(&self, info: &mut Info) -> Data; /// if true, local variables etc. will be contained inside their own scope. fn has_scope(&self) -> bool; - fn check(&self, info: &mut CheckInfo, assign: Option<&Type>) -> Result { + fn check(&self, info: &mut CheckInfo, init_to: Option<&Type>) -> Result { info.global.depth += 1; if self.has_scope() { info.create_scope(); } - let o = self.check_custom(info, assign); + let o = self.check_custom(info, init_to); if info.global.enable_hooks { // Hooks - keep in sync with run/mod.rs/compile() hooks section 'hook_save_info_at: {