diff --git a/sway-core/src/ir_generation/compile.rs b/sway-core/src/ir_generation/compile.rs index 2016f04d881..4c46b8d4095 100644 --- a/sway-core/src/ir_generation/compile.rs +++ b/sway-core/src/ir_generation/compile.rs @@ -205,9 +205,9 @@ pub(crate) fn compile_constants( module: Module, module_ns: &namespace::Module, ) -> Result<(), CompileError> { - for decl_name in module_ns.items().get_all_declared_symbols() { + for decl_name in module_ns.current_items().get_all_declared_symbols() { if let Some(ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id, .. })) = - module_ns.items().symbols.get(decl_name) + module_ns.current_items().symbols.get(decl_name) { let const_decl = engines.de().get_constant(decl_id); let call_path = const_decl.call_path.clone(); diff --git a/sway-core/src/ir_generation/const_eval.rs b/sway-core/src/ir_generation/const_eval.rs index dc49ddeeb32..0fad3ec685d 100644 --- a/sway-core/src/ir_generation/const_eval.rs +++ b/sway-core/src/ir_generation/const_eval.rs @@ -124,7 +124,7 @@ pub(crate) fn compile_const_decl( (_, Some(config_val), _) => Ok(Some(config_val)), (None, None, Some(module_ns)) => { // See if we it's a global const and whether we can compile it *now*. - let decl = module_ns.items().check_symbol(&call_path.suffix); + let decl = module_ns.current_items().check_symbol(&call_path.suffix); let const_decl = match const_decl { Some(decl) => Some(decl), None => None, @@ -1179,10 +1179,12 @@ mod tests { let handler = Handler::default(); let mut context = Context::new(engines.se(), sway_ir::ExperimentalFlags::default()); let mut md_mgr = MetadataManager::default(); - let mut core_lib = namespace::Module::default(); - core_lib.name = Some(sway_types::Ident::new_no_span( - "assert_is_constant_test".to_string(), - )); + let core_lib = namespace::Module { + name: Some(sway_types::Ident::new_no_span( + "assert_is_constant_test".to_string(), + )), + ..Default::default() + }; let r = crate::compile_to_ast( &handler, diff --git a/sway-core/src/language/call_path.rs b/sway-core/src/language/call_path.rs index feaf04e5d61..d19c5c404ed 100644 --- a/sway-core/src/language/call_path.rs +++ b/sway-core/src/language/call_path.rs @@ -313,7 +313,12 @@ impl CallPath { let mut is_external = false; let mut is_absolute = false; - if let Some(use_synonym) = namespace.module().items().use_synonyms.get(&self.suffix) { + if let Some(use_synonym) = namespace + .module() + .current_items() + .use_synonyms + .get(&self.suffix) + { synonym_prefixes = use_synonym.0.clone(); is_absolute = use_synonym.3; let submodule = namespace.module().submodule(&[use_synonym.0[0].clone()]); diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs b/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs index b4887a16c39..d07cfcb5c12 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/abi.rs @@ -302,17 +302,21 @@ impl ty::TyAbiDecl { all_items.push(TyImplItem::Constant(decl_ref.clone())); let const_shadowing_mode = ctx.const_shadowing_mode(); let generic_shadowing_mode = ctx.generic_shadowing_mode(); - let _ = ctx.namespace.module_mut().items_mut().insert_symbol( - handler, - const_name.clone(), - ty::TyDecl::ConstantDecl(ty::ConstantDecl { - name: const_name, - decl_id: *decl_ref.id(), - decl_span: const_decl.span.clone(), - }), - const_shadowing_mode, - generic_shadowing_mode, - ); + let _ = ctx + .namespace + .module_mut() + .current_items_mut() + .insert_symbol( + handler, + const_name.clone(), + ty::TyDecl::ConstantDecl(ty::ConstantDecl { + name: const_name, + decl_id: *decl_ref.id(), + decl_span: const_decl.span.clone(), + }), + const_shadowing_mode, + generic_shadowing_mode, + ); } ty::TyTraitInterfaceItem::Type(decl_ref) => { all_items.push(TyImplItem::Type(decl_ref.clone())); diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs b/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs index 865d49d4f5d..b029a326b8c 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs @@ -532,7 +532,7 @@ impl<'a, 'b> AutoImplAbiEncodeContext<'a, 'b> { self.ctx .namespace .module_mut() - .items_mut() + .current_items_mut() .implemented_traits .check_if_trait_constraints_are_satisfied_for_type( &handler, diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs index 558579d574b..15a260aeda2 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs @@ -363,7 +363,7 @@ impl TyDecl { // declarations are not allowed ctx.namespace .module_mut() - .items_mut() + .current_items_mut() .set_storage_declaration(handler, decl_ref.clone())?; decl_ref.into() } diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs b/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs index 9cc2e048371..33e52dd376d 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs @@ -105,25 +105,29 @@ impl ty::TyFunctionParameter { pub fn insert_into_namespace(&self, handler: &Handler, ctx: TypeCheckContext) { let const_shadowing_mode = ctx.const_shadowing_mode(); let generic_shadowing_mode = ctx.generic_shadowing_mode(); - let _ = ctx.namespace.module_mut().items_mut().insert_symbol( - handler, - self.name.clone(), - ty::TyDecl::VariableDecl(Box::new(ty::TyVariableDecl { - name: self.name.clone(), - body: ty::TyExpression { - expression: ty::TyExpressionVariant::FunctionParameter, + let _ = ctx + .namespace + .module_mut() + .current_items_mut() + .insert_symbol( + handler, + self.name.clone(), + ty::TyDecl::VariableDecl(Box::new(ty::TyVariableDecl { + name: self.name.clone(), + body: ty::TyExpression { + expression: ty::TyExpressionVariant::FunctionParameter, + return_type: self.type_argument.type_id, + span: self.name.span(), + }, + mutability: ty::VariableMutability::new_from_ref_mut( + self.is_reference, + self.is_mutable, + ), return_type: self.type_argument.type_id, - span: self.name.span(), - }, - mutability: ty::VariableMutability::new_from_ref_mut( - self.is_reference, - self.is_mutable, - ), - return_type: self.type_argument.type_id, - type_ascription: self.type_argument.clone(), - })), - const_shadowing_mode, - generic_shadowing_mode, - ); + type_ascription: self.type_argument.clone(), + })), + const_shadowing_mode, + generic_shadowing_mode, + ); } } diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs index 5c698deed17..55fb667ad64 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs @@ -631,7 +631,7 @@ fn type_check_trait_implementation( // supertraits of this trait. ctx.namespace .module_mut() - .items_mut() + .current_items_mut() .implemented_traits .check_if_trait_constraints_are_satisfied_for_type( handler, diff --git a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs index 6e557087871..c5ec376778b 100644 --- a/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs +++ b/sway-core/src/semantic_analysis/ast_node/declaration/trait.rs @@ -447,17 +447,21 @@ impl TyTraitDecl { all_items.push(TyImplItem::Constant(decl_ref.clone())); let const_shadowing_mode = ctx.const_shadowing_mode(); let generic_shadowing_mode = ctx.generic_shadowing_mode(); - let _ = ctx.namespace.module_mut().items_mut().insert_symbol( - handler, - const_name.clone(), - ty::TyDecl::ConstantDecl(ty::ConstantDecl { - name: const_name, - decl_id: *decl_ref.id(), - decl_span: const_decl.span.clone(), - }), - const_shadowing_mode, - generic_shadowing_mode, - ); + let _ = ctx + .namespace + .module_mut() + .current_items_mut() + .insert_symbol( + handler, + const_name.clone(), + ty::TyDecl::ConstantDecl(ty::ConstantDecl { + name: const_name, + decl_id: *decl_ref.id(), + decl_span: const_decl.span.clone(), + }), + const_shadowing_mode, + generic_shadowing_mode, + ); } ty::TyTraitInterfaceItem::Type(decl_ref) => { all_items.push(TyImplItem::Type(decl_ref.clone())); 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 304103337fe..cca657c3d4c 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 @@ -1027,25 +1027,31 @@ impl ty::TyExpression { let decl_engine = ctx.engines.de(); let engines = ctx.engines(); - if !ctx.namespace.module().items().has_storage_declared() { + if !ctx + .namespace + .module() + .current_items() + .has_storage_declared() + { return Err(handler.emit_err(CompileError::NoDeclaredStorage { span: span.clone() })); } let storage_fields = ctx .namespace .module() - .items() + .current_items() .get_storage_field_descriptors(handler, decl_engine)?; // Do all namespace checking here! - let (storage_access, mut access_type) = ctx.namespace.module().items().apply_storage_load( - handler, - ctx.engines, - ctx.namespace, - checkee, - &storage_fields, - storage_keyword_span, - )?; + let (storage_access, mut access_type) = + ctx.namespace.module().current_items().apply_storage_load( + handler, + ctx.engines, + ctx.namespace, + checkee, + &storage_fields, + storage_keyword_span, + )?; // The type of a storage access is `core::storage::StorageKey`. This is // the path to it. @@ -2030,7 +2036,7 @@ impl ty::TyExpression { }; let names_vec = names_vec.into_iter().rev().collect::>(); let (ty_of_field, _ty_of_parent) = - ctx.namespace.module().items().find_subfield_type( + ctx.namespace.module().current_items().find_subfield_type( handler, ctx.engines(), ctx.namespace, diff --git a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/struct_instantiation.rs b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/struct_instantiation.rs index 666056d3e4f..33b0e8c083f 100644 --- a/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/struct_instantiation.rs +++ b/sway-core/src/semantic_analysis/ast_node/expression/typed_expression/struct_instantiation.rs @@ -255,7 +255,7 @@ fn collect_struct_constructors( // that we wish to encourage. namespace .module() - .items() + .current_items() .get_items_for_type(engines, struct_type_id) .iter() .filter_map(|item| match item { diff --git a/sway-core/src/semantic_analysis/namespace/items.rs b/sway-core/src/semantic_analysis/namespace/lexical_scope.rs similarity index 95% rename from sway-core/src/semantic_analysis/namespace/items.rs rename to sway-core/src/semantic_analysis/namespace/lexical_scope.rs index 73281ebf5d4..f0665e1a670 100644 --- a/sway-core/src/semantic_analysis/namespace/items.rs +++ b/sway-core/src/semantic_analysis/namespace/lexical_scope.rs @@ -32,6 +32,26 @@ pub(super) type SymbolMap = im::OrdMap; pub(super) type UseSynonyms = im::HashMap, GlobImport, ty::TyDecl, bool)>; pub(super) type UseAliases = im::HashMap; +/// Represents a lexical scope integer-based identifier, which can be used to reference +/// specific a lexical scope. +pub type LexicalScopeId = usize; + +/// Represents a lexical scope path, a vector of lexical scope identifiers, which specifies +/// the path from root to a specific lexical scope in the hierarchy. +pub type LexicalScopePath = Vec; + +/// A `LexicalScope` contains a set of all items that exist within the lexical scope via declaration or +/// importing, along with all its associated hierarchical scopes. +#[derive(Clone, Debug, Default)] +pub struct LexicalScope { + /// The set of symbols, implementations, synonyms and aliases present within this scope. + pub items: Items, + /// The set of available scopes defined inside this scope's hierarchy. + pub children: Vec, + /// The parent scope associated with this scope. Will be None for a root scope. + pub parent: Option, +} + /// The set of items that exist within some lexical scope via declaration or importing. #[derive(Clone, Debug, Default)] pub struct Items { diff --git a/sway-core/src/semantic_analysis/namespace/mod.rs b/sway-core/src/semantic_analysis/namespace/mod.rs index c43ff29f874..bf6a1875bf7 100644 --- a/sway-core/src/semantic_analysis/namespace/mod.rs +++ b/sway-core/src/semantic_analysis/namespace/mod.rs @@ -1,4 +1,4 @@ -mod items; +mod lexical_scope; mod module; #[allow(clippy::module_inception)] mod namespace; @@ -6,7 +6,7 @@ mod root; mod submodule_namespace; mod trait_map; -pub use items::Items; +pub use lexical_scope::{Items, LexicalScope, LexicalScopeId, LexicalScopePath}; pub use module::Module; pub use namespace::Namespace; pub use namespace::TryInsertingTraitImplOnFailure; diff --git a/sway-core/src/semantic_analysis/namespace/module.rs b/sway-core/src/semantic_analysis/namespace/module.rs index aac01ab7624..15318aecda2 100644 --- a/sway-core/src/semantic_analysis/namespace/module.rs +++ b/sway-core/src/semantic_analysis/namespace/module.rs @@ -12,10 +12,10 @@ use crate::{ }; use super::{ - items::{GlobImport, Items, SymbolMap}, + lexical_scope::{GlobImport, Items, LexicalScope, SymbolMap}, root::Root, trait_map::TraitMap, - ModuleName, Path, PathBuf, + LexicalScopeId, ModuleName, Path, PathBuf, }; use sway_ast::ItemConst; @@ -43,8 +43,10 @@ pub struct Module { /// /// Note that we *require* this map to be ordered to produce deterministic codegen results. pub(crate) submodules: im::OrdMap, - /// The set of symbols, implementations, synonyms and aliases present within this module. - items: Items, + /// Keeps all lexical scopes associated with this module. + pub lexical_scopes: Vec, + /// Current lexical scope id in the lexical scope hierarchy stack. + pub current_lexical_scope_id: LexicalScopeId, /// Name of the module, package name for root module, module name for other modules. /// Module name used is the same as declared in `mod name;`. pub name: Option, @@ -67,7 +69,8 @@ impl Default for Module { Self { visibility: Visibility::Private, submodules: Default::default(), - items: Default::default(), + lexical_scopes: vec![LexicalScope::default()], + current_lexical_scope_id: 0, name: Default::default(), span: Default::default(), is_external: Default::default(), @@ -176,7 +179,7 @@ impl Module { compiled_constants.insert(name, typed_decl); let mut ret = Self::default(); - ret.items.symbols = compiled_constants; + ret.current_lexical_scope_mut().items.symbols = compiled_constants; Ok(ret) } @@ -228,14 +231,54 @@ impl Module { } } - /// The collection of items declared by this module - pub fn items(&self) -> &Items { - &self.items + /// Returns the current lexical scope associated with this module. + fn current_lexical_scope(&self) -> &LexicalScope { + self.lexical_scopes + .get(self.current_lexical_scope_id) + .unwrap() } - /// The mutable collection of items declared by this module - pub fn items_mut(&mut self) -> &mut Items { - &mut self.items + /// Returns the mutable current lexical scope associated with this module. + fn current_lexical_scope_mut(&mut self) -> &mut LexicalScope { + self.lexical_scopes + .get_mut(self.current_lexical_scope_id) + .unwrap() + } + + /// The collection of items declared by this module's root lexical scope. + pub fn current_items(&self) -> &Items { + &self.current_lexical_scope().items + } + + /// The mutable collection of items declared by this module's root lexical scope. + pub fn current_items_mut(&mut self) -> &mut Items { + &mut self.current_lexical_scope_mut().items + } + + pub fn current_lexical_scope_id(&self) -> LexicalScopeId { + self.current_lexical_scope_id + } + + /// Pushes a new scope to the module's lexical scope hierarchy. + pub fn push_new_lexical_scope(&mut self) -> LexicalScopeId { + let previous_scope_id = self.current_lexical_scope_id(); + let new_scoped_id = { + self.lexical_scopes.push(LexicalScope { + parent: Some(previous_scope_id), + ..Default::default() + }); + self.current_lexical_scope_id() + }; + let previous_scope = self.lexical_scopes.get_mut(previous_scope_id).unwrap(); + previous_scope.children.push(new_scoped_id); + self.current_lexical_scope_id = new_scoped_id; + new_scoped_id + } + + /// Pops the current scope from the module's lexical scope hierarchy. + pub fn pop_lexical_scope(&mut self) { + let parent_scope_id = self.current_lexical_scope().parent; + self.current_lexical_scope_id = parent_scope_id.unwrap_or(0); } /// Given a path to a `src` module, create synonyms to every symbol in that module to the given @@ -258,9 +301,9 @@ impl Module { let src_mod = self.check_submodule(handler, src)?; - let implemented_traits = src_mod.items().implemented_traits.clone(); + let implemented_traits = src_mod.current_items().implemented_traits.clone(); let mut symbols_and_decls = vec![]; - for (symbol, decl) in src_mod.items().symbols.iter() { + for (symbol, decl) in src_mod.current_items().symbols.iter() { if is_ancestor(src, dst) || decl.visibility(decl_engine).is_public() { symbols_and_decls.push((symbol.clone(), decl.clone())); } @@ -268,11 +311,11 @@ impl Module { let dst_mod = &mut self[dst]; dst_mod - .items_mut() + .current_items_mut() .implemented_traits .extend(implemented_traits, engines); for symbol_and_decl in symbols_and_decls { - dst_mod.items_mut().use_synonyms.insert( + dst_mod.current_items_mut().use_synonyms.insert( symbol_and_decl.0, ( src.to_vec(), @@ -306,15 +349,15 @@ impl Module { let src_mod = self.check_submodule(handler, src)?; - let implemented_traits = src_mod.items().implemented_traits.clone(); - let use_synonyms = src_mod.items().use_synonyms.clone(); + let implemented_traits = src_mod.current_items().implemented_traits.clone(); + let use_synonyms = src_mod.current_items().use_synonyms.clone(); let mut symbols_and_decls = src_mod - .items() + .current_items() .use_synonyms .iter() .map(|(symbol, (_, _, decl, _))| (symbol.clone(), decl.clone())) .collect::>(); - for (symbol, decl) in src_mod.items().symbols.iter() { + for (symbol, decl) in src_mod.current_items().symbols.iter() { if is_ancestor(src, dst) || decl.visibility(decl_engine).is_public() { symbols_and_decls.push((symbol.clone(), decl.clone())); } @@ -340,13 +383,13 @@ impl Module { let dst_mod = &mut self[dst]; dst_mod - .items_mut() + .current_items_mut() .implemented_traits .extend(implemented_traits, engines); let mut try_add = |symbol, path, decl: ty::TyDecl| { dst_mod - .items_mut() + .current_items_mut() .use_synonyms .insert(symbol, (path, GlobImport::Yes, decl, is_src_absolute)); }; @@ -407,7 +450,7 @@ impl Module { let src_mod = self.check_submodule(handler, src)?; let mut impls_to_insert = TraitMap::default(); - match src_mod.items().symbols.get(item).cloned() { + match src_mod.current_items().symbols.get(item).cloned() { Some(decl) => { if !decl.visibility(decl_engine).is_public() && !is_ancestor(src, dst) { handler.emit_err(CompileError::ImportPrivateSymbol { @@ -420,7 +463,7 @@ impl Module { if let Ok(type_id) = decl.return_type(&Handler::default(), engines) { impls_to_insert.extend( src_mod - .items() + .current_items() .implemented_traits .filter_by_type_item_import(type_id, engines), engines, @@ -433,7 +476,7 @@ impl Module { // this is okay for now but we'll need to device some mechanism to collect all available trait impls impls_to_insert.extend( src_mod - .items() + .current_items() .implemented_traits .filter_by_trait_decl_span(decl_span), engines, @@ -442,11 +485,12 @@ impl Module { // no matter what, import it this way though. let dst_mod = &mut self[dst]; let add_synonym = |name| { - if let Some((_, GlobImport::No, _, _)) = dst_mod.items().use_synonyms.get(name) + if let Some((_, GlobImport::No, _, _)) = + dst_mod.current_items().use_synonyms.get(name) { handler.emit_err(CompileError::ShadowsOtherSymbol { name: name.into() }); } - dst_mod.items_mut().use_synonyms.insert( + dst_mod.current_items_mut().use_synonyms.insert( name.clone(), (src.to_vec(), GlobImport::No, decl, is_src_absolute), ); @@ -455,7 +499,7 @@ impl Module { Some(alias) => { add_synonym(&alias); dst_mod - .items_mut() + .current_items_mut() .use_aliases .insert(alias.as_str().to_string(), item.clone()); } @@ -472,7 +516,7 @@ impl Module { let dst_mod = &mut self[dst]; dst_mod - .items_mut() + .current_items_mut() .implemented_traits .extend(impls_to_insert, engines); @@ -499,7 +543,7 @@ impl Module { let decl_engine = engines.de(); let src_mod = self.check_submodule(handler, src)?; - match src_mod.items().symbols.get(enum_name).cloned() { + match src_mod.current_items().symbols.get(enum_name).cloned() { Some(decl) => { if !decl.visibility(decl_engine).is_public() && !is_ancestor(src, dst) { handler.emit_err(CompileError::ImportPrivateSymbol { @@ -528,13 +572,13 @@ impl Module { let dst_mod = &mut self[dst]; let mut add_synonym = |name| { if let Some((_, GlobImport::No, _, _)) = - dst_mod.items().use_synonyms.get(name) + dst_mod.current_items().use_synonyms.get(name) { handler.emit_err(CompileError::ShadowsOtherSymbol { name: name.into(), }); } - dst_mod.items_mut().use_synonyms.insert( + dst_mod.current_items_mut().use_synonyms.insert( name.clone(), ( src.to_vec(), @@ -552,7 +596,7 @@ impl Module { Some(alias) => { add_synonym(&alias); dst_mod - .items_mut() + .current_items_mut() .use_aliases .insert(alias.as_str().to_string(), variant_name.clone()); } @@ -599,7 +643,7 @@ impl Module { let decl_engine = engines.de(); let src_mod = self.check_submodule(handler, src)?; - match src_mod.items().symbols.get(enum_name).cloned() { + match src_mod.current_items().symbols.get(enum_name).cloned() { Some(decl) => { if !decl.visibility(decl_engine).is_public() && !is_ancestor(src, dst) { handler.emit_err(CompileError::ImportPrivateSymbol { @@ -626,7 +670,7 @@ impl Module { // import it this way. let dst_mod = &mut self[dst]; - dst_mod.items_mut().use_synonyms.insert( + dst_mod.current_items_mut().use_synonyms.insert( variant_name.clone(), ( src.to_vec(), @@ -965,7 +1009,7 @@ impl Module { type_id }; let item_ref = self - .items() + .current_items() .implemented_traits .get_trait_item_for_type(handler, engines, symbol, type_id, as_trait)?; match item_ref { @@ -985,11 +1029,11 @@ impl Module { self_type: Option, ) -> Result { let true_symbol = self[mod_path] - .items() + .current_items() .use_aliases .get(symbol.as_str()) .unwrap_or(symbol); - match module.items().use_synonyms.get(symbol) { + match module.current_items().use_synonyms.get(symbol) { Some((_, _, decl @ ty::TyDecl::EnumVariantDecl { .. }, _)) => Ok(decl.clone()), Some((src_path, _, _, _)) if mod_path != src_path => { // If the symbol is imported, before resolving to it, @@ -1001,13 +1045,13 @@ impl Module { // - non-glob import, in which case we will already have a name clash reported // as an error, but still have to resolve to the local module symbol // if it exists. - match module.items().symbols.get(true_symbol) { + match module.current_items().symbols.get(true_symbol) { Some(decl) => Ok(decl.clone()), None => self.resolve_symbol(handler, engines, src_path, true_symbol, self_type), } } _ => module - .items() + .current_items() .check_symbol(true_symbol) .map_err(|e| handler.emit_err(e)) .cloned(), diff --git a/sway-core/src/semantic_analysis/namespace/namespace.rs b/sway-core/src/semantic_analysis/namespace/namespace.rs index 7d68e5a27d6..89d3b9bfae6 100644 --- a/sway-core/src/semantic_analysis/namespace/namespace.rs +++ b/sway-core/src/semantic_analysis/namespace/namespace.rs @@ -80,17 +80,11 @@ impl Namespace { } /// Access to the current [Module], i.e. the module at the inner `mod_path`. - /// - /// Note that the [Namespace] will automatically dereference to this [Module] when attempting - /// to call any [Module] methods. pub fn module(&self) -> &Module { &self.root.module[&self.mod_path] } /// Mutable access to the current [Module], i.e. the module at the inner `mod_path`. - /// - /// Note that the [Namespace] will automatically dereference to this [Module] when attempting - /// to call any [Module] methods. pub fn module_mut(&mut self) -> &mut Module { &mut self.root.module[&self.mod_path] } @@ -175,7 +169,7 @@ impl Namespace { ) -> Result { self.root .module - .items() + .current_items() .implemented_traits .get_trait_item_for_type(handler, engines, name, type_id, as_trait) } diff --git a/sway-core/src/semantic_analysis/type_check_context.rs b/sway-core/src/semantic_analysis/type_check_context.rs index 1df3f4b8c11..07bd60b8f7e 100644 --- a/sway-core/src/semantic_analysis/type_check_context.rs +++ b/sway-core/src/semantic_analysis/type_check_context.rs @@ -448,13 +448,16 @@ impl<'a> TypeCheckContext<'a> { name: Ident, item: TyDecl, ) -> Result<(), ErrorEmitted> { - self.namespace.module_mut().items_mut().insert_symbol( - handler, - name, - item, - self.const_shadowing_mode, - self.generic_shadowing_mode, - ) + self.namespace + .module_mut() + .current_items_mut() + .insert_symbol( + handler, + name, + item, + self.const_shadowing_mode, + self.generic_shadowing_mode, + ) } /// Get the engines needed for engine threading. @@ -947,7 +950,7 @@ impl<'a> TypeCheckContext<'a> { // grab the local items from the local module let local_items = local_module - .items() + .current_items() .get_items_for_type(self.engines, type_id); // resolve the type @@ -971,7 +974,7 @@ impl<'a> TypeCheckContext<'a> { // grab the items from where the type is declared let mut type_items = type_module - .items() + .current_items() .get_items_for_type(self.engines, type_id); let mut items = local_items; @@ -1368,7 +1371,7 @@ impl<'a> TypeCheckContext<'a> { self.namespace .module_mut() - .items_mut() + .current_items_mut() .implemented_traits .insert( handler, @@ -1404,7 +1407,7 @@ impl<'a> TypeCheckContext<'a> { self.namespace .module() - .items() + .current_items() .implemented_traits .get_items_for_type_and_trait_name_and_trait_type_arguments( self.engines, @@ -1590,7 +1593,7 @@ impl<'a> TypeCheckContext<'a> { pub(crate) fn insert_trait_implementation_for_type(&mut self, type_id: TypeId) { self.namespace .module_mut() - .items_mut() + .current_items_mut() .implemented_traits .insert_for_type(self.engines, type_id); } @@ -1615,7 +1618,7 @@ impl<'a> TypeCheckContext<'a> { self.namespace .module_mut() - .items_mut() + .current_items_mut() .implemented_traits .check_if_trait_constraints_are_satisfied_for_type( &handler, diff --git a/sway-core/src/type_system/ast_elements/type_parameter.rs b/sway-core/src/type_system/ast_elements/type_parameter.rs index f29c96cbcdf..0d20fef2c3b 100644 --- a/sway-core/src/type_system/ast_elements/type_parameter.rs +++ b/sway-core/src/type_system/ast_elements/type_parameter.rs @@ -164,20 +164,28 @@ impl TypeParameter { let name_b = Ident::new_with_override("Self".into(), self.name_ident.span()); let const_shadowing_mode = ctx.const_shadowing_mode(); let generic_shadowing_mode = ctx.generic_shadowing_mode(); - let _ = ctx.namespace.module_mut().items_mut().insert_symbol( - handler, - name_a, - type_parameter_decl.clone(), - const_shadowing_mode, - generic_shadowing_mode, - ); - let _ = ctx.namespace.module_mut().items_mut().insert_symbol( - handler, - name_b, - type_parameter_decl, - const_shadowing_mode, - generic_shadowing_mode, - ); + let _ = ctx + .namespace + .module_mut() + .current_items_mut() + .insert_symbol( + handler, + name_a, + type_parameter_decl.clone(), + const_shadowing_mode, + generic_shadowing_mode, + ); + let _ = ctx + .namespace + .module_mut() + .current_items_mut() + .insert_symbol( + handler, + name_b, + type_parameter_decl, + const_shadowing_mode, + generic_shadowing_mode, + ); } /// Type check a list of [TypeParameter] and return a new list of @@ -351,7 +359,7 @@ impl TypeParameter { if let Some(sy) = ctx .namespace .module() - .items() + .current_items() .symbols .get(&type_parameter.name_ident) { @@ -466,7 +474,7 @@ impl TypeParameter { match ctx .namespace .module_mut() - .items_mut() + .current_items_mut() .implemented_traits .check_if_trait_constraints_are_satisfied_for_type( handler, diff --git a/sway-core/src/type_system/id.rs b/sway-core/src/type_system/id.rs index 1dfbe538d6d..43ead1368a9 100644 --- a/sway-core/src/type_system/id.rs +++ b/sway-core/src/type_system/id.rs @@ -579,7 +579,7 @@ impl TypeId { let generic_trait_constraints_trait_names_and_args = ctx .namespace .module() - .items() + .current_items() .implemented_traits .get_trait_names_and_type_arguments_for_type(engines, *structure_type_id); for structure_trait_constraint in structure_trait_constraints { diff --git a/sway-lsp/src/capabilities/hover/hover_link_contents.rs b/sway-lsp/src/capabilities/hover/hover_link_contents.rs index e5c8001ebf1..2f83a46f74d 100644 --- a/sway-lsp/src/capabilities/hover/hover_link_contents.rs +++ b/sway-lsp/src/capabilities/hover/hover_link_contents.rs @@ -93,7 +93,7 @@ impl<'a> HoverLinkContents<'a> { let call_path = CallPath::from(trait_decl.name.clone()).to_fullpath(&namespace); let impl_spans = namespace .module() - .items() + .current_items() .get_impl_spans_for_trait_name(&call_path); self.add_implementations(&trait_decl.span(), impl_spans); } @@ -104,7 +104,7 @@ impl<'a> HoverLinkContents<'a> { if let Some(namespace) = self.session.namespace() { let impl_spans = namespace .module() - .items() + .current_items() .get_impl_spans_for_decl(self.engines, ty_decl); self.add_implementations(&ty_decl.span(), impl_spans); } @@ -115,7 +115,7 @@ impl<'a> HoverLinkContents<'a> { if let Some(namespace) = self.session.namespace() { let impl_spans = namespace .module() - .items() + .current_items() .get_impl_spans_for_type(self.engines, type_id); self.add_implementations(decl_span, impl_spans); } diff --git a/sway-lsp/src/core/session.rs b/sway-lsp/src/core/session.rs index a1fff539a38..e208b0e0c52 100644 --- a/sway-lsp/src/core/session.rs +++ b/sway-lsp/src/core/session.rs @@ -186,7 +186,7 @@ impl Session { if let Some(TypedAstToken::TypedFunctionDeclaration(fn_decl)) = fn_token.typed.clone() { let program = compiled_program.typed.clone()?; return Some(capabilities::completion::to_completion_items( - program.root.namespace.module().items(), + program.root.namespace.module().current_items(), &self.engines.read(), ident_to_complete, &fn_decl, diff --git a/sway-lsp/src/traverse/typed_tree.rs b/sway-lsp/src/traverse/typed_tree.rs index 5eaf5460227..a0d43315788 100644 --- a/sway-lsp/src/traverse/typed_tree.rs +++ b/sway-lsp/src/traverse/typed_tree.rs @@ -129,7 +129,7 @@ impl Parse for ty::TySideEffect { if let Some(decl_ident) = ctx .namespace .submodule(call_path) - .and_then(|module| module.items().symbols().get(item)) + .and_then(|module| module.current_items().symbols().get(item)) .and_then(|decl| decl.get_decl_ident()) { // Update the symbol kind to match the declarations symbol kind @@ -465,7 +465,7 @@ impl Parse for ty::TyExpression { if let Some(abi_def_ident) = ctx .namespace .submodule(&abi_name.prefixes) - .and_then(|module| module.items().symbols().get(&abi_name.suffix)) + .and_then(|module| module.current_items().symbols().get(&abi_name.suffix)) .and_then(|decl| decl.get_decl_ident()) { token.type_def = Some(TypeDefinition::Ident(abi_def_ident)); @@ -479,8 +479,10 @@ impl Parse for ty::TyExpression { &ctx.ident(&Ident::new(storage_access.storage_keyword_span.clone())), ) { token.typed = Some(TypedAstToken::TypedStorageAccess(storage_access.clone())); - if let Some(storage) = - ctx.namespace.items().get_declared_storage(ctx.engines.de()) + if let Some(storage) = ctx + .namespace + .current_items() + .get_declared_storage(ctx.engines.de()) { token.type_def = Some(TypeDefinition::Ident(storage.storage_keyword.clone())); @@ -497,7 +499,7 @@ impl Parse for ty::TyExpression { )); if let Some(storage_field) = ctx .namespace - .items() + .current_items() .get_declared_storage(ctx.engines.de()) .and_then(|storage| { storage @@ -1201,7 +1203,7 @@ fn collect_call_path_tree(ctx: &ParseContext, tree: &CallPathTree, type_arg: &Ty .submodule(&abi_call_path.call_path.prefixes) .and_then(|module| { module - .items() + .current_items() .symbols() .get(&abi_call_path.call_path.suffix) }) @@ -1382,7 +1384,7 @@ fn collect_trait_constraint( if let Some(trait_def_ident) = ctx .namespace .submodule(&trait_name.prefixes) - .and_then(|module| module.items().symbols().get(&trait_name.suffix)) + .and_then(|module| module.current_items().symbols().get(&trait_name.suffix)) .and_then(|decl| decl.get_decl_ident()) { token.type_def = Some(TypeDefinition::Ident(trait_def_ident));