Skip to content

Commit

Permalink
Remove Namespace.lookup_submodule_from_absolute_path.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Oct 21, 2024
1 parent f7d4f75 commit 52799e0
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -787,20 +787,17 @@ pub(crate) fn resolve_method_name(
});

// find the module that the symbol is in
let type_info_prefix = ctx
let mod_path = ctx
.namespace()
.prepend_module_path(&call_path_binding.inner.prefixes);
ctx.namespace().lookup_submodule_from_absolute_path(
handler,
engines,
&type_info_prefix,
)?;
.lookup_submodule(handler, engines, &call_path_binding.inner.prefixes)?
.mod_path()
.to_vec();

// find the method
let decl_ref = ctx.find_method_for_type(
handler,
type_id,
&type_info_prefix,
&mod_path,
method_name,
ctx.type_annotation(),
&arguments_types,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ pub(crate) fn struct_instantiation(
};

// find the module that the struct decl is in
let type_info_prefix = ctx.namespace().prepend_module_path(prefixes);
ctx.namespace()
.lookup_submodule_from_absolute_path(handler, engines, &type_info_prefix)?;
let module = ctx
.namespace()
.lookup_submodule(handler, engines, prefixes)?;

// resolve the type of the struct decl
let type_id = ctx
Expand All @@ -90,7 +90,7 @@ pub(crate) fn struct_instantiation(
type_engine.insert(engines, type_info, suffix.span().source_id()),
inner_span,
EnforceTypeArguments::No,
Some(&type_info_prefix),
Some(module.mod_path()),
)
.unwrap_or_else(|err| type_engine.insert(engines, TypeInfo::ErrorRecovery(err), None));

Expand Down
7 changes: 5 additions & 2 deletions sway-core/src/semantic_analysis/namespace/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,16 @@ impl Namespace {
.unwrap()
}

pub fn lookup_submodule_from_absolute_path(
pub(crate) fn lookup_submodule(
&self,
handler: &Handler,
engines: &Engines,
path: &ModulePath,
) -> Result<&Module, ErrorEmitted> {
self.root.module.lookup_submodule(handler, engines, path)
let submod_path = self.prepend_module_path(path);
self.root
.module
.lookup_submodule(handler, engines, &submod_path)
}

/// Short-hand for calling [Root::resolve_symbol] on `root` with the `mod_path`.
Expand Down
11 changes: 11 additions & 0 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,17 @@ impl Root {
Ok(())
}

////// MODULE RESOLUTION //////

pub fn lookup_submodule(
&self,
handler: &Handler,
engines: &Engines,
path: &ModulePath,
) -> Result<&Module, ErrorEmitted> {
self.module.lookup_submodule(handler, engines, path)
}

////// NAME RESOLUTION //////

/// Resolve a symbol that is potentially prefixed with some path, e.g. `foo::bar::symbol`.
Expand Down
9 changes: 4 additions & 5 deletions sway-core/src/semantic_analysis/symbol_resolve_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,10 @@ impl<'a> SymbolResolveContext<'a> {
// check the visibility of the call path elements
// we don't check the first prefix because direct children are always accessible
for prefix in iter_prefixes(&call_path.prefixes).skip(1) {
let module = self.namespace().lookup_submodule_from_absolute_path(
handler,
self.engines(),
prefix,
)?;
let module =
self.namespace()
.root()
.lookup_submodule(handler, self.engines(), prefix)?;
if module.visibility().is_private() {
let prefix_last = prefix[prefix.len() - 1].clone();
handler.emit_err(CompileError::ImportPrivateModule {
Expand Down
15 changes: 5 additions & 10 deletions sway-core/src/semantic_analysis/type_check_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,11 +725,7 @@ impl<'a> TypeCheckContext<'a> {
}

// grab the local module
let local_module = self.namespace().lookup_submodule_from_absolute_path(
handler,
self.engines(),
&self.namespace().mod_path,
)?;
let local_module = self.namespace().module(self.engines);

// grab the local items from the local module
let local_items = local_module
Expand All @@ -752,11 +748,10 @@ impl<'a> TypeCheckContext<'a> {
.unwrap_or_else(|err| type_engine.insert(self.engines, TypeInfo::ErrorRecovery(err), None));

// grab the module where the type itself is declared
let type_module = self.namespace().lookup_submodule_from_absolute_path(
handler,
self.engines(),
item_prefix,
)?;
let type_module =
self.namespace()
.root()
.lookup_submodule(handler, self.engines(), item_prefix)?;

// grab the items from where the type is declared
let mut type_items = type_module
Expand Down
4 changes: 3 additions & 1 deletion sway-core/src/semantic_analysis/type_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ pub fn resolve_call_path(
// check the visibility of the call path elements
// we don't check the first prefix because direct children are always accessible
for prefix in iter_prefixes(&call_path.prefixes).skip(1) {
let module = namespace.lookup_submodule_from_absolute_path(handler, engines, prefix)?;
let module = namespace
.root()
.lookup_submodule(handler, engines, prefix)?;
if module.visibility().is_private() {
let prefix_last = prefix[prefix.len() - 1].clone();
handler.emit_err(CompileError::ImportPrivateModule {
Expand Down
8 changes: 4 additions & 4 deletions sway-core/src/type_system/ast_elements/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ impl TypeBinding<CallPath<(TypeInfo, Ident)>> {
let type_info_span = type_ident.span();

// find the module that the symbol is in
let type_info_prefix = ctx.namespace().prepend_module_path(&self.inner.prefixes);
ctx.namespace()
.lookup_submodule_from_absolute_path(handler, engines, &type_info_prefix)?;
let module = ctx
.namespace()
.lookup_submodule(handler, engines, &self.inner.prefixes)?;

// create the type info object
let type_info = type_info.apply_type_arguments(
Expand All @@ -236,7 +236,7 @@ impl TypeBinding<CallPath<(TypeInfo, Ident)>> {
type_engine.insert(engines, type_info, type_info_span.source_id()),
&type_info_span,
EnforceTypeArguments::No,
Some(&type_info_prefix),
Some(module.mod_path()),
)
.unwrap_or_else(|err| type_engine.insert(engines, TypeInfo::ErrorRecovery(err), None));

Expand Down

0 comments on commit 52799e0

Please sign in to comment.