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) } }