Skip to content

Commit

Permalink
Move monomorphization conceptually inside of the type engine (FuelLab…
Browse files Browse the repository at this point in the history
…s#2093)

* 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

* Remove self_type from monomorphization.

* Add conceptual distinction between replacing TypeInfo::Self and monomorphization.

* Bug is fixed.

* Add forc.lock.

* update

* Move test to inside of the SDK.

* Fix test cases.

* Add lock files.

* Fix test.

* Move the stuff.

* Move monomorphization conceptually inside of the type engine.

* Remove commented out test.
  • Loading branch information
emilyaherbert authored Jun 29, 2022
1 parent c118e2d commit a3dae09
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 300 deletions.
2 changes: 0 additions & 2 deletions sway-core/src/semantic_analysis/ast_node/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod abi;
mod r#enum;
mod function;
mod impl_trait;
mod monomorphize;
mod storage;
mod r#struct;
mod r#trait;
Expand All @@ -11,7 +10,6 @@ mod variable;
pub use abi::*;
pub use function::*;
pub use impl_trait::*;
pub(crate) use monomorphize::*;
pub use r#enum::*;
pub use r#struct::*;
pub use r#trait::*;
Expand Down
11 changes: 2 additions & 9 deletions sway-core/src/semantic_analysis/ast_node/declaration/enum.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::{
error::*,
namespace::*,
parse_tree::*,
semantic_analysis::*,
type_engine::{
insert_type, look_up_type_id, CopyTypes, CreateTypeId, ReplaceSelfType, TypeId,
TypeMapping, TypeParameter,
insert_type, look_up_type_id, CopyTypes, CreateTypeId, EnforceTypeArguments,
MonomorphizeHelper, ReplaceSelfType, TypeId, TypeMapping, TypeParameter,
},
types::{JsonAbiString, ToJsonAbi},
TypeInfo,
Expand Down Expand Up @@ -63,19 +62,13 @@ impl Spanned for TypedEnumDeclaration {
}

impl MonomorphizeHelper for TypedEnumDeclaration {
type Output = TypedEnumDeclaration;

fn type_parameters(&self) -> &[TypeParameter] {
&self.type_parameters
}

fn name(&self) -> &Ident {
&self.name
}

fn monomorphize_inner(self, type_mapping: &TypeMapping, namespace: &mut Items) -> Self::Output {
monomorphize_inner(self, type_mapping, namespace)
}
}

impl TypedEnumDeclaration {
Expand Down
16 changes: 1 addition & 15 deletions sway-core/src/semantic_analysis/ast_node/declaration/function.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod function_parameter;
pub use function_parameter::*;

use crate::{
error::*, namespace::*, parse_tree::*, semantic_analysis::*, style::*, type_engine::*, types::*,
};
use crate::{error::*, parse_tree::*, semantic_analysis::*, style::*, type_engine::*, types::*};
use fuels_types::{Function, Property};
use sha2::{Digest, Sha256};
use sway_types::{Ident, Span, Spanned};
Expand Down Expand Up @@ -74,25 +72,13 @@ impl Spanned for TypedFunctionDeclaration {
}

impl MonomorphizeHelper for TypedFunctionDeclaration {
type Output = TypedFunctionDeclaration;

fn type_parameters(&self) -> &[TypeParameter] {
&self.type_parameters
}

fn name(&self) -> &Ident {
&self.name
}

fn monomorphize_inner(
self,
type_mapping: &TypeMapping,
_namespace: &mut Items,
) -> Self::Output {
let mut new_decl = self;
new_decl.copy_types(type_mapping);
new_decl
}
}

impl ToJsonAbi for TypedFunctionDeclaration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
error::ok,
semantic_analysis::{
EnforceTypeArguments, IsConstant, TypeCheckContext, TypedExpression,
TypedExpressionVariant, TypedVariableDeclaration, VariableMutability,
IsConstant, TypeCheckContext, TypedExpression, TypedExpressionVariant,
TypedVariableDeclaration, VariableMutability,
},
type_engine::*,
CompileResult, FunctionParameter, Ident, TypedDeclaration,
Expand Down
176 changes: 0 additions & 176 deletions sway-core/src/semantic_analysis/ast_node/declaration/monomorphize.rs

This file was deleted.

10 changes: 1 addition & 9 deletions sway-core/src/semantic_analysis/ast_node/declaration/struct.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{
error::*, namespace::*, parse_tree::*, semantic_analysis::*, type_engine::*, types::*,
};
use crate::{error::*, parse_tree::*, semantic_analysis::*, type_engine::*, types::*};
use fuels_types::Property;
use std::hash::{Hash, Hasher};
use sway_types::{Ident, Span, Spanned};
Expand Down Expand Up @@ -54,19 +52,13 @@ impl Spanned for TypedStructDeclaration {
}

impl MonomorphizeHelper for TypedStructDeclaration {
type Output = TypedStructDeclaration;

fn type_parameters(&self) -> &[TypeParameter] {
&self.type_parameters
}

fn name(&self) -> &Ident {
&self.name
}

fn monomorphize_inner(self, type_mapping: &TypeMapping, namespace: &mut Items) -> Self::Output {
monomorphize_inner(self, type_mapping, namespace)
}
}

impl TypedStructDeclaration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sway_types::Span;

use crate::{
error::{err, ok},
semantic_analysis::{EnforceTypeArguments, TypeCheckContext},
semantic_analysis::TypeCheckContext,
type_engine::*,
types::DeterministicallyAborts,
CompileError, CompileResult, IntrinsicFunctionKind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use sway_types::{Ident, Span, Spanned};

use crate::{
error::{err, ok},
semantic_analysis::{declaration::EnforceTypeArguments, TypeCheckContext, TypedEnumVariant},
type_engine::{insert_type, CreateTypeId, TypeArgument, TypeId},
semantic_analysis::{TypeCheckContext, TypedEnumVariant},
type_engine::{insert_type, CreateTypeId, EnforceTypeArguments, TypeArgument, TypeId},
CompileError, CompileResult, Literal, Scrutinee, StructScrutineeField, TypeInfo,
};

Expand Down Expand Up @@ -70,15 +70,20 @@ impl TypedScrutinee {
warnings,
errors
);
let struct_decl = check!(
let mut struct_decl = check!(
unknown_decl.expect_struct().cloned(),
return err(warnings, errors),
warnings,
errors
);
// monomorphize the struct definition
let struct_decl = check!(
ctx.monomorphize(struct_decl, vec!(), EnforceTypeArguments::No, None),
check!(
ctx.monomorphize(
&mut struct_decl,
vec!(),
EnforceTypeArguments::No,
&struct_name.span()
),
return err(warnings, errors),
warnings,
errors
Expand Down Expand Up @@ -164,15 +169,20 @@ impl TypedScrutinee {
warnings,
errors
);
let enum_decl = check!(
let mut enum_decl = check!(
unknown_decl.expect_enum().cloned(),
return err(warnings, errors),
warnings,
errors
);
// monomorphize the enum definition
let enum_decl = check!(
ctx.monomorphize(enum_decl, vec!(), EnforceTypeArguments::No, None),
check!(
ctx.monomorphize(
&mut enum_decl,
vec!(),
EnforceTypeArguments::No,
&enum_name.span()
),
return err(warnings, errors),
warnings,
errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,17 +976,21 @@ impl TypedExpression {
warnings,
errors
);
let struct_decl = check!(
unknown_decl.expect_struct(),
let mut struct_decl = check!(
unknown_decl.expect_struct().cloned(),
return err(warnings, errors),
warnings,
errors
)
.clone();
);

// monomorphize the struct definition
let mut struct_decl = check!(
ctx.monomorphize(struct_decl, type_arguments, EnforceTypeArguments::No, None),
check!(
ctx.monomorphize(
&mut struct_decl,
type_arguments,
EnforceTypeArguments::No,
&call_path.span()
),
return err(warnings, errors),
warnings,
errors
Expand Down
Loading

0 comments on commit a3dae09

Please sign in to comment.