Skip to content

Commit

Permalink
Make resolve_type_with_self and resolve_type_without_self take `T…
Browse files Browse the repository at this point in the history
…ypeId`'s instead of `TypeInfo`'s (#1982)

* Do not rely on TypeMapping when type checking declarations.

* Prevent leaking types in impls.

* Prevent unconstrained type parameters.

* WIP

* clippy

* WIP

* Use TypeId in TypeMapping and in TraitMap.

* Add semantic type constraints.

* Update test case.

* fix

* Use TypeId inside of resolve_type_with_self and resolve_type_without_self.

* clippy

* X

* Bug is fixed.

* Add forc.lock.

* update

* Move test to inside of the SDK.

* Fix test cases.

* Add lock files.

* Fix test.
  • Loading branch information
emilyaherbert authored Jun 21, 2022
1 parent 6da682d commit 99cda91
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl TypedEnumVariant {
let mut errors = vec![];
let enum_variant_type = check!(
namespace.resolve_type_with_self(
variant.type_info.clone(),
insert_type(variant.type_info),
self_type,
&span,
EnforceTypeArguments::Yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl TypedFunctionDeclaration {
// type check the return type
let return_type = check!(
namespace.resolve_type_with_self(
return_type,
insert_type(return_type),
self_type,
&return_type_span,
EnforceTypeArguments::Yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl TypedFunctionParameter {
let mut errors = vec![];
let type_id = check!(
namespace.resolve_type_with_self(
look_up_type_id(parameter.type_id),
parameter.type_id,
self_type,
&parameter.type_span,
EnforceTypeArguments::Yes
Expand Down
28 changes: 26 additions & 2 deletions sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ impl TypedImplTrait {

// type check the type that we are implementing for
let implementing_for_type_id = check!(
namespace.resolve_type_without_self(type_implementing_for),
namespace.resolve_type_without_self(insert_type(type_implementing_for)),
return err(warnings, errors),
warnings,
errors
);

// check for unconstrained type parameters
check!(
check_for_unconstrained_type_parameters(
&new_type_parameters,
implementing_for_type_id,
&type_implementing_for_span
),
return err(warnings, errors),
warnings,
errors
Expand Down Expand Up @@ -215,7 +227,19 @@ impl TypedImplTrait {

// type check the type that we are implementing for
let implementing_for_type_id = check!(
namespace.resolve_type_without_self(type_implementing_for),
namespace.resolve_type_without_self(insert_type(type_implementing_for)),
return err(warnings, errors),
warnings,
errors
);

// check for unconstrained type parameters
check!(
check_for_unconstrained_type_parameters(
&new_type_parameters,
implementing_for_type_id,
&type_implementing_for_span
),
return err(warnings, errors),
warnings,
errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,15 @@ where
for type_argument in type_arguments.iter_mut() {
let type_id = match self_type {
Some(self_type) => namespace.resolve_type_with_self(
look_up_type_id(type_argument.type_id),
type_argument.type_id,
self_type,
&type_argument.span,
enforce_type_arguments,
module_path,
),
None => namespace.resolve_type_without_self(
look_up_type_id(type_argument.type_id),
module_path,
),
None => {
namespace.resolve_type_without_self(type_argument.type_id, module_path)
}
};
type_argument.type_id = check!(
type_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl TypedStructField {
let mut errors = vec![];
let r#type = check!(
namespace.resolve_type_with_self(
field.type_info,
insert_type(field.type_info),
self_type,
&field.type_span,
EnforceTypeArguments::Yes
Expand Down
6 changes: 3 additions & 3 deletions sway-core/src/semantic_analysis/ast_node/declaration/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
Mode, TypedCodeBlock,
},
style::is_upper_camel_case,
type_engine::{insert_type, look_up_type_id, CopyTypes, TypeMapping},
type_engine::{insert_type, CopyTypes, TypeMapping},
CallPath, CompileError, CompileResult, FunctionDeclaration, FunctionParameter, Namespace,
Supertrait, TraitDeclaration, TypeInfo, TypedDeclaration, TypedFunctionDeclaration, Visibility,
};
Expand Down Expand Up @@ -205,7 +205,7 @@ fn convert_trait_methods_to_dummy_funcs(
is_mutable: *is_mutable,
type_id: check!(
trait_namespace.resolve_type_with_self(
look_up_type_id(*type_id),
*type_id,
insert_type(TypeInfo::SelfType),
type_span,
EnforceTypeArguments::Yes
Expand All @@ -221,7 +221,7 @@ fn convert_trait_methods_to_dummy_funcs(
span: name.span(),
return_type: check!(
trait_namespace.resolve_type_with_self(
return_type.clone(),
insert_type(return_type.clone()),
insert_type(TypeInfo::SelfType),
return_type_span,
EnforceTypeArguments::Yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl TypedIntrinsicFunctionKind {
} => {
let type_id = check!(
namespace.resolve_type_with_self(
type_name,
insert_type(type_name),
self_type,
&type_span,
EnforceTypeArguments::Yes
Expand All @@ -159,7 +159,7 @@ impl TypedIntrinsicFunctionKind {
} => {
let type_id = check!(
namespace.resolve_type_with_self(
type_name,
insert_type(type_name),
self_type,
&type_span,
EnforceTypeArguments::Yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ impl TypedExpression {
// The annotation may result in a cast, which is handled in the type engine.
typed_expression.return_type = check!(
namespace.resolve_type_with_self(
look_up_type_id(typed_expression.return_type),
typed_expression.return_type,
self_type,
&expr_span,
EnforceTypeArguments::No
Expand Down Expand Up @@ -1042,7 +1042,7 @@ impl TypedExpression {
.unwrap_or_else(|| asm.whole_block_span.clone());
let return_type = check!(
namespace.resolve_type_with_self(
asm.return_type.clone(),
insert_type(asm.return_type.clone()),
self_type,
&asm_span,
EnforceTypeArguments::No
Expand Down
26 changes: 10 additions & 16 deletions sway-core/src/semantic_analysis/ast_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl TypedAstNode {
|namespace: &mut Namespace, type_ascription: TypeInfo, value| {
let type_id = check!(
namespace.resolve_type_with_self(
type_ascription,
insert_type(type_ascription),
self_type,
&node.span,
EnforceTypeArguments::No
Expand Down Expand Up @@ -289,7 +289,7 @@ impl TypedAstNode {
};
let type_ascription = check!(
namespace.resolve_type_with_self(
type_ascription,
insert_type(type_ascription),
self_type,
&type_ascription_span,
EnforceTypeArguments::Yes,
Expand Down Expand Up @@ -478,13 +478,9 @@ impl TypedAstNode {
}
Declaration::StorageDeclaration(StorageDeclaration { span, fields }) => {
let mut fields_buf = Vec::with_capacity(fields.len());
for StorageField {
name,
type_info: r#type,
} in fields
{
for StorageField { name, type_info } in fields {
let r#type = check!(
namespace.resolve_type_without_self(r#type),
namespace.resolve_type_without_self(insert_type(type_info)),
return err(warnings, errors),
warnings,
errors
Expand Down Expand Up @@ -778,7 +774,7 @@ fn type_check_interface_surface(
is_mutable,
type_id: check!(
namespace.resolve_type_with_self(
look_up_type_id(type_id),
type_id,
insert_type(TypeInfo::SelfType),
&type_span,
EnforceTypeArguments::Yes
Expand All @@ -793,7 +789,7 @@ fn type_check_interface_surface(
.collect(),
return_type: check!(
namespace.resolve_type_with_self(
return_type,
insert_type(return_type),
insert_type(TypeInfo::SelfType),
&return_type_span,
EnforceTypeArguments::Yes
Expand Down Expand Up @@ -830,13 +826,11 @@ fn type_check_trait_methods(
{
parameters.clone().into_iter().for_each(
|FunctionParameter {
name,
type_id: ref r#type,
..
name, ref type_id, ..
}| {
let r#type = check!(
namespace.resolve_type_with_self(
look_up_type_id(*r#type),
*type_id,
insert_type(TypeInfo::SelfType),
&name.span(),
EnforceTypeArguments::Yes
Expand Down Expand Up @@ -916,7 +910,7 @@ fn type_check_trait_methods(
is_mutable,
type_id: check!(
namespace.resolve_type_with_self(
look_up_type_id(type_id),
type_id,
crate::type_engine::insert_type(TypeInfo::SelfType),
&type_span,
EnforceTypeArguments::Yes
Expand All @@ -934,7 +928,7 @@ fn type_check_trait_methods(
// TODO check code block implicit return
let return_type = check!(
namespace.resolve_type_with_self(
return_type,
insert_type(return_type),
self_type,
&return_type_span,
EnforceTypeArguments::Yes
Expand Down
15 changes: 5 additions & 10 deletions sway-core/src/semantic_analysis/namespace/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use crate::{
declaration::{EnforceTypeArguments, Monomorphize, MonomorphizeHelper},
},
type_engine::*,
CallPath, CompileResult, Ident, TypeArgument, TypeInfo, TypedDeclaration,
TypedFunctionDeclaration,
CallPath, CompileResult, Ident, TypeArgument, TypedDeclaration, TypedFunctionDeclaration,
};

use super::{module::Module, root::Root, submodule_namespace::SubmoduleNamespace, Path, PathBuf};
Expand Down Expand Up @@ -100,13 +99,13 @@ impl Namespace {
/// Short-hand for calling [Root::resolve_type_with_self] on `root` with the `mod_path`.
pub(crate) fn resolve_type_with_self(
&mut self,
type_info: TypeInfo,
type_id: TypeId,
self_type: TypeId,
span: &Span,
enforce_type_args: EnforceTypeArguments,
) -> CompileResult<TypeId> {
self.root.resolve_type_with_self(
type_info,
type_id,
self_type,
span,
enforce_type_args,
Expand All @@ -115,12 +114,8 @@ impl Namespace {
}

/// Short-hand for calling [Root::resolve_type_without_self] on `root` and with the `mod_path`.
pub(crate) fn resolve_type_without_self(
&mut self,
type_info: TypeInfo,
) -> CompileResult<TypeId> {
self.root
.resolve_type_without_self(type_info, &self.mod_path)
pub(crate) fn resolve_type_without_self(&mut self, type_id: TypeId) -> CompileResult<TypeId> {
self.root.resolve_type_without_self(type_id, &self.mod_path)
}

/// Short-hand for calling `monomorphize` from the `Monomorphize` trait, on `root` with the `mod_path`.
Expand Down
Loading

0 comments on commit 99cda91

Please sign in to comment.