From 4a0143302f488d9af17f82b44431d3e25923db73 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Mon, 5 Dec 2022 17:21:46 +0900 Subject: [PATCH] Fix #272 --- compiler/erg_common/dict.rs | 8 ++++++++ compiler/erg_compiler/context/mod.rs | 6 ++++++ compiler/erg_compiler/lower.rs | 1 + 3 files changed, 15 insertions(+) diff --git a/compiler/erg_common/dict.rs b/compiler/erg_common/dict.rs index 233da03f..dae08406 100644 --- a/compiler/erg_common/dict.rs +++ b/compiler/erg_common/dict.rs @@ -127,6 +127,14 @@ impl Dict { pub fn clear(&mut self) { self.dict.clear(); } + + /// remove all elements for which the predicate returns false + pub fn retain(&mut self, f: F) + where + F: FnMut(&K, &mut V) -> bool, + { + self.dict.retain(f); + } } impl IntoIterator for Dict { diff --git a/compiler/erg_compiler/context/mod.rs b/compiler/erg_compiler/context/mod.rs index a4e40f67..7f53244a 100644 --- a/compiler/erg_compiler/context/mod.rs +++ b/compiler/erg_compiler/context/mod.rs @@ -950,6 +950,12 @@ impl Context { self.kind = kind; } + pub(crate) fn clear_invalid_vars(&mut self) { + self.locals.retain(|_, v| v.t != Failure); + self.decls.retain(|_, v| v.t != Failure); + self.params.retain(|(_, v)| v.t != Failure); + } + pub fn pop(&mut self) -> Context { if let Some(parent) = self.outer.as_mut() { let parent = mem::take(parent); diff --git a/compiler/erg_compiler/lower.rs b/compiler/erg_compiler/lower.rs index b01a884d..66bd7cc1 100644 --- a/compiler/erg_compiler/lower.rs +++ b/compiler/erg_compiler/lower.rs @@ -1978,6 +1978,7 @@ impl ASTLowerer { } } } + self.ctx.clear_invalid_vars(); self.ctx.check_decls().unwrap_or_else(|mut errs| { self.errs.append(&mut errs); });