Skip to content

Commit 195acc6

Browse files
authored
Module refactors (#5793)
## Description Some minor cleanups and refactors to module-related code. ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
1 parent 98d8f4c commit 195acc6

File tree

17 files changed

+130
-117
lines changed

17 files changed

+130
-117
lines changed

sway-core/src/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tools related to handling/recovering from Sway compile errors and reporting them to the user.
22
3-
use crate::{language::parsed::VariableDeclaration, namespace::Path, Namespace};
3+
use crate::{language::parsed::VariableDeclaration, namespace::ModulePath, Namespace};
44

55
/// Acts as the result of parsing `Declaration`s, `Expression`s, etc.
66
/// Some `Expression`s need to be able to create `VariableDeclaration`s,
@@ -31,7 +31,7 @@ impl<T> ParserLifter<T> {
3131
/// this function returns true if the programmer can change that module.
3232
pub(crate) fn module_can_be_changed(
3333
issue_namespace: &Namespace,
34-
absolute_module_path: &Path,
34+
absolute_module_path: &ModulePath,
3535
) -> bool {
3636
// For now, we assume that the programmers can change the module
3737
// if the module is in the same package where the issue is.

sway-core/src/language/call_path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use sway_error::{
1919
};
2020
use sway_types::{span::Span, Spanned};
2121

22-
use super::parsed::QualifiedPathRootTypes;
22+
use super::parsed::QualifiedPathType;
2323

2424
#[derive(Clone, Debug)]
2525
pub struct CallPathTree {
@@ -70,7 +70,7 @@ impl OrdWithEngines for CallPathTree {
7070

7171
pub struct QualifiedCallPath {
7272
pub call_path: CallPath,
73-
pub qualified_path_root: Option<Box<QualifiedPathRootTypes>>,
73+
pub qualified_path_root: Option<Box<QualifiedPathType>>,
7474
}
7575

7676
impl std::convert::From<Ident> for QualifiedCallPath {

sway-core/src/language/parsed/expression/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ impl Spanned for AmbiguousSuffix {
112112
}
113113

114114
#[derive(Debug, Clone)]
115-
pub struct QualifiedPathRootTypes {
115+
pub struct QualifiedPathType {
116116
pub ty: TypeArgument,
117117
pub as_trait: TypeId,
118118
pub as_trait_span: Span,
119119
}
120120

121-
impl HashWithEngines for QualifiedPathRootTypes {
121+
impl HashWithEngines for QualifiedPathType {
122122
fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
123-
let QualifiedPathRootTypes {
123+
let QualifiedPathType {
124124
ty,
125125
as_trait,
126126
// ignored fields
@@ -131,10 +131,10 @@ impl HashWithEngines for QualifiedPathRootTypes {
131131
}
132132
}
133133

134-
impl EqWithEngines for QualifiedPathRootTypes {}
135-
impl PartialEqWithEngines for QualifiedPathRootTypes {
134+
impl EqWithEngines for QualifiedPathType {}
135+
impl PartialEqWithEngines for QualifiedPathType {
136136
fn eq(&self, other: &Self, engines: &Engines) -> bool {
137-
let QualifiedPathRootTypes {
137+
let QualifiedPathType {
138138
ty,
139139
as_trait,
140140
// ignored fields
@@ -148,15 +148,15 @@ impl PartialEqWithEngines for QualifiedPathRootTypes {
148148
}
149149
}
150150

151-
impl OrdWithEngines for QualifiedPathRootTypes {
151+
impl OrdWithEngines for QualifiedPathType {
152152
fn cmp(&self, other: &Self, engines: &Engines) -> Ordering {
153-
let QualifiedPathRootTypes {
153+
let QualifiedPathType {
154154
ty: l_ty,
155155
as_trait: l_as_trait,
156156
// ignored fields
157157
as_trait_span: _,
158158
} = self;
159-
let QualifiedPathRootTypes {
159+
let QualifiedPathType {
160160
ty: r_ty,
161161
as_trait: r_as_trait,
162162
// ignored fields
@@ -171,7 +171,7 @@ impl OrdWithEngines for QualifiedPathRootTypes {
171171
}
172172
}
173173

174-
impl DisplayWithEngines for QualifiedPathRootTypes {
174+
impl DisplayWithEngines for QualifiedPathType {
175175
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
176176
write!(
177177
f,
@@ -182,15 +182,15 @@ impl DisplayWithEngines for QualifiedPathRootTypes {
182182
}
183183
}
184184

185-
impl DebugWithEngines for QualifiedPathRootTypes {
185+
impl DebugWithEngines for QualifiedPathType {
186186
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
187187
write!(f, "{}", engines.help_out(self),)
188188
}
189189
}
190190

191191
#[derive(Debug, Clone)]
192192
pub struct AmbiguousPathExpression {
193-
pub qualified_path_root: Option<QualifiedPathRootTypes>,
193+
pub qualified_path_root: Option<QualifiedPathType>,
194194
pub call_path_binding: TypeBinding<CallPath<AmbiguousSuffix>>,
195195
pub args: Vec<Expression>,
196196
}

sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1159,12 +1159,12 @@ impl ty::TyExpression {
11591159
}: TypeBinding<CallPath<AmbiguousSuffix>>,
11601160
span: Span,
11611161
args: Vec<Expression>,
1162-
qualified_path_root: Option<QualifiedPathRootTypes>,
1162+
qualified_path_root: Option<QualifiedPathType>,
11631163
) -> Result<ty::TyExpression, ErrorEmitted> {
11641164
let engines = ctx.engines;
11651165
let decl_engine = engines.de();
11661166

1167-
if let Some(QualifiedPathRootTypes { ty, as_trait, .. }) = qualified_path_root.clone() {
1167+
if let Some(QualifiedPathType { ty, as_trait, .. }) = qualified_path_root.clone() {
11681168
let method_name_binding = if !prefixes.is_empty() || before.is_some() {
11691169
let mut prefixes_and_before = prefixes.clone();
11701170
if let Some(before) = before {
@@ -1282,7 +1282,10 @@ impl ty::TyExpression {
12821282
path.push(before.inner.clone());
12831283
let not_module = {
12841284
let h = Handler::default();
1285-
ctx.namespace().module().check_submodule(&h, &path).is_err()
1285+
ctx.namespace()
1286+
.module()
1287+
.lookup_submodule(&h, &path)
1288+
.is_err()
12861289
};
12871290

12881291
// Not a module? Not a `Enum::Variant` either?
@@ -1402,7 +1405,7 @@ impl ty::TyExpression {
14021405
let call_path_binding = unknown_call_path_binding.clone();
14031406
ctx.namespace()
14041407
.module()
1405-
.check_submodule(
1408+
.lookup_submodule(
14061409
&module_probe_handler,
14071410
&[
14081411
call_path_binding.inner.call_path.prefixes,

sway-core/src/semantic_analysis/ast_node/expression/typed_expression/method_application.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,9 @@ pub(crate) fn resolve_method_name(
635635
// find the module that the symbol is in
636636
let type_info_prefix = ctx
637637
.namespace()
638-
.find_module_path(&call_path_binding.inner.prefixes);
638+
.prepend_module_path(&call_path_binding.inner.prefixes);
639639
ctx.namespace()
640-
.check_absolute_path_to_submodule(handler, &type_info_prefix)?;
640+
.lookup_submodule_from_absolute_path(handler, &type_info_prefix)?;
641641

642642
// find the method
643643
let decl_ref = ctx.find_method_for_type(
@@ -656,7 +656,7 @@ pub(crate) fn resolve_method_name(
656656
MethodName::FromTrait { call_path } => {
657657
// find the module that the symbol is in
658658
let module_path = if !call_path.is_absolute {
659-
ctx.namespace().find_module_path(&call_path.prefixes)
659+
ctx.namespace().prepend_module_path(&call_path.prefixes)
660660
} else {
661661
let mut module_path = call_path.prefixes.clone();
662662
if let (Some(root_mod), Some(root_name)) = (
@@ -692,7 +692,7 @@ pub(crate) fn resolve_method_name(
692692
}
693693
MethodName::FromModule { method_name } => {
694694
// find the module that the symbol is in
695-
let module_path = ctx.namespace().find_module_path(vec![]);
695+
let module_path = ctx.namespace().prepend_module_path(vec![]);
696696

697697
// find the type of the first argument
698698
let type_id = arguments_types

sway-core/src/semantic_analysis/ast_node/expression/typed_expression/struct_instantiation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ pub(crate) fn struct_instantiation(
7878
};
7979

8080
// find the module that the struct decl is in
81-
let type_info_prefix = ctx.namespace().find_module_path(prefixes);
81+
let type_info_prefix = ctx.namespace().prepend_module_path(prefixes);
8282
ctx.namespace()
83-
.check_absolute_path_to_submodule(handler, &type_info_prefix)?;
83+
.lookup_submodule_from_absolute_path(handler, &type_info_prefix)?;
8484

8585
// resolve the type of the struct decl
8686
let type_id = ctx

sway-core/src/semantic_analysis/ast_node/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl ty::TyAstNode {
6262
let path = if is_external || a.is_absolute {
6363
a.call_path.clone()
6464
} else {
65-
ctx.namespace().find_module_path(&a.call_path)
65+
ctx.namespace().prepend_module_path(&a.call_path)
6666
};
6767
let _ = match a.import_type {
6868
ImportType::Star => {

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ pub(super) use trait_map::TraitMap;
1717

1818
use sway_types::Ident;
1919

20-
pub type Path = [Ident];
21-
pub type PathBuf = Vec<Ident>;
22-
2320
type ModuleName = String;
24-
pub type ModulePath = PathBuf;
21+
pub type ModulePath = [Ident];
22+
pub type ModulePathBuf = Vec<Ident>;

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

+23-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
use super::{
1515
lexical_scope::{Items, LexicalScope, SymbolMap},
1616
root::Root,
17-
LexicalScopeId, ModuleName, ModulePath, Path,
17+
LexicalScopeId, ModuleName, ModulePath, ModulePathBuf,
1818
};
1919

2020
use sway_ast::ItemConst;
@@ -59,7 +59,7 @@ pub struct Module {
5959
///
6060
/// When this is the root module, this is equal to `[]`. When this is a
6161
/// submodule of the root called "foo", this would be equal to `[foo]`.
62-
pub mod_path: ModulePath,
62+
pub(crate) mod_path: ModulePathBuf,
6363
}
6464

6565
impl Default for Module {
@@ -78,6 +78,14 @@ impl Default for Module {
7878
}
7979

8080
impl Module {
81+
pub fn mod_path(&self) -> &ModulePath {
82+
self.mod_path.as_slice()
83+
}
84+
85+
pub fn mod_path_buf(&self) -> ModulePathBuf {
86+
self.mod_path.clone()
87+
}
88+
8189
/// `contract_id_value` is injected here via forc-pkg when producing the `dependency_namespace` for a contract which has tests enabled.
8290
/// This allows us to provide a contract's `CONTRACT_ID` constant to its own unit tests.
8391
///
@@ -200,7 +208,7 @@ impl Module {
200208
}
201209

202210
/// Lookup the submodule at the given path.
203-
pub fn submodule(&self, path: &Path) -> Option<&Module> {
211+
pub fn submodule(&self, path: &ModulePath) -> Option<&Module> {
204212
let mut module = self;
205213
for ident in path.iter() {
206214
match module.submodules.get(ident.as_str()) {
@@ -212,7 +220,7 @@ impl Module {
212220
}
213221

214222
/// Unique access to the submodule at the given path.
215-
pub fn submodule_mut(&mut self, path: &Path) -> Option<&mut Module> {
223+
pub fn submodule_mut(&mut self, path: &ModulePath) -> Option<&mut Module> {
216224
let mut module = self;
217225
for ident in path.iter() {
218226
match module.submodules.get_mut(ident.as_str()) {
@@ -226,7 +234,7 @@ impl Module {
226234
/// Lookup the submodule at the given path.
227235
///
228236
/// This should be used rather than `Index` when we don't yet know whether the module exists.
229-
pub(crate) fn check_submodule(
237+
pub(crate) fn lookup_submodule(
230238
&self,
231239
handler: &Handler,
232240
path: &[Ident],
@@ -295,7 +303,7 @@ impl Module {
295303
&self,
296304
handler: &Handler,
297305
engines: &Engines,
298-
mod_path: &Path,
306+
mod_path: &ModulePath,
299307
call_path: &CallPath,
300308
self_type: Option<TypeId>,
301309
) -> Result<ty::TyDecl, ErrorEmitted> {
@@ -308,7 +316,7 @@ impl Module {
308316
&self,
309317
handler: &Handler,
310318
engines: &Engines,
311-
mod_path: &Path,
319+
mod_path: &ModulePath,
312320
call_path: &CallPath,
313321
self_type: Option<TypeId>,
314322
) -> Result<(ty::TyDecl, Vec<Ident>), ErrorEmitted> {
@@ -397,7 +405,7 @@ impl Module {
397405
&self,
398406
handler: &Handler,
399407
engines: &Engines,
400-
mod_path: &Path,
408+
mod_path: &ModulePath,
401409
symbol: &Ident,
402410
self_type: Option<TypeId>,
403411
) -> Result<ty::TyDecl, ErrorEmitted> {
@@ -410,7 +418,7 @@ impl Module {
410418
&self,
411419
handler: &Handler,
412420
engines: &Engines,
413-
mod_path: &Path,
421+
mod_path: &ModulePath,
414422
symbol: &Ident,
415423
self_type: Option<TypeId>,
416424
) -> Result<(ty::TyDecl, Vec<Ident>), ErrorEmitted> {
@@ -448,7 +456,7 @@ impl Module {
448456
return Ok((decl, current_mod_path));
449457
}
450458

451-
self.check_submodule(handler, mod_path).and_then(|module| {
459+
self.lookup_submodule(handler, mod_path).and_then(|module| {
452460
let decl =
453461
self.resolve_symbol_helper(handler, engines, mod_path, symbol, module, self_type)?;
454462
Ok((decl, mod_path.to_vec()))
@@ -589,7 +597,7 @@ impl Module {
589597
&self,
590598
handler: &Handler,
591599
engines: &Engines,
592-
mod_path: &Path,
600+
mod_path: &ModulePath,
593601
symbol: &Ident,
594602
module: &Module,
595603
self_type: Option<TypeId>,
@@ -625,16 +633,16 @@ impl Module {
625633
}
626634
}
627635

628-
impl<'a> std::ops::Index<&'a Path> for Module {
636+
impl<'a> std::ops::Index<&'a ModulePath> for Module {
629637
type Output = Module;
630-
fn index(&self, path: &'a Path) -> &Self::Output {
638+
fn index(&self, path: &'a ModulePath) -> &Self::Output {
631639
self.submodule(path)
632640
.unwrap_or_else(|| panic!("no module for the given path {path:?}"))
633641
}
634642
}
635643

636-
impl<'a> std::ops::IndexMut<&'a Path> for Module {
637-
fn index_mut(&mut self, path: &'a Path) -> &mut Self::Output {
644+
impl<'a> std::ops::IndexMut<&'a ModulePath> for Module {
645+
fn index_mut(&mut self, path: &'a ModulePath) -> &mut Self::Output {
638646
self.submodule_mut(path)
639647
.unwrap_or_else(|| panic!("no module for the given path {path:?}"))
640648
}

0 commit comments

Comments
 (0)