Skip to content

Commit

Permalink
small examples
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyaherbert authored Feb 21, 2023
1 parent 2c9a4d5 commit 8f7cb42
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,63 @@ use std::collections::HashMap;
use sway_error::error::CompileError;
use sway_types::Spanned;

/*
fn baz<T>() -> u64 {
log(size_of_type::<T>());
1u64
}
fn foo<T>(bar: T) -> T {
baz::<T>();
bar
}
foo(false);
*/

/*
DeclId: 0
[T] fn baz<0>() -> u64 {
log(size_of_type::<0>());
1u64
}
DeclId: 1
[T] fn foo<0>(bar: 0) -> 0 {
baz::<0>();
bar
}
foo(false);
-> instantiate_function_application
fn_decl_id: 1
fn_type_subst_list: [T: TypeId(0) -> TypeInfo::GenericType(T)]
// "prep" the function.
fn_type_subst_list: [T: TypeId(1) -> TypeInfo::GenericType(T)]
// Type check the arguments.
false: bool
// Unify the types of the arguments and the types of the parameters.
unify "found type" with "expected type"
unify bool with TypeInfo::TypeParam(0) (this is coming from bar: TypeInfo::TypeParam(0))
unify bool with "the element at 0 in the TypeSubstList from foo [T]" T: TypeId(1) -> TypeInfo::GenericType(T)
this will replace TypeInfo::GenericType(T) with bool to create:
[bool] fn foo<0>(bar: 0) -> 0 {
baz::<0>();
bar
}
for us to use in type checking this function application
*/

#[allow(clippy::too_many_arguments)]
pub(crate) fn instantiate_function_application(
mut ctx: TypeCheckContext,
Expand Down
2 changes: 2 additions & 0 deletions sway-core/src/type_system/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ pub enum TypeInfo {
call_path: CallPath,
type_parameters: Vec<TypeParameter>,
variant_types: Vec<ty::TyEnumVariant>,
// decl_ref: DeclRef, (contains a DeclId and TypeSubstList)
},
Struct {
call_path: CallPath,
type_parameters: Vec<TypeParameter>,
fields: Vec<ty::TyStructField>,
// decl_ref: DeclRef, (contains a DeclId and TypeSubstList)
},
Boolean,
Tuple(Vec<TypeArgument>),
Expand Down
18 changes: 18 additions & 0 deletions sway-core/src/type_system/substitute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ pub struct TypeSubstList {
num_from_the_current_scope: usize,
}

/*
[T] struct Data<0> {
value: 0
}
[T] impl<0> Data<0> {
[T, U] fn foo<1>(value: 0, other: 1) -> Data<0> {
log(other);
Data {
value
}
}
}
*/

impl TypeSubstList {
pub(crate) fn new() -> TypeSubstList {
TypeSubstList {
Expand Down

0 comments on commit 8f7cb42

Please sign in to comment.