Skip to content

Commit

Permalink
Accumulate typechecking methods in impl TypeCheckContext
Browse files Browse the repository at this point in the history
All methods from `impl Namespace` that also take `Engines` as a parameter
have been moved into `impl TypeCheckContext`.

List of changes:

- `ctx.namespace.find_method_for_type` -> `ctx.find_method_for_type`
- `ctx.namespace.find_constant_for_type` -> `ctx.find_constant_for_type`
- `ctx.namespace.get_items_for_type_and_trait_name` -> `ctx.get_items_for_type_and_trait_name`
- `ctx.namespace.insert_trait_implementation` -> `ctx.insert_trait_implementation`
- `ctx.namespace.variant_import` -> `ctx.variant_import`
- `ctx.namespace.item_import` -> `ctx.item_import`
- `ctx.namespace.self_import` -> `ctx.self_import`
- `ctx.namespace.variant_star_import` -> `ctx.variant_star_import`
- `ctx.namespace.star_import` -> `ctx.star_import`
- `ctx.namespace.find_items_for_type` -> `ctx.find_items_for_type`
- `ctx.namespace.resolve_call_path_with_visibility_check` -> `ctx.resolve_call_path_with_visibility_check`
- `ctx.namespace.resolve_type_with_self` -> `ctx.resolve_type_with_self` (and the previous specialized version in `ctx` removed)
- `ctx.namespace.resolve_type_without_self` -> `ctx.resolve_type_without_self` (and the previous specialized version in `ctx` removed)

Partially resolves issue #5050
  • Loading branch information
