Skip to content

Refactor: Access to externals moved from Root to Namespace #6999

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

Merged
merged 19 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 16 additions & 16 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::{
str::FromStr,
sync::{atomic::AtomicBool, Arc},
};
use sway_core::namespace::Root;
use sway_core::namespace::Package;
pub use sway_core::Programs;
use sway_core::{
abi_generation::{
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::Root,
pub namespace: namespace::Package,
pub warnings: Vec<CompileWarning>,
pub metrics: PerformanceData,
}
Expand Down Expand Up @@ -1583,28 +1583,28 @@ pub fn sway_build_config(
/// This allows us to provide a contract's `CONTRACT_ID` constant to its own unit tests.
#[allow(clippy::too_many_arguments)]
pub fn dependency_namespace(
lib_namespace_map: &HashMap<NodeIx, namespace::Root>,
lib_namespace_map: &HashMap<NodeIx, namespace::Package>,
compiled_contract_deps: &CompiledContractDeps,
graph: &Graph,
node: NodeIx,
engines: &Engines,
contract_id_value: Option<ContractIdConst>,
program_id: ProgramId,
experimental: ExperimentalFeatures,
) -> Result<namespace::Root, vec1::Vec1<CompileError>> {
) -> Result<namespace::Package, vec1::Vec1<CompileError>> {
// 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_namespace = if let Some(contract_id_value) = contract_id_value {
namespace::namespace_with_contract_id(
let mut namespace = if let Some(contract_id_value) = contract_id_value {
namespace::package_with_contract_id(
engines,
name.clone(),
program_id,
contract_id_value,
experimental,
)?
} else {
Root::new(name.clone(), None, program_id, false)
Package::new(name.clone(), None, program_id, false)
};

// Add direct dependencies.
Expand All @@ -1628,7 +1628,7 @@ 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::namespace_with_contract_id(
namespace::package_with_contract_id(
engines,
name.clone(),
program_id,
Expand All @@ -1637,10 +1637,10 @@ pub fn dependency_namespace(
)?
}
};
root_namespace.add_external(dep_name, dep_namespace);
namespace.add_external(dep_name, dep_namespace);
}

Ok(root_namespace)
Ok(namespace)
}

/// Compiles the given package.
Expand All @@ -1665,7 +1665,7 @@ pub fn compile(
pkg: &PackageDescriptor,
profile: &BuildProfile,
engines: &Engines,
namespace: namespace::Root,
namespace: namespace::Package,
source_map: &mut SourceMap,
experimental: ExperimentalFeatures,
) -> Result<CompiledPackage> {
Expand Down Expand Up @@ -1888,7 +1888,7 @@ pub fn compile(
storage_slots,
tree_type,
bytecode,
root_module: typed_program.namespace.root_ref().clone(),
namespace: typed_program.namespace.current_package_ref().clone(),
warnings,
metrics,
};
Expand Down Expand Up @@ -2515,7 +2515,7 @@ pub fn build(
}

if let TreeType::Library = compiled.tree_type {
lib_namespace_map.insert(node, compiled.root_module);
lib_namespace_map.insert(node, compiled.namespace);
}
source_map.insert_dependency(descriptor.manifest_file.dir());

Expand Down Expand Up @@ -2647,8 +2647,8 @@ pub fn check(

if let Ok(typed_program) = programs.typed.as_ref() {
if let TreeType::Library = typed_program.kind.tree_type() {
let mut lib_root = typed_program.namespace.root_ref().clone();
lib_root.current_package_root_module_mut().set_span(
let mut lib_namespace = typed_program.namespace.current_package_ref().clone();
lib_namespace.root_module_mut().set_span(
Span::new(
manifest.entry_string()?,
0,
Expand All @@ -2657,7 +2657,7 @@ pub fn check(
)
.unwrap(),
);
lib_namespace_map.insert(node, lib_root);
lib_namespace_map.insert(node, lib_namespace);
}
source_map.insert_dependency(manifest.dir());
} else {
Expand Down
8 changes: 4 additions & 4 deletions sway-core/src/ir_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub fn compile_program<'eng>(
engines,
&mut ctx,
entry_function,
namespace.root_ref(),
namespace,
&logged_types,
&messages_types,
&test_fns,
Expand All @@ -176,7 +176,7 @@ pub fn compile_program<'eng>(
engines,
&mut ctx,
entry_function,
namespace.root_ref(),
namespace,
&logged_types,
&messages_types,
&test_fns,
Expand All @@ -189,7 +189,7 @@ pub fn compile_program<'eng>(
&mut ctx,
entry_function.as_ref(),
abi_entries,
namespace.root_ref(),
namespace,
declarations,
&logged_types,
&messages_types,
Expand All @@ -200,7 +200,7 @@ pub fn compile_program<'eng>(
ty::TyProgramKind::Library { .. } => compile::compile_library(
engines,
&mut ctx,
namespace.root_ref(),
namespace,
&logged_types,
&messages_types,
&test_fns,
Expand Down
42 changes: 23 additions & 19 deletions sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(super) fn compile_script(
engines: &Engines,
context: &mut Context,
entry_function: &DeclId<ty::TyFunctionDecl>,
namespace: &namespace::Root,
namespace: &namespace::Namespace,
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
test_fns: &[(Arc<ty::TyFunctionDecl>, DeclRefFunction)],
Expand Down Expand Up @@ -80,7 +80,7 @@ pub(super) fn compile_predicate(
engines: &Engines,
context: &mut Context,
entry_function: &DeclId<ty::TyFunctionDecl>,
namespace: &namespace::Root,
namespace: &namespace::Namespace,
logged_types: &HashMap<TypeId, LogId>,
messages_types: &HashMap<TypeId, MessageId>,
test_fns: &[(Arc<ty::TyFunctionDecl>, DeclRefFunction)],
Expand Down Expand Up @@ -133,7 +133,7 @@ pub(super) fn compile_contract(
context: &mut Context,
entry_function: Option<&DeclId<ty::TyFunctionDecl>>,
abi_entries: &[DeclId<ty::TyFunctionDecl>],
namespace: &namespace::Root,
namespace: &namespace::Namespace,
declarations: &[ty::TyDecl],
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
Expand Down Expand Up @@ -226,7 +226,7 @@ pub(super) fn compile_contract(
pub(super) fn compile_library(
engines: &Engines,
context: &mut Context,
namespace: &namespace::Root,
namespace: &namespace::Namespace,
logged_types_map: &HashMap<TypeId, LogId>,
messages_types_map: &HashMap<TypeId, MessageId>,
test_fns: &[(Arc<ty::TyFunctionDecl>, DeclRefFunction)],
Expand Down Expand Up @@ -257,25 +257,29 @@ pub(crate) fn compile_constants_for_package(
engines: &Engines,
context: &mut Context,
module: Module,
namespace: &namespace::Root,
namespace: &namespace::Namespace,
) -> Result<Module, Vec<CompileError>> {
let mut md_mgr = MetadataManager::default();
// Traverses the tree of externals and collects all constants
fn traverse(
engines: &Engines,
context: &mut Context,
module: Module,
current: &namespace::Package,
) -> Result<Module, Vec<CompileError>> {
let mut md_mgr = MetadataManager::default();

// Collect constant for all dependencies
for ext_package in current.external_packages.values() {
traverse(engines, context, module, ext_package)?;
}

// Collect constant for all dependencies
for ext_package in namespace.external_packages().values() {
compile_constants_for_package(engines, context, module, ext_package)?;
}
compile_constants(engines, context, &mut md_mgr, module, current.root_module())
.map_err(|err| vec![err])?;

compile_constants(
engines,
context,
&mut md_mgr,
module,
namespace.current_package_root_module(),
)
.map_err(|err| vec![err])?;
Ok(module)
}

Ok(module)
traverse(engines, context, module, namespace.current_package_ref())
}

fn compile_constants(
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,7 @@ mod tests {
let handler = Handler::default();
let mut context = Context::new(engines.se(), ExperimentalFeatures::default());
let mut md_mgr = MetadataManager::default();
let core_lib = namespace::Root::new(
let core_lib = namespace::Package::new(
sway_types::Ident::new_no_span("assert_is_constant_test".to_string()),
None,
ProgramId::new(0),
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/language/call_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl CallPath {
CallPathDisplayType::Regular => {}
CallPathDisplayType::StripPackagePrefix => {
if let Some(first) = self.prefixes.first() {
if namespace.root_ref().current_package_root_module().name() == first {
if namespace.current_package_name() == first {
display_path = display_path.lshift();
}
}
Expand Down
22 changes: 11 additions & 11 deletions sway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ pub fn parsed_to_ast(
handler: &Handler,
engines: &Engines,
parse_program: &mut parsed::ParseProgram,
initial_namespace: namespace::Root,
initial_namespace: namespace::Package,
build_config: Option<&BuildConfig>,
package_name: &str,
retrigger_compilation: Option<Arc<AtomicBool>>,
Expand Down Expand Up @@ -586,7 +586,7 @@ pub fn parsed_to_ast(
Namespace::new(handler, engines, initial_namespace, true).map_err(|error| {
TypeCheckFailed {
root_module: None,
namespace: collection_ctx.namespace().root_ref().clone(),
namespace: collection_ctx.namespace().current_package_ref().clone(),
error,
}
})?;
Expand All @@ -607,7 +607,7 @@ pub fn parsed_to_ast(
check_should_abort(handler, retrigger_compilation.clone()).map_err(|error| {
TypeCheckFailed {
root_module: Some(Arc::new(typed_program.root_module.clone())),
namespace: typed_program.namespace.root_ref().clone(),
namespace: typed_program.namespace.current_package_ref().clone(),
error,
}
})?;
Expand All @@ -626,7 +626,7 @@ pub fn parsed_to_ast(
handler.dedup();
return Err(TypeCheckFailed {
root_module: Some(Arc::new(typed_program.root_module.clone())),
namespace: typed_program.namespace.root().clone(),
namespace: typed_program.namespace.current_package().clone(),
error,
});
}
Expand All @@ -645,7 +645,7 @@ pub fn parsed_to_ast(
handler.dedup();
return Err(TypeCheckFailed {
root_module: Some(Arc::new(typed_program.root_module.clone())),
namespace: typed_program.namespace.root().clone(),
namespace: typed_program.namespace.current_package().clone(),
error,
});
}
Expand Down Expand Up @@ -676,7 +676,7 @@ pub fn parsed_to_ast(
check_should_abort(handler, retrigger_compilation.clone()).map_err(|error| {
TypeCheckFailed {
root_module: Some(Arc::new(typed_program.root_module.clone())),
namespace: typed_program.namespace.root_ref().clone(),
namespace: typed_program.namespace.current_package_ref().clone(),
error,
}
})?;
Expand All @@ -702,7 +702,7 @@ pub fn parsed_to_ast(
engines,
&mut ctx,
module,
typed_program.namespace.root_ref(),
&typed_program.namespace,
) {
errs.into_iter().for_each(|err| {
handler.emit_err(err.clone());
Expand Down Expand Up @@ -730,7 +730,7 @@ pub fn parsed_to_ast(
handler.dedup();
TypeCheckFailed {
root_module: Some(Arc::new(typed_program.root_module.clone())),
namespace: typed_program.namespace.root_ref().clone(),
namespace: typed_program.namespace.current_package_ref().clone(),
error,
}
})?;
Expand All @@ -756,7 +756,7 @@ pub fn compile_to_ast(
handler: &Handler,
engines: &Engines,
input: Arc<str>,
initial_namespace: namespace::Root,
initial_namespace: namespace::Package,
build_config: Option<&BuildConfig>,
package_name: &str,
retrigger_compilation: Option<Arc<AtomicBool>>,
Expand Down Expand Up @@ -852,7 +852,7 @@ pub fn compile_to_asm(
handler: &Handler,
engines: &Engines,
input: Arc<str>,
initial_namespace: namespace::Root,
initial_namespace: namespace::Package,
build_config: &BuildConfig,
package_name: &str,
experimental: ExperimentalFeatures,
Expand Down Expand Up @@ -1019,7 +1019,7 @@ pub fn compile_to_bytecode(
handler: &Handler,
engines: &Engines,
input: Arc<str>,
initial_namespace: namespace::Root,
initial_namespace: namespace::Package,
build_config: &BuildConfig,
source_map: &mut SourceMap,
package_name: &str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3291,7 +3291,7 @@ mod tests {
experimental: ExperimentalFeatures,
) -> Result<ty::TyExpression, ErrorEmitted> {
let root_module_name = sway_types::Ident::new_no_span("do_type_check_test".to_string());
let root_module = namespace::Root::new(root_module_name, None, ProgramId::new(0), false);
let root_module = namespace::Package::new(root_module_name, None, ProgramId::new(0), false);
let collection_ctx_ns = Namespace::new(handler, engines, root_module.clone(), true)?;
let mut collection_ctx = SymbolCollectionContext::new(collection_ctx_ns);
let mut namespace = Namespace::new(handler, engines, root_module, true)?;
Expand Down
Loading
Loading