Skip to content

Commit

Permalink
Introduce the TypeBinding type (FuelLabs#2195)
Browse files Browse the repository at this point in the history
* 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

* Remove self_type from monomorphization.

* Add conceptual distinction between replacing TypeInfo::Self and monomorphization.

* Bug is fixed.

* Add forc.lock.

* update

* Move test to inside of the SDK.

* Fix test cases.

* Add lock files.

* Fix test.

* Move the stuff.

* Move monomorphization conceptually inside of the type engine.

* Remove commented out test.

* Remove dead code.

* Introduce CallPath as a wrapper around type T.

* Use CallPath.

* Use Self in Vec.

* Fix bug.

* Add TypeBinding type.

* clippy

* clippy

* Fix bug.

* Add missing Forc.toml file.

* forc-fmt

* Add TypeBinding to function application.

* Update test.

* Add TypeBinding to StructExpression.

* clippy

* clippy

* update

* Use TypeBinding for MethodApplication.

* Use TypeBinding.

* Use TypeBinding with intrinsics.

* Add test case.

* Update test.

* Give CallPath a default type argument.

* Remove duplicate check to length of function arguments.

* Fix bug.

* Fix bug.

* Remove duplicate check to length of function arguments.

* Fix bug.

* Fix bug.
  • Loading branch information
emilyaherbert authored Jul 13, 2022
1 parent 84fec00 commit 916ab86
Show file tree
Hide file tree
Showing 28 changed files with 935 additions and 493 deletions.
264 changes: 178 additions & 86 deletions sway-core/src/convert_parse_tree.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sway-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ pub enum CompileError {
},
#[error("This op does not take an immediate value.")]
UnnecessaryImmediate { span: Span },
#[error("This reference is ambiguous, and could refer to either a module or an enum of the same name. Try qualifying the name with a path.")]
#[error("This reference is ambiguous, and could refer to a module, enum, or function of the same name. Try qualifying the name with a path.")]
AmbiguousPath { span: Span },
#[error("This value is not valid within a \"str\" type.")]
InvalidStrType { raw: String, span: Span },
Expand Down
4 changes: 3 additions & 1 deletion sway-core/src/parse_tree/expression/method_name.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use sway_types::Span;

use crate::parse_tree::CallPath;
use crate::type_engine::TypeBinding;
use crate::{Ident, TypeInfo};

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum MethodName {
/// Represents a method lookup with a type somewhere in the path
/// like a::b::~C::d()
FromType {
call_path: CallPath<(TypeInfo, Span)>,
call_path_binding: TypeBinding<CallPath<(TypeInfo, Span)>>,
method_name: Ident,
},
/// Represents a method lookup that does not contain any types in the path
Expand Down
17 changes: 6 additions & 11 deletions sway-core/src/parse_tree/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
parse_tree::{CallPath, Literal},
type_engine::TypeArgument,
type_engine::TypeBinding,
CodeBlock, TypeInfo,
};
use sway_types::{ident::Ident, Span, Spanned};
Expand All @@ -23,9 +23,8 @@ pub enum Expression {
span: Span,
},
FunctionApplication {
name: CallPath,
call_path_binding: TypeBinding<CallPath>,
arguments: Vec<Expression>,
type_arguments: Vec<TypeArgument>,
span: Span,
},
LazyOperator {
Expand Down Expand Up @@ -53,8 +52,7 @@ pub enum Expression {
span: Span,
},
StructExpression {
struct_name: CallPath<(TypeInfo, Span)>,
type_arguments: Vec<TypeArgument>,
call_path_binding: TypeBinding<CallPath<(TypeInfo, Span)>>,
fields: Vec<StructExpressionField>,
span: Span,
},
Expand All @@ -79,10 +77,9 @@ pub enum Expression {
asm: AsmExpression,
},
MethodApplication {
method_name: MethodName,
method_name_binding: TypeBinding<MethodName>,
contract_call_params: Vec<StructExpressionField>,
arguments: Vec<Expression>,
type_arguments: Vec<TypeArgument>,
span: Span,
},
/// A _subfield expression_ is anything of the form:
Expand Down Expand Up @@ -117,10 +114,9 @@ pub enum Expression {
/// MyEnum::Variant1
/// ```
DelineatedPath {
call_path: CallPath,
call_path_binding: TypeBinding<CallPath>,
args: Vec<Expression>,
span: Span,
type_arguments: Vec<TypeArgument>,
},
/// A cast of a hash to an ABI for calling a contract.
AbiCast {
Expand All @@ -138,9 +134,8 @@ pub enum Expression {
span: Span,
},
IntrinsicFunction {
kind: Intrinsic,
kind_binding: TypeBinding<Intrinsic>,
arguments: Vec<Expression>,
type_arguments: Vec<TypeArgument>,
span: Span,
},
}
Expand Down
54 changes: 0 additions & 54 deletions sway-core/src/semantic_analysis/ast_node/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,60 +215,6 @@ impl UnresolvedTypeCheck for TypedDeclaration {
}

impl TypedDeclaration {
/// Attempt to retrieve the declaration as an enum declaration.
///
/// Returns `None` if `self` is not an `TypedEnumDeclaration`.
pub(crate) fn as_enum(&self) -> Option<&TypedEnumDeclaration> {
match self {
TypedDeclaration::EnumDeclaration(decl) => Some(decl),
_ => None,
}
}

/// Attempt to retrieve the declaration as a struct declaration.
///
/// Returns `None` if `self` is not a `TypedStructDeclaration`.
#[allow(dead_code)]
pub(crate) fn as_struct(&self) -> Option<&TypedStructDeclaration> {
match self {
TypedDeclaration::StructDeclaration(decl) => Some(decl),
_ => None,
}
}

/// Attempt to retrieve the declaration as a function declaration.
///
/// Returns `None` if `self` is not a `TypedFunctionDeclaration`.
#[allow(dead_code)]
pub(crate) fn as_function(&self) -> Option<&TypedFunctionDeclaration> {
match self {
TypedDeclaration::FunctionDeclaration(decl) => Some(decl),
_ => None,
}
}

/// Attempt to retrieve the declaration as a variable declaration.
///
/// Returns `None` if `self` is not a `TypedVariableDeclaration`.
#[allow(dead_code)]
pub(crate) fn as_variable(&self) -> Option<&TypedVariableDeclaration> {
match self {
TypedDeclaration::VariableDeclaration(decl) => Some(decl),
_ => None,
}
}

/// Attempt to retrieve the declaration as an Abi declaration.
///
/// Returns `None` if `self` is not a `TypedAbiDeclaration`.
#[allow(dead_code)]
pub(crate) fn as_abi(&self) -> Option<&TypedAbiDeclaration> {
match self {
TypedDeclaration::AbiDeclaration(decl) => Some(decl),
_ => None,
}
}

/// Retrieves the declaration as an enum declaration.
///
/// Returns an error if `self` is not a `TypedEnumDeclaration`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ impl UnresolvedTypeCheck for TypedIntrinsicFunctionKind {
impl TypedIntrinsicFunctionKind {
pub(crate) fn type_check(
mut ctx: TypeCheckContext,
kind: Intrinsic,
type_arguments: Vec<TypeArgument>,
kind_binding: TypeBinding<Intrinsic>,
arguments: Vec<Expression>,
span: Span,
) -> CompileResult<(Self, TypeId)> {
let mut warnings = vec![];
let mut errors = vec![];
let TypeBinding {
inner: kind,
type_arguments,
..
} = kind_binding;
let (intrinsic_function, return_type) = match kind {
Intrinsic::SizeOfVal => {
if arguments.len() != 1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl TypedScrutinee {
check!(
ctx.monomorphize(
&mut struct_decl,
vec!(),
&mut [],
EnforceTypeArguments::No,
&struct_name.span()
),
Expand Down Expand Up @@ -179,7 +179,7 @@ impl TypedScrutinee {
check!(
ctx.monomorphize(
&mut enum_decl,
vec!(),
&mut [],
EnforceTypeArguments::No,
&enum_name.span()
),
Expand Down
Loading

0 comments on commit 916ab86

Please sign in to comment.