Respect barrier when reusing shadowed variable.

This commit is contained in:
Stephen Chung 2022-02-28 22:13:03 +08:00
parent d41d36c8bb
commit 99ca6de822

View File

@ -91,7 +91,36 @@ impl<'e> ParseState<'e> {
/// Find explicitly declared variable by name in the [`ParseState`], searching in reverse order. /// Find explicitly declared variable by name in the [`ParseState`], searching in reverse order.
/// ///
/// If the variable is not present in the scope adds it to the list of external variables /// The first return value is the offset to be deducted from `ParseState::stack::len()`,
/// i.e. the top element of [`ParseState`]'s variables stack is offset 1.
///
/// If the variable is not present in the scope, the first return value is zero.
///
/// The second return value indicates whether the barrier has been hit before finding the variable.
pub fn find_var(&self, name: &str) -> (usize, bool) {
let mut hit_barrier = false;
(
self.stack
.iter_rev_raw()
.enumerate()
.find(|&(.., (n, ..))| {
if n == SCOPE_SEARCH_BARRIER_MARKER {
// Do not go beyond the barrier
hit_barrier = true;
false
} else {
n == name
}
})
.map_or(0, |(i, ..)| i + 1),
hit_barrier,
)
}
/// Find explicitly declared variable by name in the [`ParseState`], searching in reverse order.
///
/// If the variable is not present in the scope adds it to the list of external variables.
/// ///
/// The return value is the offset to be deducted from `ParseState::stack::len()`, /// The return value is the offset to be deducted from `ParseState::stack::len()`,
/// i.e. the top element of [`ParseState`]'s variables stack is offset 1. /// i.e. the top element of [`ParseState`]'s variables stack is offset 1.
@ -100,27 +129,13 @@ impl<'e> ParseState<'e> {
#[inline] #[inline]
#[must_use] #[must_use]
pub fn access_var(&mut self, name: &str, pos: Position) -> Option<NonZeroUsize> { pub fn access_var(&mut self, name: &str, pos: Position) -> Option<NonZeroUsize> {
let mut hit_barrier = false;
let _pos = pos; let _pos = pos;
let index = self let (index, hit_barrier) = self.find_var(name);
.stack
.iter_rev_raw()
.enumerate()
.find(|&(.., (n, ..))| {
if n == SCOPE_SEARCH_BARRIER_MARKER {
// Do not go beyond the barrier
hit_barrier = true;
false
} else {
n == name
}
})
.and_then(|(i, ..)| NonZeroUsize::new(i + 1));
#[cfg(not(feature = "no_closure"))] #[cfg(not(feature = "no_closure"))]
if self.allow_capture { if self.allow_capture {
if index.is_none() && !self.external_vars.iter().any(|v| v.name == name) { if index == 0 && !self.external_vars.iter().any(|v| v.name == name) {
self.external_vars.push(crate::ast::Ident { self.external_vars.push(crate::ast::Ident {
name: name.into(), name: name.into(),
pos: _pos, pos: _pos,
@ -133,7 +148,7 @@ impl<'e> ParseState<'e> {
if hit_barrier { if hit_barrier {
None None
} else { } else {
index NonZeroUsize::new(index)
} }
} }
@ -2721,14 +2736,18 @@ impl Engine {
ASTFlags::NONE ASTFlags::NONE
}; };
let existing = state.stack.get_index(&name).and_then(|(n, ..)| { let (existing, hit_barrier) = state.find_var(&name);
if n < state.block_stack_len { let existing = if !hit_barrier && existing > 0 {
let offset = state.stack.len() - existing;
if offset < state.block_stack_len {
// Defined in parent block // Defined in parent block
None None
} else { } else {
Some(n) Some(offset)
} }
}); } else {
None
};
let idx = if let Some(n) = existing { let idx = if let Some(n) = existing {
state.stack.get_mut_by_index(n).set_access_mode(access); state.stack.get_mut_by_index(n).set_access_mode(access);