Skip to content

Commit

Permalink
Accumulate typechecking methods in impl TypeCheckContext, part 2 (#…
Browse files Browse the repository at this point in the history
…5088)

## Description

Partially resolves issue #5050.

All the `impl TypeEngine` methods from the file `type_system/engine.rs`
that also take `Engines` and `Namespace` as parameters have been moved
into `impl TypeCheckContext`.

All the `impl Root` methods from the file `namespace/root.rs` that also
take `Engines` as parameters have been moved into `impl
TypeCheckContext`.

I'm splitting the refactoring into several PRs to solicit early
feedback, make review process a bit easier and potentially create fewer
merge conflicts.

List of changes:

- `type_engine.monomorphize` -> `ctx.monomorphize_with_modpath`
- `type_engine.resolve` -> `ctx.resolve`
- `type_engine.resolve_with_self` -> `ctx.resolve_with_self`
- `type_engine.MonomorphizeHelper` -> `ctx.MonomorphizeHelper`
- `type_engine.EnforceTypeArguments` -> `ctx.EnforceTypeArguments`
- `namespace.root.resolve_call_path_with_visibility_check` ->
`ctx.resolve_call_path_with_visibility_check_and_modpath`

## 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] 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.
  • Loading branch information
anton-trunov authored Sep 6, 2023
1 parent 159a240 commit e00df8a
Show file tree
Hide file tree
Showing 29 changed files with 456 additions and 468 deletions.
1 change: 1 addition & 0 deletions sway-core/src/language/ty/declaration/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use sway_types::{Ident, Named, Span, Spanned};
use crate::{
engine_threading::*,
language::{CallPath, Visibility},
semantic_analysis::type_check_context::MonomorphizeHelper,
transform,
type_system::*,
};
Expand Down
2 changes: 2 additions & 0 deletions sway-core/src/language/ty/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
use sha2::{Digest, Sha256};
use sway_error::handler::{ErrorEmitted, Handler};

use crate::semantic_analysis::type_check_context::MonomorphizeHelper;

use crate::{
decl_engine::*,
engine_threading::*,
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/language/ty/declaration/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use sway_types::{Ident, Named, Span, Spanned};
use crate::{
engine_threading::*,
language::{CallPath, Visibility},
semantic_analysis::type_check_context::MonomorphizeHelper,
transform,
type_system::*,
};
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/language/ty/declaration/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
},
engine_threading::*,
language::{parsed, Visibility},
semantic_analysis::type_check_context::MonomorphizeHelper,
transform,
type_system::*,
};
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/language/ty/declaration/trait_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use sway_types::{Ident, Named, Span, Spanned};
use crate::{
engine_threading::*,
language::{ty::*, Purity},
semantic_analysis::type_check_context::MonomorphizeHelper,
transform,
type_system::*,
};
Expand Down
3 changes: 1 addition & 2 deletions sway-core/src/language/ty/expression/expression_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,7 @@ impl ReplaceDecls for TyExpressionVariant {
for type_param in method.type_parameters.iter() {
let TypeParameter { type_id, .. } = type_param;

ctx.namespace
.insert_trait_implementation_for_type(engines, *type_id);
ctx.insert_trait_implementation_for_type(*type_id);
}

// Handle the trait constraints. This includes checking to see if the trait
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/semantic_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod module;
pub mod namespace;
mod node_dependencies;
mod program;
mod type_check_context;
pub(crate) mod type_check_context;
pub use ast_node::*;
pub use namespace::Namespace;
pub(crate) use type_check_context::TypeCheckContext;
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
ty::{self, TyConstantDecl},
CallPath,
},
semantic_analysis::*,
EnforceTypeArguments, Engines, TypeInfo,
semantic_analysis::{type_check_context::EnforceTypeArguments, *},
Engines, TypeInfo,
};