anton-trunov committed Sep 4, 2023
1 parent 7497fb6 commit d42359b
Show file tree
Hide file tree
Showing 18 changed files with 587 additions and 652 deletions.
14 changes: 5 additions & 9 deletions sway-core/src/semantic_analysis/ast_node/declaration/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl ty::TyAbiDecl {

let error_on_shadowing_superabi_method =
|method_name: &Ident, ctx: &mut TypeCheckContext| {
if let Ok(superabi_impl_method_ref) = ctx.namespace.find_method_for_type(
if let Ok(superabi_impl_method_ref) = ctx.find_method_for_type(
&Handler::default(),
ctx.self_type(),
&[],
Expand All @@ -77,7 +77,6 @@ impl ty::TyAbiDecl {
ctx.type_annotation(),
&Default::default(),
None,
ctx.engines,
TryInsertingTraitImplOnFailure::No,
) {
let superabi_impl_method =
Expand Down Expand Up @@ -188,7 +187,7 @@ impl ty::TyAbiDecl {
&self,
handler: &Handler,
self_decl_id: DeclId<ty::TyAbiDecl>,
ctx: TypeCheckContext,
mut ctx: TypeCheckContext,
type_id: TypeId,
subabi_span: Option<Span>,
) -> Result<(), ErrorEmitted> {
Expand Down Expand Up @@ -216,7 +215,7 @@ impl ty::TyAbiDecl {
let mut method = decl_engine.get_trait_fn(decl_ref);
if look_for_conflicting_abi_methods {
// looking for conflicting ABI methods for triangle-like ABI hierarchies
if let Ok(superabi_method_ref) = ctx.namespace.find_method_for_type(
if let Ok(superabi_method_ref) = ctx.find_method_for_type(
&Handler::default(),
ctx.self_type(),
&[],
Expand All @@ -225,7 +224,6 @@ impl ty::TyAbiDecl {
ctx.type_annotation(),
&Default::default(),
None,
ctx.engines,
TryInsertingTraitImplOnFailure::No,
) {
let superabi_method =
Expand Down Expand Up @@ -293,7 +291,7 @@ impl ty::TyAbiDecl {
// check if we inherit the same impl method from different branches
// XXX this piece of code can be abstracted out into a closure
// and reused for interface methods if the issue of mutable ctx is solved
if let Ok(superabi_impl_method_ref) = ctx.namespace.find_method_for_type(
if let Ok(superabi_impl_method_ref) = ctx.find_method_for_type(
&Handler::default(),
ctx.self_type(),
&[],
Expand All @@ -302,7 +300,6 @@ impl ty::TyAbiDecl {
ctx.type_annotation(),
&Default::default(),
None,
ctx.engines,
TryInsertingTraitImplOnFailure::No,
) {
let superabi_impl_method =
Expand Down Expand Up @@ -342,7 +339,7 @@ impl ty::TyAbiDecl {
// these are not actual impl blocks.
// We check that a contract method cannot call a contract method
// from the same ABI later, during method application typechecking.
let _ = ctx.namespace.insert_trait_implementation(
let _ = ctx.insert_trait_implementation(
&Handler::default(),
CallPath::from(self.name.clone()),
vec![],
Expand All @@ -351,7 +348,6 @@ impl ty::TyAbiDecl {
&self.span,
Some(self.span()),
false,
ctx.engines,
);
Ok(())
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl ty::TyConstantDecl {
.resolve_type_with_self(
handler,
type_ascription.type_id,
ctx.self_type(),
&type_ascription.span,
EnforceTypeArguments::No,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl ty::TyDecl {
.resolve_type_with_self(
handler,
type_ascription.type_id,
ctx.self_type(),
&type_ascription.span,
EnforceTypeArguments::Yes,
None,
Expand Down Expand Up @@ -169,7 +170,7 @@ impl ty::TyDecl {
} else {
&emp_vec
};
ctx.namespace.insert_trait_implementation(
ctx.insert_trait_implementation(
handler,
impl_trait.trait_name.clone(),
impl_trait.trait_type_arguments.clone(),
Expand All @@ -181,7 +182,6 @@ impl ty::TyDecl {
.as_ref()
.map(|decl_ref| decl_ref.decl_span().clone()),
false,
engines,
)?;
let impl_trait_decl: ty::TyDecl = decl_engine.insert(impl_trait.clone()).into();
impl_trait.items.iter_mut().for_each(|item| {
Expand All @@ -196,7 +196,7 @@ impl ty::TyDecl {
Ok(val) => val,
Err(err) => return Ok(ty::TyDecl::ErrorRecovery(span, err)),
};
ctx.namespace.insert_trait_implementation(
ctx.insert_trait_implementation(
handler,
impl_trait.trait_name.clone(),
impl_trait.trait_type_arguments.clone(),
Expand All @@ -208,7 +208,6 @@ impl ty::TyDecl {
.as_ref()
.map(|decl_ref| decl_ref.decl_span().clone()),
true,
engines,
)?;
let impl_trait_decl: ty::TyDecl = decl_engine.insert(impl_trait.clone()).into();
impl_trait.items.iter_mut().for_each(|item| {
Expand Down Expand Up @@ -332,6 +331,7 @@ impl ty::TyDecl {
.resolve_type_with_self(
handler,
ty.type_id,
ctx.self_type(),
&span,
EnforceTypeArguments::Yes,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl ty::TyEnumVariant {
.resolve_type_with_self(
handler,
type_argument.type_id,
ctx.self_type(),
&type_argument.span,
EnforceTypeArguments::Yes,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl ty::TyFunctionDecl {
.resolve_type_with_self(
handler,
return_type.type_id,
ctx.self_type(),
&return_type.span,
EnforceTypeArguments::Yes,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl ty::TyFunctionParameter {
.resolve_type_with_self(
handler,
type_argument.type_id,
ctx.self_type(),
&type_argument.span,
EnforceTypeArguments::Yes,
None,
Expand Down Expand Up @@ -85,6 +86,7 @@ impl ty::TyFunctionParameter {
.resolve_type_with_self(
handler,
type_argument.type_id,
ctx.self_type(),
&type_argument.span,
EnforceTypeArguments::Yes,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ fn type_check_trait_implementation(
let (this_supertrait_stub_method_refs, this_supertrait_impld_method_refs) =
handle_supertraits(handler, ctx.by_ref(), trait_supertraits)?;

let _ = ctx.namespace.insert_trait_implementation(
let _ = ctx.insert_trait_implementation(
&Handler::default(),
trait_name.clone(),
trait_type_arguments.to_vec(),
Expand All @@ -444,7 +444,6 @@ fn type_check_trait_implementation(
&trait_name.span(),
Some(trait_decl_span.clone()),
false,
engines,
);

supertrait_interface_item_refs = this_supertrait_stub_method_refs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl ty::TyStructField {
.resolve_type_with_self(
handler,
type_argument.type_id,
ctx.self_type(),
&type_argument.span,
EnforceTypeArguments::Yes,
None,
Expand Down
16 changes: 5 additions & 11 deletions sway-core/src/semantic_analysis/ast_node/declaration/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl ty::TyTraitDecl {

// insert placeholder functions representing the interface surface
// to allow methods to use those functions
ctx.namespace.insert_trait_implementation(
ctx.insert_trait_implementation(
handler,
CallPath {
prefixes: vec![],
Expand All @@ -140,7 +140,6 @@ impl ty::TyTraitDecl {
&span,
None,
false,
engines,
)?;

// Type check the items.
Expand Down Expand Up @@ -179,8 +178,6 @@ impl ty::TyTraitDecl {
interface_surface, ..
} = self;

let engines = ctx.engines();

// Retrieve the interface surface for this trait.
for item in interface_surface.iter() {
match item {
Expand All @@ -197,8 +194,7 @@ impl ty::TyTraitDecl {

// Retrieve the implemented items for this type.
for item in ctx
.namespace
.get_items_for_type_and_trait_name(engines, type_id, call_path)
.get_items_for_type_and_trait_name(type_id, call_path)
.into_iter()
{
match &item {
Expand Down Expand Up @@ -275,8 +271,7 @@ impl ty::TyTraitDecl {
.collect(),
);
for item in ctx
.namespace
.get_items_for_type_and_trait_name(engines, type_id, call_path)
.get_items_for_type_and_trait_name(type_id, call_path)
.into_iter()
{
match item {
Expand Down Expand Up @@ -309,7 +304,7 @@ impl ty::TyTraitDecl {
pub(crate) fn insert_interface_surface_and_items_into_namespace(
&self,
handler: &Handler,
ctx: TypeCheckContext,
mut ctx: TypeCheckContext,
trait_name: &CallPath,
type_arguments: &[TypeArgument],
type_id: TypeId,
Expand Down Expand Up @@ -397,7 +392,7 @@ impl ty::TyTraitDecl {
// Specifically do not check for conflicting definitions because
// this is just a temporary namespace for type checking and
// these are not actual impl blocks.
let _ = ctx.namespace.insert_trait_implementation(
let _ = ctx.insert_trait_implementation(
&Handler::default(),
trait_name.clone(),
type_arguments.to_vec(),
Expand All @@ -406,7 +401,6 @@ impl ty::TyTraitDecl {
&trait_name.span(),
Some(self.span()),
false,
engines,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl ty::TyTraitFn {
.resolve_type_with_self(
handler,
return_type.type_id,
ctx.self_type(),
&return_type.span,
EnforceTypeArguments::Yes,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ fn type_check_size_of_type(
.resolve_type_with_self(
handler,
initial_type_id,
ctx.self_type(),
&targ.span,
EnforceTypeArguments::Yes,
None,
Expand Down Expand Up @@ -253,6 +254,7 @@ fn type_check_is_reference_type(
.resolve_type_with_self(
handler,
initial_type_id,
ctx.self_type(),
&targ.span,
EnforceTypeArguments::Yes,
None,
Expand Down Expand Up @@ -306,6 +308,7 @@ fn type_check_check_str_type(
.resolve_type_with_self(
handler,
initial_type_id,
ctx.self_type(),
&targ.span,
EnforceTypeArguments::Yes,
None,
Expand Down Expand Up @@ -447,6 +450,7 @@ fn type_check_gtf(
.resolve_type_with_self(
handler,
initial_type_id,
ctx.self_type(),
&targ.span,
EnforceTypeArguments::Yes,
None,
Expand Down Expand Up @@ -678,6 +682,7 @@ fn type_check_state_store_word(
.resolve_type_with_self(
handler,
initial_type_id,
ctx.self_type(),
&targ.span,
EnforceTypeArguments::Yes,
None,
Expand Down Expand Up @@ -769,6 +774,7 @@ fn type_check_state_quad(
.resolve_type_with_self(
handler,
initial_type_id,
ctx.self_type(),
&targ.span,
EnforceTypeArguments::Yes,
None,
Expand Down Expand Up @@ -1055,6 +1061,7 @@ fn type_check_ptr_ops(
.resolve_type_with_self(
handler,
initial_type_id,
ctx.self_type(),
&targ.span,
EnforceTypeArguments::No,
None,
Expand Down Expand Up @@ -1152,6 +1159,7 @@ fn type_check_smo(
.resolve_type_with_self(
handler,
initial_type_id,
ctx.self_type(),
&targ.span,
EnforceTypeArguments::Yes,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ impl ty::TyExpression {
.resolve_type_with_self(
handler,
typed_expression.return_type,
ctx.self_type(),
&expr_span,
EnforceTypeArguments::No,
None,
Expand Down Expand Up @@ -798,6 +799,7 @@ impl ty::TyExpression {
.resolve_type_with_self(
handler,
type_engine.insert(engines, asm.return_type.clone()),
ctx.self_type(),
&asm_span,
EnforceTypeArguments::No,
None,
Expand Down Expand Up @@ -1386,12 +1388,11 @@ impl ty::TyExpression {
Err(_) => return None,
};

let const_decl_ref = match ctx.namespace.find_constant_for_type(
let const_decl_ref = match ctx.find_constant_for_type(
const_probe_handler,
struct_type_id.unwrap(),
&suffix,
ctx.self_type(),
ctx.engines(),
) {
Ok(Some(val)) => val,
Ok(None) | Err(_) => return None,
Expand Down Expand Up @@ -1531,7 +1532,7 @@ impl ty::TyExpression {
)?;

// Insert the abi methods into the namespace.
ctx.namespace.insert_trait_implementation(
ctx.insert_trait_implementation(
handler,
abi_name.clone(),
vec![],
Expand All @@ -1540,7 +1541,6 @@ impl ty::TyExpression {
&span,
Some(span.clone()),
false,
engines,
)?;

let exp = ty::TyExpression {
Expand Down
Loading

0 comments on commit d42359b

Please sign in to comment.