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

More namespace cleanups #6675

Merged
merged 20 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8dfdaa5
Extract `Root.resolve_symbol_and_mod_path` to `type_resolve.rs`.
tritao Oct 26, 2024
f368989
Remove `Root.resolve_call_path`.
tritao Oct 26, 2024
45ef65c
Remove `Root.resolve_call_path_and_mod_path`.
tritao Oct 26, 2024
f039234
Remove `Root.resolve_symbol`.
tritao Oct 26, 2024
abef8a9
Remove `Namespace.resolve_symbol`.
tritao Oct 26, 2024
413ff76
Remove `Namespace.resolve_call_path`.
tritao Oct 26, 2024
62ca270
Simplify `SymbolResolveContext.resolve_call_path_with_visibility_chec…
tritao Oct 26, 2024
b238e4b
Unify `resolve_associated_type` and `resolve_associated_item`.
tritao Oct 26, 2024
53d550e
Remove `TypeCheckContext::*_with_visibility_check` suffix.
tritao Oct 26, 2024
000a1bb
Move `Namespace.resolve_symbol/call_path_typed` to `TypeCheckContext`.
tritao Oct 26, 2024
c9cc670
Return typed declarations from `TypeCheckContext.resolve(_qualified)_…
tritao Oct 26, 2024
ca41ecb
Use the context `self_type` when resolving typed symbols.
tritao Oct 26, 2024
ee9750c
Restore `TypeCheckContext.resolve_call_path_with_visibility_check` na…
tritao Oct 26, 2024
b1fee57
Remove `_typed` suffix from `TypeCheckContext` resolve methods.
tritao Nov 28, 2024
2ae69fd
Refactor visibility checking into a flag.
tritao Nov 28, 2024
f15350c
Remove `resolve_call_path_and_mod_path`.
tritao Oct 26, 2024
cb07a75
Remove `Namespace.lookup_submodule_from_absolute_path`.
tritao Oct 26, 2024
0f90910
Change `resolve_symbol_and_mod_path` to be private.
tritao Oct 26, 2024
7dbb31f
Hide `type_resolve.rs` internals.
tritao Oct 26, 2024
46e12ef
Merge branch 'master' into more-namespace-unification
JoshuaBatty Dec 2, 2024
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
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
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
Loading