Skip to content

Commit

Permalink
Small refactor to the method call to type check a list of `TypeParame…
Browse files Browse the repository at this point in the history
…ter` (FuelLabs#4089)

## Description

This PR is a small refactor to the method call to type check a list of
`TypeParameter`. This is to reduce the diff on FuelLabs#3744. This PR should
have no practical or logical effect on `master`.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

Co-authored-by: emilyaherbert <[email protected]>
  • Loading branch information
emilyaherbert and emilyaherbert authored Feb 15, 2023
1 parent 19e75cd commit 77a1eea
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 99 deletions.
27 changes: 8 additions & 19 deletions sway-core/src/semantic_analysis/ast_node/declaration/enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use sway_error::error::CompileError;

use crate::{
error::*,
language::{parsed::*, ty, CallPath},
Expand All @@ -26,23 +24,14 @@ impl ty::TyEnumDeclaration {
let mut decl_namespace = ctx.namespace.clone();
let mut ctx = ctx.scoped(&mut decl_namespace);

// type check the type parameters
// insert them into the namespace
let mut new_type_parameters = vec![];
for type_parameter in type_parameters.into_iter() {
if !type_parameter.trait_constraints.is_empty() {
errors.push(CompileError::WhereClauseNotYetSupported {
span: type_parameter.trait_constraints_span,
});
return err(warnings, errors);
}
new_type_parameters.push(check!(
TypeParameter::type_check(ctx.by_ref(), type_parameter),
return err(warnings, errors),
warnings,
errors
));
}
// Type check the type parameters. This will also insert them into the
// current namespace.
let new_type_parameters = check!(
TypeParameter::type_check_type_params(ctx.by_ref(), type_parameters, true),
return err(warnings, errors),
warnings,
errors
);

// type check the variants
let mut variants_buf = vec![];
Expand Down
21 changes: 8 additions & 13 deletions sway-core/src/semantic_analysis/ast_node/declaration/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,14 @@ impl ty::TyFunctionDeclaration {
.with_purity(purity)
.disallow_functions();

// type check the type parameters, which will also insert them into the namespace
let mut new_type_parameters = vec![];
for type_parameter in type_parameters.into_iter() {
new_type_parameters.push(check!(
TypeParameter::type_check(ctx.by_ref(), type_parameter),
continue,
warnings,
errors
));
}
if !errors.is_empty() {
return err(warnings, errors);
}
// Type check the type parameters. This will also insert them into the
// current namespace.
let new_type_parameters = check!(
TypeParameter::type_check_type_params(ctx.by_ref(), type_parameters, false),
return err(warnings, errors),
warnings,
errors
);

// type check the function parameters, which will also insert them into the namespace
let mut new_parameters = vec![];
Expand Down
51 changes: 16 additions & 35 deletions sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,14 @@ impl ty::TyImplTrait {
let mut impl_namespace = ctx.namespace.clone();
let mut ctx = ctx.by_ref().scoped(&mut impl_namespace).allow_functions();

// type check the type parameters which also inserts them into the namespace
let mut new_impl_type_parameters = vec![];
for type_parameter in impl_type_parameters.into_iter() {
if !type_parameter.trait_constraints.is_empty() {
errors.push(CompileError::WhereClauseNotYetSupported {
span: type_parameter.trait_constraints_span,
});
return err(warnings, errors);
}
new_impl_type_parameters.push(check!(
TypeParameter::type_check(ctx.by_ref(), type_parameter),
return err(warnings, errors),
warnings,
errors
));
}
// Type check the type parameters. This will also insert them into the
// current namespace.
let new_impl_type_parameters = check!(
TypeParameter::type_check_type_params(ctx.by_ref(), impl_type_parameters, true),
return err(warnings, errors),
warnings,
errors
);

// resolve the types of the trait type arguments
for type_arg in trait_type_arguments.iter_mut() {
Expand Down Expand Up @@ -463,25 +455,14 @@ impl ty::TyImplTrait {
is_absolute: false,
};

// type check the type parameters which also inserts them into the namespace
let mut new_impl_type_parameters = vec![];
for type_parameter in impl_type_parameters.into_iter() {
if !type_parameter.trait_constraints.is_empty() {
errors.push(CompileError::WhereClauseNotYetSupported {
span: type_parameter.trait_constraints_span,
});
continue;
}
new_impl_type_parameters.push(check!(
TypeParameter::type_check(ctx.by_ref(), type_parameter),
continue,
warnings,
errors
));
}
if !errors.is_empty() {
return err(warnings, errors);
}
// Type check the type parameters. This will also insert them into the
// current namespace.
let new_impl_type_parameters = check!(
TypeParameter::type_check_type_params(ctx.by_ref(), impl_type_parameters, true),
return err(warnings, errors),
warnings,
errors
);

// type check the type that we are implementing for
let implementing_for_type_id = check!(
Expand Down
27 changes: 8 additions & 19 deletions sway-core/src/semantic_analysis/ast_node/declaration/struct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use sway_error::error::CompileError;

use crate::{
error::*,
language::{parsed::*, ty, CallPath},
Expand Down Expand Up @@ -29,23 +27,14 @@ impl ty::TyStructDeclaration {
let mut decl_namespace = ctx.namespace.clone();
let mut ctx = ctx.scoped(&mut decl_namespace);

// type check the type parameters
// insert them into the namespace
let mut new_type_parameters = vec![];
for type_parameter in type_parameters.into_iter() {
if !type_parameter.trait_constraints.is_empty() {
errors.push(CompileError::WhereClauseNotYetSupported {
span: type_parameter.trait_constraints_span,
});
return err(warnings, errors);
}
new_type_parameters.push(check!(
TypeParameter::type_check(ctx.by_ref(), type_parameter),
return err(warnings, errors),
warnings,
errors
));
}
// Type check the type parameters. This will also insert them into the
// current namespace.
let new_type_parameters = check!(
TypeParameter::type_check_type_params(ctx.by_ref(), type_parameters, true),
return err(warnings, errors),
warnings,
errors
);

// type check the fields
let mut new_fields = vec![];
Expand Down
18 changes: 8 additions & 10 deletions sway-core/src/semantic_analysis/ast_node/declaration/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ impl ty::TyTraitDeclaration {
let mut trait_namespace = ctx.namespace.clone();
let mut ctx = ctx.scoped(&mut trait_namespace).with_self_type(self_type);

// type check the type parameters, which will insert them into the namespace
let mut new_type_parameters = vec![];
for type_parameter in type_parameters.into_iter() {
new_type_parameters.push(check!(
TypeParameter::type_check(ctx.by_ref(), type_parameter),
return err(warnings, errors),
warnings,
errors
));
}
// Type check the type parameters. This will also insert them into the
// current namespace.
let new_type_parameters = check!(
TypeParameter::type_check_type_params(ctx.by_ref(), type_parameters, true),
return err(warnings, errors),
warnings,
errors
);

// Recursively make the interface surfaces and methods of the
// supertraits available to this trait.
Expand Down
40 changes: 37 additions & 3 deletions sway-core/src/type_system/type_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,44 @@ impl fmt::Debug for TypeParameter {
}

impl TypeParameter {
pub(crate) fn type_check(
/// Type check a list of [TypeParameter] and return a new list of
/// [TypeParameter]. This will also insert this new list into the current
/// namespace.
pub(crate) fn type_check_type_params(
mut ctx: TypeCheckContext,
type_parameter: TypeParameter,
) -> CompileResult<Self> {
type_params: Vec<TypeParameter>,
disallow_trait_constraints: bool,
) -> CompileResult<Vec<TypeParameter>> {
let mut warnings = vec![];
let mut errors = vec![];

let mut new_type_params: Vec<TypeParameter> = vec![];

for type_param in type_params.into_iter() {
if disallow_trait_constraints && !type_param.trait_constraints.is_empty() {
let errors = vec![CompileError::WhereClauseNotYetSupported {
span: type_param.trait_constraints_span,
}];
return err(vec![], errors);
}
new_type_params.push(check!(
TypeParameter::type_check(ctx.by_ref(), type_param),
continue,
warnings,
errors
));
}

if errors.is_empty() {
ok(new_type_params, warnings, errors)
} else {
err(warnings, errors)
}
}

/// Type checks a [TypeParameter] (including its [TraitConstraint]s) and
/// inserts into into the current namespace.
fn type_check(mut ctx: TypeCheckContext, type_parameter: TypeParameter) -> CompileResult<Self> {
let mut warnings = vec![];
let mut errors = vec![];

Expand Down

0 comments on commit 77a1eea

Please sign in to comment.