impl ty::TyConstantDecl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sway_types::{Named, Spanned};
use crate::{
decl_engine::{DeclEngineInsert, DeclRef, ReplaceFunctionImplementingType},
language::{parsed, ty},
semantic_analysis::TypeCheckContext,
semantic_analysis::{type_check_context::EnforceTypeArguments, TypeCheckContext},
type_system::*,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sway_error::handler::{ErrorEmitted, Handler};

use crate::{
language::{parsed::*, ty, CallPath},
semantic_analysis::*,
semantic_analysis::{type_check_context::EnforceTypeArguments, *},
type_system::*,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
ty::{self, TyCodeBlock},
Visibility,
},
semantic_analysis::*,
semantic_analysis::{type_check_context::EnforceTypeArguments, *},
type_system::*,
};
use sway_types::{style::is_snake_case, Spanned};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
language::{parsed::FunctionParameter, ty},
semantic_analysis::TypeCheckContext,
semantic_analysis::{type_check_context::EnforceTypeArguments, TypeCheckContext},
type_system::*,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use crate::{
*,
},
namespace::TryInsertingTraitImplOnFailure,
semantic_analysis::{AbiMode, ConstShadowingMode, TypeCheckContext},
semantic_analysis::{
type_check_context::EnforceTypeArguments, AbiMode, ConstShadowingMode, TypeCheckContext,
},
type_system::*,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use sway_error::handler::{ErrorEmitted, Handler};

