Skip to content

Commit a3ad7a2

Browse files
committed
Remove require_module from Root
1 parent b26eaaf commit a3ad7a2

File tree

3 files changed

+34
-39
lines changed

3 files changed

+34
-39
lines changed

sway-core/src/semantic_analysis/namespace/namespace.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,20 @@ impl Namespace {
170170
handler: &Handler,
171171
path: &ModulePathBuf,
172172
) -> Result<&Module, ErrorEmitted> {
173-
self.root.require_module(handler, path)
173+
if path.is_empty() {
174+
return Err(handler.emit_err(CompileError::Internal(
175+
"Found empty absolute mod path",
176+
Span::dummy(),
177+
)));
178+
}
179+
let is_in_current_package = self.root.check_path_is_in_current_package(path);
180+
match self.module_from_absolute_path(path) {
181+
Some(module) => Ok(module),
182+
None => Err(handler.emit_err(crate::namespace::module::module_not_found(
183+
path,
184+
is_in_current_package,
185+
))),
186+
}
174187
}
175188

176189
/// Returns true if the current module being checked is a direct or indirect submodule of
@@ -369,7 +382,7 @@ impl Namespace {
369382
) -> Result<(), ErrorEmitted> {
370383
self.check_module_visibility(handler, src)?;
371384

372-
let src_mod = self.root.require_module(handler, &src.to_vec())?;
385+
let src_mod = self.require_module_from_absolute_path(handler, &src.to_vec())?;
373386

374387
let mut decls_and_item_imports = vec![];
375388

@@ -565,7 +578,7 @@ impl Namespace {
565578
) -> Result<(), ErrorEmitted> {
566579
self.check_module_visibility(handler, src)?;
567580

568-
let src_mod = self.root.require_module(handler, &src.to_vec())?;
581+
let src_mod = self.require_module_from_absolute_path(handler, &src.to_vec())?;
569582

570583
let (decl, path) = self.item_lookup(handler, engines, item, src, false)?;
571584

@@ -810,7 +823,7 @@ impl Namespace {
810823
src: &ModulePath,
811824
ignore_visibility: bool,
812825
) -> Result<(ResolvedDeclaration, ModulePathBuf), ErrorEmitted> {
813-
let src_mod = self.root.require_module(handler, &src.to_vec())?;
826+
let src_mod = self.require_module_from_absolute_path(handler, &src.to_vec())?;
814827
let src_items = src_mod.root_items();
815828

816829
let (decl, path, src_visibility) = if let Some(decl) = src_items.symbols.get(item) {

sway-core/src/semantic_analysis/namespace/root.rs

+2-28
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ use crate::{
1414
};
1515
use rustc_hash::FxHasher;
1616
use std::hash::BuildHasherDefault;
17-
use sway_error::{
18-
error::CompileError,
19-
handler::{ErrorEmitted, Handler},
20-
};
17+
use sway_error::handler::{ErrorEmitted, Handler};
2118
use sway_types::{span::Span, ProgramId};
2219

2320
#[derive(Clone, Debug)]
@@ -222,7 +219,7 @@ impl Root {
222219
self.program_id
223220
}
224221

225-
fn check_path_is_in_current_package(&self, mod_path: &ModulePathBuf) -> bool {
222+
pub(crate) fn check_path_is_in_current_package(&self, mod_path: &ModulePathBuf) -> bool {
226223
!mod_path.is_empty() && mod_path[0] == *self.current_package.name()
227224
}
228225

@@ -250,29 +247,6 @@ impl Root {
250247
}
251248
}
252249

253-
// Find module in the current environment. `mod_path` must be a fully qualified path.
254-
// Throw an error if the module doesn't exist
255-
pub(crate) fn require_module(
256-
&self,
257-
handler: &Handler,
258-
mod_path: &ModulePathBuf,
259-
) -> Result<&Module, ErrorEmitted> {
260-
if mod_path.is_empty() {
261-
return Err(handler.emit_err(CompileError::Internal(
262-
"Found empty absolute mod path",
263-
Span::dummy(),
264-
)));
265-
}
266-
let is_in_current_package = self.check_path_is_in_current_package(mod_path);
267-
match self.module_from_absolute_path(mod_path) {
268-
Some(module) => Ok(module),
269-
None => Err(handler.emit_err(crate::namespace::module::module_not_found(
270-
mod_path,
271-
is_in_current_package,
272-
))),
273-
}
274-
}
275-
276250
// Find a module in the current package. `mod_path` must be a fully qualified path
277251
pub(super) fn module_in_current_package(&self, mod_path: &ModulePathBuf) -> Option<&Module> {
278252
assert!(self.check_path_is_in_current_package(mod_path));

sway-core/src/semantic_analysis/type_resolve.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
CallPath, QualifiedCallPath,
1111
},
1212
monomorphization::type_decl_opt_to_type_id,
13-
namespace::{Module, ModulePath, ResolvedDeclaration, ResolvedTraitImplItem, Root},
13+
namespace::{Module, ModulePath, ResolvedDeclaration, ResolvedTraitImplItem},
1414
type_system::SubstTypes,
1515
EnforceTypeArguments, Engines, Namespace, SubstTypesContext, TypeId, TypeInfo,
1616
};
@@ -336,7 +336,14 @@ pub(super) fn resolve_symbol_and_mod_path(
336336
assert!(!mod_path.is_empty());
337337
let root = namespace.root_ref();
338338
if mod_path[0] == *root.current_package_name() {
339-
resolve_symbol_and_mod_path_inner(handler, engines, root, mod_path, symbol, self_type)
339+
resolve_symbol_and_mod_path_inner(
340+
handler,
341+
engines,
342+
root.current_package_root_module(),
343+
mod_path,
344+
symbol,
345+
self_type,
346+
)
340347
} else {
341348
match namespace.get_external_package(&mod_path[0].to_string()) {
342349
Some(ext_root) => {
@@ -350,7 +357,7 @@ pub(super) fn resolve_symbol_and_mod_path(
350357
resolve_symbol_and_mod_path_inner(
351358
handler,
352359
engines,
353-
ext_root,
360+
ext_root.current_package_root_module(),
354361
&new_mod_path,
355362
symbol,
356363
self_type,
@@ -367,16 +374,16 @@ pub(super) fn resolve_symbol_and_mod_path(
367374
fn resolve_symbol_and_mod_path_inner(
368375
handler: &Handler,
369376
engines: &Engines,
370-
root: &Root,
377+
root_module: &Module,
371378
mod_path: &ModulePath,
372379
symbol: &Ident,
373380
self_type: Option<TypeId>,
374381
) -> Result<(ResolvedDeclaration, Vec<Ident>), ErrorEmitted> {
375382
assert!(!mod_path.is_empty());
376-
assert!(mod_path[0] == *root.current_package_name());
383+
assert!(mod_path[0] == root_module.mod_path()[0]);
377384

378385
// This block tries to resolve associated types
379-
let mut current_module = root.current_package_root_module();
386+
let mut current_module = root_module;
380387
let mut current_mod_path = vec![mod_path[0].clone()];
381388
let mut decl_opt = None;
382389
for ident in mod_path.iter().skip(1) {
@@ -416,7 +423,8 @@ fn resolve_symbol_and_mod_path_inner(
416423
return Ok((decl, current_mod_path));
417424
}
418425

419-
root.require_module(handler, &mod_path.to_vec())
426+
root_module
427+
.lookup_submodule(handler, &mod_path[1..])
420428
.and_then(|module| {
421429
let (decl, decl_path) = module.resolve_symbol(handler, engines, symbol)?;
422430
Ok((decl, decl_path))

0 commit comments

Comments
 (0)