Skip to content

Commit 99cda91

Browse files
Make resolve_type_with_self and resolve_type_without_self take TypeId'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.
1 parent 6da682d commit 99cda91

File tree

12 files changed

+70
-61
lines changed

12 files changed

+70
-61
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl TypedEnumVariant {
225225
let mut errors = vec![];
226226
let enum_variant_type = check!(
227227
namespace.resolve_type_with_self(
228-
variant.type_info.clone(),
228+
insert_type(variant.type_info),
229229
self_type,
230230
&span,
231231
EnforceTypeArguments::Yes

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl TypedFunctionDeclaration {
179179
// type check the return type
180180
let return_type = check!(
181181
namespace.resolve_type_with_self(
182-
return_type,
182+
insert_type(return_type),
183183
self_type,
184184
&return_type_span,
185185
EnforceTypeArguments::Yes

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl TypedFunctionParameter {
4545
let mut errors = vec![];
4646
let type_id = check!(
4747
namespace.resolve_type_with_self(
48-
look_up_type_id(parameter.type_id),
48+
parameter.type_id,
4949
self_type,
5050
&parameter.type_span,
5151
EnforceTypeArguments::Yes

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,19 @@ impl TypedImplTrait {
6666

6767
// type check the type that we are implementing for
6868
let implementing_for_type_id = check!(
69-
namespace.resolve_type_without_self(type_implementing_for),
69+
namespace.resolve_type_without_self(insert_type(type_implementing_for)),
70+
return err(warnings, errors),
71+
warnings,
72+
errors
73+
);
74+
75+
// check for unconstrained type parameters
76+
check!(
77+
check_for_unconstrained_type_parameters(
78+
&new_type_parameters,
79+
implementing_for_type_id,
80+
&type_implementing_for_span
81+
),
7082
return err(warnings, errors),
7183
warnings,
7284
errors
@@ -215,7 +227,19 @@ impl TypedImplTrait {
215227

216228
// type check the type that we are implementing for
217229
let implementing_for_type_id = check!(
218-
namespace.resolve_type_without_self(type_implementing_for),
230+
namespace.resolve_type_without_self(insert_type(type_implementing_for)),
231+
return err(warnings, errors),
232+
warnings,
233+
errors
234+
);
235+
236+
// check for unconstrained type parameters
237+
check!(
238+
check_for_unconstrained_type_parameters(
239+
&new_type_parameters,
240+
implementing_for_type_id,
241+
&type_implementing_for_span
242+
),
219243
return err(warnings, errors),
220244
warnings,
221245
errors

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,15 @@ where
110110
for type_argument in type_arguments.iter_mut() {
111111
let type_id = match self_type {
112112
Some(self_type) => namespace.resolve_type_with_self(
113-
look_up_type_id(type_argument.type_id),
113+
type_argument.type_id,
114114
self_type,
115115
&type_argument.span,
116116
enforce_type_arguments,
117117
module_path,
118118
),
119-
None => namespace.resolve_type_without_self(
120-
look_up_type_id(type_argument.type_id),
121-
module_path,
122-
),
119+
None => {
120+
namespace.resolve_type_without_self(type_argument.type_id, module_path)
121+
}
123122
};
124123
type_argument.type_id = check!(
125124
type_id,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl TypedStructField {
210210
let mut errors = vec![];
211211
let r#type = check!(
212212
namespace.resolve_type_with_self(
213-
field.type_info,
213+
insert_type(field.type_info),
214214
self_type,
215215
&field.type_span,
216216
EnforceTypeArguments::Yes

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
Mode, TypedCodeBlock,
99
},
1010
style::is_upper_camel_case,
11-
type_engine::{insert_type, look_up_type_id, CopyTypes, TypeMapping},
11+
type_engine::{insert_type, CopyTypes, TypeMapping},
1212
CallPath, CompileError, CompileResult, FunctionDeclaration, FunctionParameter, Namespace,
1313
Supertrait, TraitDeclaration, TypeInfo, TypedDeclaration, TypedFunctionDeclaration, Visibility,
1414
};
@@ -205,7 +205,7 @@ fn convert_trait_methods_to_dummy_funcs(
205205
is_mutable: *is_mutable,
206206
type_id: check!(
207207
trait_namespace.resolve_type_with_self(
208-
look_up_type_id(*type_id),
208+
*type_id,
209209
insert_type(TypeInfo::SelfType),
210210
type_span,
211211
EnforceTypeArguments::Yes
@@ -221,7 +221,7 @@ fn convert_trait_methods_to_dummy_funcs(
221221
span: name.span(),
222222
return_type: check!(
223223
trait_namespace.resolve_type_with_self(
224-
return_type.clone(),
224+
insert_type(return_type.clone()),
225225
insert_type(TypeInfo::SelfType),
226226
return_type_span,
227227
EnforceTypeArguments::Yes

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl TypedIntrinsicFunctionKind {
139139
} => {
140140
let type_id = check!(
141141
namespace.resolve_type_with_self(
142-
type_name,
142+
insert_type(type_name),
143143
self_type,
144144
&type_span,
145145
EnforceTypeArguments::Yes
@@ -159,7 +159,7 @@ impl TypedIntrinsicFunctionKind {
159159
} => {
160160
let type_id = check!(
161161
namespace.resolve_type_with_self(
162-
type_name,
162+
insert_type(type_name),
163163
self_type,
164164
&type_span,
165165
EnforceTypeArguments::Yes

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ impl TypedExpression {
617617
// The annotation may result in a cast, which is handled in the type engine.
618618
typed_expression.return_type = check!(
619619
namespace.resolve_type_with_self(
620-
look_up_type_id(typed_expression.return_type),
620+
typed_expression.return_type,
621621
self_type,
622622
&expr_span,
623623
EnforceTypeArguments::No
@@ -1042,7 +1042,7 @@ impl TypedExpression {
10421042
.unwrap_or_else(|| asm.whole_block_span.clone());
10431043
let return_type = check!(
10441044
namespace.resolve_type_with_self(
1045-
asm.return_type.clone(),
1045+
insert_type(asm.return_type.clone()),
10461046
self_type,
10471047
&asm_span,
10481048
EnforceTypeArguments::No

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl TypedAstNode {
234234
|namespace: &mut Namespace, type_ascription: TypeInfo, value| {
235235
let type_id = check!(
236236
namespace.resolve_type_with_self(
237-
type_ascription,
237+
insert_type(type_ascription),
238238
self_type,
239239
&node.span,
240240
EnforceTypeArguments::No
@@ -289,7 +289,7 @@ impl TypedAstNode {
289289
};
290290
let type_ascription = check!(
291291
namespace.resolve_type_with_self(
292-
type_ascription,
292+
insert_type(type_ascription),
293293
self_type,
294294
&type_ascription_span,
295295
EnforceTypeArguments::Yes,
@@ -478,13 +478,9 @@ impl TypedAstNode {
478478
}
479479
Declaration::StorageDeclaration(StorageDeclaration { span, fields }) => {
480480
let mut fields_buf = Vec::with_capacity(fields.len());
481-
for StorageField {
482-
name,
483-
type_info: r#type,
484-
} in fields
485-
{
481+
for StorageField { name, type_info } in fields {
486482
let r#type = check!(
487-
namespace.resolve_type_without_self(r#type),
483+
namespace.resolve_type_without_self(insert_type(type_info)),
488484
return err(warnings, errors),
489485
warnings,
490486
errors
@@ -778,7 +774,7 @@ fn type_check_interface_surface(
778774
is_mutable,
779775
type_id: check!(
780776
namespace.resolve_type_with_self(
781-
look_up_type_id(type_id),
777+
type_id,
782778
insert_type(TypeInfo::SelfType),
783779
&type_span,
784780
EnforceTypeArguments::Yes
@@ -793,7 +789,7 @@ fn type_check_interface_surface(
793789
.collect(),
794790
return_type: check!(
795791
namespace.resolve_type_with_self(
796-
return_type,
792+
insert_type(return_type),
797793
insert_type(TypeInfo::SelfType),
798794
&return_type_span,
799795
EnforceTypeArguments::Yes
@@ -830,13 +826,11 @@ fn type_check_trait_methods(
830826
{
831827
parameters.clone().into_iter().for_each(
832828
|FunctionParameter {
833-
name,
834-
type_id: ref r#type,
835-
..
829+
name, ref type_id, ..
836830
}| {
837831
let r#type = check!(
838832
namespace.resolve_type_with_self(
839-
look_up_type_id(*r#type),
833+
*type_id,
840834
insert_type(TypeInfo::SelfType),
841835
&name.span(),
842836
EnforceTypeArguments::Yes
@@ -916,7 +910,7 @@ fn type_check_trait_methods(
916910
is_mutable,
917911
type_id: check!(
918912
namespace.resolve_type_with_self(
919-
look_up_type_id(type_id),
913+
type_id,
920914
crate::type_engine::insert_type(TypeInfo::SelfType),
921915
&type_span,
922916
EnforceTypeArguments::Yes
@@ -934,7 +928,7 @@ fn type_check_trait_methods(
934928
// TODO check code block implicit return
935929
let return_type = check!(
936930
namespace.resolve_type_with_self(
937-
return_type,
931+
insert_type(return_type),
938932
self_type,
939933
&return_type_span,
940934
EnforceTypeArguments::Yes

0 commit comments

Comments
 (0)