Skip to content

Commit

Permalink
Introduce walk_scope_chain_early_return and walk_scope_chain.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Feb 25, 2025
1 parent dd0fd73 commit ad608ac
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/semantic_analysis/namespace/lexical_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 6 additions & 13 deletions sway-core/src/semantic_analysis/namespace/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
pub fn walk_scope_chain_early_return<T>(
&self,
mut f: impl FnMut(&LexicalScope) -> Result<Option<T>, ErrorEmitted>,
) -> Result<Option<T>, ErrorEmitted> {
Expand All @@ -310,23 +310,16 @@ impl Module {
Ok(None)
}

pub fn walk_scope_chain_mut<T>(
&mut self,
mut f: impl FnMut(&mut LexicalScope) -> Result<Option<T>, ErrorEmitted>,
) -> Result<Option<T>, 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(
Expand All @@ -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
Expand Down
19 changes: 11 additions & 8 deletions sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -892,7 +895,7 @@ impl TraitMap {
/// spans of the impls.
pub fn get_impl_spans_for_trait_name(module: &Module, trait_name: &CallPath) -> Vec<Span> {
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1388,7 +1391,7 @@ impl TraitMap {
let unify_check_equality = UnifyCheck::constraint_subset(engines);

let mut impld_traits_type_ids: Vec<Vec<(TypeId, String)>> = vec![];
let _ = module.walk_scope_chain(|lexical_scope| {
let _ = module.walk_scope_chain_early_return(|lexical_scope| {
let impls = lexical_scope
.items
.implemented_traits
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/semantic_analysis/type_check_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ad608ac

Please sign in to comment.