Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the module structure of the namespace module #6291

Open
wants to merge 106 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
8aa2e55
Make module name mandatory and private
jjcnn Jul 15, 2024
aa9fec4
Merge branch 'master' of github.com:FuelLabs/sway into jjcnn/encapsul…
jjcnn Jul 15, 2024
158dfd9
Encapsulate visiblity and span fiels of Module
jjcnn Jul 16, 2024
9c301f2
Merge branch 'master' into jjcnn/encapsulate-namespace-module
jjcnn Jul 16, 2024
b87dcd1
Fix sway-core tests
jjcnn Jul 16, 2024
329fecb
clippy; fmt
jjcnn Jul 16, 2024
19ecf89
Save game
jjcnn Jul 21, 2024
d7061c1
Rebase
jjcnn Jul 22, 2024
960b2b5
Namespace module builds, interface defined
jjcnn Jul 22, 2024
74ca425
Change to new namespace interface
jjcnn Jul 23, 2024
3f802bb
Merge branch 'master' of github.com:FuelLabs/sway into jjcnn/move-ext…
jjcnn Jul 25, 2024
428996a
Change externals to be packages rather than modules
jjcnn Jul 29, 2024
1b06988
forc-pkg and sway-lsp usage of namespace module updated
jjcnn Jul 31, 2024
fcf2644
Updated test harness
jjcnn Aug 1, 2024
20863df
Merge branch 'master' of github.com:FuelLabs/sway into jjcnn/move-ext…
jjcnn Aug 1, 2024
d463cf8
Simplify compilation of core during testing
jjcnn Aug 1, 2024
93aab4d
Ensure the preludes are imported into all modules
jjcnn Aug 2, 2024
a63db83
Root module is not visited before its submodules
jjcnn Aug 2, 2024
3fc997f
Remove SubmoduleNamespace
jjcnn Aug 6, 2024
a76a1ee
Rename is_absolute to is_relative_to_package_root, convert relative p…
jjcnn Aug 7, 2024
019c089
Pop submodule when done typechecking it
jjcnn Aug 7, 2024
3a45380
Fix various path issues
jjcnn Aug 12, 2024
e6bbc60
Fix require_module
jjcnn Aug 13, 2024
4f80b55
Fix resolution of ambiguous paths
jjcnn Aug 15, 2024
a17290e
Merge
jjcnn Aug 16, 2024
c028199
Changed CallPath.is_absolute to CallPath.callpath_type
jjcnn Aug 16, 2024
cacbd4e
Merge
jjcnn Sep 3, 2024
51a673c
Fix path in shadow check in asm blocks
jjcnn Sep 5, 2024
b2b2893
Merge
jjcnn Sep 13, 2024
b1ec95c
Do not require core as external
jjcnn Sep 13, 2024
8df8f4b
prepend_module_path(vec[]) is the same as current_mod_path()
jjcnn Sep 16, 2024
285bf01
Merge branch 'master' of github.com:FuelLabs/sway into jjcnn/move-ext…
jjcnn Sep 16, 2024
6b2e03c
Add enum name to enum variant paths
jjcnn Oct 4, 2024
35f0ba4
Partial merge of master
jjcnn Oct 11, 2024
eb34879
Merge finalized
jjcnn Oct 11, 2024
7ec3af9
Fixed resolution of enum names
jjcnn Oct 15, 2024
512ce8a
Renamed CallPathType::Resolved to CallPathType::Full
jjcnn Oct 15, 2024
6185867
Make CallPath::to_fullpath work on all cloneable suffixes
jjcnn Oct 18, 2024
2a4ffe4
Temporary save
jjcnn Oct 18, 2024
1c2b602
Fix visibility check for method applications within the same module
jjcnn Oct 18, 2024
f4fffc8
Fix mod path for abis
jjcnn Oct 20, 2024
a1740d6
Merge
jjcnn Oct 21, 2024
ecce5d7
Fix resolution of enum variant paths
jjcnn Oct 22, 2024
eda97db
Fix resolution of absolute and relative paths for modules
jjcnn Oct 22, 2024
64c98ba
Add comment about module paths during path resolution
jjcnn Oct 22, 2024
f799d08
Merge
jjcnn Oct 24, 2024
6d50867
Merge
jjcnn Oct 24, 2024
e521b9d
Fix privacy check for module paths
jjcnn Oct 29, 2024
246b25c
Fix privacy check for module paths
jjcnn Oct 29, 2024
3667762
Merge
jjcnn Oct 29, 2024
57afd26
Merge
jjcnn Nov 27, 2024
6907271
Fix SymbolWithMultipleBindings error when path is in the current package
jjcnn Nov 28, 2024
558326b
Fix resolve_call_path for explicit paths
jjcnn Dec 5, 2024
26385f1
Merge
jjcnn Dec 9, 2024
7e70ea3
Fix resolution of full path in the source program
jjcnn Dec 10, 2024
707c307
Fix privacy check in type_resolve.rs
jjcnn Dec 10, 2024
395459f
Rename borrow_root to root_ref
jjcnn Dec 10, 2024
81b1637
Remove unused import of iter_prefixes
jjcnn Dec 10, 2024
de466e0
Fix submodule check
jjcnn Dec 13, 2024
248bc7c
Merge branch 'jjcnn/move-external-modules-to-root' of github.com:Fuel…
jjcnn Dec 13, 2024
68f0acc
Fix check for variant imports when path length is 2
jjcnn Dec 13, 2024
ee52cc7
Fix mod_path for abis
jjcnn Dec 13, 2024
09c5c22
Fix path in typecheck of TypeBinding
jjcnn Dec 14, 2024
3a332f3
Fix to_fullpath for the Contract type
jjcnn Dec 14, 2024
4bbbfa4
Revert "Fix to_fullpath for the Contract type"
jjcnn Dec 16, 2024
71b5054
Fix check of maybe_enum_variant
jjcnn Dec 16, 2024
a43943a
Handle raw_slice correctly
jjcnn Dec 17, 2024
6f0590b
Proper fix of paths with builtin types
jjcnn Dec 19, 2024
9920d77
Ensure the trait declaration path is used when performing lookups in …
jjcnn Dec 21, 2024
be3aa43
Fix remaining trait stub method issues
jjcnn Dec 21, 2024
8ae7754
Use canonical path when checking type constraints
jjcnn Dec 22, 2024
a29abef
Remove internal package name from ModuleNotFound error
jjcnn Dec 22, 2024
12a9e90
Fixed visibility issues in tests
jjcnn Dec 23, 2024
171218a
Fix typecheck of variant imports
jjcnn Dec 23, 2024
5731bf6
Avoid checking for module privacy for non-module parts of paths
jjcnn Dec 23, 2024
2be2249
Use canonical paths for trait names in as_trait parts of paths
jjcnn Dec 23, 2024
f46de5c
Comment out dbg
jjcnn Dec 23, 2024
035b366
Collect constants from externals during IR generation
jjcnn Dec 26, 2024
7933758
Don't collect test functiosn for externals
jjcnn Dec 26, 2024
fbe27f4
Merge
jjcnn Dec 27, 2024
cfb62af
Fix str_slice test
jjcnn Dec 27, 2024
1b46ba0
Remove debug markers and commented-out code
jjcnn Dec 27, 2024
6169a1a
Clippy, fmt
jjcnn Dec 27, 2024
d476136
Fix spelling errors
jjcnn Dec 27, 2024
b2aef5d
Fix use of single-identifier paths referring to an enum variant
jjcnn Jan 2, 2025
003ed9e
Fix unit tests
jjcnn Jan 2, 2025
74a027c
Change is_absolute to callpath_type (for CallPath) or is_relative_to_…
jjcnn Jan 2, 2025
4392fa8
Only check for variant star imports if the path is long enough
jjcnn Jan 2, 2025
b328e1b
Workaround to avoid CI aborts on warning
jjcnn Jan 2, 2025
830275a
Fixed constant collection for storage declarations
jjcnn Jan 7, 2025
dc8faed
Move namespace from TyModule to TyProgram
jjcnn Jan 7, 2025
e6cf7d0
Simplify AbiContext::to_str_context
jjcnn Jan 8, 2025
e58ec1b
Improve error message when library dependency isn't found
jjcnn Jan 8, 2025
810a877
Eliminate Root::add_span_to_root_module
jjcnn Jan 8, 2025
2bc81ce
Simplify compilation of constants
jjcnn Jan 8, 2025
168d8d3
Fixed duplicate cloning of current mod path
jjcnn Jan 8, 2025
cec5bd3
Merge branch 'master' into jjcnn/move-external-modules-to-root
jjcnn Jan 8, 2025
73142ca
Consume error iterator when constant compilation results in errors
jjcnn Jan 9, 2025
9c39636
Merge branch 'master' into jjcnn/move-external-modules-to-root
jjcnn Jan 9, 2025
22f79d7
Merge branch 'master' into jjcnn/move-external-modules-to-root
jjcnn Jan 9, 2025
bbf99af
Fix is_module check in type_check_delineated_path
jjcnn Jan 10, 2025
eb2dc06
Visibility check for paths should take reexports into account
jjcnn Jan 10, 2025
c8fdd02
Merge branch 'master' into jjcnn/move-external-modules-to-root
jjcnn Jan 11, 2025
75f407c
Fix test after improved visibility check
jjcnn Jan 11, 2025
de64127
Merge branch 'master' of github.com:FuelLabs/sway into jjcnn/move-ext…
jjcnn Jan 11, 2025
37df6ec
Merge branch 'jjcnn/move-external-modules-to-root' of github.com:Fuel…
jjcnn Jan 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 25 additions & 76 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use sway_core::{
fuel_crypto,
fuel_tx::{self, Contract, ContractId, StorageSlot},
},
language::{parsed::TreeType, Visibility},
language::parsed::TreeType,
semantic_analysis::namespace,
source_map::SourceMap,
transform::AttributeKind,
Expand All @@ -49,7 +49,7 @@ use sway_core::{
use sway_core::{set_bytecode_configurables_offset, PrintAsm, PrintIr};
use sway_error::{error::CompileError, handler::Handler, warning::CompileWarning};
use sway_features::ExperimentalFeatures;
use sway_types::constants::{CORE, PRELUDE, STD};
use sway_types::constants::{CORE, STD};
use sway_types::{Ident, Span, Spanned};
use sway_utils::{constants, time_expr, PerformanceData, PerformanceMetric};
use tracing::{debug, info};
Expand Down Expand Up @@ -181,7 +181,7 @@ pub struct CompiledPackage {
pub program_abi: ProgramABI,
pub storage_slots: Vec<StorageSlot>,
pub bytecode: BuiltPackageBytecode,
pub root_module: namespace::Module,
pub root_module: namespace::Root,
pub warnings: Vec<CompileWarning>,
pub metrics: PerformanceData,
}
Expand Down Expand Up @@ -1569,9 +1569,6 @@ pub fn sway_build_config(
Ok(build_config)
}

/// The name of the constant holding the contract's id.
pub const CONTRACT_ID_CONSTANT_NAME: &str = "CONTRACT_ID";

/// Builds the dependency namespace for the package at the given node index within the graph.
///
/// This function is designed to be called for each node in order of compilation.
Expand All @@ -1585,7 +1582,7 @@ pub const CONTRACT_ID_CONSTANT_NAME: &str = "CONTRACT_ID";
/// `contract_id_value` should only be Some when producing the `dependency_namespace` for a contract with tests enabled.
/// This allows us to provide a contract's `CONTRACT_ID` constant to its own unit tests.
pub fn dependency_namespace(
lib_namespace_map: &HashMap<NodeIx, namespace::Module>,
lib_namespace_map: &HashMap<NodeIx, namespace::Root>,
compiled_contract_deps: &CompiledContractDeps,
graph: &Graph,
node: NodeIx,
Expand All @@ -1596,16 +1593,15 @@ pub fn dependency_namespace(
// TODO: Clean this up when config-time constants v1 are removed.
let node_idx = &graph[node];
let name = Ident::new_no_span(node_idx.name.clone());
let mut root_module = if let Some(contract_id_value) = contract_id_value {
namespace::default_with_contract_id(
let mut root_namespace = if let Some(contract_id_value) = contract_id_value {
namespace::namespace_with_contract_id(
engines,
name.clone(),
Visibility::Public,
contract_id_value,
experimental,
)?
} else {
namespace::Module::new(name, Visibility::Public, None)
namespace::namespace_without_contract_id(name.clone())
};

// Add direct dependencies.
Expand All @@ -1614,12 +1610,12 @@ pub fn dependency_namespace(
let dep_node = edge.target();
let dep_name = kebab_to_snake_case(&edge.weight().name);
let dep_edge = edge.weight();
let mut dep_namespace = match dep_edge.kind {
let dep_namespace = match dep_edge.kind {
DepKind::Library => lib_namespace_map
.get(&dep_node)
.cloned()
.expect("no namespace module")
.read(engines, Clone::clone),
.expect("no root namespace module")
.clone(),
DepKind::Contract { salt } => {
let dep_contract_id = compiled_contract_deps
.get(&dep_node)
Expand All @@ -1630,17 +1626,15 @@ pub fn dependency_namespace(
let contract_id_value = format!("0x{dep_contract_id}");
let node_idx = &graph[dep_node];
let name = Ident::new_no_span(node_idx.name.clone());
namespace::default_with_contract_id(
namespace::namespace_with_contract_id(
engines,
name.clone(),
Visibility::Private,
contract_id_value,
experimental,
)?
}
};
dep_namespace.is_external = true;
root_module.insert_submodule(dep_name, dep_namespace);
root_namespace.add_external(dep_name, dep_namespace);
let dep = &graph[dep_node];
if dep.name == CORE {
core_added = true;
Expand All @@ -1651,50 +1645,11 @@ pub fn dependency_namespace(
if !core_added {
if let Some(core_node) = find_core_dep(graph, node) {
let core_namespace = &lib_namespace_map[&core_node];
root_module.insert_submodule(CORE.to_string(), core_namespace.clone());
core_added = true;
root_namespace.add_external(CORE.to_string(), core_namespace.clone());
}
}

let mut root = namespace::Root::from(root_module);

if core_added {
let _ = root.star_import(
&Handler::default(),
engines,
&[CORE, PRELUDE].map(|s| Ident::new_no_span(s.into())),
&[],
Visibility::Private,
);
}

if has_std_dep(graph, node) {
let _ = root.star_import(
&Handler::default(),
engines,
&[STD, PRELUDE].map(|s| Ident::new_no_span(s.into())),
&[],
Visibility::Private,
);
}

Ok(root)
}

/// Find the `std` dependency, if it is a direct one, of the given node.
fn has_std_dep(graph: &Graph, node: NodeIx) -> bool {
// If we are `std`, do nothing.
let pkg = &graph[node];
if pkg.name == STD {
return false;
}

// If we have `std` as a direct dep, use it.
graph.edges_directed(node, Direction::Outgoing).any(|edge| {
let dep_node = edge.target();
let dep = &graph[dep_node];
matches!(&dep.name[..], STD)
})
Ok(root_namespace)
}

/// Find the `core` dependency (whether direct or transitive) for the given node if it exists.
Expand Down Expand Up @@ -1755,7 +1710,7 @@ pub fn compile(
pkg: &PackageDescriptor,
profile: &BuildProfile,
engines: &Engines,
namespace: &mut namespace::Root,
namespace: namespace::Root,
source_map: &mut SourceMap,
experimental: ExperimentalFeatures,
) -> Result<CompiledPackage> {
Expand Down Expand Up @@ -1816,8 +1771,6 @@ pub fn compile(
let storage_slots = typed_program.storage_slots.clone();
let tree_type = typed_program.kind.tree_type();

let namespace = typed_program.root.namespace.clone();

if handler.has_errors() {
return fail(handler);
}
Expand Down Expand Up @@ -1980,7 +1933,7 @@ pub fn compile(
storage_slots,
tree_type,
bytecode,
root_module: namespace.root_module().clone(),
root_module: typed_program.namespace.root_ref().clone(),
warnings,
metrics,
};
Expand Down Expand Up @@ -2488,7 +2441,7 @@ pub fn build(

// `ContractIdConst` is a None here since we do not yet have a
// contract ID value at this point.
let mut dep_namespace = match dependency_namespace(
let dep_namespace = match dependency_namespace(
&lib_namespace_map,
&compiled_contract_deps,
plan.graph(),
Expand All @@ -2505,7 +2458,7 @@ pub fn build(
&descriptor,
&profile,
&engines,
&mut dep_namespace,
dep_namespace,
&mut source_map,
experimental,
)?;
Expand Down Expand Up @@ -2553,7 +2506,7 @@ pub fn build(
};

// Note that the contract ID value here is only Some if tests are enabled.
let mut dep_namespace = match dependency_namespace(
let dep_namespace = match dependency_namespace(
&lib_namespace_map,
&compiled_contract_deps,
plan.graph(),
Expand All @@ -2579,7 +2532,7 @@ pub fn build(
&descriptor,
&profile,
&engines,
&mut dep_namespace,
dep_namespace,
&mut source_map,
experimental,
)?;
Expand Down Expand Up @@ -2659,7 +2612,7 @@ pub fn check(
let contract_id_value =
(idx == plan.compilation_order.len() - 1).then(|| DUMMY_CONTRACT_ID.to_string());

let mut dep_namespace = dependency_namespace(
let dep_namespace = dependency_namespace(
&lib_namespace_map,
&compiled_contract_deps,
&plan.graph,
Expand Down Expand Up @@ -2690,7 +2643,7 @@ pub fn check(
&handler,
engines,
input,
&mut dep_namespace,
dep_namespace,
Some(&build_config),
&pkg.name,
retrigger_compilation.clone(),
Expand All @@ -2714,12 +2667,8 @@ pub fn check(

if let Ok(typed_program) = programs.typed.as_ref() {
if let TreeType::Library = typed_program.kind.tree_type() {
let mut module = typed_program
.root
.namespace
.program_id(engines)
.read(engines, |m| m.clone());
module.set_span(
let mut lib_root = typed_program.namespace.root_ref().clone();
lib_root.current_package_root_module_mut().set_span(
Span::new(
manifest.entry_string()?,
0,
Expand All @@ -2728,7 +2677,7 @@ pub fn check(
)
.unwrap(),
);
lib_namespace_map.insert(node, module);
lib_namespace_map.insert(node, lib_root);
}
source_map.insert_dependency(manifest.dir());
} else {
Expand Down
6 changes: 3 additions & 3 deletions forc-plugins/forc-doc/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ impl Documentation {
Documentation::from_ty_module(
engines.de(),
&module_info,
&typed_program.root,
&typed_program.root_module,
&mut docs,
&mut impl_traits,
document_private_items,
)?;

// this is the same process as before but for submodules
for (_, ref typed_submodule) in &typed_program.root.submodules {
for (_, ref typed_submodule) in &typed_program.root_module.submodules {
let attributes = (!typed_submodule.module.attributes.is_empty())
.then(|| typed_submodule.module.attributes.to_html_string());
let module_prefix =
Expand Down Expand Up @@ -107,7 +107,7 @@ impl Documentation {
} else {
impl_trait.trait_name = impl_trait
.trait_name
.to_fullpath(engines, &typed_program.root.namespace);
.to_fullpath(engines, &typed_program.namespace);
None
};

Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn build_docs(
document_private_items,
)?;
let root_attributes =
(!ty_program.root.attributes.is_empty()).then_some(ty_program.root.attributes);
(!ty_program.root_module.attributes.is_empty()).then_some(ty_program.root_module.attributes);
let forc_version = pkg_manifest
.project
.forc_version
Expand Down
30 changes: 10 additions & 20 deletions sway-core/src/abi_generation/fuel_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ pub struct AbiContext<'a> {
}

impl<'a> AbiContext<'a> {
fn to_str_context(&self, engines: &Engines, abi_full: bool) -> AbiStrContext {
fn to_str_context(&self) -> AbiStrContext {
AbiStrContext {
program_name: self
.program
.root
.namespace
.program_id(engines)
.read(engines, |m| m.name().to_string()),
.current_package_name()
.to_string(),
abi_with_callpaths: self.abi_with_callpaths,
abi_with_fully_specified_types: abi_full,
abi_root_type_without_generic_type_parameters: !abi_full,
abi_with_fully_specified_types: false,
abi_root_type_without_generic_type_parameters: true,
}
}
}
Expand All @@ -48,10 +47,9 @@ impl TypeId {
&AbiStrContext {
program_name: ctx
.program
.root
.namespace
.program_id(engines)
.read(engines, |m| m.name().clone().as_str().to_string()),
.current_package_name()
.to_string(),
abi_with_callpaths: true,
abi_with_fully_specified_types: true,
abi_root_type_without_generic_type_parameters: false,
Expand Down Expand Up @@ -381,11 +379,7 @@ fn generate_concrete_type_declaration(
let mut new_metadata_types_to_add = Vec::<program_abi::TypeMetadataDeclaration>::new();
let type_metadata_decl = program_abi::TypeMetadataDeclaration {
metadata_type_id: MetadataTypeId(type_id.index()),
type_field: type_id.get_abi_type_str(
&ctx.to_str_context(engines, false),
engines,
resolved_type_id,
),
type_field: type_id.get_abi_type_str(&ctx.to_str_context(), engines, resolved_type_id),
components: type_id.get_abi_type_components(
handler,
ctx,
Expand Down Expand Up @@ -475,11 +469,7 @@ fn generate_type_metadata_declaration(
)?;
let type_metadata_decl = program_abi::TypeMetadataDeclaration {
metadata_type_id: MetadataTypeId(type_id.index()),
type_field: type_id.get_abi_type_str(
&ctx.to_str_context(engines, false),
engines,
resolved_type_id,
),
type_field: type_id.get_abi_type_str(&ctx.to_str_context(), engines, resolved_type_id),
components,
type_parameters,
};
Expand Down Expand Up @@ -1249,7 +1239,7 @@ impl TypeParameter {
let type_parameter = program_abi::TypeMetadataDeclaration {
metadata_type_id: type_id.clone(),
type_field: self.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines, false),
&ctx.to_str_context(),
engines,
self.type_id,
),
Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
self, ConfigurableDecl, ConstantDecl, FunctionDecl, ProjectionKind, StructDecl,
TraitDecl, TyAstNode, TyAstNodeContent, TyDecl, TyImplItem, TypeAliasDecl,
},
CallPath, Visibility,
CallPath, CallPathType, Visibility,
},
transform::{self, AttributesMap},
type_system::TypeInfo,
Expand Down Expand Up @@ -857,7 +857,7 @@ fn connect_trait_declaration(
CallPath {
prefixes: vec![],
suffix: decl.name.clone(),
is_absolute: false,
callpath_type: CallPathType::Ambiguous,
},
TraitNamespaceEntry {
trait_idx: entry_node,
Expand All @@ -881,7 +881,7 @@ fn connect_abi_declaration(
CallPath {
prefixes: vec![],
suffix: decl.name.clone(),
is_absolute: false,
callpath_type: CallPathType::Ambiguous,
},
TraitNamespaceEntry {
trait_idx: entry_node,
Expand Down
Loading
Loading