Skip to content

Commit a3dae09

Browse files
Move monomorphization conceptually inside of the type engine (#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.
1 parent c118e2d commit a3dae09

File tree

16 files changed

+228
-300
lines changed

16 files changed

+228
-300
lines changed

sway-core/src/semantic_analysis/ast_node/declaration.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ mod abi;
22
mod r#enum;
33
mod function;
44
mod impl_trait;
5-
mod monomorphize;
65
mod storage;
76
mod r#struct;
87
mod r#trait;
@@ -11,7 +10,6 @@ mod variable;
1110
pub use abi::*;
1211
pub use function::*;
1312
pub use impl_trait::*;
14-
pub(crate) use monomorphize::*;
1513
pub use r#enum::*;
1614
pub use r#struct::*;
1715
pub use r#trait::*;

sway-core/src/semantic_analysis/ast_node/declaration/enum.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::{
22
error::*,
3-
namespace::*,
43
parse_tree::*,
54
semantic_analysis::*,
65
type_engine::{
7-
insert_type, look_up_type_id, CopyTypes, CreateTypeId, ReplaceSelfType, TypeId,
8-
TypeMapping, TypeParameter,
6+
insert_type, look_up_type_id, CopyTypes, CreateTypeId, EnforceTypeArguments,
7+
MonomorphizeHelper, ReplaceSelfType, TypeId, TypeMapping, TypeParameter,
98
},
109
types::{JsonAbiString, ToJsonAbi},
1110
TypeInfo,
@@ -63,19 +62,13 @@ impl Spanned for TypedEnumDeclaration {
6362
}
6463

6564
impl MonomorphizeHelper for TypedEnumDeclaration {
66-
type Output = TypedEnumDeclaration;
67-
6865
fn type_parameters(&self) -> &[TypeParameter] {
6966
&self.type_parameters
7067
}
7168

7269
fn name(&self) -> &Ident {
7370
&self.name
7471
}
75-
76-
fn monomorphize_inner(self, type_mapping: &TypeMapping, namespace: &mut Items) -> Self::Output {
77-
monomorphize_inner(self, type_mapping, namespace)
78-
}
7972
}
8073

8174
impl TypedEnumDeclaration {

sway-core/src/semantic_analysis/ast_node/declaration/function.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
mod function_parameter;
22
pub use function_parameter::*;
33

4-
use crate::{
5-
error::*, namespace::*, parse_tree::*, semantic_analysis::*, style::*, type_engine::*, types::*,
6-
};
4+
use crate::{error::*, parse_tree::*, semantic_analysis::*, style::*, type_engine::*, types::*};
75
use fuels_types::{Function, Property};
86
use sha2::{Digest, Sha256};
97
use sway_types::{Ident, Span, Spanned};
@@ -74,25 +72,13 @@ impl Spanned for TypedFunctionDeclaration {
7472
}
7573

7674
impl MonomorphizeHelper for TypedFunctionDeclaration {
77-
type Output = TypedFunctionDeclaration;
78-
7975
fn type_parameters(&self) -> &[TypeParameter] {
8076
&self.type_parameters
8177
}
8278

8379
fn name(&self) -> &Ident {
8480
&self.name
8581
}
86-
87-
fn monomorphize_inner(
88-
self,
89-
type_mapping: &TypeMapping,
90-
_namespace: &mut Items,
91-
) -> Self::Output {
92-
let mut new_decl = self;
93-
new_decl.copy_types(type_mapping);
94-
new_decl
95-
}
9682
}
9783

9884
impl ToJsonAbi for TypedFunctionDeclaration {

sway-core/src/semantic_analysis/ast_node/declaration/function/function_parameter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
error::ok,
33
semantic_analysis::{
4-
EnforceTypeArguments, IsConstant, TypeCheckContext, TypedExpression,
5-
TypedExpressionVariant, TypedVariableDeclaration, VariableMutability,
4+
IsConstant, TypeCheckContext, TypedExpression, TypedExpressionVariant,
5+
TypedVariableDeclaration, VariableMutability,
66
},
77
type_engine::*,
88
CompileResult, FunctionParameter, Ident, TypedDeclaration,

sway-core/src/semantic_analysis/ast_node/declaration/monomorphize.rs

Lines changed: 0 additions & 176 deletions
This file was deleted.

sway-core/src/semantic_analysis/ast_node/declaration/struct.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::{
2-
error::*, namespace::*, parse_tree::*, semantic_analysis::*, type_engine::*, types::*,
3-
};
1+
use crate::{error::*, parse_tree::*, semantic_analysis::*, type_engine::*, types::*};
42
use fuels_types::Property;
53
use std::hash::{Hash, Hasher};
64
use sway_types::{Ident, Span, Spanned};
@@ -54,19 +52,13 @@ impl Spanned for TypedStructDeclaration {
5452
}
5553

5654
impl MonomorphizeHelper for TypedStructDeclaration {
57-
type Output = TypedStructDeclaration;
58-
5955
fn type_parameters(&self) -> &[TypeParameter] {
6056
&self.type_parameters
6157
}
6258

6359
fn name(&self) -> &Ident {
6460
&self.name
6561
}
66-
67-
fn monomorphize_inner(self, type_mapping: &TypeMapping, namespace: &mut Items) -> Self::Output {
68-
monomorphize_inner(self, type_mapping, namespace)
69-
}
7062
}
7163

7264
impl TypedStructDeclaration {

sway-core/src/semantic_analysis/ast_node/expression/intrinsic_function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use sway_types::Span;
44

55
use crate::{
66
error::{err, ok},
7-
semantic_analysis::{EnforceTypeArguments, TypeCheckContext},
7+
semantic_analysis::TypeCheckContext,
88
type_engine::*,
99
types::DeterministicallyAborts,
1010
CompileError, CompileResult, IntrinsicFunctionKind,

sway-core/src/semantic_analysis/ast_node/expression/match_expression/typed/typed_scrutinee.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use sway_types::{Ident, Span, Spanned};
22

33
use crate::{
44
error::{err, ok},
5-
semantic_analysis::{declaration::EnforceTypeArguments, TypeCheckContext, TypedEnumVariant},
6-
type_engine::{insert_type, CreateTypeId, TypeArgument, TypeId},
5+
semantic_analysis::{TypeCheckContext, TypedEnumVariant},
6+
type_engine::{insert_type, CreateTypeId, EnforceTypeArguments, TypeArgument, TypeId},
77
CompileError, CompileResult, Literal, Scrutinee, StructScrutineeField, TypeInfo,
88
};
99

@@ -70,15 +70,20 @@ impl TypedScrutinee {
7070
warnings,
7171
errors
7272
);
73-
let struct_decl = check!(
73+
let mut struct_decl = check!(
7474
unknown_decl.expect_struct().cloned(),
7575
return err(warnings, errors),
7676
warnings,
7777
errors
7878
);
7979
// monomorphize the struct definition
80-
let struct_decl = check!(
81-
ctx.monomorphize(struct_decl, vec!(), EnforceTypeArguments::No, None),
80+
check!(
81+
ctx.monomorphize(
82+
&mut struct_decl,
83+
vec!(),
84+
EnforceTypeArguments::No,
85+
&struct_name.span()
86+
),
8287
return err(warnings, errors),
8388
warnings,
8489
errors
@@ -164,15 +169,20 @@ impl TypedScrutinee {
164169
warnings,
165170
errors
166171
);
167-
let enum_decl = check!(
172+
let mut enum_decl = check!(
168173
unknown_decl.expect_enum().cloned(),
169174
return err(warnings, errors),
170175
warnings,
171176
errors
172177
);
173178
// monomorphize the enum definition
174-
let enum_decl = check!(
175-
ctx.monomorphize(enum_decl, vec!(), EnforceTypeArguments::No, None),
179+
check!(
180+
ctx.monomorphize(
181+
&mut enum_decl,
182+
vec!(),
183+
EnforceTypeArguments::No,
184+
&enum_name.span()
185+
),
176186
return err(warnings, errors),
177187
warnings,
178188
errors

sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -976,17 +976,21 @@ impl TypedExpression {
976976
warnings,
977977
errors
978978
);
979-
let struct_decl = check!(
980-
unknown_decl.expect_struct(),
979+
let mut struct_decl = check!(
980+
unknown_decl.expect_struct().cloned(),
981981
return err(warnings, errors),
982982
warnings,
983983
errors
984-
)
985-
.clone();
984+
);
986985

987986
// monomorphize the struct definition
988-
let mut struct_decl = check!(
989-
ctx.monomorphize(struct_decl, type_arguments, EnforceTypeArguments::No, None),
987+
check!(
988+
ctx.monomorphize(
989+
&mut struct_decl,
990+
type_arguments,
991+
EnforceTypeArguments::No,
992+
&call_path.span()
993+
),
990994
return err(warnings, errors),
991995
warnings,
992996
errors

0 commit comments

Comments
 (0)