From ad608ac51c69936539cc2e3b85788f7b60a486cf Mon Sep 17 00:00:00 2001 From: tritao Date: Tue, 25 Feb 2025 19:17:47 +0000 Subject: [PATCH] Introduce `walk_scope_chain_early_return` and `walk_scope_chain`. --- .../ast_node/expression/typed_expression.rs | 2 +- .../namespace/lexical_scope.rs | 2 +- .../src/semantic_analysis/namespace/module.rs | 19 ++++++------------- .../semantic_analysis/namespace/trait_map.rs | 19 +++++++++++-------- .../semantic_analysis/type_check_context.rs | 2 +- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs index d8a51da3eb5..a4da94f1903 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs @@ -2555,7 +2555,7 @@ impl ty::TyExpression { base_name: &Ident, projections: &[ty::ProjectionKind], ) -> Result<(TypeId, TypeId), ErrorEmitted> { - let ret = module.walk_scope_chain(|lexical_scope| { + let ret = module.walk_scope_chain_early_return(|lexical_scope| { Self::find_subfield_type_helper( lexical_scope, handler, diff --git a/sway-core/src/semantic_analysis/namespace/lexical_scope.rs b/sway-core/src/semantic_analysis/namespace/lexical_scope.rs index f35ee74514b..ea431b6e84b 100644 --- a/sway-core/src/semantic_analysis/namespace/lexical_scope.rs +++ b/sway-core/src/semantic_analysis/namespace/lexical_scope.rs @@ -678,7 +678,7 @@ impl Items { } }; - let _ = module.walk_scope_chain(|lexical_scope| { + let _ = module.walk_scope_chain_early_return(|lexical_scope| { if let Some((ident, decl)) = lexical_scope.items.symbols.get_key_value(&name) { append_shadowing_error( ident, diff --git a/sway-core/src/semantic_analysis/namespace/module.rs b/sway-core/src/semantic_analysis/namespace/module.rs index b360da87d89..4bc6749d656 100644 --- a/sway-core/src/semantic_analysis/namespace/module.rs +++ b/sway-core/src/semantic_analysis/namespace/module.rs @@ -291,7 +291,7 @@ impl Module { self.current_lexical_scope_id = parent_scope_id.unwrap(); // panics if pops do not match pushes } - pub fn walk_scope_chain( + pub fn walk_scope_chain_early_return( &self, mut f: impl FnMut(&LexicalScope) -> Result, ErrorEmitted>, ) -> Result, ErrorEmitted> { @@ -310,23 +310,16 @@ impl Module { Ok(None) } - pub fn walk_scope_chain_mut( - &mut self, - mut f: impl FnMut(&mut LexicalScope) -> Result, ErrorEmitted>, - ) -> Result, ErrorEmitted> { - let mut lexical_scope_opt = Some(self.current_lexical_scope_mut()); + pub fn walk_scope_chain(&self, mut f: impl FnMut(&LexicalScope)) { + let mut lexical_scope_opt = Some(self.current_lexical_scope()); while let Some(lexical_scope) = lexical_scope_opt { - let result = f(lexical_scope)?; - if let Some(result) = result { - return Ok(Some(result)); - } + f(lexical_scope); if let Some(parent_scope_id) = lexical_scope.parent { - lexical_scope_opt = self.get_lexical_scope_mut(parent_scope_id); + lexical_scope_opt = self.get_lexical_scope(parent_scope_id); } else { lexical_scope_opt = None; } } - Ok(None) } pub fn get_items_for_type( @@ -344,7 +337,7 @@ impl Module { symbol: &Ident, ) -> Result<(ResolvedDeclaration, ModulePathBuf), ErrorEmitted> { let mut last_handler = Handler::default(); - let ret = self.walk_scope_chain(|lexical_scope| { + let ret = self.walk_scope_chain_early_return(|lexical_scope| { last_handler = Handler::default(); Ok(lexical_scope .items diff --git a/sway-core/src/semantic_analysis/namespace/trait_map.rs b/sway-core/src/semantic_analysis/namespace/trait_map.rs index 209c5b3b9bd..9a262a28f5c 100644 --- a/sway-core/src/semantic_analysis/namespace/trait_map.rs +++ b/sway-core/src/semantic_analysis/namespace/trait_map.rs @@ -19,7 +19,10 @@ use crate::{ parsed::{EnumDeclaration, ImplItem, StructDeclaration}, ty::{self, TyDecl, TyImplItem, TyTraitItem}, CallPath, - }, type_system::{SubstTypes, TypeId}, IncludeSelf, SubstTypesContext, TraitConstraint, TypeArgument, TypeEngine, TypeInfo, TypeParameter, TypeSubstMap, UnifyCheck + }, + type_system::{SubstTypes, TypeId}, + IncludeSelf, SubstTypesContext, TraitConstraint, TypeArgument, TypeEngine, TypeInfo, + TypeParameter, TypeSubstMap, UnifyCheck, }; use super::Module; @@ -809,7 +812,7 @@ impl TraitMap { return items; } - let _ = module.walk_scope_chain(|lexical_scope| { + let _ = module.walk_scope_chain_early_return(|lexical_scope| { let impls = lexical_scope .items .implemented_traits @@ -859,7 +862,7 @@ impl TraitMap { if matches!(&*type_engine.get(*type_id), TypeInfo::ErrorRecovery(_)) { return spans; } - let _ = module.walk_scope_chain(|lexical_scope| { + let _ = module.walk_scope_chain_early_return(|lexical_scope| { let impls = lexical_scope .items .implemented_traits @@ -892,7 +895,7 @@ impl TraitMap { /// spans of the impls. pub fn get_impl_spans_for_trait_name(module: &Module, trait_name: &CallPath) -> Vec { let mut spans = vec![]; - let _ = module.walk_scope_chain(|lexical_scope| { + let _ = module.walk_scope_chain_early_return(|lexical_scope| { spans.push( lexical_scope .items @@ -951,7 +954,7 @@ impl TraitMap { if matches!(&*type_engine.get(type_id), TypeInfo::ErrorRecovery(_)) { return items; } - let _ = module.walk_scope_chain(|lexical_scope| { + let _ = module.walk_scope_chain_early_return(|lexical_scope| { let impls = lexical_scope .items .implemented_traits @@ -1047,7 +1050,7 @@ impl TraitMap { if matches!(&*type_engine.get(type_id), TypeInfo::ErrorRecovery(_)) { return trait_names; } - let _ = module.walk_scope_chain(|lexical_scope| { + let _ = module.walk_scope_chain_early_return(|lexical_scope| { let impls = lexical_scope .items .implemented_traits @@ -1260,7 +1263,7 @@ impl TraitMap { engines: &Engines, ) -> BTreeSet<(Ident, TypeId)> { let mut all_impld_traits: BTreeSet<(Ident, TypeId)> = Default::default(); - let _ = module.walk_scope_chain(|lexical_scope| { + let _ = module.walk_scope_chain_early_return(|lexical_scope| { all_impld_traits.extend( lexical_scope .items @@ -1388,7 +1391,7 @@ impl TraitMap { let unify_check_equality = UnifyCheck::constraint_subset(engines); let mut impld_traits_type_ids: Vec> = vec![]; - let _ = module.walk_scope_chain(|lexical_scope| { + let _ = module.walk_scope_chain_early_return(|lexical_scope| { let impls = lexical_scope .items .implemented_traits diff --git a/sway-core/src/semantic_analysis/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index f20918520b7..43064e3bc48 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -1220,7 +1220,7 @@ impl<'a> TypeCheckContext<'a> { return; }; - let _ = src_mod.walk_scope_chain(|lexical_scope| { + let _ = src_mod.walk_scope_chain_early_return(|lexical_scope| { impls_to_insert.extend( lexical_scope .items