Skip to content

Commit

Permalink
Merge branch 'master' into mitchmindtree/type-check-context
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchmindtree authored Jun 21, 2022
2 parents 56b6934 + 3f781e0 commit 5390494
Show file tree
Hide file tree
Showing 23 changed files with 618 additions and 68 deletions.
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
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,7 +107,6 @@ 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(
Expand Down Expand Up @@ -191,13 +190,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
}
27 changes: 0 additions & 27 deletions sway-core/src/semantic_analysis/namespace/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,33 +138,6 @@ impl Items {
.get_methods_for_type(implementing_for_type_id)
}

// Given a TypeInfo old_type with a set of methods available to it, make those same methods
// available to TypeInfo new_type. This is useful in situations where old_type is being
// monomorphized to new_type and and we want `get_methods_for_type()` to return the same set of
// methods for new_type as it does for old_type.
pub(crate) fn copy_methods_to_type(
&mut self,
old_type: TypeId,
new_type: TypeId,
type_mapping: &TypeMapping,
) {
// This map grabs all (trait name, vec of methods) from self.implemented_traits
// corresponding to `old_type`.
let methods = self
.implemented_traits
.get_methods_for_type_by_trait(old_type);

// Insert into `self.implemented_traits` the contents of the map above but with `new_type`
// as the `TypeInfo` key.
for (trait_name, mut trait_methods) in methods.into_iter() {
trait_methods
.iter_mut()
.for_each(|method| method.copy_types(type_mapping));
self.implemented_traits
.insert(trait_name, new_type, trait_methods);
}
}

pub(crate) fn get_canonical_path(&self, symbol: &Ident) -> &[Ident] {
self.use_synonyms.get(symbol).map(|v| &v[..]).unwrap_or(&[])
}
Expand Down
40 changes: 11 additions & 29 deletions sway-core/src/semantic_analysis/namespace/trait_map.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::{
type_engine::{look_up_type_id, TypeId},
type_engine::{create_type_mapping, look_up_type_id, CopyTypes, TypeId},
CallPath, TypeInfo, TypedFunctionDeclaration,
};

use std::collections::HashMap;

type TraitName = CallPath;

// This cannot be a HashMap because of how TypeInfo's are handled.
Expand Down Expand Up @@ -46,8 +44,7 @@ impl TraitMap {
) {
let mut methods_map = im::HashMap::new();
for method in methods.into_iter() {
let method_name = method.name.as_str().to_string();
methods_map.insert(method_name, method);
methods_map.insert(method.name.as_str().to_string(), method);
}
self.trait_map
.push_back(((trait_name, incoming_type_id), methods_map));
Expand All @@ -69,7 +66,7 @@ impl TraitMap {
) -> Vec<((CallPath, TypeId), Vec<TypedFunctionDeclaration>)> {
let mut ret = vec![];
for ((call_path, map_type_id), methods) in self.trait_map.iter() {
if look_up_type_id(*map_type_id) == look_up_type_id(incoming_type_id) {
if look_up_type_id(incoming_type_id).is_subset_of(&look_up_type_id(*map_type_id)) {
ret.push((
(call_path.clone(), *map_type_id),
methods.values().cloned().collect(),
Expand All @@ -88,29 +85,14 @@ impl TraitMap {
if look_up_type_id(incoming_type_id) == TypeInfo::ErrorRecovery {
return methods;
}
for ((_, map_type_id), l_methods) in self.trait_map.iter() {
if look_up_type_id(*map_type_id) == look_up_type_id(incoming_type_id) {
methods.append(&mut l_methods.values().cloned().collect());
}
}
methods
}

pub(crate) fn get_methods_for_type_by_trait(
&self,
incoming_type_id: TypeId,
) -> HashMap<TraitName, Vec<TypedFunctionDeclaration>> {
let mut methods: HashMap<TraitName, Vec<TypedFunctionDeclaration>> = HashMap::new();
// small performance gain in bad case
if look_up_type_id(incoming_type_id) == TypeInfo::ErrorRecovery {
return methods;
}
for ((trait_name, map_type_id), trait_methods) in self.trait_map.iter() {
if look_up_type_id(*map_type_id) == look_up_type_id(incoming_type_id) {
methods.insert(
(*trait_name).clone(),
trait_methods.values().cloned().collect(),
);
for ((_, map_type_id), trait_methods) in self.trait_map.iter() {
if look_up_type_id(incoming_type_id).is_subset_of(&look_up_type_id(*map_type_id)) {
let type_mapping = create_type_mapping(*map_type_id, incoming_type_id);
let mut trait_methods = trait_methods.values().cloned().collect::<Vec<_>>();
trait_methods
.iter_mut()
.for_each(|x| x.copy_types(&type_mapping));
methods.append(&mut trait_methods);
}
}
methods
Expand Down
Loading

0 comments on commit 5390494

Please sign in to comment.