Skip to content

Commit

Permalink
Partially resolves issue #5050
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
anton-trunov committed Sep 5, 2023
1 parent 36ccf52 commit 5221ba3
Show file tree
Hide file tree
Showing 26 changed files with 450 additions and 449 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
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
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
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
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 5221ba3

Please sign in to comment.