use crate::{
language::{parsed::*, ty, CallPath},
semantic_analysis::*,
semantic_analysis::{type_check_context::EnforceTypeArguments, *},
type_system::*,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use sway_error::error::CompileError;
use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::{Span, Spanned};

use crate::semantic_analysis::type_check_context::EnforceTypeArguments;
use crate::{
language::{parsed, ty},
semantic_analysis::TypeCheckContext,
EnforceTypeArguments, TypeId,
TypeId,
};

#[derive(Clone, PartialEq, Eq)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use sway_types::{Span, Spanned};
use crate::{
decl_engine::DeclId,
language::{parsed, ty, Visibility},
semantic_analysis::type_check_context::EnforceTypeArguments,
};
use sway_error::handler::{ErrorEmitted, Handler};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sway_types::Span;
use crate::{
engine_threading::*,
language::{parsed::Expression, ty},
semantic_analysis::TypeCheckContext,
semantic_analysis::{type_check_context::EnforceTypeArguments, TypeCheckContext},
type_system::*,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
ty::{self, TyDecl},
CallPath,
},
semantic_analysis::TypeCheckContext,
semantic_analysis::{type_check_context::EnforceTypeArguments, TypeCheckContext},
type_system::*,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
ty::{self, TyImplItem},
*,
},
semantic_analysis::{expression::ReachableReport, *},
semantic_analysis::{expression::ReachableReport, type_check_context::EnforceTypeArguments, *},
transform::to_parsed_lang::type_name_to_type_info_opt,
type_system::*,
Engines,
Expand Down Expand Up @@ -1009,8 +1009,7 @@ impl ty::TyExpression {

// take any trait items that apply to `StorageKey<T>` and copy them to the
// monomorphized type
ctx.namespace
.insert_trait_implementation_for_type(engines, access_type);
ctx.insert_trait_implementation_for_type(access_type);

Ok(ty::TyExpression {
expression: ty::TyExpressionVariant::StorageAccess(storage_access),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub(crate) fn instantiate_function_application(
span: Span,
) -> Result<ty::TyExpression, ErrorEmitted> {
let decl_engine = ctx.engines.de();
let engines = ctx.engines();

let mut function_decl = decl_engine.get_function(&function_decl_ref);

Expand Down Expand Up @@ -59,8 +58,7 @@ pub(crate) fn instantiate_function_application(

// Retrieve the implemented traits for the type of the return type and
// insert them in the broader namespace.
ctx.namespace
.insert_trait_implementation_for_type(engines, function_decl.return_type.type_id);
ctx.insert_trait_implementation_for_type(function_decl.return_type.type_id);

// Handle the trait constraints. This includes checking to see if the trait
// constraints are satisfied and replacing old decl ids based on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
decl_engine::{DeclEngineInsert, DeclRefFunction, ReplaceDecls, UpdateConstantExpression},
language::{parsed::*, ty, *},
namespace::TryInsertingTraitImplOnFailure,
semantic_analysis::*,
semantic_analysis::{type_check_context::EnforceTypeArguments, *},
type_system::*,
};
use ast_node::typed_expression::check_function_arguments_arity;
Expand Down Expand Up @@ -325,8 +325,7 @@ pub(crate) fn type_check_method_application(

// Retrieve the implemented traits for the type of the return type and
// insert them in the broader namespace.
ctx.namespace
.insert_trait_implementation_for_type(engines, method.return_type.type_id);
ctx.insert_trait_implementation_for_type(method.return_type.type_id);

// Handle the trait constraints. This includes checking to see if the trait
// constraints are satisfied and replacing old decl ids based on the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sway_types::{Ident, Span, Spanned};
use crate::{
decl_engine::DeclRefStruct,
language::{parsed::*, ty, CallPath},
semantic_analysis::TypeCheckContext,
semantic_analysis::{type_check_context::EnforceTypeArguments, TypeCheckContext},
type_system::*,
};

Expand Down
8 changes: 0 additions & 8 deletions sway-core/src/semantic_analysis/namespace/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,6 @@ impl Items {
})
}

pub(crate) fn insert_trait_implementation_for_type(
&mut self,
engines: &Engines,
type_id: TypeId,
) {
self.implemented_traits.insert_for_type(engines, type_id);
}

pub fn get_items_for_type(&self, engines: &Engines, type_id: TypeId) -> Vec<ty::TyTraitItem> {
self.implemented_traits.get_items_for_type(engines, type_id)
}
Expand Down
55 changes: 2 additions & 53 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use sway_error::{
error::CompileError,
handler::{ErrorEmitted, Handler},
};
use sway_types::Spanned;
use sway_utils::iter_prefixes;
use sway_error::handler::{ErrorEmitted, Handler};

use crate::{
language::{ty, CallPath},
Engines, Ident,
Ident,
};

use super::{module::Module, namespace::Namespace, Path};
Expand Down Expand Up @@ -44,52 +39,6 @@ impl Root {
self.resolve_symbol(handler, &symbol_path, &call_path.suffix)
}

/// Resolve a symbol that is potentially prefixed with some path, e.g. `foo::bar::symbol`.
///
/// This will concatenate the `mod_path` with the `call_path`'s prefixes and
/// then calling `resolve_symbol` with the resulting path and call_path's suffix.
///
/// The `mod_path` is significant here as we assume the resolution is done within the
/// context of the module pointed to by `mod_path` and will only check the call path prefixes
/// and the symbol's own visibility
pub(crate) fn resolve_call_path_with_visibility_check(
&self,
handler: &Handler,
engines: &Engines,
mod_path: &Path,
call_path: &CallPath,
) -> Result<&ty::TyDecl, ErrorEmitted> {
let decl = self.resolve_call_path(handler, mod_path, call_path)?;

// In case there are no prefixes we don't need to check visibility
if call_path.prefixes.is_empty() {
return Ok(decl);
}

// check the visibility of the call path elements
// we don't check the first prefix because direct children are always accessible
for prefix in iter_prefixes(&call_path.prefixes).skip(1) {
let module = self.check_submodule(handler, prefix)?;
if module.visibility.is_private() {
let prefix_last = prefix[prefix.len() - 1].clone();
handler.emit_err(CompileError::ImportPrivateModule {
span: prefix_last.span(),
name: prefix_last,
});
}
}

// check the visibility of the symbol itself
if !decl.visibility(engines.de()).is_public() {
handler.emit_err(CompileError::ImportPrivateSymbol {
name: call_path.suffix.clone(),
span: call_path.suffix.span(),
});
}

Ok(decl)
}

/// Given a path to a module and the identifier of a symbol within that module, resolve its
/// declaration.
///
Expand Down
Loading

0 comments on commit e00df8a

Please sign in to comment.