From 0edb42836d7c576ad5654d1025bb972cf3b73b15 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 2 Sep 2024 12:57:27 -0400 Subject: [PATCH] Fix context scope bug --- dust-lang/src/context.rs | 42 ++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/dust-lang/src/context.rs b/dust-lang/src/context.rs index 1e3d041..cbb8024 100644 --- a/dust-lang/src/context.rs +++ b/dust-lang/src/context.rs @@ -268,6 +268,32 @@ impl Context { &self, identifier: &Identifier, position: usize, + ) -> Result { + let found = self.update_position_if_found(identifier, position)?; + + if found { + Ok(true) + } else if let Some(parent) = &self.parent { + let found_in_ancestor = parent.update_last_position(identifier, position)?; + + if !found_in_ancestor { + let mut associations = self.associations.write()?; + + log::trace!("Updating {identifier}'s last position to {position:?}"); + + associations.insert(identifier.clone(), (ContextData::Reserved, position)); + } + + Ok(false) + } else { + Ok(false) + } + } + + fn update_position_if_found( + &self, + identifier: &Identifier, + position: usize, ) -> Result { let mut associations = self.associations.write()?; @@ -278,21 +304,7 @@ impl Context { Ok(true) } else { - let ancestor_contains = if let Some(parent) = &self.parent { - parent.contains(identifier)? - } else { - false - }; - - if ancestor_contains { - Ok(true) - } else { - log::trace!("Reserving {identifier} at {position:?}"); - - associations.insert(identifier.clone(), (ContextData::Reserved, position)); - - Ok(false) - } + Ok(false) } }