Skip to content

Commit

Permalink
Merge branch 'master' into mitchmindtree/type-check-context
Browse files Browse the repository at this point in the history
Resolves conflicts between the PR that uses type-id's with resolution
rather than type infos.
  • Loading branch information
mitchmindtree committed Jun 21, 2022
2 parents 56b6934 + 7e0f858 commit 51e9584
Show file tree
Hide file tree
Showing 40 changed files with 890 additions and 227 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/src/reference/known_issues_and_workarounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

## Known Issues

* [#1663](https://github.com/FuelLabs/sway/issues/1663): Using an explicit `return` in all branches of an `if let` expression causes a compile error. The workaround is to use implicit returns instead.

* [#870](https://github.com/FuelLabs/sway/issues/870): All `impl` blocks need to be defined before any of the functions they define can be called.

## Missing Features
Expand All @@ -12,7 +10,9 @@

* [#428](https://github.com/FuelLabs/sway/issues/428): Arrays are currently immutable which means that changing elements of an array once initialized is not yet possible.

* [#1077](https://github.com/FuelLabs/sway/issues/1077): Dynamic vectors, i.e. `Vec<T>`, have not yet been implemented.
* [#2035](https://github.com/FuelLabs/sway/issues/2035): Dynamic vectors _in storage_ have not yet been implemented. Only [vectors in memory](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/vec.sw) are available at the moment.

* [#1188](https://github.com/FuelLabs/sway/issues/1188): Mutable function arguments are not yet allowed except for `self`.

## General

Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn const_eval_typed_expr(
TypedExpressionVariant::StructExpression { fields, .. } => {
let (field_typs, field_vals): (Vec<_>, Vec<_>) = fields
.iter()
.filter_map(|TypedStructExpressionField { name: _, value }| {
.filter_map(|TypedStructExpressionField { name: _, value, .. }| {
const_eval_typed_expr(context, module, known_consts, value)
.map(|cv| (value.return_type, cv))
})
Expand Down
11 changes: 10 additions & 1 deletion sway-core/src/parse_tree/declaration/type_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use crate::{error::*, parse_tree::*, semantic_analysis::*, type_engine::*};

use sway_types::{ident::Ident, span::Span, Spanned};

use std::hash::{Hash, Hasher};
use std::{
fmt,
hash::{Hash, Hasher},
};

#[derive(Debug, Clone, Eq)]
pub struct TypeParameter {
Expand Down Expand Up @@ -57,6 +60,12 @@ impl ReplaceSelfType for TypeParameter {
}
}

impl fmt::Display for TypeParameter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.name_ident, self.type_id)
}
}

impl TypeParameter {
pub(crate) fn type_check(
ctx: TypeCheckContext,
Expand Down
8 changes: 8 additions & 0 deletions sway-core/src/semantic_analysis/ast_node/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ impl UnresolvedTypeCheck for TypedDeclaration {
.flat_map(UnresolvedTypeCheck::check_for_unresolved_types)
.collect(),
);
body.append(
&mut decl
.parameters
.iter()
.map(|x| &x.type_id)
.flat_map(UnresolvedTypeCheck::check_for_unresolved_types)
.collect(),
);
body
}
ConstantDeclaration(TypedConstantDeclaration { value, .. }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl TypedEnumVariant {
let mut errors = vec![];
let enum_variant_type = check!(
ctx.resolve_type_with_self(
variant.type_info.clone(),
insert_type(variant.type_info),
&variant.span,
EnforceTypeArguments::Yes
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ impl TypedFunctionDeclaration {

// type check the return type
let return_type = check!(
ctx.resolve_type_with_self(return_type, &return_type_span, EnforceTypeArguments::Yes),
ctx.resolve_type_with_self(
insert_type(return_type),
&return_type_span,
EnforceTypeArguments::Yes
),
insert_type(TypeInfo::ErrorRecovery),
warnings,
errors,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl TypedFunctionParameter {
let mut errors = vec![];
let type_id = check!(
ctx.resolve_type_with_self(
look_up_type_id(parameter.type_id),
parameter.type_id,
&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 @@ -67,7 +67,19 @@ impl TypedImplTrait {
// type check the type that we are implementing for
let implementing_for_type_id = check!(
ctx.namespace
.resolve_type_without_self(type_implementing_for),
.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 @@ -217,7 +229,19 @@ impl TypedImplTrait {
// type check the type that we are implementing for
let implementing_for_type_id = check!(
ctx.namespace
.resolve_type_without_self(type_implementing_for),
.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 @@ -64,7 +64,7 @@ where

fn monomorphize(
self,
type_arguments: Vec<TypeArgument>,
mut type_arguments: Vec<TypeArgument>,
enforce_type_arguments: EnforceTypeArguments,
self_type: Option<TypeId>,
call_site_span: Option<&Span>,
Expand Down Expand Up @@ -107,20 +107,18 @@ where
err(warnings, errors)
}
(false, false) => {
let mut type_arguments = type_arguments;
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 Expand Up @@ -191,13 +189,15 @@ pub(crate) trait MonomorphizeHelper {
fn monomorphize_inner(self, type_mapping: &TypeMapping, namespace: &mut Items) -> Self::Output;
}

pub(crate) fn monomorphize_inner<T>(decl: T, type_mapping: &TypeMapping, namespace: &mut Items) -> T
pub(crate) fn monomorphize_inner<T>(
decl: T,
type_mapping: &TypeMapping,
_namespace: &mut Items,
) -> T
where
T: CopyTypes + CreateTypeId,
{
let old_type_id = decl.create_type_id();
let mut new_decl = decl;
new_decl.copy_types(type_mapping);
namespace.copy_methods_to_type(old_type_id, new_decl.create_type_id(), type_mapping);
new_decl
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl TypedStructField {
let mut errors = vec![];
let r#type = check!(
ctx.resolve_type_with_self(
field.type_info,
insert_type(field.type_info),
&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, TypeCheckContext, 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 @@ -203,7 +203,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 @@ -219,7 +219,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 @@ -131,7 +131,11 @@ impl TypedIntrinsicFunctionKind {
type_span,
} => {
let type_id = check!(
ctx.resolve_type_with_self(type_name, &type_span, EnforceTypeArguments::Yes),
ctx.resolve_type_with_self(
insert_type(type_name),
&type_span,
EnforceTypeArguments::Yes
),
insert_type(TypeInfo::ErrorRecovery),
warnings,
errors,
Expand All @@ -146,7 +150,11 @@ impl TypedIntrinsicFunctionKind {
type_span,
} => {
let type_id = check!(
ctx.resolve_type_with_self(type_name, &type_span, EnforceTypeArguments::Yes),
ctx.resolve_type_with_self(
insert_type(type_name),
&type_span,
EnforceTypeArguments::Yes
),
insert_type(TypeInfo::ErrorRecovery),
warnings,
errors,
Expand Down
Loading

0 comments on commit 51e9584

Please sign in to comment.