Skip to content

Commit

Permalink
More namespace cleanups (#6675)
Browse files Browse the repository at this point in the history
## Description

Another round of namespace-related cleanups.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] 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.

---------

Co-authored-by: Joshua Batty <[email protected]>
  • Loading branch information
tritao and JoshuaBatty authored Dec 2, 2024
1 parent 3720544 commit 07dfa55
Show file tree
Hide file tree
Showing 18 changed files with 269 additions and 499 deletions.
75 changes: 29 additions & 46 deletions sway-core/src/semantic_analysis/ast_node/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,26 +187,20 @@ impl TyDecl {

// save decl_refs for the LSP
for supertrait in trait_decl.supertraits.iter_mut() {
let _ = ctx
.namespace()
.resolve_call_path_typed(
handler,
engines,
&supertrait.name,
ctx.self_type(),
)
.map(|supertrait_decl| {
if let ty::TyDecl::TraitDecl(ty::TraitDecl {
decl_id: supertrait_decl_id,
}) = supertrait_decl
{
supertrait.decl_ref = Some(DeclRef::new(
engines.de().get(&supertrait_decl_id).name.clone(),
supertrait_decl_id,
engines.de().get(&supertrait_decl_id).span.clone(),
));
}
});
let _ =
ctx.resolve_call_path(handler, &supertrait.name)
.map(|supertrait_decl| {
if let ty::TyDecl::TraitDecl(ty::TraitDecl {
decl_id: supertrait_decl_id,
}) = supertrait_decl
{
supertrait.decl_ref = Some(DeclRef::new(
engines.de().get(&supertrait_decl_id).name.clone(),
supertrait_decl_id,
engines.de().get(&supertrait_decl_id).span.clone(),
));
}
});
}

let decl: ty::TyDecl = decl_engine
Expand Down Expand Up @@ -301,12 +295,7 @@ impl TyDecl {

// Choose which items are going to be visible depending if this is an abi impl
// or trait impl
let t = ctx.namespace().resolve_call_path_typed(
&Handler::default(),
engines,
&impl_trait.trait_name,
ctx.self_type(),
);
let t = ctx.resolve_call_path(&Handler::default(), &impl_trait.trait_name);

let empty_vec = vec![];
let impl_trait_items = if let Ok(ty::TyDecl::TraitDecl { .. }) = t {
Expand Down Expand Up @@ -369,26 +358,20 @@ impl TyDecl {

// save decl_refs for the LSP
for supertrait in abi_decl.supertraits.iter_mut() {
let _ = ctx
.namespace()
.resolve_call_path_typed(
handler,
engines,
&supertrait.name,
ctx.self_type(),
)
.map(|supertrait_decl| {
if let ty::TyDecl::TraitDecl(ty::TraitDecl {
decl_id: supertrait_decl_id,
}) = supertrait_decl
{
supertrait.decl_ref = Some(DeclRef::new(
engines.de().get(&supertrait_decl_id).name.clone(),
supertrait_decl_id,
engines.de().get(&supertrait_decl_id).span.clone(),
));
}
});
let _ =
ctx.resolve_call_path(handler, &supertrait.name)
.map(|supertrait_decl| {
if let ty::TyDecl::TraitDecl(ty::TraitDecl {
decl_id: supertrait_decl_id,
}) = supertrait_decl
{
supertrait.decl_ref = Some(DeclRef::new(
engines.de().get(&supertrait_decl_id).name.clone(),
supertrait_decl_id,
engines.de().get(&supertrait_decl_id).span.clone(),
));
}
});
}

let decl: ty::TyDecl = decl_engine.insert(abi_decl.clone(), Some(&decl_id)).into();
Expand Down
14 changes: 2 additions & 12 deletions sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,7 @@ impl TyImplSelfOrTrait {
.with_type_annotation(type_engine.new_unknown())
.with_self_type(Some(implementing_for.type_id));

let impl_trait = match ctx
.namespace()
.resolve_call_path_typed(handler, engines, &trait_name, ctx.self_type())
.ok()
{
let impl_trait = match ctx.resolve_call_path(handler, &trait_name).ok() {
Some(ty::TyDecl::TraitDecl(ty::TraitDecl { decl_id, .. })) => {
let mut trait_decl = (*decl_engine.get_trait(&decl_id)).clone();

Expand Down Expand Up @@ -1567,14 +1563,8 @@ fn handle_supertraits(
}

match ctx
.namespace()
// Use the default Handler to avoid emitting the redundant SymbolNotFound error.
.resolve_call_path_typed(
&Handler::default(),
engines,
&supertrait.name,
ctx.self_type(),
)
.resolve_call_path(&Handler::default(), &supertrait.name)
.ok()
{
Some(ty::TyDecl::TraitDecl(ty::TraitDecl { decl_id, .. })) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,8 @@ pub(crate) fn insert_supertraits_into_namespace(
}

let decl = ctx
.namespace()
// Use the default Handler to avoid emitting the redundant SymbolNotFound error.
.resolve_call_path_typed(
&Handler::default(),
engines,
&supertrait.name,
ctx.self_type(),
)
.resolve_call_path(&Handler::default(), &supertrait.name)
.ok();

match (decl.clone(), supertraits_of) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ impl ty::TyMatchBranch {
for (ident, is_struct_field) in variables {
let default_handler = &Handler::default();
// If there exist a configurable with the same name as the pattern variable.
if let Ok(ty::TyDecl::ConfigurableDecl(configurable_decl)) = ctx
.namespace()
.resolve_symbol_typed(default_handler, engines, &ident, ctx.self_type())
if let Ok(ty::TyDecl::ConfigurableDecl(configurable_decl)) =
ctx.resolve_symbol(default_handler, &ident)
{
let name = (&ident).into();
let configurable_span = engines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ fn type_check_variable(
let type_engine = engines.te();
let decl_engine = engines.de();

let typed_scrutinee = match ctx
.namespace()
.resolve_symbol_typed(&Handler::default(), engines, &name, ctx.self_type())
.ok()
{
let typed_scrutinee = match ctx.resolve_symbol(&Handler::default(), &name).ok() {
// If the name represents a constant, then we turn it into a [ty::TyScrutineeVariant::Constant].
Some(ty::TyDecl::ConstantDecl(ty::ConstantDecl { decl_id, .. })) => {
let constant_decl = (*decl_engine.get_constant(&decl_id)).clone();
Expand Down Expand Up @@ -219,9 +215,7 @@ fn type_check_struct(
let decl_engine = engines.de();

// find the struct definition from the name
let unknown_decl =
ctx.namespace()
.resolve_symbol_typed(handler, engines, &struct_name, ctx.self_type())?;
let unknown_decl = ctx.resolve_symbol(handler, &struct_name)?;
let struct_id = unknown_decl.to_struct_decl(handler, ctx.engines())?;
let mut struct_decl = (*decl_engine.get_struct(&struct_id)).clone();

Expand Down Expand Up @@ -484,23 +478,13 @@ fn type_check_enum(
is_absolute: call_path.is_absolute,
};
// find the enum definition from the name
let unknown_decl = ctx.namespace().resolve_call_path_typed(
handler,
engines,
&enum_callpath,
ctx.self_type(),
)?;
let unknown_decl = ctx.resolve_call_path(handler, &enum_callpath)?;
let enum_id = unknown_decl.to_enum_id(handler, ctx.engines())?;
(enum_callpath.span(), enum_id, unknown_decl)
}
None => {
// we may have an imported variant
let decl = ctx.namespace().resolve_call_path_typed(
handler,
engines,
&call_path,
ctx.self_type(),
)?;
let decl = ctx.resolve_call_path(handler, &call_path)?;
if let TyDecl::EnumVariantDecl(ty::EnumVariantDecl { enum_ref, .. }) = decl.clone() {
(call_path.suffix.span(), *enum_ref.id(), decl)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use sway_error::{
};
use sway_types::{integer_bits::IntegerBits, u256::U256, Ident, Named, Span, Spanned};
use symbol_collection_context::SymbolCollectionContext;
use type_resolve::{resolve_call_path, VisibilityCheck};

#[allow(clippy::too_many_arguments)]
impl ty::TyExpression {
Expand Down Expand Up @@ -309,14 +310,7 @@ impl ty::TyExpression {
is_absolute: false,
};
if matches!(
ctx.namespace()
.resolve_call_path_typed(
&Handler::default(),
engines,
&call_path,
ctx.self_type()
)
.ok(),
ctx.resolve_call_path(&Handler::default(), &call_path,).ok(),
Some(ty::TyDecl::EnumVariantDecl { .. })
) {
Self::type_check_delineated_path(
Expand Down Expand Up @@ -651,11 +645,7 @@ impl ty::TyExpression {
let decl_engine = ctx.engines.de();
let engines = ctx.engines();

let exp = match ctx
.namespace()
.resolve_symbol_typed(&Handler::default(), engines, &name, ctx.self_type())
.ok()
{
let exp = match ctx.resolve_symbol(&Handler::default(), &name).ok() {
Some(ty::TyDecl::VariableDecl(decl)) => {
let ty::TyVariableDecl {
name: decl_name,
Expand Down Expand Up @@ -1260,12 +1250,14 @@ impl ty::TyExpression {
let storage_key_ident = Ident::new_with_override("StorageKey".into(), span.clone());

// Search for the struct declaration with the call path above.
let storage_key_decl = ctx.namespace().root().resolve_symbol(
let storage_key_decl = resolve_call_path(
handler,
engines,
ctx.namespace().root(),
&storage_key_mod_path,
&storage_key_ident,
&storage_key_ident.into(),
None,
VisibilityCheck::No,
)?;

let storage_key_struct_decl_id = storage_key_decl
Expand Down Expand Up @@ -1420,12 +1412,7 @@ impl ty::TyExpression {
is_absolute,
};
if matches!(
ctx.namespace().resolve_call_path_typed(
&Handler::default(),
engines,
&call_path,
ctx.self_type()
),
ctx.resolve_call_path(&Handler::default(), &call_path,),
Ok(ty::TyDecl::EnumVariantDecl { .. })
) {
// if it's a singleton it's either an enum variant or a function
Expand Down Expand Up @@ -1480,13 +1467,7 @@ impl ty::TyExpression {
suffix: before.inner.clone(),
is_absolute,
};
ctx.namespace()
.resolve_call_path_typed(
&Handler::default(),
engines,
&probe_call_path,
ctx.self_type(),
)
ctx.resolve_call_path(&Handler::default(), &probe_call_path)
.and_then(|decl| decl.to_enum_id(&Handler::default(), ctx.engines()))
.map(|decl_ref| decl_engine.get_enum(&decl_ref))
.and_then(|decl| {
Expand Down Expand Up @@ -1799,12 +1780,7 @@ impl ty::TyExpression {
};

// look up the call path and get the declaration it references
let abi = ctx.namespace().resolve_call_path_typed(
handler,
engines,
&abi_name,
ctx.self_type(),
)?;
let abi = ctx.resolve_call_path(handler, &abi_name)?;
let abi_ref = match abi {
ty::TyDecl::AbiDecl(ty::AbiDecl { decl_id }) => {
let abi_decl = engines.de().get(&decl_id);
Expand All @@ -1825,12 +1801,7 @@ impl ty::TyExpression {
match abi_name {
// look up the call path and get the declaration it references
AbiName::Known(abi_name) => {
let unknown_decl = ctx.namespace().resolve_call_path_typed(
handler,
engines,
abi_name,
ctx.self_type(),
)?;
let unknown_decl = ctx.resolve_call_path(handler, abi_name)?;
unknown_decl.to_abi_ref(handler, engines)?
}
AbiName::Deferred => {
Expand Down Expand Up @@ -2244,12 +2215,7 @@ impl ty::TyExpression {
let (decl_reference_name, decl_reference_rhs, decl_reference_type) =
match &reference_exp.expression {
TyExpressionVariant::VariableExpression { name, .. } => {
let var_decl = ctx.namespace().resolve_symbol_typed(
handler,
engines,
name,
ctx.self_type(),
)?;
let var_decl = ctx.resolve_symbol(handler, name)?;

let TyDecl::VariableDecl(var_decl) = var_decl else {
return Err(handler.emit_err(CompileError::Internal(
Expand Down Expand Up @@ -2309,12 +2275,7 @@ impl ty::TyExpression {
match expr.kind {
ExpressionKind::Variable(name) => {
// check that the reassigned name exists
let unknown_decl = ctx.namespace().resolve_symbol_typed(
handler,
engines,
&name,
ctx.self_type(),
)?;
let unknown_decl = ctx.resolve_symbol(handler, &name)?;

match unknown_decl {
TyDecl::VariableDecl(variable_decl) => {
Expand Down Expand Up @@ -2803,15 +2764,13 @@ fn check_asm_block_validity(

// Emit warning if this register shadows a constant, or a configurable, or a variable.
let temp_handler = Handler::default();
let decl = ctx.namespace().resolve_call_path_typed(
let decl = ctx.resolve_call_path(
&temp_handler,
ctx.engines,
&CallPath {
prefixes: vec![],
suffix: sway_types::BaseIdent::new(span.clone()),
is_absolute: true,
},
None,
);

let shadowing_item = match decl {
Expand Down
Loading

0 comments on commit 07dfa55

Please sign in to comment.