From 5ac761fec7d1a41c3bee3d24cd79d2d8d4f8a7b8 Mon Sep 17 00:00:00 2001 From: Christoph Burgdorf Date: Fri, 3 Jun 2022 15:59:05 +0200 Subject: [PATCH] Support implementing trait functions in `impl` blocks --- crates/analyzer/src/context.rs | 27 +- crates/analyzer/src/db.rs | 26 +- crates/analyzer/src/db/queries.rs | 1 + crates/analyzer/src/db/queries/contracts.rs | 22 +- crates/analyzer/src/db/queries/functions.rs | 81 +++- crates/analyzer/src/db/queries/impls.rs | 54 +++ crates/analyzer/src/db/queries/module.rs | 51 +- crates/analyzer/src/db/queries/structs.rs | 19 +- crates/analyzer/src/db/queries/traits.rs | 52 +- crates/analyzer/src/namespace/items.rs | 454 +++++++++++++++--- crates/analyzer/src/namespace/scopes.rs | 2 + crates/analyzer/src/namespace/types.rs | 27 +- crates/analyzer/src/traversal/expressions.rs | 45 +- crates/analyzer/tests/analysis.rs | 8 +- crates/analyzer/tests/errors.rs | 7 + .../snapshots/errors__bad_visibility.snap | 101 ++-- ...neric_function_with_unsatisfied_bound.snap | 14 +- ..._call_non_pub_fn_on_external_contract.snap | 14 +- ...contract_function_with_generic_params.snap | 6 +- .../errors__duplicate_generic_params.snap | 14 + .../errors__invalid_impl_location.snap | 6 +- .../errors__trait_conflicting_impls.snap | 14 + .../errors__trait_fn_with_generic_params.snap | 14 + .../errors__trait_fn_without_self.snap | 14 + .../errors__trait_impl_mismatch.snap | 36 ++ .../snapshots/errors__traits_as_fields.snap | 18 + .../errors__traits_with_wrong_bounds.snap | 14 + crates/codegen/src/db/queries/function.rs | 25 +- crates/library/std/src/traits.fe | 16 +- crates/mir/src/db/queries/function.rs | 55 ++- crates/mir/src/graphviz/function.rs | 2 +- crates/mir/src/lower/function.rs | 36 +- crates/mir/src/pretty_print/inst.rs | 2 +- crates/parser/src/ast.rs | 31 +- crates/parser/src/grammar/functions.rs | 38 +- crates/parser/src/grammar/types.rs | 18 +- crates/parser/src/lexer/token.rs | 2 +- .../cases__parse_ast__contract_def.snap | 60 ++- .../snapshots/cases__parse_ast__fn_def.snap | 210 ++++---- .../cases__parse_ast__fn_def_generic.snap | 226 ++++----- .../cases__parse_ast__fn_def_pub.snap | 140 +++--- .../cases__parse_ast__fn_def_pub_unsafe.snap | 146 +++--- .../cases__parse_ast__fn_def_unsafe.snap | 140 +++--- .../cases__parse_ast__guest_book.snap | 206 ++++---- ...cases__parse_ast__module_level_events.snap | 166 ++++--- .../cases__parse_ast__module_stmts.snap | 152 +++--- .../cases__parse_ast__pub_contract_def.snap | 60 ++- .../cases__parse_ast__struct_def.snap | 116 +++-- .../compile_errors/bad_visibility/src/foo.fe | 2 + .../compile_errors/bad_visibility/src/main.fe | 5 +- ...generic_function_with_unsatisfied_bound.fe | 8 +- .../contract_function_with_generic_params.fe | 4 +- .../duplicate_generic_params.fe | 13 + .../compile_errors/invalid_impl_location.fe | 5 +- .../compile_errors/trait_conflicting_impls.fe | 7 + .../trait_fn_with_generic_params.fe | 5 + .../compile_errors/trait_fn_without_self.fe | 3 + .../compile_errors/trait_impl_mismatch.fe | 16 + .../compile_errors/traits_as_fields.fe | 9 + .../traits_with_wrong_bounds.fe | 5 + .../fixtures/features/generic_functions.fe | 43 +- crates/tests/src/features.rs | 4 +- newsfragments/710.feature.md | 55 +++ 63 files changed, 2132 insertions(+), 1040 deletions(-) create mode 100644 crates/analyzer/src/db/queries/impls.rs create mode 100644 crates/analyzer/tests/snapshots/errors__duplicate_generic_params.snap create mode 100644 crates/analyzer/tests/snapshots/errors__trait_conflicting_impls.snap create mode 100644 crates/analyzer/tests/snapshots/errors__trait_fn_with_generic_params.snap create mode 100644 crates/analyzer/tests/snapshots/errors__trait_fn_without_self.snap create mode 100644 crates/analyzer/tests/snapshots/errors__trait_impl_mismatch.snap create mode 100644 crates/analyzer/tests/snapshots/errors__traits_as_fields.snap create mode 100644 crates/analyzer/tests/snapshots/errors__traits_with_wrong_bounds.snap create mode 100644 crates/test-files/fixtures/compile_errors/duplicate_generic_params.fe create mode 100644 crates/test-files/fixtures/compile_errors/trait_conflicting_impls.fe create mode 100644 crates/test-files/fixtures/compile_errors/trait_fn_with_generic_params.fe create mode 100644 crates/test-files/fixtures/compile_errors/trait_fn_without_self.fe create mode 100644 crates/test-files/fixtures/compile_errors/trait_impl_mismatch.fe create mode 100644 crates/test-files/fixtures/compile_errors/traits_as_fields.fe create mode 100644 crates/test-files/fixtures/compile_errors/traits_with_wrong_bounds.fe create mode 100644 newsfragments/710.feature.md diff --git a/crates/analyzer/src/context.rs b/crates/analyzer/src/context.rs index cf7dcab96d..2fe0517d36 100644 --- a/crates/analyzer/src/context.rs +++ b/crates/analyzer/src/context.rs @@ -1,5 +1,7 @@ -use crate::namespace::items::{Class, ContractId, DiagnosticSink, EventId, FunctionId, Item}; -use crate::namespace::types::{SelfDecl, Struct, Type}; +use crate::namespace::items::{ + Class, ContractId, DiagnosticSink, EventId, FunctionId, FunctionSigId, Item, TraitId, +}; +use crate::namespace::types::{Generic, SelfDecl, Struct, Type}; use crate::AnalyzerDb; use crate::{ builtins::{ContractTypeMethod, GlobalFunction, Intrinsic, ValueMethod}, @@ -361,7 +363,12 @@ impl Location { pub fn assign_location(typ: &Type) -> Self { match typ { Type::Base(_) | Type::Contract(_) => Location::Value, - Type::Array(_) | Type::Tuple(_) | Type::String(_) | Type::Struct(_) => Location::Memory, + // For now assume that generics can only ever refer to structs + Type::Array(_) + | Type::Tuple(_) + | Type::String(_) + | Type::Struct(_) + | Type::Generic(_) => Location::Memory, other => panic!("Type {other} can not be assigned, returned or passed"), } } @@ -458,11 +465,21 @@ pub enum CallType { class: Class, function: FunctionId, }, + // some_struct_or_contract.foo() ValueMethod { - is_self: bool, class: Class, method: FunctionId, }, + // some_trait.foo() + // The reason this can not use `ValueMethod` is mainly because the trait might not have a function implementation + // and even if it had it might not be the one that ends up getting executed. An `impl` block will decide that. + TraitValueMethod { + trait_id: TraitId, + method: FunctionSigId, + // Traits can not directly be used as types but can act as bounds for generics. This is the generic type + // that the method is called on. + generic_type: Generic, + }, External { contract: ContractId, function: FunctionId, @@ -479,6 +496,7 @@ impl CallType { | BuiltinValueMethod { .. } | TypeConstructor(_) | Intrinsic(_) + | TraitValueMethod { .. } | BuiltinAssociatedFunction { .. } => None, AssociatedFunction { function: id, .. } | ValueMethod { method: id, .. } @@ -497,6 +515,7 @@ impl CallType { | CallType::ValueMethod { method: id, .. } | CallType::External { function: id, .. } | CallType::Pure(id) => id.name(db), + CallType::TraitValueMethod { method: id, .. } => id.name(db), CallType::TypeConstructor(typ) => typ.name(), } } diff --git a/crates/analyzer/src/db.rs b/crates/analyzer/src/db.rs index 6181ae463b..71df437858 100644 --- a/crates/analyzer/src/db.rs +++ b/crates/analyzer/src/db.rs @@ -1,10 +1,10 @@ use crate::context::{Analysis, Constant, FunctionBody}; use crate::errors::{ConstEvalError, TypeError}; use crate::namespace::items::{ - self, ContractFieldId, ContractId, DepGraphWrapper, EventId, FunctionId, Impl, IngotId, Item, - ModuleConstantId, ModuleId, StructFieldId, StructId, TraitId, TypeAliasId, + self, ContractFieldId, ContractId, DepGraphWrapper, EventId, FunctionId, FunctionSigId, ImplId, + IngotId, Item, ModuleConstantId, ModuleId, StructFieldId, StructId, TraitId, TypeAliasId, }; -use crate::namespace::types; +use crate::namespace::types::{self, Type}; use fe_common::db::{SourceDb, SourceDbStorage, Upcast, UpcastMut}; use fe_common::{SourceFileId, Span}; use fe_parser::ast; @@ -26,6 +26,8 @@ pub trait AnalyzerDb: SourceDb + Upcast + UpcastMut #[salsa::interned] fn intern_trait(&self, data: Rc) -> TraitId; #[salsa::interned] + fn intern_impl(&self, data: Rc) -> ImplId; + #[salsa::interned] fn intern_struct_field(&self, data: Rc) -> StructFieldId; #[salsa::interned] fn intern_type_alias(&self, data: Rc) -> TypeAliasId; @@ -34,6 +36,8 @@ pub trait AnalyzerDb: SourceDb + Upcast + UpcastMut #[salsa::interned] fn intern_contract_field(&self, data: Rc) -> ContractFieldId; #[salsa::interned] + fn intern_function_sig(&self, data: Rc) -> FunctionSigId; + #[salsa::interned] fn intern_function(&self, data: Rc) -> FunctionId; #[salsa::interned] fn intern_event(&self, data: Rc) -> EventId; @@ -63,9 +67,11 @@ pub trait AnalyzerDb: SourceDb + Upcast + UpcastMut #[salsa::invoke(queries::module::module_all_items)] fn module_all_items(&self, module: ModuleId) -> Rc<[Item]>; #[salsa::invoke(queries::module::module_all_impls)] - fn module_all_impls(&self, module: ModuleId) -> Rc<[Impl]>; + fn module_all_impls(&self, module: ModuleId) -> Rc<[ImplId]>; #[salsa::invoke(queries::module::module_item_map)] fn module_item_map(&self, module: ModuleId) -> Analysis>>; + #[salsa::invoke(queries::module::module_impl_map)] + fn module_impl_map(&self, module: ModuleId) -> Analysis>>; #[salsa::invoke(queries::module::module_contracts)] fn module_contracts(&self, module: ModuleId) -> Rc<[ContractId]>; #[salsa::invoke(queries::module::module_structs)] @@ -134,7 +140,7 @@ pub trait AnalyzerDb: SourceDb + Upcast + UpcastMut // Function #[salsa::invoke(queries::functions::function_signature)] - fn function_signature(&self, id: FunctionId) -> Analysis>; + fn function_signature(&self, id: FunctionSigId) -> Analysis>; #[salsa::invoke(queries::functions::function_body)] fn function_body(&self, id: FunctionId) -> Analysis>; #[salsa::cycle(queries::functions::function_dependency_graph_cycle)] @@ -161,6 +167,16 @@ pub trait AnalyzerDb: SourceDb + Upcast + UpcastMut // Trait #[salsa::invoke(queries::traits::trait_type)] fn trait_type(&self, id: TraitId) -> Rc; + #[salsa::invoke(queries::traits::trait_all_functions)] + fn trait_all_functions(&self, id: TraitId) -> Rc<[FunctionSigId]>; + #[salsa::invoke(queries::traits::trait_function_map)] + fn trait_function_map(&self, id: TraitId) -> Analysis>>; + + // Impl + #[salsa::invoke(queries::impls::impl_all_functions)] + fn impl_all_functions(&self, id: ImplId) -> Rc<[FunctionId]>; + #[salsa::invoke(queries::impls::impl_function_map)] + fn impl_function_map(&self, id: ImplId) -> Analysis>>; // Event #[salsa::invoke(queries::events::event_type)] diff --git a/crates/analyzer/src/db/queries.rs b/crates/analyzer/src/db/queries.rs index 59519f493f..3a90513735 100644 --- a/crates/analyzer/src/db/queries.rs +++ b/crates/analyzer/src/db/queries.rs @@ -1,6 +1,7 @@ pub mod contracts; pub mod events; pub mod functions; +pub mod impls; pub mod ingots; pub mod module; pub mod structs; diff --git a/crates/analyzer/src/db/queries/contracts.rs b/crates/analyzer/src/db/queries/contracts.rs index 43ad43ce26..e97b086a51 100644 --- a/crates/analyzer/src/db/queries/contracts.rs +++ b/crates/analyzer/src/db/queries/contracts.rs @@ -22,13 +22,9 @@ pub fn contract_all_functions(db: &dyn AnalyzerDb, contract: ContractId) -> Rc<[ body.iter() .filter_map(|stmt| match stmt { ast::ContractStmt::Event(_) => None, - ast::ContractStmt::Function(node) => { - Some(db.intern_function(Rc::new(items::Function { - ast: node.clone(), - module, - parent: Some(items::Class::Contract(contract)), - }))) - } + ast::ContractStmt::Function(node) => Some(db.intern_function(Rc::new( + items::Function::new(db, node, Some(items::Class::Contract(contract)), module), + ))), }) .collect() } @@ -53,7 +49,7 @@ pub fn contract_function_map( def_name, &NamedThing::Item(Item::Event(event)), Some(event.name_span(db)), - def.kind.name.span, + def.kind.sig.kind.name.span, ); continue; } @@ -64,7 +60,7 @@ pub fn contract_function_map( def_name, &named_item, named_item.name_span(db), - def.kind.name.span, + def.kind.sig.kind.name.span, ); continue; } @@ -334,6 +330,14 @@ pub fn contract_field_type( let node = &field.data(db).ast; + if let Ok(Type::Trait(ref val)) = typ { + scope.error( + "traits can not be used as contract fields", + node.span, + &format!("trait `{}` can not appear here", val.name), + ); + } + if node.kind.is_pub { scope.not_yet_implemented("contract `pub` fields", node.span); } diff --git a/crates/analyzer/src/db/queries/functions.rs b/crates/analyzer/src/db/queries/functions.rs index ac7c8906bc..2aab4dc6b9 100644 --- a/crates/analyzer/src/db/queries/functions.rs +++ b/crates/analyzer/src/db/queries/functions.rs @@ -2,16 +2,17 @@ use crate::context::{AnalyzerContext, CallType, FunctionBody}; use crate::db::{Analysis, AnalyzerDb}; use crate::errors::TypeError; use crate::namespace::items::{ - Class, DepGraph, DepGraphWrapper, DepLocality, FunctionId, Item, TypeDef, + Class, DepGraph, DepGraphWrapper, DepLocality, FunctionId, FunctionSigId, Item, TypeDef, }; use crate::namespace::scopes::{BlockScope, BlockScopeType, FunctionScope, ItemScope}; use crate::namespace::types::{self, Contract, CtxDecl, Generic, SelfDecl, Struct, Type}; use crate::traversal::functions::traverse_statements; use crate::traversal::types::type_desc; use fe_common::diagnostics::Label; -use fe_parser::ast; +use fe_parser::ast::{self, GenericParameter}; use fe_parser::node::Node; use if_chain::if_chain; +use smol_str::SmolStr; use std::collections::HashMap; use std::rc::Rc; @@ -19,10 +20,9 @@ use std::rc::Rc; /// errors. Does not inspect the function body. pub fn function_signature( db: &dyn AnalyzerDb, - function: FunctionId, + function: FunctionSigId, ) -> Analysis> { - let node = &function.data(db).ast; - let def = &node.kind; + let def = &function.data(db).ast; let mut scope = ItemScope::new(db, function.module(db)); let fn_parent = function.class(db); @@ -32,9 +32,27 @@ pub fn function_signature( let mut names = HashMap::new(); let mut labels = HashMap::new(); - if let (Some(Class::Contract(_)), true) = (fn_parent, function.is_generic(db)) { + function.data(db).ast.kind.generic_params.kind.iter().fold( + HashMap::>::new(), + |mut accum, param| { + if let Some(previous) = accum.get(¶m.name()) { + scope.duplicate_name_error( + "duplicate generic parameter", + ¶m.name(), + previous.span, + param.name_node().span, + ); + } else { + accum.insert(param.name(), param.name_node()); + }; + + accum + }, + ); + + if !matches!(fn_parent, Some(Class::Struct(_))) && function.is_generic(db) { scope.fancy_error( - "generic function parameters aren't yet supported in contract functions", + "generic function parameters aren't yet supported outside of struct functions", vec![Label::primary( function.data(db).ast.kind.generic_params.span, "This can not appear here", @@ -43,7 +61,26 @@ pub fn function_signature( ); } + if function.is_generic(db) { + for param in function.data(db).ast.kind.generic_params.kind.iter() { + if let GenericParameter::Unbounded(val) = param { + scope.fancy_error( + "unbounded generic parameters aren't yet supported", + vec![Label::primary( + val.span, + format!("`{}` needs to be bound by some trait", val.kind), + )], + vec![format!( + "Hint: Change `{}` to `{}: SomeTrait`", + val.kind, val.kind + )], + ); + } + } + } + let params = def + .kind .args .iter() .enumerate() @@ -73,7 +110,7 @@ pub fn function_signature( _ => Err(TypeError::new(scope.error( "function parameter types must have fixed size", reg.typ.span, - "`Map` type can't be used as a function parameter", + &format!("`{}` type can't be used as a function parameter", typ.name()), ))), }); @@ -114,9 +151,9 @@ pub fn function_signature( if label.kind != "_"; if let Some(dup_idx) = labels.get(&label.kind); then { - let dup_arg: &Node = &def.args[*dup_idx]; + let dup_arg: &Node = &def.kind.args[*dup_idx]; scope.fancy_error( - &format!("duplicate parameter labels in function `{}`", def.name.kind), + &format!("duplicate parameter labels in function `{}`", def.kind.name.kind), vec![ Label::primary(dup_arg.span, "the label `{}` was first used here"), Label::primary(label.span, "label `{}` used again here"), @@ -138,9 +175,9 @@ pub fn function_signature( ); None } else if let Some(dup_idx) = names.get(®.name.kind) { - let dup_arg: &Node = &def.args[*dup_idx]; + let dup_arg: &Node = &def.kind.args[*dup_idx]; scope.duplicate_name_error( - &format!("duplicate parameter names in function `{}`", def.name.kind), + &format!("duplicate parameter names in function `{}`", function.name(db)), ®.name.kind, dup_arg.span, arg.span, @@ -159,10 +196,11 @@ pub fn function_signature( .collect(); let return_type = def + .kind .return_type .as_ref() .map(|type_node| { - let fn_name = &def.name.kind; + let fn_name = &function.name(db); if fn_name == "__init__" || fn_name == "__call__" { // `__init__` and `__call__` must not return any type other than `()`. if type_node.kind != ast::TypeDesc::Unit { @@ -207,7 +245,7 @@ pub fn function_signature( pub fn resolve_function_param_type( db: &dyn AnalyzerDb, - function: FunctionId, + function: FunctionSigId, context: &mut dyn AnalyzerContext, desc: &Node, ) -> Result { @@ -254,11 +292,11 @@ pub fn function_body(db: &dyn AnalyzerDb, function: FunctionId) -> Analysis D // A function that takes `self` depends on the type of `self`, so that any // relevant struct getters/setters are included when compiling. if let Some(class) = function.class(db) { - directs.push((root, class.as_item(), DepLocality::Local)); + directs.push((root, class.as_item(db), DepLocality::Local)); } let body = function.body(db); @@ -359,9 +397,16 @@ pub fn function_dependency_graph(db: &dyn AnalyzerDb, function: FunctionId) -> D // Including the "class" type here is probably redundant; the type will // also be part of the fn sig, or some type decl, or some create/create2 call, // or... - directs.push((root, class.as_item(), DepLocality::Local)); + directs.push((root, class.as_item(db), DepLocality::Local)); directs.push((root, Item::Function(*method), DepLocality::Local)); } + CallType::TraitValueMethod { trait_id, .. } => { + directs.push(( + root, + Item::Type(TypeDef::Trait(*trait_id)), + DepLocality::Local, + )); + } CallType::External { contract, function } => { directs.push((root, Item::Function(*function), DepLocality::External)); // Probably redundant: diff --git a/crates/analyzer/src/db/queries/impls.rs b/crates/analyzer/src/db/queries/impls.rs new file mode 100644 index 0000000000..ac67c8e589 --- /dev/null +++ b/crates/analyzer/src/db/queries/impls.rs @@ -0,0 +1,54 @@ +use indexmap::map::Entry; +use indexmap::IndexMap; +use smol_str::SmolStr; + +use crate::context::{Analysis, AnalyzerContext}; +use crate::namespace::items::{Class, Function, FunctionId, ImplId}; +use crate::namespace::scopes::ItemScope; +use crate::AnalyzerDb; +use std::rc::Rc; + +pub fn impl_all_functions(db: &dyn AnalyzerDb, impl_: ImplId) -> Rc<[FunctionId]> { + let impl_data = impl_.data(db); + impl_data + .ast + .kind + .functions + .iter() + .map(|node| { + db.intern_function(Rc::new(Function::new( + db, + node, + Some(Class::Impl(impl_)), + impl_data.module, + ))) + }) + .collect() +} + +pub fn impl_function_map( + db: &dyn AnalyzerDb, + impl_: ImplId, +) -> Analysis>> { + let scope = ItemScope::new(db, impl_.module(db)); + let mut map = IndexMap::::new(); + + for func in db.impl_all_functions(impl_).iter() { + let def_name = func.name(db); + + match map.entry(def_name) { + Entry::Occupied(entry) => { + scope.duplicate_name_error( + "duplicate function names in `impl` block", + entry.key(), + entry.get().name_span(db), + func.name_span(db), + ); + } + Entry::Vacant(entry) => { + entry.insert(*func); + } + } + } + Analysis::new(Rc::new(map), scope.diagnostics.take().into()) +} diff --git a/crates/analyzer/src/db/queries/module.rs b/crates/analyzer/src/db/queries/module.rs index f8d59a9552..380c0af16b 100644 --- a/crates/analyzer/src/db/queries/module.rs +++ b/crates/analyzer/src/db/queries/module.rs @@ -2,8 +2,8 @@ use crate::context::{Analysis, AnalyzerContext, Constant}; use crate::db::AnalyzerDb; use crate::errors::{self, ConstEvalError, TypeError}; use crate::namespace::items::{ - Contract, ContractId, Event, Function, Impl, Item, ModuleConstant, ModuleConstantId, ModuleId, - ModuleSource, Struct, StructId, Trait, TypeAlias, TypeDef, + Contract, ContractId, Event, Function, Impl, ImplId, Item, ModuleConstant, ModuleConstantId, + ModuleId, ModuleSource, Struct, StructId, Trait, TraitId, TypeAlias, TypeDef, }; use crate::namespace::scopes::ItemScope; use crate::namespace::types::{self, Type}; @@ -86,13 +86,9 @@ pub fn module_all_items(db: &dyn AnalyzerDb, module: ModuleId) -> Rc<[Item]> { module, }), ))), - ast::ModuleStmt::Function(node) => { - Some(Item::Function(db.intern_function(Rc::new(Function { - ast: node.clone(), - module, - parent: None, - })))) - } + ast::ModuleStmt::Function(node) => Some(Item::Function( + db.intern_function(Rc::new(Function::new(db, node, None, module))), + )), ast::ModuleStmt::Trait(node) => Some(Item::Type(TypeDef::Trait(db.intern_trait( Rc::new(Trait { ast: node.clone(), @@ -110,7 +106,7 @@ pub fn module_all_items(db: &dyn AnalyzerDb, module: ModuleId) -> Rc<[Item]> { .collect() } -pub fn module_all_impls(db: &dyn AnalyzerDb, module: ModuleId) -> Rc<[Impl]> { +pub fn module_all_impls(db: &dyn AnalyzerDb, module: ModuleId) -> Rc<[ImplId]> { let body = &module.ast(db).body; body.iter() .filter_map(|stmt| match stmt { @@ -124,12 +120,12 @@ pub fn module_all_impls(db: &dyn AnalyzerDb, module: ModuleId) -> Rc<[Impl]> { let receiver_type = type_desc(&mut scope, &impl_node.kind.receiver).unwrap(); if let Some(Item::Type(TypeDef::Trait(val))) = treit { - Some(Impl { + Some(db.intern_impl(Rc::new(Impl { trait_id: val, receiver: receiver_type, ast: impl_node.clone(), module, - }) + }))) } else { None } @@ -237,6 +233,37 @@ pub fn module_item_map( ) } +pub fn module_impl_map( + db: &dyn AnalyzerDb, + module: ModuleId, +) -> Analysis>> { + let scope = ItemScope::new(db, module); + let mut map = IndexMap::<(TraitId, Type), ImplId>::new(); + + for impl_ in db.module_all_impls(module).iter() { + let key = &(impl_.trait_id(db), impl_.receiver(db)); + + match map.entry(key.clone()) { + Entry::Occupied(entry) => { + scope.duplicate_name_error( + &format!( + "duplicate `impl` blocks for trait `{}` for type `{}`", + key.0.name(db), + key.1.name() + ), + "", + entry.get().ast(db).span, + impl_.ast(db).span, + ); + } + Entry::Vacant(entry) => { + entry.insert(*impl_); + } + } + } + Analysis::new(Rc::new(map), scope.diagnostics.take().into()) +} + pub fn module_contracts(db: &dyn AnalyzerDb, module: ModuleId) -> Rc<[ContractId]> { module .all_items(db) diff --git a/crates/analyzer/src/db/queries/structs.rs b/crates/analyzer/src/db/queries/structs.rs index 0f764d6b69..1b68306c65 100644 --- a/crates/analyzer/src/db/queries/structs.rs +++ b/crates/analyzer/src/db/queries/structs.rs @@ -3,8 +3,8 @@ use crate::context::AnalyzerContext; use crate::db::Analysis; use crate::errors::TypeError; use crate::namespace::items::{ - self, DepGraph, DepGraphWrapper, DepLocality, Function, FunctionId, Item, StructField, - StructFieldId, StructId, TypeDef, + self, DepGraph, DepGraphWrapper, DepLocality, FunctionId, Item, StructField, StructFieldId, + StructId, TypeDef, }; use crate::namespace::scopes::ItemScope; use crate::namespace::types::{self, Contract, Struct, Type}; @@ -121,11 +121,12 @@ pub fn struct_all_functions(db: &dyn AnalyzerDb, struct_: StructId) -> Rc<[Funct .functions .iter() .map(|node| { - db.intern_function(Rc::new(Function { - ast: node.clone(), - module: struct_data.module, - parent: Some(items::Class::Struct(struct_)), - })) + db.intern_function(Rc::new(items::Function::new( + db, + node, + Some(items::Class::Struct(struct_)), + struct_data.module, + ))) }) .collect() } @@ -150,7 +151,7 @@ pub fn struct_function_map( def_name, &named_item, named_item.name_span(db), - def.kind.name.span, + func.name_span(db), ); continue; } @@ -161,7 +162,7 @@ pub fn struct_function_map( "function name `{}` conflicts with built-in function", def_name ), - def.kind.name.span, + func.name_span(db), &format!("`{}` is a built-in function", def_name), ); continue; diff --git a/crates/analyzer/src/db/queries/traits.rs b/crates/analyzer/src/db/queries/traits.rs index e0a4cbd2f5..285dd4c3b2 100644 --- a/crates/analyzer/src/db/queries/traits.rs +++ b/crates/analyzer/src/db/queries/traits.rs @@ -1,4 +1,10 @@ -use crate::namespace::items::TraitId; +use indexmap::map::Entry; +use indexmap::IndexMap; +use smol_str::SmolStr; + +use crate::context::{Analysis, AnalyzerContext}; +use crate::namespace::items::{FunctionSig, FunctionSigId, TraitId}; +use crate::namespace::scopes::ItemScope; use crate::namespace::types; use crate::AnalyzerDb; use std::rc::Rc; @@ -9,3 +15,47 @@ pub fn trait_type(db: &dyn AnalyzerDb, trait_: TraitId) -> Rc { id: trait_, }) } + +pub fn trait_all_functions(db: &dyn AnalyzerDb, trait_: TraitId) -> Rc<[FunctionSigId]> { + let trait_data = trait_.data(db); + trait_data + .ast + .kind + .functions + .iter() + .map(|node| { + db.intern_function_sig(Rc::new(FunctionSig { + ast: node.clone(), + module: trait_.module(db), + parent: Some(trait_.as_class()), + })) + }) + .collect() +} + +pub fn trait_function_map( + db: &dyn AnalyzerDb, + trait_: TraitId, +) -> Analysis>> { + let scope = ItemScope::new(db, trait_.module(db)); + let mut map = IndexMap::::new(); + + for func in db.trait_all_functions(trait_).iter() { + let def_name = func.name(db); + + match map.entry(def_name) { + Entry::Occupied(entry) => { + scope.duplicate_name_error( + &format!("duplicate function names in `trait {}`", trait_.name(db)), + entry.key(), + entry.get().name_span(db), + func.name_span(db), + ); + } + Entry::Vacant(entry) => { + entry.insert(*func); + } + } + } + Analysis::new(Rc::new(map), scope.diagnostics.take().into()) +} diff --git a/crates/analyzer/src/namespace/items.rs b/crates/analyzer/src/namespace/items.rs index 944d6bf3c4..accf55db06 100644 --- a/crates/analyzer/src/namespace/items.rs +++ b/crates/analyzer/src/namespace/items.rs @@ -17,9 +17,10 @@ use indexmap::{indexmap, IndexMap}; use smol_str::SmolStr; use std::ops::Deref; use std::rc::Rc; + use strum::IntoEnumIterator; -use super::types::Type; +use super::types::{Type, TypeDowncast}; /// A named item. This does not include things inside of /// a function body. @@ -483,10 +484,14 @@ impl ModuleId { } /// Includes duplicate names - pub fn all_impls(&self, db: &dyn AnalyzerDb) -> Rc<[Impl]> { + pub fn all_impls(&self, db: &dyn AnalyzerDb) -> Rc<[ImplId]> { db.module_all_impls(*self) } + pub fn impls(&self, db: &dyn AnalyzerDb) -> Rc> { + db.module_impl_map(*self).value + } + /// Returns a map of the named items in the module pub fn items(&self, db: &dyn AnalyzerDb) -> Rc> { db.module_item_map(*self).value @@ -678,6 +683,9 @@ impl ModuleId { // duplicate item name errors sink.push_all(db.module_item_map(*self).diagnostics.iter()); + // duplicate impl errors + sink.push_all(db.module_impl_map(*self).diagnostics.iter()); + // errors for each item self.all_items(db) .iter() @@ -1072,33 +1080,30 @@ impl ContractFieldId { } #[derive(Debug, PartialEq, Eq, Hash, Clone)] -pub struct Function { - pub ast: Node, - pub module: ModuleId, +pub struct FunctionSig { + pub ast: Node, pub parent: Option, + pub module: ModuleId, } #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone)] -pub struct FunctionId(pub(crate) u32); -impl_intern_key!(FunctionId); -impl FunctionId { - pub fn data(&self, db: &dyn AnalyzerDb) -> Rc { - db.lookup_intern_function(*self) - } - pub fn span(&self, db: &dyn AnalyzerDb) -> Span { - self.data(db).ast.span - } - pub fn name(&self, db: &dyn AnalyzerDb) -> SmolStr { - self.data(db).ast.name().into() - } - pub fn name_span(&self, db: &dyn AnalyzerDb) -> Span { - self.data(db).ast.kind.name.span +pub struct FunctionSigId(pub(crate) u32); +impl_intern_key!(FunctionSigId); + +impl FunctionSigId { + pub fn data(&self, db: &dyn AnalyzerDb) -> Rc { + db.lookup_intern_function_sig(*self) } // This should probably be scrapped in favor of `parent(` pub fn class(&self, db: &dyn AnalyzerDb) -> Option { self.data(db).parent } + + pub fn takes_self(&self, db: &dyn AnalyzerDb) -> bool { + self.signature(db).self_decl.is_some() + } + pub fn self_typ(&self, db: &dyn AnalyzerDb) -> Option { match self.parent(db) { Item::Type(TypeDef::Contract(cid)) => { @@ -1110,21 +1115,6 @@ impl FunctionId { _ => None, } } - pub fn parent(&self, db: &dyn AnalyzerDb) -> Item { - let data = self.data(db); - data.parent - .map(|class| class.as_item()) - .unwrap_or(Item::Module(data.module)) - } - - pub fn module(&self, db: &dyn AnalyzerDb) -> ModuleId { - self.data(db).module - } - - pub fn takes_self(&self, db: &dyn AnalyzerDb) -> bool { - self.signature(db).self_decl.is_some() - } - pub fn self_span(&self, db: &dyn AnalyzerDb) -> Option { if self.takes_self(db) { self.data(db) @@ -1137,6 +1127,39 @@ impl FunctionId { None } } + pub fn signature(&self, db: &dyn AnalyzerDb) -> Rc { + db.function_signature(*self).value + } + + pub fn is_public(&self, db: &dyn AnalyzerDb) -> bool { + self.is_trait_fn(db) || self.pub_span(db).is_some() + } + pub fn name(&self, db: &dyn AnalyzerDb) -> SmolStr { + self.data(db).ast.kind.name.kind.clone() + } + pub fn name_span(&self, db: &dyn AnalyzerDb) -> Span { + self.data(db).ast.kind.name.span + } + pub fn unsafe_span(&self, db: &dyn AnalyzerDb) -> Option { + self.data(db).ast.kind.unsafe_ + } + pub fn is_constructor(&self, db: &dyn AnalyzerDb) -> bool { + self.name(db) == "__init__" + } + pub fn pub_span(&self, db: &dyn AnalyzerDb) -> Option { + self.data(db).ast.kind.pub_ + } + + pub fn parent(&self, db: &dyn AnalyzerDb) -> Item { + let data = self.data(db); + data.parent + .map(|class| class.as_item(db)) + .unwrap_or(Item::Module(data.module)) + } + + pub fn is_trait_fn(&self, db: &dyn AnalyzerDb) -> bool { + matches!(self.parent(db), Item::Type(TypeDef::Trait(_))) + } pub fn is_generic(&self, db: &dyn AnalyzerDb) -> bool { !self.data(db).ast.kind.generic_params.kind.is_empty() @@ -1157,23 +1180,99 @@ impl FunctionId { .cloned() } + pub fn module(&self, db: &dyn AnalyzerDb) -> ModuleId { + self.data(db).module + } + + pub fn is_contract_func(self, db: &dyn AnalyzerDb) -> bool { + matches! {self.parent(db), Item::Type(TypeDef::Contract(_))} + } + + pub fn sink_diagnostics(&self, db: &dyn AnalyzerDb, sink: &mut impl DiagnosticSink) { + sink.push_all(db.function_signature(*self).diagnostics.iter()); + } +} + +#[derive(Debug, PartialEq, Eq, Hash, Clone)] +pub struct Function { + pub ast: Node, + pub sig: FunctionSigId, +} + +impl Function { + pub fn new( + db: &dyn AnalyzerDb, + ast: &Node, + parent: Option, + module: ModuleId, + ) -> Self { + let sig = db.intern_function_sig(Rc::new(FunctionSig { + ast: ast.kind.sig.clone(), + parent, + module, + })); + Function { + sig, + ast: ast.clone(), + } + } +} + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone)] +pub struct FunctionId(pub(crate) u32); +impl_intern_key!(FunctionId); +impl FunctionId { + pub fn data(&self, db: &dyn AnalyzerDb) -> Rc { + db.lookup_intern_function(*self) + } + pub fn span(&self, db: &dyn AnalyzerDb) -> Span { + self.data(db).ast.span + } + pub fn name(&self, db: &dyn AnalyzerDb) -> SmolStr { + self.sig(db).name(db) + } + pub fn name_span(&self, db: &dyn AnalyzerDb) -> Span { + self.sig(db).name_span(db) + } + pub fn module(&self, db: &dyn AnalyzerDb) -> ModuleId { + self.sig(db).module(db) + } + // This should probably be scrapped in favor of `parent(` + pub fn class(&self, db: &dyn AnalyzerDb) -> Option { + self.sig(db).class(db) + } + pub fn self_typ(&self, db: &dyn AnalyzerDb) -> Option { + self.sig(db).self_typ(db) + } + pub fn parent(&self, db: &dyn AnalyzerDb) -> Item { + self.sig(db).parent(db) + } + pub fn takes_self(&self, db: &dyn AnalyzerDb) -> bool { + self.sig(db).takes_self(db) + } + pub fn self_span(&self, db: &dyn AnalyzerDb) -> Option { + self.sig(db).self_span(db) + } + pub fn is_generic(&self, db: &dyn AnalyzerDb) -> bool { + self.sig(db).is_generic(db) + } pub fn is_public(&self, db: &dyn AnalyzerDb) -> bool { - self.pub_span(db).is_some() + self.sig(db).is_public(db) } pub fn is_constructor(&self, db: &dyn AnalyzerDb) -> bool { - self.name(db) == "__init__" - } - pub fn pub_span(&self, db: &dyn AnalyzerDb) -> Option { - self.data(db).ast.kind.pub_ + self.sig(db).is_constructor(db) } pub fn is_unsafe(&self, db: &dyn AnalyzerDb) -> bool { self.unsafe_span(db).is_some() } pub fn unsafe_span(&self, db: &dyn AnalyzerDb) -> Option { - self.data(db).ast.kind.unsafe_ + self.sig(db).unsafe_span(db) } pub fn signature(&self, db: &dyn AnalyzerDb) -> Rc { - db.function_signature(*self).value + db.function_signature(self.data(db).sig).value + } + pub fn sig(&self, db: &dyn AnalyzerDb) -> FunctionSigId { + self.data(db).sig } pub fn body(&self, db: &dyn AnalyzerDb) -> Rc { db.function_body(*self).value @@ -1182,12 +1281,11 @@ impl FunctionId { db.function_dependency_graph(*self).0 } pub fn sink_diagnostics(&self, db: &dyn AnalyzerDb, sink: &mut impl DiagnosticSink) { - sink.push_all(db.function_signature(*self).diagnostics.iter()); + sink.push_all(db.function_signature(self.data(db).sig).diagnostics.iter()); sink.push_all(db.function_body(*self).diagnostics.iter()); } - pub fn is_contract_func(self, db: &dyn AnalyzerDb) -> bool { - matches! {self.parent(db), Item::Type(TypeDef::Contract(_))} + self.sig(db).is_contract_func(db) } } @@ -1196,16 +1294,29 @@ impl FunctionId { pub enum Class { Contract(ContractId), Struct(StructId), + Trait(TraitId), + Impl(ImplId), } impl Class { pub fn function(&self, db: &dyn AnalyzerDb, name: &str) -> Option { match self { Class::Contract(id) => id.function(db, name), Class::Struct(id) => id.function(db, name), + Class::Impl(id) => id.function(db, name), + // This would possibly change when in the future trait methods can provide a default implementation + Class::Trait(_) => None, } } - pub fn self_function(&self, db: &dyn AnalyzerDb, name: &str) -> Option { - let fun = self.function(db, name)?; + pub fn function_sig(&self, db: &dyn AnalyzerDb, name: &str) -> Option { + match self { + Class::Contract(id) => id.function(db, name).map(|fun| fun.sig(db)), + Class::Struct(id) => id.function(db, name).map(|fun| fun.sig(db)), + Class::Impl(id) => id.function(db, name).map(|fun| fun.sig(db)), + Class::Trait(id) => id.function(db, name), + } + } + pub fn self_function(&self, db: &dyn AnalyzerDb, name: &str) -> Option { + let fun = self.function_sig(db, name)?; fun.takes_self(db).then(|| fun) } @@ -1213,18 +1324,30 @@ impl Class { match self { Class::Contract(inner) => inner.name(db), Class::Struct(inner) => inner.name(db), + Class::Trait(inner) => inner.name(db), + Class::Impl(inner) => inner.name(db), } } pub fn kind(&self) -> &str { match self { Class::Contract(_) => "contract", Class::Struct(_) => "struct", + Class::Trait(_) => "trait", + Class::Impl(_) => "impl", } } - pub fn as_item(&self) -> Item { + pub fn as_item(&self, db: &dyn AnalyzerDb) -> Item { match self { Class::Contract(id) => Item::Type(TypeDef::Contract(*id)), Class::Struct(id) => Item::Type(TypeDef::Struct(*id)), + Class::Trait(id) => Item::Type(TypeDef::Trait(*id)), + // Coercing into an Item of the basis of the receiver doesn't seem ideal but can hopefully + // be addressed when we get rid of `Class`. + Class::Impl(id) => id + .receiver(db) + .as_struct() + .map(|val| Item::Type(TypeDef::Struct(val.id))) + .expect("missing receiver"), } } } @@ -1260,6 +1383,13 @@ impl StructId { self.data(db).module } + pub fn get_impl_for(&self, db: &dyn AnalyzerDb, trait_: TraitId) -> Option { + self.module(db) + .impls(db) + .get(&(trait_, Type::Struct((*self.typ(db)).clone()))) + .cloned() + } + pub fn typ(&self, db: &dyn AnalyzerDb) -> Rc { db.struct_type(*self) } @@ -1379,29 +1509,76 @@ pub struct Impl { pub ast: Node, } -impl Impl { +#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone)] +pub struct ImplId(pub(crate) u32); +impl_intern_key!(ImplId); +impl ImplId { + pub fn data(&self, db: &dyn AnalyzerDb) -> Rc { + db.lookup_intern_impl(*self) + } + pub fn span(&self, db: &dyn AnalyzerDb) -> Span { + self.data(db).ast.span + } + + pub fn module(&self, db: &dyn AnalyzerDb) -> ModuleId { + self.data(db).module + } + + pub fn all_functions(&self, db: &dyn AnalyzerDb) -> Rc<[FunctionId]> { + db.impl_all_functions(*self) + } + + pub fn trait_id(&self, db: &dyn AnalyzerDb) -> TraitId { + self.data(db).trait_id + } + + pub fn receiver(&self, db: &dyn AnalyzerDb) -> Type { + self.data(db).receiver.clone() + } + + pub fn ast(&self, db: &dyn AnalyzerDb) -> Node { + self.data(db).ast.clone() + } + + pub fn functions(&self, db: &dyn AnalyzerDb) -> Rc> { + db.impl_function_map(*self).value + } + pub fn function(&self, db: &dyn AnalyzerDb, name: &str) -> Option { + self.functions(db).get(name).copied() + } + pub fn parent(&self, db: &dyn AnalyzerDb) -> Item { + Item::Module(self.data(db).module) + } + pub fn name(&self, db: &dyn AnalyzerDb) -> SmolStr { + format!( + "{}_{}", + self.trait_id(db).name(db), + self.receiver(db).name() + ) + .into() + } + fn validate_type_or_trait_is_in_ingot( &self, db: &dyn AnalyzerDb, sink: &mut impl DiagnosticSink, - module: Option, + type_module: Option, ) { - let is_allowed = match module { - None => self.module == self.trait_id.module(db), - Some(type_module) => { - type_module == self.module || self.trait_id.module(db) == self.module - } + let impl_module = self.data(db).module; + let is_allowed = match type_module { + None => impl_module == self.data(db).trait_id.module(db), + Some(val) => val == impl_module || self.data(db).trait_id.module(db) == impl_module, }; if !is_allowed { sink.push(&errors::fancy_error( "illegal `impl`. Either type or trait must be in the same ingot as the `impl`", vec![Label::primary( - self.ast.span, + self.data(db).ast.span, format!( "Neither `{}` nor `{}` are in the ingot of the `impl` block", - self.receiver, - self.trait_id.name(db) + self.data(db).receiver, + self.data(db).trait_id.name(db) ), )], vec![], @@ -1410,23 +1587,138 @@ impl Impl { } pub fn sink_diagnostics(&self, db: &dyn AnalyzerDb, sink: &mut impl DiagnosticSink) { - match &self.receiver { + match &self.data(db).receiver { Type::Contract(_) | Type::Map(_) | Type::SelfContract(_) | Type::Trait(_) - | Type::Generic(_) => sink.push(&errors::fancy_error( - format!("`impl` blocks aren't allowed for {}", self.receiver), - vec![Label::primary(self.ast.span, "illegal `impl` block")], + | Type::Generic(_) + // TODO: We should find a way to support these types. We only support implementing traits for structs + // so far because it simplifies things regarding the assign location of the underlying type. + | Type::Base(_) + | Type::Array(_) + | Type::Tuple(_) + | Type::String(_) => sink.push(&errors::fancy_error( + format!( + "`impl` blocks aren't allowed for {}", + self.data(db).receiver + ), + vec![Label::primary( + self.data(db).ast.span, + "illegal `impl` block", + )], vec![], )), - Type::Base(_) | Type::Array(_) | Type::Tuple(_) | Type::String(_) => { - self.validate_type_or_trait_is_in_ingot(db, sink, None) - } Type::Struct(val) => { self.validate_type_or_trait_is_in_ingot(db, sink, Some(val.id.module(db))) } } + + if !self.trait_id(db).is_public(db) && self.trait_id(db).module(db) != self.module(db) { + let trait_module_name = self.trait_id(db).module(db).name(db); + let trait_name = self.trait_id(db).name(db); + sink.push(&errors::fancy_error( + &format!( + "the trait `{}` is private", + trait_name, + ), + vec![ + Label::primary(self.trait_id(db).data(db).ast.kind.name.span, "this trait is not `pub`"), + ], + vec![ + format!("`{}` can only be used within `{}`", trait_name, trait_module_name), + format!("Hint: use `pub trait {trait_}` to make `{trait_}` visible from outside of `{module}`", trait_=trait_name, module=trait_module_name), + ], + )); + } + + for impl_fn in self.all_functions(db).iter() { + impl_fn.sink_diagnostics(db, sink); + + if let Some(trait_fn) = self.trait_id(db).function(db, &impl_fn.name(db)) { + if impl_fn.signature(db).params != trait_fn.signature(db).params { + // TODO: This could be a nicer, more detailed report + sink.push(&errors::fancy_error( + format!( + "method `{}` has incompatible parameters for `{}` of trait `{}`", + impl_fn.name(db), + trait_fn.name(db), + self.trait_id(db).name(db) + ), + vec![ + Label::primary( + impl_fn.data(db).ast.kind.sig.span, + "signature of method in `impl` block", + ), + Label::primary( + trait_fn.data(db).ast.span, + format!( + "signature of method in trait `{}`", + self.trait_id(db).name(db) + ), + ), + ], + vec![], + )); + } + + if impl_fn.signature(db).return_type != trait_fn.signature(db).return_type { + // TODO: This could be a nicer, more detailed report + sink.push(&errors::fancy_error( + format!( + "method `{}` has an incompatible return type for `{}` of trait `{}`", + impl_fn.name(db), + trait_fn.name(db), + self.trait_id(db).name(db) + ), + vec![ + Label::primary( + impl_fn.data(db).ast.kind.sig.span, + "signature of method in `impl` block", + ), + Label::primary( + trait_fn.data(db).ast.span, + format!( + "signature of method in trait `{}`", + self.trait_id(db).name(db) + ), + ), + ], + vec![], + )); + } + } else { + sink.push(&errors::fancy_error( + format!( + "method `{}` is not a member of trait `{}`", + impl_fn.name(db), + self.trait_id(db).name(db) + ), + vec![Label::primary( + impl_fn.data(db).ast.span, + format!("not a member of trait `{}`", self.trait_id(db).name(db)), + )], + vec![], + )) + } + } + + for trait_fn in self.trait_id(db).all_functions(db).iter() { + if self.function(db, &trait_fn.name(db)).is_none() { + sink.push(&errors::fancy_error( + format!( + "not all members of trait `{}` implemented, missing: `{}`", + self.trait_id(db).name(db), + trait_fn.name(db) + ), + vec![Label::primary( + trait_fn.data(db).ast.span, + "this trait function is missing in `impl` block", + )], + vec![], + )) + } + } } } @@ -1465,7 +1757,7 @@ impl TraitId { self.module(db) .all_impls(db) .iter() - .any(|val| &val.trait_id == self && &val.receiver == ty) + .any(|val| &val.trait_id(db) == self && &val.receiver(db) == ty) } pub fn typ(&self, db: &dyn AnalyzerDb) -> Rc { @@ -1478,7 +1770,37 @@ impl TraitId { pub fn parent(&self, db: &dyn AnalyzerDb) -> Item { Item::Module(self.data(db).module) } - pub fn sink_diagnostics(&self, _db: &dyn AnalyzerDb, _sink: &mut impl DiagnosticSink) {} + pub fn all_functions(&self, db: &dyn AnalyzerDb) -> Rc<[FunctionSigId]> { + db.trait_all_functions(*self) + } + + pub fn functions(&self, db: &dyn AnalyzerDb) -> Rc> { + db.trait_function_map(*self).value + } + + pub fn function(&self, db: &dyn AnalyzerDb, name: &str) -> Option { + self.functions(db).get(name).copied() + } + + pub fn as_class(&self) -> Class { + Class::Trait(*self) + } + + pub fn sink_diagnostics(&self, db: &dyn AnalyzerDb, sink: &mut impl DiagnosticSink) { + db.trait_all_functions(*self).iter().for_each(|id| { + if !id.takes_self(db) { + sink.push(&errors::fancy_error( + "associated functions aren't yet supported in traits", + vec![Label::primary( + id.data(db).ast.span, + "function doesn't take `self`", + )], + vec!["Hint: add a `self` param to this function".into()], + )); + } + id.sink_diagnostics(db, sink) + }); + } } #[derive(Debug, PartialEq, Eq, Hash, Clone)] diff --git a/crates/analyzer/src/namespace/scopes.rs b/crates/analyzer/src/namespace/scopes.rs index 48f03ece9c..cdf4573ffc 100644 --- a/crates/analyzer/src/namespace/scopes.rs +++ b/crates/analyzer/src/namespace/scopes.rs @@ -309,6 +309,8 @@ impl<'a> AnalyzerContext for FunctionScope<'a> { .data(self.db) .ast .kind + .sig + .kind .args .iter() .find_map(|param| (param.name() == name).then(|| param.name_span())) diff --git a/crates/analyzer/src/namespace/types.rs b/crates/analyzer/src/namespace/types.rs index 602073667c..1bc5c7af29 100644 --- a/crates/analyzer/src/namespace/types.rs +++ b/crates/analyzer/src/namespace/types.rs @@ -1,7 +1,6 @@ use crate::context::AnalyzerContext; use crate::errors::TypeError; -use crate::namespace::items::TraitId; -use crate::namespace::items::{Class, ContractId, StructId}; +use crate::namespace::items::{Class, ContractId, StructId, TraitId}; use crate::AnalyzerDb; use fe_common::Span; @@ -496,6 +495,8 @@ pub trait TypeDowncast { fn as_int(&self) -> Option; fn as_primitive(&self) -> Option; fn as_class(&self) -> Option; + fn as_generic(&self) -> Option; + fn as_struct(&self) -> Option; } impl TypeDowncast for Type { @@ -539,6 +540,22 @@ impl TypeDowncast for Type { match self { Type::Struct(inner) => Some(Class::Struct(inner.id)), Type::Contract(inner) | Type::SelfContract(inner) => Some(Class::Contract(inner.id)), + Type::Generic(inner) if !inner.bounds.is_empty() => { + // FIXME: This won't hold when we support multiple bounds or traits can be implemented for non-struct types + inner.bounds.first().map(TraitId::as_class) + } + _ => None, + } + } + fn as_struct(&self) -> Option { + match self { + Type::Struct(val) => Some(val.clone()), + _ => None, + } + } + fn as_generic(&self) -> Option { + match self { + Type::Generic(generic) => Some(generic.clone()), _ => None, } } @@ -566,6 +583,12 @@ impl TypeDowncast for Option<&Type> { fn as_class(&self) -> Option { self.and_then(TypeDowncast::as_class) } + fn as_struct(&self) -> Option { + self.and_then(TypeDowncast::as_struct) + } + fn as_generic(&self) -> Option { + self.and_then(TypeDowncast::as_generic) + } } impl From for Type { diff --git a/crates/analyzer/src/traversal/expressions.rs b/crates/analyzer/src/traversal/expressions.rs index d015aebbf6..34065b1d72 100644 --- a/crates/analyzer/src/traversal/expressions.rs +++ b/crates/analyzer/src/traversal/expressions.rs @@ -3,6 +3,7 @@ use crate::context::{AnalyzerContext, CallType, ExpressionAttributes, Location, use crate::errors::{FatalError, IndexingError}; use crate::namespace::items::{Class, FunctionId, Item, TypeDef}; use crate::namespace::scopes::BlockScopeType; +use crate::namespace::types::Trait; use crate::namespace::types::{ Array, Base, Contract, FeString, Integer, Struct, Tuple, Type, TypeDowncast, U256, }; @@ -314,6 +315,14 @@ fn expr_named_thing( Type::Struct(Struct::from_id(id, context.db())), Location::Memory, )), + Class::Impl(id) => Ok(ExpressionAttributes::new( + id.receiver(context.db()), + Location::Memory, + )), + Class::Trait(id) => Ok(ExpressionAttributes::new( + Type::Trait(Trait::from_id(id, context.db())), + Location::Memory, + )), Class::Contract(id) => Ok(ExpressionAttributes::new( Type::SelfContract(Contract::from_id(id, context.db())), Location::Value, @@ -1312,7 +1321,7 @@ fn expr_call_method( if matches!(class, Class::Contract(_)) { check_for_call_to_special_fns(context, &field.kind, field.span)?; } - if let Some(method) = class.function(context.db(), &field.kind) { + if let Some(method) = class.function_sig(context.db(), &field.kind) { let is_self = is_self_value(target); if is_self && !method.takes_self(context.db()) { @@ -1348,12 +1357,11 @@ fn expr_call_method( let calltype = match class { Class::Contract(contract) => { + let method = contract + .function(context.db(), &method.name(context.db())) + .unwrap(); if is_self { - CallType::ValueMethod { - is_self, - class, - method, - } + CallType::ValueMethod { class, method } } else { // External contract address must be loaded onto the stack. context.update_expression( @@ -1369,7 +1377,10 @@ fn expr_call_method( } } } - Class::Struct(_) => { + Class::Struct(val) => { + let method = val + .function(context.db(), &method.name(context.db())) + .unwrap(); if matches!(target_attributes.final_location(), Location::Storage { .. }) { context.fancy_error( "struct functions can only be called on structs in memory", @@ -1383,12 +1394,22 @@ fn expr_call_method( vec![], ); } - CallType::ValueMethod { - is_self, - class, - method, - } + CallType::ValueMethod { class, method } + } + Class::Impl(val) => { + let method = val + .function(context.db(), &method.name(context.db())) + .unwrap(); + CallType::ValueMethod { class, method } } + Class::Trait(trait_id) => CallType::TraitValueMethod { + trait_id, + method, + generic_type: target_attributes + .typ + .as_generic() + .expect("expected generic type"), + }, }; let return_type = sig.return_type.clone()?; diff --git a/crates/analyzer/tests/analysis.rs b/crates/analyzer/tests/analysis.rs index e0fd946aca..2fcf9772dc 100644 --- a/crates/analyzer/tests/analysis.rs +++ b/crates/analyzer/tests/analysis.rs @@ -345,7 +345,13 @@ fn build_snapshot(db: &dyn AnalyzerDb, module: items::ModuleId) -> String { .collect(), ] .concat(), - Item::Type(TypeDef::Trait(_)) => vec![], // TODO: fill in when traits are allowed to have functions + Item::Type(TypeDef::Trait(val)) => val + .all_functions(db) + .iter() + .flat_map(|fun| { + build_debug_diagnostics(&[(fun.data(db).ast.span, &fun.signature(db))]) + }) + .collect::>(), Item::Function(id) => function_diagnostics(*id, db), Item::Constant(id) => vec![build_display_diagnostic(id.span(db), &id.typ(db).unwrap())], Item::Event(id) => event_diagnostics(*id, db), diff --git a/crates/analyzer/tests/errors.rs b/crates/analyzer/tests/errors.rs index d60b9e4817..906899b5ce 100644 --- a/crates/analyzer/tests/errors.rs +++ b/crates/analyzer/tests/errors.rs @@ -245,6 +245,7 @@ test_file! { duplicate_typedef_in_module } test_file! { duplicate_var_in_child_scope } test_file! { duplicate_var_in_contract_method } test_file! { duplicate_var_in_for_loop } +test_file! { duplicate_generic_params } test_file! { emit_bad_args } test_file! { external_call_type_error } test_file! { external_call_wrong_number_of_params } @@ -294,6 +295,12 @@ test_file! { struct_private_constructor } test_file! { struct_call_bad_args } test_file! { struct_call_without_kw_args } test_file! { struct_recursive_cycles } +test_file! { trait_impl_mismatch } +test_file! { trait_fn_without_self } +test_file! { trait_fn_with_generic_params } +test_file! { traits_as_fields } +test_file! { trait_conflicting_impls } +test_file! { traits_with_wrong_bounds } test_file! { non_pub_init } test_file! { init_wrong_return_type } test_file! { init_duplicate_def } diff --git a/crates/analyzer/tests/snapshots/errors__bad_visibility.snap b/crates/analyzer/tests/snapshots/errors__bad_visibility.snap index 1470def937..27fd67801f 100644 --- a/crates/analyzer/tests/snapshots/errors__bad_visibility.snap +++ b/crates/analyzer/tests/snapshots/errors__bad_visibility.snap @@ -4,9 +4,9 @@ expression: error_string_ingot(&path) --- error: the type `MyInt` is private - ┌─ compile_errors/bad_visibility/src/main.fe:5:33 + ┌─ compile_errors/bad_visibility/src/main.fe:8:33 │ -5 │ pub fn priv_type_alias() -> MyInt { +8 │ pub fn priv_type_alias() -> MyInt { │ ^^^^^ this type is not `pub` │ ┌─ compile_errors/bad_visibility/src/foo.fe:1:6 @@ -18,9 +18,9 @@ error: the type `MyInt` is private = Hint: use `pub` to make `MyInt` visible from outside of `foo` error: the type `MyInt` is private - ┌─ compile_errors/bad_visibility/src/main.fe:6:16 + ┌─ compile_errors/bad_visibility/src/main.fe:9:16 │ -6 │ let x: MyInt = 1 +9 │ let x: MyInt = 1 │ ^^^^^ this type is not `pub` │ ┌─ compile_errors/bad_visibility/src/foo.fe:1:6 @@ -32,9 +32,9 @@ error: the type `MyInt` is private = Hint: use `pub` to make `MyInt` visible from outside of `foo` error: the constant `MY_CONST` is private - ┌─ compile_errors/bad_visibility/src/main.fe:11:16 + ┌─ compile_errors/bad_visibility/src/main.fe:14:16 │ -11 │ return MY_CONST +14 │ return MY_CONST │ ^^^^^^^^ this constant is not `pub` │ ┌─ compile_errors/bad_visibility/src/foo.fe:3:7 @@ -46,9 +46,9 @@ error: the constant `MY_CONST` is private = Hint: use `pub` to make `MY_CONST` visible from outside of `foo` error: the constant `MY_CONST` is private - ┌─ compile_errors/bad_visibility/src/main.fe:11:16 + ┌─ compile_errors/bad_visibility/src/main.fe:14:16 │ -11 │ return MY_CONST +14 │ return MY_CONST │ ^^^^^^^^ this constant is not `pub` │ ┌─ compile_errors/bad_visibility/src/foo.fe:3:1 @@ -60,131 +60,140 @@ error: the constant `MY_CONST` is private = Hint: use `pub const MY_CONST` to make `MY_CONST` visible from outside of `foo` error: the event `MyEvent` is private - ┌─ compile_errors/bad_visibility/src/main.fe:15:14 + ┌─ compile_errors/bad_visibility/src/main.fe:18:14 │ -15 │ emit MyEvent(ctx, x: 1) +18 │ emit MyEvent(ctx, x: 1) │ ^^^^^^^ this event is not `pub` │ - ┌─ compile_errors/bad_visibility/src/foo.fe:5:7 + ┌─ compile_errors/bad_visibility/src/foo.fe:7:7 │ - 5 │ event MyEvent { + 7 │ event MyEvent { │ ------- `MyEvent` is defined here │ = `MyEvent` can only be used within `foo` = Hint: use `pub` to make `MyEvent` visible from outside of `foo` error: the event `MyEvent` is private - ┌─ compile_errors/bad_visibility/src/main.fe:15:14 + ┌─ compile_errors/bad_visibility/src/main.fe:18:14 │ -15 │ emit MyEvent(ctx, x: 1) +18 │ emit MyEvent(ctx, x: 1) │ ^^^^^^^ this event is not `pub` │ - ┌─ compile_errors/bad_visibility/src/foo.fe:5:1 + ┌─ compile_errors/bad_visibility/src/foo.fe:7:1 │ - 5 │ â•­ event MyEvent { - 6 │ │ x: i32 - 7 │ │ } + 7 │ â•­ event MyEvent { + 8 │ │ x: i32 + 9 │ │ } │ ╰─' `MyEvent` is defined here │ = `MyEvent` can only be used within `foo` = Hint: use `pub event MyEvent` to make `MyEvent` visible from outside of `foo` error: cannot find value `ctx` in this scope - ┌─ compile_errors/bad_visibility/src/main.fe:15:22 + ┌─ compile_errors/bad_visibility/src/main.fe:18:22 │ -15 │ emit MyEvent(ctx, x: 1) +18 │ emit MyEvent(ctx, x: 1) │ ^^^ undefined error: the type `MyStruct` is private - ┌─ compile_errors/bad_visibility/src/main.fe:19:16 + ┌─ compile_errors/bad_visibility/src/main.fe:22:16 │ -19 │ let s: MyStruct = MyStruct(x: 1) +22 │ let s: MyStruct = MyStruct(x: 1) │ ^^^^^^^^ this type is not `pub` │ - ┌─ compile_errors/bad_visibility/src/foo.fe:9:8 + ┌─ compile_errors/bad_visibility/src/foo.fe:11:8 │ - 9 │ struct MyStruct { +11 │ struct MyStruct { │ -------- `MyStruct` is defined here │ = `MyStruct` can only be used within `foo` = Hint: use `pub` to make `MyStruct` visible from outside of `foo` error: the type `MyStruct` is private - ┌─ compile_errors/bad_visibility/src/main.fe:19:27 + ┌─ compile_errors/bad_visibility/src/main.fe:22:27 │ -19 │ let s: MyStruct = MyStruct(x: 1) +22 │ let s: MyStruct = MyStruct(x: 1) │ ^^^^^^^^ this type is not `pub` │ - ┌─ compile_errors/bad_visibility/src/foo.fe:9:8 + ┌─ compile_errors/bad_visibility/src/foo.fe:11:8 │ - 9 │ struct MyStruct { +11 │ struct MyStruct { │ -------- `MyStruct` is defined here │ = `MyStruct` can only be used within `foo` = Hint: use `pub` to make `MyStruct` visible from outside of `foo` error: Can not call private constructor of struct `MyStruct` - ┌─ compile_errors/bad_visibility/src/foo.fe:10:5 + ┌─ compile_errors/bad_visibility/src/foo.fe:12:5 │ -10 │ x: i32 +12 │ x: i32 │ ^^^^^^ Field `x` is private │ = Suggestion: implement a method `new(...)` on struct `MyStruct` to call the constructor and return the struct error: the function `my_func` is private - ┌─ compile_errors/bad_visibility/src/main.fe:23:9 + ┌─ compile_errors/bad_visibility/src/main.fe:26:9 │ -23 │ my_func() +26 │ my_func() │ ^^^^^^^ this function is not `pub` │ - ┌─ compile_errors/bad_visibility/src/foo.fe:13:4 + ┌─ compile_errors/bad_visibility/src/foo.fe:15:4 │ -13 │ fn my_func() {} +15 │ fn my_func() {} │ ------- `my_func` is defined here │ = `my_func` can only be used within `foo` = Hint: use `pub` to make `my_func` visible from outside of `foo` error: the type `MyContract` is private - ┌─ compile_errors/bad_visibility/src/main.fe:27:16 + ┌─ compile_errors/bad_visibility/src/main.fe:30:16 │ -27 │ let _: MyContract = MyContract(addr) +30 │ let _: MyContract = MyContract(addr) │ ^^^^^^^^^^ this type is not `pub` │ - ┌─ compile_errors/bad_visibility/src/foo.fe:15:10 + ┌─ compile_errors/bad_visibility/src/foo.fe:17:10 │ -15 │ contract MyContract { +17 │ contract MyContract { │ ---------- `MyContract` is defined here │ = `MyContract` can only be used within `foo` = Hint: use `pub` to make `MyContract` visible from outside of `foo` error: the type `MyContract` is private - ┌─ compile_errors/bad_visibility/src/main.fe:27:29 + ┌─ compile_errors/bad_visibility/src/main.fe:30:29 │ -27 │ let _: MyContract = MyContract(addr) +30 │ let _: MyContract = MyContract(addr) │ ^^^^^^^^^^ this type is not `pub` │ - ┌─ compile_errors/bad_visibility/src/foo.fe:15:10 + ┌─ compile_errors/bad_visibility/src/foo.fe:17:10 │ -15 │ contract MyContract { +17 │ contract MyContract { │ ---------- `MyContract` is defined here │ = `MyContract` can only be used within `foo` = Hint: use `pub` to make `MyContract` visible from outside of `foo` error: the type `MyContract` is private - ┌─ compile_errors/bad_visibility/src/main.fe:28:9 + ┌─ compile_errors/bad_visibility/src/main.fe:31:9 │ -28 │ MyContract.create(ctx, 1) +31 │ MyContract.create(ctx, 1) │ ^^^^^^^^^^ this type is not `pub` │ - ┌─ compile_errors/bad_visibility/src/foo.fe:15:10 + ┌─ compile_errors/bad_visibility/src/foo.fe:17:10 │ -15 │ contract MyContract { +17 │ contract MyContract { │ ---------- `MyContract` is defined here │ = `MyContract` can only be used within `foo` = Hint: use `pub` to make `MyContract` visible from outside of `foo` +error: the trait `MyTrait` is private + ┌─ compile_errors/bad_visibility/src/foo.fe:5:7 + │ +5 │ trait MyTrait {} + │ ^^^^^^^ this trait is not `pub` + │ + = `MyTrait` can only be used within `foo` + = Hint: use `pub trait MyTrait` to make `MyTrait` visible from outside of `foo` + diff --git a/crates/analyzer/tests/snapshots/errors__call_generic_function_with_unsatisfied_bound.snap b/crates/analyzer/tests/snapshots/errors__call_generic_function_with_unsatisfied_bound.snap index 7e91beb201..abb2fdd727 100644 --- a/crates/analyzer/tests/snapshots/errors__call_generic_function_with_unsatisfied_bound.snap +++ b/crates/analyzer/tests/snapshots/errors__call_generic_function_with_unsatisfied_bound.snap @@ -3,16 +3,16 @@ source: crates/analyzer/tests/errors.rs expression: "error_string(&path, test_files::fixture(path))" --- -error: the trait bound `bool: IsNumeric` is not satisfied - ┌─ compile_errors/call_generic_function_with_unsatisfied_bound.fe:14:13 +error: the trait bound `Bar: Dummy` is not satisfied + ┌─ compile_errors/call_generic_function_with_unsatisfied_bound.fe:16:13 │ -14 │ foo.bar(true) - │ ^^^^ the trait `IsNumeric` is not implemented for `bool` +16 │ foo.bar(Bar()) + │ ^^^^^ the trait `Dummy` is not implemented for `Bar` error: incorrect type for `bar` argument at position 0 - ┌─ compile_errors/call_generic_function_with_unsatisfied_bound.fe:14:13 + ┌─ compile_errors/call_generic_function_with_unsatisfied_bound.fe:16:13 │ -14 │ foo.bar(true) - │ ^^^^ this has type `bool`; expected type `T` +16 │ foo.bar(Bar()) + │ ^^^^^ this has type `Bar`; expected type `T` diff --git a/crates/analyzer/tests/snapshots/errors__call_non_pub_fn_on_external_contract.snap b/crates/analyzer/tests/snapshots/errors__call_non_pub_fn_on_external_contract.snap index e5f8c8da80..013e0ac3e2 100644 --- a/crates/analyzer/tests/snapshots/errors__call_non_pub_fn_on_external_contract.snap +++ b/crates/analyzer/tests/snapshots/errors__call_non_pub_fn_on_external_contract.snap @@ -5,13 +5,11 @@ expression: "error_string(&path, test_files::fixture(path))" --- error: the function `do_private_thingz` on `contract Foo` is private ┌─ compile_errors/call_non_pub_fn_on_external_contract.fe:13:25 - │ - 6 │ â•­ fn do_private_thingz(self) { - 7 │ │ self.val = 100 - 8 │ │ } - │ ╰─────' `do_private_thingz` is defined here - · │ -13 │ Foo(address(0)).do_private_thingz() - │ ^^^^^^^^^^^^^^^^^ this function is not `pub` + │ + 6 │ fn do_private_thingz(self) { + │ -------------------------- `do_private_thingz` is defined here + · +13 │ Foo(address(0)).do_private_thingz() + │ ^^^^^^^^^^^^^^^^^ this function is not `pub` diff --git a/crates/analyzer/tests/snapshots/errors__contract_function_with_generic_params.snap b/crates/analyzer/tests/snapshots/errors__contract_function_with_generic_params.snap index 810727eaa3..4b9ef16c72 100644 --- a/crates/analyzer/tests/snapshots/errors__contract_function_with_generic_params.snap +++ b/crates/analyzer/tests/snapshots/errors__contract_function_with_generic_params.snap @@ -3,11 +3,11 @@ source: crates/analyzer/tests/errors.rs expression: "error_string(&path, test_files::fixture(path))" --- -error: generic function parameters aren't yet supported in contract functions +error: generic function parameters aren't yet supported outside of struct functions ┌─ compile_errors/contract_function_with_generic_params.fe:4:15 │ -4 │ pub fn bar(val: T) {} - │ ^^^^^^^^^^^^^^ This can not appear here +4 │ pub fn bar(val: T) {} + │ ^^^^^^^^^^ This can not appear here │ = Hint: Struct functions can have generic parameters diff --git a/crates/analyzer/tests/snapshots/errors__duplicate_generic_params.snap b/crates/analyzer/tests/snapshots/errors__duplicate_generic_params.snap new file mode 100644 index 0000000000..4127fa4d18 --- /dev/null +++ b/crates/analyzer/tests/snapshots/errors__duplicate_generic_params.snap @@ -0,0 +1,14 @@ +--- +source: crates/analyzer/tests/errors.rs +expression: "error_string(&path, test_files::fixture(path))" + +--- +error: duplicate generic parameter + ┌─ compile_errors/duplicate_generic_params.fe:10:10 + │ +10 │ fn foo(val1: T, val2: T) { + │ ^ - `T` redefined here + │ │ + │ `T` first defined here + + diff --git a/crates/analyzer/tests/snapshots/errors__invalid_impl_location.snap b/crates/analyzer/tests/snapshots/errors__invalid_impl_location.snap index 5009464436..ea4026845a 100644 --- a/crates/analyzer/tests/snapshots/errors__invalid_impl_location.snap +++ b/crates/analyzer/tests/snapshots/errors__invalid_impl_location.snap @@ -4,9 +4,9 @@ expression: "error_string(&path, test_files::fixture(path))" --- error: illegal `impl`. Either type or trait must be in the same ingot as the `impl` - ┌─ compile_errors/invalid_impl_location.fe:3:1 + ┌─ compile_errors/invalid_impl_location.fe:4:1 │ -3 │ impl IsNumeric for u8 {} - │ ^^^^^^^^^^^^^^^^^^^^^ Neither `u8` nor `IsNumeric` are in the ingot of the `impl` block +4 │ impl Dummy for Context {} + │ ^^^^^^^^^^^^^^^^^^^^^^ Neither `Context` nor `Dummy` are in the ingot of the `impl` block diff --git a/crates/analyzer/tests/snapshots/errors__trait_conflicting_impls.snap b/crates/analyzer/tests/snapshots/errors__trait_conflicting_impls.snap new file mode 100644 index 0000000000..faf8b54b23 --- /dev/null +++ b/crates/analyzer/tests/snapshots/errors__trait_conflicting_impls.snap @@ -0,0 +1,14 @@ +--- +source: crates/analyzer/tests/errors.rs +expression: "error_string(&path, test_files::fixture(path))" + +--- +error: duplicate `impl` blocks for trait `Foo` for type `Bar` + ┌─ compile_errors/trait_conflicting_impls.fe:6:1 + │ +6 │ impl Foo for Bar {} + │ ^^^^^^^^^^^^^^^^ `` first defined here +7 │ impl Foo for Bar {} + │ ---------------- `` redefined here + + diff --git a/crates/analyzer/tests/snapshots/errors__trait_fn_with_generic_params.snap b/crates/analyzer/tests/snapshots/errors__trait_fn_with_generic_params.snap new file mode 100644 index 0000000000..f580c42e0a --- /dev/null +++ b/crates/analyzer/tests/snapshots/errors__trait_fn_with_generic_params.snap @@ -0,0 +1,14 @@ +--- +source: crates/analyzer/tests/errors.rs +expression: "error_string(&path, test_files::fixture(path))" + +--- +error: generic function parameters aren't yet supported outside of struct functions + ┌─ compile_errors/trait_fn_with_generic_params.fe:4:18 + │ +4 │ fn generic_fn(self, val: T); + │ ^^^^^^^^ This can not appear here + │ + = Hint: Struct functions can have generic parameters + + diff --git a/crates/analyzer/tests/snapshots/errors__trait_fn_without_self.snap b/crates/analyzer/tests/snapshots/errors__trait_fn_without_self.snap new file mode 100644 index 0000000000..44802d22a2 --- /dev/null +++ b/crates/analyzer/tests/snapshots/errors__trait_fn_without_self.snap @@ -0,0 +1,14 @@ +--- +source: crates/analyzer/tests/errors.rs +expression: "error_string(&path, test_files::fixture(path))" + +--- +error: associated functions aren't yet supported in traits + ┌─ compile_errors/trait_fn_without_self.fe:2:5 + │ +2 │ fn this_has_no_self(); + │ ^^^^^^^^^^^^^^^^^^^^^ function doesn't take `self` + │ + = Hint: add a `self` param to this function + + diff --git a/crates/analyzer/tests/snapshots/errors__trait_impl_mismatch.snap b/crates/analyzer/tests/snapshots/errors__trait_impl_mismatch.snap new file mode 100644 index 0000000000..feb3959fe5 --- /dev/null +++ b/crates/analyzer/tests/snapshots/errors__trait_impl_mismatch.snap @@ -0,0 +1,36 @@ +--- +source: crates/analyzer/tests/errors.rs +expression: "error_string(&path, test_files::fixture(path))" + +--- +error: method `this_has_wrong_args_in_impl` has incompatible parameters for `this_has_wrong_args_in_impl` of trait `Foo` + ┌─ compile_errors/trait_impl_mismatch.fe:3:5 + │ + 3 │ fn this_has_wrong_args_in_impl(self, val: u8, val2: bool); + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ signature of method in trait `Foo` + · +11 │ fn this_has_wrong_args_in_impl(self, val: u16, val2: Array) {} + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ signature of method in `impl` block + +error: method `this_has_wrong_return_type_in_impl` has an incompatible return type for `this_has_wrong_return_type_in_impl` of trait `Foo` + ┌─ compile_errors/trait_impl_mismatch.fe:4:5 + │ + 4 │ fn this_has_wrong_return_type_in_impl(self) -> bool; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ signature of method in trait `Foo` + · +12 │ fn this_has_wrong_return_type_in_impl(self) -> u8 { + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ signature of method in `impl` block + +error: method `this_does_not_exist_in_trait` is not a member of trait `Foo` + ┌─ compile_errors/trait_impl_mismatch.fe:15:5 + │ +15 │ fn this_does_not_exist_in_trait() {} + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `Foo` + +error: not all members of trait `Foo` implemented, missing: `this_misses_in_impl` + ┌─ compile_errors/trait_impl_mismatch.fe:2:5 + │ +2 │ fn this_misses_in_impl(self); + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this trait function is missing in `impl` block + + diff --git a/crates/analyzer/tests/snapshots/errors__traits_as_fields.snap b/crates/analyzer/tests/snapshots/errors__traits_as_fields.snap new file mode 100644 index 0000000000..2f6755a385 --- /dev/null +++ b/crates/analyzer/tests/snapshots/errors__traits_as_fields.snap @@ -0,0 +1,18 @@ +--- +source: crates/analyzer/tests/errors.rs +expression: "error_string(&path, test_files::fixture(path))" + +--- +error: struct field type must have a fixed size + ┌─ compile_errors/traits_as_fields.fe:4:5 + │ +4 │ val: Foo + │ ^^^^^^^^ this can't be used as an struct field + +error: traits can not be used as contract fields + ┌─ compile_errors/traits_as_fields.fe:8:5 + │ +8 │ val: Foo + │ ^^^^^^^^ trait `Foo` can not appear here + + diff --git a/crates/analyzer/tests/snapshots/errors__traits_with_wrong_bounds.snap b/crates/analyzer/tests/snapshots/errors__traits_with_wrong_bounds.snap new file mode 100644 index 0000000000..c8f8b24146 --- /dev/null +++ b/crates/analyzer/tests/snapshots/errors__traits_with_wrong_bounds.snap @@ -0,0 +1,14 @@ +--- +source: crates/analyzer/tests/errors.rs +expression: "error_string(&path, test_files::fixture(path))" + +--- +error: unbounded generic parameters aren't yet supported + ┌─ compile_errors/traits_with_wrong_bounds.fe:4:22 + │ +4 │ pub fn no_bounds(val: T) {} + │ ^ `T` needs to be bound by some trait + │ + = Hint: Change `T` to `T: SomeTrait` + + diff --git a/crates/codegen/src/db/queries/function.rs b/crates/codegen/src/db/queries/function.rs index 53ab8cc158..d33613c9e4 100644 --- a/crates/codegen/src/db/queries/function.rs +++ b/crates/codegen/src/db/queries/function.rs @@ -1,5 +1,6 @@ use std::rc::Rc; +use fe_analyzer::namespace::items::Class; use fe_mir::ir::{FunctionBody, FunctionId, FunctionSignature}; use crate::{db::CodegenDb, yul::legalize}; @@ -19,7 +20,29 @@ pub fn legalized_body(db: &dyn CodegenDb, function: FunctionId) -> Rc Rc { let module = function.signature(db.upcast()).module_id; let module_name = module.name(db.upcast()); - let func_name = function.name_with_class(db.upcast()).replace("::", "$"); + + let analyzer_func = function.analyzer_func(db.upcast()); + let func_name = format!( + "{}{}", + analyzer_func.name(db.upcast()), + function.type_suffix(db.upcast()) + ); + + let func_name = match analyzer_func.class(db.upcast()) { + Some(Class::Impl(id)) => { + let class_name = format!( + "{}${}", + id.trait_id(db.upcast()).name(db.upcast()), + id.receiver(db.upcast()).name() + ); + format!("{}${}", class_name, func_name) + } + Some(class) => { + let class_name = class.name(db.upcast()); + format!("{}${}", class_name, func_name) + } + _ => func_name, + }; format!("{}${}", module_name, func_name).into() } diff --git a/crates/library/std/src/traits.fe b/crates/library/std/src/traits.fe index 15ea592387..896bb3cf80 100644 --- a/crates/library/std/src/traits.fe +++ b/crates/library/std/src/traits.fe @@ -1,15 +1,3 @@ -# Marker trait that all numeric types implement -pub trait IsNumeric {} +# Dummy trait used in testing. We can remove this once we have more useful traits -impl IsNumeric for u8 {} -impl IsNumeric for u16 {} -impl IsNumeric for u32 {} -impl IsNumeric for u64 {} -impl IsNumeric for u128 {} -impl IsNumeric for u256 {} -impl IsNumeric for i8 {} -impl IsNumeric for i16 {} -impl IsNumeric for i32 {} -impl IsNumeric for i64 {} -impl IsNumeric for i128 {} -impl IsNumeric for i256 {} +pub trait Dummy {} \ No newline at end of file diff --git a/crates/mir/src/db/queries/function.rs b/crates/mir/src/db/queries/function.rs index 7c32cb8470..e9484dade2 100644 --- a/crates/mir/src/db/queries/function.rs +++ b/crates/mir/src/db/queries/function.rs @@ -1,6 +1,7 @@ use std::rc::Rc; use fe_analyzer::namespace::items as analyzer_items; +use fe_analyzer::namespace::items::Class; use fe_analyzer::namespace::types as analyzer_types; use smol_str::SmolStr; @@ -56,7 +57,21 @@ impl ir::FunctionId { } pub fn is_contract_init(self, db: &dyn MirDb) -> bool { - self.analyzer_func(db).is_constructor(db.upcast()) + self.analyzer_func(db) + .data(db.upcast()) + .sig + .is_constructor(db.upcast()) + } + + /// Returns a type suffix if a generic function was monomorphized + pub fn type_suffix(&self, db: &dyn MirDb) -> SmolStr { + self.signature(db) + .resolved_generics + .values() + .fold(String::new(), |acc, param| { + format!("{}_{}", acc, param.name()) + }) + .into() } pub fn name(&self, db: &dyn MirDb) -> SmolStr { @@ -65,24 +80,28 @@ impl ir::FunctionId { } /// Returns `class_name::fn_name` if a function is a method else `fn_name`. - pub fn name_with_class(self, db: &dyn MirDb) -> SmolStr { + pub fn debug_name(self, db: &dyn MirDb) -> SmolStr { let analyzer_func = self.analyzer_func(db); - let func_name = analyzer_func.name(db.upcast()); - - // Construct a suffix for any monomorphized generic function parameters - let type_suffix = self - .signature(db) - .resolved_generics - .values() - .fold(String::new(), |acc, param| { - format!("{}_{}", acc, param.name()) - }); - - if let Some(class) = analyzer_func.class(db.upcast()) { - let class_name = class.name(db.upcast()); - format!("{}::{}{}", class_name, func_name, type_suffix).into() - } else { - format!("{}{}", func_name, type_suffix).into() + let func_name = format!( + "{}{}", + analyzer_func.name(db.upcast()), + self.type_suffix(db) + ); + + match analyzer_func.class(db.upcast()) { + Some(Class::Impl(id)) => { + let class_name = format!( + "<{} as {}>", + id.receiver(db.upcast()).name(), + id.trait_id(db.upcast()).name(db.upcast()) + ); + format!("{}::{}", class_name, func_name).into() + } + Some(class) => { + let class_name = class.name(db.upcast()); + format!("{}::{}", class_name, func_name).into() + } + _ => func_name.into(), } } diff --git a/crates/mir/src/graphviz/function.rs b/crates/mir/src/graphviz/function.rs index 85840e2c0e..424c90492a 100644 --- a/crates/mir/src/graphviz/function.rs +++ b/crates/mir/src/graphviz/function.rs @@ -52,7 +52,7 @@ impl FunctionNode { let body = self.func.body(db); let sig_data = self.func.signature(db); - let mut sig = format!("fn {}(", self.func.name_with_class(db)); + let mut sig = format!("fn {}(", self.func.debug_name(db)); let params = &sig_data.params; let param_len = params.len(); diff --git a/crates/mir/src/lower/function.rs b/crates/mir/src/lower/function.rs index 7562523816..ef6e3445d3 100644 --- a/crates/mir/src/lower/function.rs +++ b/crates/mir/src/lower/function.rs @@ -5,7 +5,7 @@ use fe_analyzer::{ context::CallType as AnalyzerCallType, namespace::{ items as analyzer_items, - types::{self as analyzer_types, Type}, + types::{self as analyzer_types, Type, TypeDowncast}, }, }; use fe_common::numeric::Literal; @@ -42,6 +42,7 @@ pub fn lower_monomorphized_func_signature( let mut params = vec![]; let mut concrete_args: &[Type] = concrete_args; let has_self = func.takes_self(db.upcast()); + if has_self { let self_ty = func.self_typ(db.upcast()).unwrap(); let source = self_arg_source(db, func); @@ -914,7 +915,36 @@ impl<'db, 'a> BodyLowerHelper<'db, 'a> { self.builder .call(func_id, method_args, CallType::Internal, source) } + AnalyzerCallType::TraitValueMethod { + method, + trait_id, + generic_type, + .. + } => { + let mut method_args = vec![self.lower_method_receiver(func)]; + method_args.append(&mut args); + + let struct_type = self + .func + .signature(self.db) + .resolved_generics + .get(generic_type) + .as_struct() + .expect("unexpected implementer of trait"); + + let impl_ = struct_type + .id + .get_impl_for(self.db.upcast(), *trait_id) + .expect("missing impl"); + let function = impl_ + .function(self.db.upcast(), &method.name(self.db.upcast())) + .expect("missing function"); + + let func_id = self.db.mir_lowered_func_signature(function); + self.builder + .call(func_id, method_args, CallType::Internal, source) + } AnalyzerCallType::External { function, .. } => { let mut method_args = vec![self.lower_method_receiver(func)]; method_args.append(&mut args); @@ -1191,6 +1221,8 @@ fn self_arg_source(db: &dyn MirDb, func: analyzer_items::FunctionId) -> SourceIn func.data(db.upcast()) .ast .kind + .sig + .kind .args .iter() .find(|arg| matches!(arg.kind, ast::FunctionArg::Self_)) @@ -1202,6 +1234,8 @@ fn arg_source(db: &dyn MirDb, func: analyzer_items::FunctionId, arg_name: &str) func.data(db.upcast()) .ast .kind + .sig + .kind .args .iter() .find_map(|arg| match &arg.kind { diff --git a/crates/mir/src/pretty_print/inst.rs b/crates/mir/src/pretty_print/inst.rs index d047a54494..6bd8eaf042 100644 --- a/crates/mir/src/pretty_print/inst.rs +++ b/crates/mir/src/pretty_print/inst.rs @@ -99,7 +99,7 @@ impl PrettyPrint for InstId { args, call_type, } => { - let name = func.name_with_class(db); + let name = func.debug_name(db); write!(w, "{}@{}(", name, call_type)?; args.as_slice().pretty_print(db, store, w)?; write!(w, ")") diff --git a/crates/parser/src/ast.rs b/crates/parser/src/ast.rs index cad4a21915..69b58a4290 100644 --- a/crates/parser/src/ast.rs +++ b/crates/parser/src/ast.rs @@ -92,6 +92,7 @@ pub struct Struct { #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)] pub struct Trait { pub name: Node, + pub functions: Vec>, pub pub_qual: Option, } @@ -99,6 +100,7 @@ pub struct Trait { pub struct Impl { pub impl_trait: Node, pub receiver: Node, + pub functions: Vec>, } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)] @@ -146,6 +148,19 @@ pub enum GenericParameter { }, } +impl GenericParameter { + pub fn name(&self) -> SmolStr { + self.name_node().kind + } + + pub fn name_node(&self) -> Node { + match self { + GenericParameter::Unbounded(node) => node.clone(), + GenericParameter::Bounded { name, .. } => name.clone(), + } + } +} + impl Spanned for GenericParameter { fn span(&self) -> Span { match self { @@ -180,7 +195,7 @@ pub struct Event { } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)] -pub struct Function { +pub struct FunctionSignature { // qualifier order: `pub unsafe fn` pub pub_: Option, pub unsafe_: Option, @@ -188,6 +203,11 @@ pub struct Function { pub generic_params: Node>, pub args: Vec>, pub return_type: Option>, +} + +#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone)] +pub struct Function { + pub sig: Node, pub body: Vec>, } @@ -404,7 +424,7 @@ impl Node { impl Node { pub fn name(&self) -> &str { - &self.kind.name.kind + &self.kind.sig.kind.name.kind } } @@ -729,15 +749,14 @@ impl fmt::Display for Node { impl fmt::Display for Function { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - let Function { + let FunctionSignature { pub_, unsafe_, name, generic_params, args, return_type, - body, - } = self; + } = &self.sig.kind; if pub_.is_some() { write!(f, "pub ")?; @@ -755,7 +774,7 @@ impl fmt::Display for Function { write!(f, " -> {}", return_type.kind)?; } write!(f, " {{")?; - write_nodes_line_wrapped(&mut indented(f), body)?; + write_nodes_line_wrapped(&mut indented(f), &self.body)?; write!(f, "}}") } } diff --git a/crates/parser/src/grammar/functions.rs b/crates/parser/src/grammar/functions.rs index e0c872f270..4f55e5fc3d 100644 --- a/crates/parser/src/grammar/functions.rs +++ b/crates/parser/src/grammar/functions.rs @@ -2,15 +2,18 @@ use super::expressions::{parse_call_args, parse_expr}; use super::types::parse_type_desc; use crate::ast::{ - BinOperator, Expr, FuncStmt, Function, FunctionArg, GenericParameter, RegularFunctionArg, - TypeDesc, VarDeclTarget, + BinOperator, Expr, FuncStmt, Function, FunctionArg, FunctionSignature, GenericParameter, + RegularFunctionArg, TypeDesc, VarDeclTarget, }; use crate::node::{Node, Span}; use crate::{Label, ParseFailed, ParseResult, Parser, TokenKind}; -/// Parse a function definition. The optional `pub` qualifier must be parsed by +/// Parse a function definition without a body. The optional `pub` qualifier must be parsed by /// the caller, and passed in. Next token must be `unsafe` or `fn`. -pub fn parse_fn_def(par: &mut Parser, mut pub_qual: Option) -> ParseResult> { +pub fn parse_fn_sig( + par: &mut Parser, + mut pub_qual: Option, +) -> ParseResult> { let unsafe_qual = par.optional(TokenKind::Unsafe).map(|tok| tok.span); if let Some(pub_) = par.optional(TokenKind::Pub) { let unsafe_span = @@ -84,22 +87,35 @@ pub fn parse_fn_def(par: &mut Parser, mut pub_qual: Option) -> ParseResult None }; - // TODO: allow multi-line return type? `fn f()\n ->\n u8` - par.enter_block(span, "function definition")?; - let body = parse_block_stmts(par)?; - let rbrace = par.expect(TokenKind::BraceClose, "missing `}` in fn definition")?; - Ok(Node::new( - Function { + FunctionSignature { pub_: pub_qual, unsafe_: unsafe_qual, name: name.into(), args, generic_params, return_type, + }, + span, + )) +} + +/// Parse a function definition. The optional `pub` qualifier must be parsed by +/// the caller, and passed in. Next token must be `unsafe` or `fn`. +pub fn parse_fn_def(par: &mut Parser, pub_qual: Option) -> ParseResult> { + let sig = parse_fn_sig(par, pub_qual)?; + + // TODO: allow multi-line return type? `fn f()\n ->\n u8` + par.enter_block(sig.span, "function definition")?; + let body = parse_block_stmts(par)?; + let rbrace = par.expect(TokenKind::BraceClose, "missing `}` in fn definition")?; + + Ok(Node::new( + Function { + sig: sig.clone(), body, }, - span + rbrace.span, + sig.span + rbrace.span, )) } diff --git a/crates/parser/src/grammar/types.rs b/crates/parser/src/grammar/types.rs index afcf98e3fe..c3c44c60cb 100644 --- a/crates/parser/src/grammar/types.rs +++ b/crates/parser/src/grammar/types.rs @@ -1,6 +1,6 @@ use crate::ast::{self, EventField, Field, GenericArg, Impl, Path, Trait, TypeAlias, TypeDesc}; use crate::grammar::expressions::parse_expr; -use crate::grammar::functions::parse_fn_def; +use crate::grammar::functions::{parse_fn_def, parse_fn_sig}; use crate::node::{Node, Span}; use crate::Token; use crate::{ParseFailed, ParseResult, Parser, TokenKind}; @@ -78,12 +78,20 @@ pub fn parse_trait_def(par: &mut Parser, trait_pub_qual: Option) -> ParseR )?; let header_span = trait_tok.span + trait_name.span; + let mut functions = vec![]; par.enter_block(header_span, "trait definition")?; loop { match par.peek_or_err()? { TokenKind::Fn => { - // TODO: parse trait functions + // TODO: Traits should also be allowed to have functions that do contain a body. + functions.push(parse_fn_sig(par, None)?); + par.expect_with_notes( + TokenKind::Semi, + "failed to parse trait definition", + |_| vec!["Note: trait functions must appear without body and followed by a semicolon.".into()], + )?; + par.eat_newlines(); } TokenKind::BraceClose => { par.next()?; @@ -101,6 +109,7 @@ pub fn parse_trait_def(par: &mut Parser, trait_pub_qual: Option) -> ParseR Ok(Node::new( Trait { name: Node::new(trait_name.text.into(), trait_name.span), + functions, pub_qual: trait_pub_qual, }, span, @@ -128,15 +137,17 @@ pub fn parse_impl_def(par: &mut Parser) -> ParseResult> { })?; let receiver = parse_type_desc(par)?; + let mut functions = vec![]; let header_span = impl_tok.span + trait_name.span + for_tok.span + receiver.span; par.enter_block(header_span, "impl definition")?; loop { + par.eat_newlines(); match par.peek_or_err()? { TokenKind::Fn => { - // TODO: parse impl functions + functions.push(parse_fn_def(par, None)?); } TokenKind::BraceClose => { par.next()?; @@ -154,6 +165,7 @@ pub fn parse_impl_def(par: &mut Parser) -> ParseResult> { Impl { impl_trait: Node::new(trait_name.text.into(), trait_name.span), receiver, + functions, }, header_span, )) diff --git a/crates/parser/src/lexer/token.rs b/crates/parser/src/lexer/token.rs index 561afda918..92b380f251 100644 --- a/crates/parser/src/lexer/token.rs +++ b/crates/parser/src/lexer/token.rs @@ -261,7 +261,7 @@ impl TokenKind { Colon => "symbol `:`", ColonColon => "symbol `::`", Comma => "symbol `,`", - Semi => "symbol ``", + Semi => "symbol `;`", Plus => "symbol `+`", Minus => "symbol `-`", Star => "symbol `*`", diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__contract_def.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__contract_def.snap index 9155669950..3b28b98e5b 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__contract_def.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__contract_def.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(contract_def), try_parse_module,\n r#\"contract Foo {\n x: address\n pub y: u8\n pub const z: Map\n\n pub fn foo() -> u8 {\n return 10\n }\n event Bar {\n idx from: address\n }\n}\n\"#)" +expression: "ast_string(stringify!(contract_def), try_parse_module,\n r#\"contract Foo {\n x: address\n pub y: u8\n pub const z: Map\n\n pub fn foo() -> u8 {\n return 10\n }\n event Bar {\n idx from: address\n }\n}\n\"#)" --- Node( @@ -133,35 +133,43 @@ Node( body: [ Function(Node( kind: Function( - pub_: Some(Span( - start: 75, - end: 78, - )), - unsafe_: None, - name: Node( - kind: "foo", - span: Span( - start: 82, - end: 85, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 82, - end: 85, - ), - ), - args: [], - return_type: Some(Node( - kind: Base( - base: "u8", + sig: Node( + kind: FunctionSignature( + pub_: Some(Span( + start: 75, + end: 78, + )), + unsafe_: None, + name: Node( + kind: "foo", + span: Span( + start: 82, + end: 85, + ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 82, + end: 85, + ), + ), + args: [], + return_type: Some(Node( + kind: Base( + base: "u8", + ), + span: Span( + start: 91, + end: 93, + ), + )), ), span: Span( - start: 91, + start: 75, end: 93, ), - )), + ), body: [ Node( kind: Return( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def.snap index 9805e19812..d4d286490b 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(fn_def), try_parse_module,\n \"fn transfer(from sender: address, to recip: address, _ val: u64) -> bool {\\n false \\n}\")" +expression: "ast_string(stringify!(fn_def), try_parse_module,\n \"fn transfer(from sender: address, to recip: address, _ val: u64) -> bool {\\n false \\n}\")" --- Node( @@ -8,126 +8,134 @@ Node( body: [ Function(Node( kind: Function( - pub_: None, - unsafe_: None, - name: Node( - kind: "transfer", - span: Span( - start: 3, - end: 11, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 3, - end: 11, - ), - ), - args: [ - Node( - kind: Regular(RegularFunctionArg( - label: Some(Node( - kind: "from", - span: Span( - start: 12, - end: 16, - ), - )), - name: Node( - kind: "sender", - span: Span( - start: 17, - end: 23, - ), + sig: Node( + kind: FunctionSignature( + pub_: None, + unsafe_: None, + name: Node( + kind: "transfer", + span: Span( + start: 3, + end: 11, ), - typ: Node( - kind: Base( - base: "address", - ), - span: Span( - start: 25, - end: 32, - ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 3, + end: 11, ), - )), - span: Span( - start: 17, - end: 32, ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: Some(Node( - kind: "to", - span: Span( - start: 34, - end: 36, - ), - )), - name: Node( - kind: "recip", + args: [ + Node( + kind: Regular(RegularFunctionArg( + label: Some(Node( + kind: "from", + span: Span( + start: 12, + end: 16, + ), + )), + name: Node( + kind: "sender", + span: Span( + start: 17, + end: 23, + ), + ), + typ: Node( + kind: Base( + base: "address", + ), + span: Span( + start: 25, + end: 32, + ), + ), + )), span: Span( - start: 37, - end: 42, + start: 17, + end: 32, ), ), - typ: Node( - kind: Base( - base: "address", - ), + Node( + kind: Regular(RegularFunctionArg( + label: Some(Node( + kind: "to", + span: Span( + start: 34, + end: 36, + ), + )), + name: Node( + kind: "recip", + span: Span( + start: 37, + end: 42, + ), + ), + typ: Node( + kind: Base( + base: "address", + ), + span: Span( + start: 44, + end: 51, + ), + ), + )), span: Span( - start: 44, + start: 37, end: 51, ), ), - )), - span: Span( - start: 37, - end: 51, - ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: Some(Node( - kind: "_", - span: Span( - start: 53, - end: 54, - ), - )), - name: Node( - kind: "val", + Node( + kind: Regular(RegularFunctionArg( + label: Some(Node( + kind: "_", + span: Span( + start: 53, + end: 54, + ), + )), + name: Node( + kind: "val", + span: Span( + start: 55, + end: 58, + ), + ), + typ: Node( + kind: Base( + base: "u64", + ), + span: Span( + start: 60, + end: 63, + ), + ), + )), span: Span( start: 55, - end: 58, - ), - ), - typ: Node( - kind: Base( - base: "u64", - ), - span: Span( - start: 60, end: 63, ), ), + ], + return_type: Some(Node( + kind: Base( + base: "bool", + ), + span: Span( + start: 68, + end: 72, + ), )), - span: Span( - start: 55, - end: 63, - ), - ), - ], - return_type: Some(Node( - kind: Base( - base: "bool", ), span: Span( - start: 68, + start: 0, end: 72, ), - )), + ), body: [ Node( kind: Expr( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_generic.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_generic.snap index 43a5edaf1f..3fd4275830 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_generic.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_generic.snap @@ -8,140 +8,148 @@ Node( body: [ Function(Node( kind: Function( - pub_: None, - unsafe_: None, - name: Node( - kind: "foo", - span: Span( - start: 3, - end: 6, - ), - ), - generic_params: Node( - kind: [ - Unbounded(Node( - kind: "T", + sig: Node( + kind: FunctionSignature( + pub_: None, + unsafe_: None, + name: Node( + kind: "foo", span: Span( - start: 7, - end: 8, - ), - )), - Bounded( - name: Node( - kind: "R", - span: Span( - start: 10, - end: 11, - ), + start: 3, + end: 6, ), - bound: Node( - kind: Base( - base: "Event", - ), - span: Span( - start: 13, - end: 18, + ), + generic_params: Node( + kind: [ + Unbounded(Node( + kind: "T", + span: Span( + start: 7, + end: 8, + ), + )), + Bounded( + name: Node( + kind: "R", + span: Span( + start: 10, + end: 11, + ), + ), + bound: Node( + kind: Base( + base: "Event", + ), + span: Span( + start: 13, + end: 18, + ), + ), ), + ], + span: Span( + start: 6, + end: 19, ), ), - ], - span: Span( - start: 6, - end: 19, - ), - ), - args: [ - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "this", + args: [ + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "this", + span: Span( + start: 20, + end: 24, + ), + ), + typ: Node( + kind: Base( + base: "T", + ), + span: Span( + start: 26, + end: 27, + ), + ), + )), span: Span( start: 20, - end: 24, - ), - ), - typ: Node( - kind: Base( - base: "T", - ), - span: Span( - start: 26, end: 27, ), ), - )), - span: Span( - start: 20, - end: 27, - ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "that", + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "that", + span: Span( + start: 29, + end: 33, + ), + ), + typ: Node( + kind: Base( + base: "R", + ), + span: Span( + start: 35, + end: 36, + ), + ), + )), span: Span( start: 29, - end: 33, - ), - ), - typ: Node( - kind: Base( - base: "R", - ), - span: Span( - start: 35, end: 36, ), ), - )), - span: Span( - start: 29, - end: 36, - ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: Some(Node( - kind: "_", - span: Span( - start: 38, - end: 39, - ), - )), - name: Node( - kind: "val", + Node( + kind: Regular(RegularFunctionArg( + label: Some(Node( + kind: "_", + span: Span( + start: 38, + end: 39, + ), + )), + name: Node( + kind: "val", + span: Span( + start: 40, + end: 43, + ), + ), + typ: Node( + kind: Base( + base: "u64", + ), + span: Span( + start: 45, + end: 48, + ), + ), + )), span: Span( start: 40, - end: 43, - ), - ), - typ: Node( - kind: Base( - base: "u64", - ), - span: Span( - start: 45, end: 48, ), ), + ], + return_type: Some(Node( + kind: Base( + base: "bool", + ), + span: Span( + start: 53, + end: 57, + ), )), - span: Span( - start: 40, - end: 48, - ), - ), - ], - return_type: Some(Node( - kind: Base( - base: "bool", ), span: Span( - start: 53, + start: 0, end: 57, ), - )), + ), body: [ Node( kind: Expr( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_pub.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_pub.snap index ce4cf5375b..2715639f1a 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_pub.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_pub.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(fn_def_pub), try_parse_module,\n \"pub fn foo21(x: bool, y: address,) -> bool { x }\")" +expression: "ast_string(stringify!(fn_def_pub), try_parse_module,\n \"pub fn foo21(x: bool, y: address,) -> bool { x }\")" --- Node( @@ -8,86 +8,94 @@ Node( body: [ Function(Node( kind: Function( - pub_: Some(Span( - start: 0, - end: 3, - )), - unsafe_: None, - name: Node( - kind: "foo21", - span: Span( - start: 7, - end: 12, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 7, - end: 12, - ), - ), - args: [ - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "x", - span: Span( - start: 13, - end: 14, - ), + sig: Node( + kind: FunctionSignature( + pub_: Some(Span( + start: 0, + end: 3, + )), + unsafe_: None, + name: Node( + kind: "foo21", + span: Span( + start: 7, + end: 12, ), - typ: Node( - kind: Base( - base: "bool", - ), - span: Span( - start: 16, - end: 20, - ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 7, + end: 12, ), - )), - span: Span( - start: 13, - end: 20, ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "y", + args: [ + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "x", + span: Span( + start: 13, + end: 14, + ), + ), + typ: Node( + kind: Base( + base: "bool", + ), + span: Span( + start: 16, + end: 20, + ), + ), + )), span: Span( - start: 22, - end: 23, + start: 13, + end: 20, ), ), - typ: Node( - kind: Base( - base: "address", - ), + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "y", + span: Span( + start: 22, + end: 23, + ), + ), + typ: Node( + kind: Base( + base: "address", + ), + span: Span( + start: 25, + end: 32, + ), + ), + )), span: Span( - start: 25, + start: 22, end: 32, ), ), + ], + return_type: Some(Node( + kind: Base( + base: "bool", + ), + span: Span( + start: 38, + end: 42, + ), )), - span: Span( - start: 22, - end: 32, - ), - ), - ], - return_type: Some(Node( - kind: Base( - base: "bool", ), span: Span( - start: 38, + start: 0, end: 42, ), - )), + ), body: [ Node( kind: Expr( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_pub_unsafe.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_pub_unsafe.snap index 40008a51ad..3116b53ba5 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_pub_unsafe.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_pub_unsafe.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(fn_def_pub_unsafe), try_parse_module,\n \"pub unsafe fn foo21(x: bool, y: address,) -> bool{x}\")" +expression: "ast_string(stringify!(fn_def_pub_unsafe), try_parse_module,\n \"pub unsafe fn foo21(x: bool, y: address,) -> bool{x}\")" --- Node( @@ -8,89 +8,97 @@ Node( body: [ Function(Node( kind: Function( - pub_: Some(Span( - start: 0, - end: 3, - )), - unsafe_: Some(Span( - start: 4, - end: 10, - )), - name: Node( - kind: "foo21", - span: Span( - start: 14, - end: 19, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 14, - end: 19, - ), - ), - args: [ - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "x", - span: Span( - start: 20, - end: 21, - ), + sig: Node( + kind: FunctionSignature( + pub_: Some(Span( + start: 0, + end: 3, + )), + unsafe_: Some(Span( + start: 4, + end: 10, + )), + name: Node( + kind: "foo21", + span: Span( + start: 14, + end: 19, ), - typ: Node( - kind: Base( - base: "bool", - ), - span: Span( - start: 23, - end: 27, - ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 14, + end: 19, ), - )), - span: Span( - start: 20, - end: 27, ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "y", + args: [ + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "x", + span: Span( + start: 20, + end: 21, + ), + ), + typ: Node( + kind: Base( + base: "bool", + ), + span: Span( + start: 23, + end: 27, + ), + ), + )), span: Span( - start: 29, - end: 30, + start: 20, + end: 27, ), ), - typ: Node( - kind: Base( - base: "address", - ), + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "y", + span: Span( + start: 29, + end: 30, + ), + ), + typ: Node( + kind: Base( + base: "address", + ), + span: Span( + start: 32, + end: 39, + ), + ), + )), span: Span( - start: 32, + start: 29, end: 39, ), ), + ], + return_type: Some(Node( + kind: Base( + base: "bool", + ), + span: Span( + start: 45, + end: 49, + ), )), - span: Span( - start: 29, - end: 39, - ), - ), - ], - return_type: Some(Node( - kind: Base( - base: "bool", ), span: Span( - start: 45, + start: 0, end: 49, ), - )), + ), body: [ Node( kind: Expr( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_unsafe.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_unsafe.snap index c381419f85..c76075ff42 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_unsafe.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__fn_def_unsafe.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(fn_def_unsafe), try_parse_module,\n \"unsafe fn foo21(x: bool, y: address,) -> bool {\\n x\\n}\")" +expression: "ast_string(stringify!(fn_def_unsafe), try_parse_module,\n \"unsafe fn foo21(x: bool, y: address,) -> bool {\\n x\\n}\")" --- Node( @@ -8,86 +8,94 @@ Node( body: [ Function(Node( kind: Function( - pub_: None, - unsafe_: Some(Span( - start: 0, - end: 6, - )), - name: Node( - kind: "foo21", - span: Span( - start: 10, - end: 15, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 10, - end: 15, - ), - ), - args: [ - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "x", - span: Span( - start: 16, - end: 17, - ), + sig: Node( + kind: FunctionSignature( + pub_: None, + unsafe_: Some(Span( + start: 0, + end: 6, + )), + name: Node( + kind: "foo21", + span: Span( + start: 10, + end: 15, ), - typ: Node( - kind: Base( - base: "bool", - ), - span: Span( - start: 19, - end: 23, - ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 10, + end: 15, ), - )), - span: Span( - start: 16, - end: 23, ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "y", + args: [ + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "x", + span: Span( + start: 16, + end: 17, + ), + ), + typ: Node( + kind: Base( + base: "bool", + ), + span: Span( + start: 19, + end: 23, + ), + ), + )), span: Span( - start: 25, - end: 26, + start: 16, + end: 23, ), ), - typ: Node( - kind: Base( - base: "address", - ), + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "y", + span: Span( + start: 25, + end: 26, + ), + ), + typ: Node( + kind: Base( + base: "address", + ), + span: Span( + start: 28, + end: 35, + ), + ), + )), span: Span( - start: 28, + start: 25, end: 35, ), ), + ], + return_type: Some(Node( + kind: Base( + base: "bool", + ), + span: Span( + start: 41, + end: 45, + ), )), - span: Span( - start: 25, - end: 35, - ), - ), - ], - return_type: Some(Node( - kind: Base( - base: "bool", ), span: Span( - start: 41, + start: 0, end: 45, ), - )), + ), body: [ Node( kind: Expr( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__guest_book.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__guest_book.snap index 268c529205..d145fbed9b 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__guest_book.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__guest_book.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(guest_book), try_parse_module,\n r#\"\ntype BookMsg = Array\n\ncontract GuestBook {\n pub guest_book: Map\n\n event Signed {\n idx book_msg: BookMsg\n }\n\n pub fn sign(self, book_msg: BookMsg) {\n self.guest_book[msg.sender] = book_msg\n\n emit Signed(book_msg: book_msg)\n }\n pub fn get_msg(self, addr: address) -> BookMsg {\n return self.guest_book[addr]\n }\n}\"#)" +expression: "ast_string(stringify!(guest_book), try_parse_module,\n r#\"\ntype BookMsg = Array\n\ncontract GuestBook {\n pub guest_book: Map\n\n event Signed {\n idx book_msg: BookMsg\n }\n\n pub fn sign(self, book_msg: BookMsg) {\n self.guest_book[msg.sender] = book_msg\n\n emit Signed(book_msg: book_msg)\n }\n pub fn get_msg(self, addr: address) -> BookMsg {\n return self.guest_book[addr]\n }\n}\"#)" --- Node( @@ -177,60 +177,68 @@ Node( )), Function(Node( kind: Function( - pub_: Some(Span( - start: 159, - end: 162, - )), - unsafe_: None, - name: Node( - kind: "sign", - span: Span( - start: 166, - end: 170, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 166, - end: 170, - ), - ), - args: [ - Node( - kind: Self_, - span: Span( - start: 171, - end: 175, + sig: Node( + kind: FunctionSignature( + pub_: Some(Span( + start: 159, + end: 162, + )), + unsafe_: None, + name: Node( + kind: "sign", + span: Span( + start: 166, + end: 170, + ), ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "book_msg", + generic_params: Node( + kind: [], + span: Span( + start: 166, + end: 170, + ), + ), + args: [ + Node( + kind: Self_, span: Span( - start: 177, - end: 185, + start: 171, + end: 175, ), ), - typ: Node( - kind: Base( - base: "BookMsg", - ), + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "book_msg", + span: Span( + start: 177, + end: 185, + ), + ), + typ: Node( + kind: Base( + base: "BookMsg", + ), + span: Span( + start: 187, + end: 194, + ), + ), + )), span: Span( - start: 187, + start: 177, end: 194, ), ), - )), - span: Span( - start: 177, - end: 194, - ), + ], + return_type: None, ), - ], - return_type: None, + span: Span( + start: 159, + end: 195, + ), + ), body: [ Node( kind: Assign( @@ -353,68 +361,76 @@ Node( )), Function(Node( kind: Function( - pub_: Some(Span( - start: 296, - end: 299, - )), - unsafe_: None, - name: Node( - kind: "get_msg", - span: Span( - start: 303, - end: 310, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 303, - end: 310, - ), - ), - args: [ - Node( - kind: Self_, - span: Span( - start: 311, - end: 315, + sig: Node( + kind: FunctionSignature( + pub_: Some(Span( + start: 296, + end: 299, + )), + unsafe_: None, + name: Node( + kind: "get_msg", + span: Span( + start: 303, + end: 310, + ), ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "addr", + generic_params: Node( + kind: [], + span: Span( + start: 303, + end: 310, + ), + ), + args: [ + Node( + kind: Self_, span: Span( - start: 317, - end: 321, + start: 311, + end: 315, ), ), - typ: Node( - kind: Base( - base: "address", - ), + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "addr", + span: Span( + start: 317, + end: 321, + ), + ), + typ: Node( + kind: Base( + base: "address", + ), + span: Span( + start: 323, + end: 330, + ), + ), + )), span: Span( - start: 323, + start: 317, end: 330, ), ), + ], + return_type: Some(Node( + kind: Base( + base: "BookMsg", + ), + span: Span( + start: 335, + end: 342, + ), )), - span: Span( - start: 317, - end: 330, - ), - ), - ], - return_type: Some(Node( - kind: Base( - base: "BookMsg", ), span: Span( - start: 335, + start: 296, end: 342, ), - )), + ), body: [ Node( kind: Return( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__module_level_events.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__module_level_events.snap index 90e00d2467..9f0a3c4b3c 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__module_level_events.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__module_level_events.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(module_level_events), try_parse_module,\n r#\"\nuse std::context::Context\n\nevent Transfer {\n idx sender: address\n idx receiver: address\n value: u256\n}\ncontract Foo {\n fn transfer(ctx: Context, to: address, value: u256) {\n emit Transfer(ctx, sender: msg.sender, receiver: to, value)\n }\n}\n\"#)" +expression: "ast_string(stringify!(module_level_events), try_parse_module,\n r#\"\nuse std::context::Context\n\nevent Transfer {\n idx sender: address\n idx receiver: address\n value: u256\n}\ncontract Foo {\n fn transfer(ctx: Context, to: address, value: u256) {\n emit Transfer(ctx, sender: msg.sender, receiver: to, value)\n }\n}\n\"#)" --- Node( @@ -154,100 +154,108 @@ Node( body: [ Function(Node( kind: Function( - pub_: None, - unsafe_: None, - name: Node( - kind: "transfer", - span: Span( - start: 135, - end: 143, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 135, - end: 143, - ), - ), - args: [ - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "ctx", - span: Span( - start: 144, - end: 147, - ), + sig: Node( + kind: FunctionSignature( + pub_: None, + unsafe_: None, + name: Node( + kind: "transfer", + span: Span( + start: 135, + end: 143, ), - typ: Node( - kind: Base( - base: "Context", - ), - span: Span( - start: 149, - end: 156, - ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 135, + end: 143, ), - )), - span: Span( - start: 144, - end: 156, ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "to", + args: [ + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "ctx", + span: Span( + start: 144, + end: 147, + ), + ), + typ: Node( + kind: Base( + base: "Context", + ), + span: Span( + start: 149, + end: 156, + ), + ), + )), span: Span( - start: 158, - end: 160, + start: 144, + end: 156, ), ), - typ: Node( - kind: Base( - base: "address", - ), + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "to", + span: Span( + start: 158, + end: 160, + ), + ), + typ: Node( + kind: Base( + base: "address", + ), + span: Span( + start: 162, + end: 169, + ), + ), + )), span: Span( - start: 162, + start: 158, end: 169, ), ), - )), - span: Span( - start: 158, - end: 169, - ), - ), - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "value", + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "value", + span: Span( + start: 171, + end: 176, + ), + ), + typ: Node( + kind: Base( + base: "u256", + ), + span: Span( + start: 178, + end: 182, + ), + ), + )), span: Span( start: 171, - end: 176, - ), - ), - typ: Node( - kind: Base( - base: "u256", - ), - span: Span( - start: 178, end: 182, ), ), - )), - span: Span( - start: 171, - end: 182, - ), + ], + return_type: None, ), - ], - return_type: None, + span: Span( + start: 132, + end: 183, + ), + ), body: [ Node( kind: Emit( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__module_stmts.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__module_stmts.snap index 95c9accc16..579339eaa3 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__module_stmts.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__module_stmts.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(module_stmts), try_parse_module,\n r#\"\npragma 0.5.0\n\nuse foo::bar::{\n bing as bong,\n food::*\n}\n\ntype X = Map\n\npub fn double(x: u8) -> u8 {\n return x * 2\n}\n\nfn secret() -> u8 { return 0xBEEF }\n\ncontract A {\n pub const x: u256 = 10\n}\n\ncontract B {\n pub x: X\n}\"#)" +expression: "ast_string(stringify!(module_stmts), try_parse_module,\n r#\"\npragma 0.5.0\n\nuse foo::bar::{\n bing as bong,\n food::*\n}\n\ntype X = Map\n\npub fn double(x: u8) -> u8 {\n return x * 2\n}\n\nfn secret() -> u8 { return 0xBEEF }\n\ncontract A {\n pub const x: u256 = 10\n}\n\ncontract B {\n pub x: X\n}\"#)" --- Node( @@ -161,61 +161,69 @@ Node( )), Function(Node( kind: Function( - pub_: Some(Span( - start: 87, - end: 90, - )), - unsafe_: None, - name: Node( - kind: "double", - span: Span( - start: 94, - end: 100, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 94, - end: 100, - ), - ), - args: [ - Node( - kind: Regular(RegularFunctionArg( - label: None, - name: Node( - kind: "x", - span: Span( - start: 101, - end: 102, - ), + sig: Node( + kind: FunctionSignature( + pub_: Some(Span( + start: 87, + end: 90, + )), + unsafe_: None, + name: Node( + kind: "double", + span: Span( + start: 94, + end: 100, ), - typ: Node( - kind: Base( - base: "u8", - ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 94, + end: 100, + ), + ), + args: [ + Node( + kind: Regular(RegularFunctionArg( + label: None, + name: Node( + kind: "x", + span: Span( + start: 101, + end: 102, + ), + ), + typ: Node( + kind: Base( + base: "u8", + ), + span: Span( + start: 104, + end: 106, + ), + ), + )), span: Span( - start: 104, + start: 101, end: 106, ), ), + ], + return_type: Some(Node( + kind: Base( + base: "u8", + ), + span: Span( + start: 111, + end: 113, + ), )), - span: Span( - start: 101, - end: 106, - ), - ), - ], - return_type: Some(Node( - kind: Base( - base: "u8", ), span: Span( - start: 111, + start: 87, end: 113, ), - )), + ), body: [ Node( kind: Return( @@ -263,32 +271,40 @@ Node( )), Function(Node( kind: Function( - pub_: None, - unsafe_: None, - name: Node( - kind: "secret", - span: Span( - start: 139, - end: 145, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 139, - end: 145, - ), - ), - args: [], - return_type: Some(Node( - kind: Base( - base: "u8", + sig: Node( + kind: FunctionSignature( + pub_: None, + unsafe_: None, + name: Node( + kind: "secret", + span: Span( + start: 139, + end: 145, + ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 139, + end: 145, + ), + ), + args: [], + return_type: Some(Node( + kind: Base( + base: "u8", + ), + span: Span( + start: 151, + end: 153, + ), + )), ), span: Span( - start: 151, + start: 136, end: 153, ), - )), + ), body: [ Node( kind: Return( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__pub_contract_def.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__pub_contract_def.snap index 21cc0bead1..ae34137516 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__pub_contract_def.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__pub_contract_def.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(pub_contract_def), try_parse_module,\n r#\"\npub contract Foo {\n pub fn foo() -> u8 {\n return 10\n }\n}\"#)" +expression: "ast_string(stringify!(pub_contract_def), try_parse_module,\n r#\"\npub contract Foo {\n pub fn foo() -> u8 {\n return 10\n }\n}\"#)" --- Node( @@ -19,35 +19,43 @@ Node( body: [ Function(Node( kind: Function( - pub_: Some(Span( - start: 24, - end: 27, - )), - unsafe_: None, - name: Node( - kind: "foo", - span: Span( - start: 31, - end: 34, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 31, - end: 34, - ), - ), - args: [], - return_type: Some(Node( - kind: Base( - base: "u8", + sig: Node( + kind: FunctionSignature( + pub_: Some(Span( + start: 24, + end: 27, + )), + unsafe_: None, + name: Node( + kind: "foo", + span: Span( + start: 31, + end: 34, + ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 31, + end: 34, + ), + ), + args: [], + return_type: Some(Node( + kind: Base( + base: "u8", + ), + span: Span( + start: 40, + end: 42, + ), + )), ), span: Span( - start: 40, + start: 24, end: 42, ), - )), + ), body: [ Node( kind: Return( diff --git a/crates/parser/tests/cases/snapshots/cases__parse_ast__struct_def.snap b/crates/parser/tests/cases/snapshots/cases__parse_ast__struct_def.snap index c19c7f19ee..9e8461d9cc 100644 --- a/crates/parser/tests/cases/snapshots/cases__parse_ast__struct_def.snap +++ b/crates/parser/tests/cases/snapshots/cases__parse_ast__struct_def.snap @@ -1,6 +1,6 @@ --- source: crates/parser/tests/cases/parse_ast.rs -expression: "ast_string(stringify!(struct_def), try_parse_module,\n r#\"struct S {\n x: address\n pub y: u8\n z: u8\n pub a: Map\n\n pub fn foo(self) -> u8 {\n return self.z + self.y\n }\n unsafe fn bar() {}\n}\"#)" +expression: "ast_string(stringify!(struct_def), try_parse_module,\n r#\"struct S {\n x: address\n pub y: u8\n z: u8\n pub a: Map\n\n pub fn foo(self) -> u8 {\n return self.z + self.y\n }\n unsafe fn bar() {}\n}\"#)" --- Node( @@ -160,43 +160,51 @@ Node( functions: [ Node( kind: Function( - pub_: Some(Span( - start: 69, - end: 72, - )), - unsafe_: None, - name: Node( - kind: "foo", - span: Span( - start: 76, - end: 79, - ), - ), - generic_params: Node( - kind: [], - span: Span( - start: 76, - end: 79, - ), - ), - args: [ - Node( - kind: Self_, - span: Span( - start: 80, - end: 84, + sig: Node( + kind: FunctionSignature( + pub_: Some(Span( + start: 69, + end: 72, + )), + unsafe_: None, + name: Node( + kind: "foo", + span: Span( + start: 76, + end: 79, + ), ), - ), - ], - return_type: Some(Node( - kind: Base( - base: "u8", + generic_params: Node( + kind: [], + span: Span( + start: 76, + end: 79, + ), + ), + args: [ + Node( + kind: Self_, + span: Span( + start: 80, + end: 84, + ), + ), + ], + return_type: Some(Node( + kind: Base( + base: "u8", + ), + span: Span( + start: 89, + end: 91, + ), + )), ), span: Span( - start: 89, + start: 69, end: 91, ), - )), + ), body: [ Node( kind: Return( @@ -274,27 +282,35 @@ Node( ), Node( kind: Function( - pub_: None, - unsafe_: Some(Span( - start: 127, - end: 133, - )), - name: Node( - kind: "bar", - span: Span( - start: 137, - end: 140, + sig: Node( + kind: FunctionSignature( + pub_: None, + unsafe_: Some(Span( + start: 127, + end: 133, + )), + name: Node( + kind: "bar", + span: Span( + start: 137, + end: 140, + ), + ), + generic_params: Node( + kind: [], + span: Span( + start: 137, + end: 140, + ), + ), + args: [], + return_type: None, ), - ), - generic_params: Node( - kind: [], span: Span( - start: 137, - end: 140, + start: 127, + end: 142, ), ), - args: [], - return_type: None, body: [], ), span: Span( diff --git a/crates/test-files/fixtures/compile_errors/bad_visibility/src/foo.fe b/crates/test-files/fixtures/compile_errors/bad_visibility/src/foo.fe index 08fc81c6e4..2be73b7d16 100644 --- a/crates/test-files/fixtures/compile_errors/bad_visibility/src/foo.fe +++ b/crates/test-files/fixtures/compile_errors/bad_visibility/src/foo.fe @@ -2,6 +2,8 @@ type MyInt = i32 const MY_CONST: MyInt = 1 +trait MyTrait {} + event MyEvent { x: i32 } diff --git a/crates/test-files/fixtures/compile_errors/bad_visibility/src/main.fe b/crates/test-files/fixtures/compile_errors/bad_visibility/src/main.fe index ab55b559c8..82ecfb93ce 100644 --- a/crates/test-files/fixtures/compile_errors/bad_visibility/src/main.fe +++ b/crates/test-files/fixtures/compile_errors/bad_visibility/src/main.fe @@ -1,6 +1,9 @@ -use foo::{MyInt, MY_CONST, MyEvent, MyStruct, my_func, MyContract} +use foo::{MyInt, MY_CONST, MyEvent, MyStruct, MyTrait, my_func, MyContract} use std::context::Context +struct SomeThing {} +impl MyTrait for SomeThing {} + contract Main { pub fn priv_type_alias() -> MyInt { let x: MyInt = 1 diff --git a/crates/test-files/fixtures/compile_errors/call_generic_function_with_unsatisfied_bound.fe b/crates/test-files/fixtures/compile_errors/call_generic_function_with_unsatisfied_bound.fe index 5cca0e114e..a3b65ddab0 100644 --- a/crates/test-files/fixtures/compile_errors/call_generic_function_with_unsatisfied_bound.fe +++ b/crates/test-files/fixtures/compile_errors/call_generic_function_with_unsatisfied_bound.fe @@ -1,8 +1,10 @@ -use std::traits::IsNumeric +trait Dummy {} + +struct Bar {} struct Foo { - pub fn bar(self, _ x: T) -> bool { + pub fn bar(self, _ x: T) -> bool { return true } } @@ -11,6 +13,6 @@ contract Meh { pub fn call_bar(self) { let foo: Foo = Foo(); - foo.bar(true) + foo.bar(Bar()) } } diff --git a/crates/test-files/fixtures/compile_errors/contract_function_with_generic_params.fe b/crates/test-files/fixtures/compile_errors/contract_function_with_generic_params.fe index 758cc31b64..8339a96c37 100644 --- a/crates/test-files/fixtures/compile_errors/contract_function_with_generic_params.fe +++ b/crates/test-files/fixtures/compile_errors/contract_function_with_generic_params.fe @@ -1,5 +1,5 @@ -use std::traits::IsNumeric +trait Dummy {} contract Foo { - pub fn bar(val: T) {} + pub fn bar(val: T) {} } diff --git a/crates/test-files/fixtures/compile_errors/duplicate_generic_params.fe b/crates/test-files/fixtures/compile_errors/duplicate_generic_params.fe new file mode 100644 index 0000000000..f5e4f741d9 --- /dev/null +++ b/crates/test-files/fixtures/compile_errors/duplicate_generic_params.fe @@ -0,0 +1,13 @@ +trait Computable { + fn compute(self, val: u256) -> u256; +} + +trait Computable2 { + fn compute2(self, val: u256) -> u256; +} + +struct Yay { + fn foo(val1: T, val2: T) { + return + } +} diff --git a/crates/test-files/fixtures/compile_errors/invalid_impl_location.fe b/crates/test-files/fixtures/compile_errors/invalid_impl_location.fe index 648e4b2ea9..e25354073a 100644 --- a/crates/test-files/fixtures/compile_errors/invalid_impl_location.fe +++ b/crates/test-files/fixtures/compile_errors/invalid_impl_location.fe @@ -1,3 +1,4 @@ -use std::traits::IsNumeric +use std::traits::Dummy +use std::context::Context -impl IsNumeric for u8 {} +impl Dummy for Context {} diff --git a/crates/test-files/fixtures/compile_errors/trait_conflicting_impls.fe b/crates/test-files/fixtures/compile_errors/trait_conflicting_impls.fe new file mode 100644 index 0000000000..08f7f2dbdc --- /dev/null +++ b/crates/test-files/fixtures/compile_errors/trait_conflicting_impls.fe @@ -0,0 +1,7 @@ +trait Foo {} + +struct Bar { +} + +impl Foo for Bar {} +impl Foo for Bar {} \ No newline at end of file diff --git a/crates/test-files/fixtures/compile_errors/trait_fn_with_generic_params.fe b/crates/test-files/fixtures/compile_errors/trait_fn_with_generic_params.fe new file mode 100644 index 0000000000..e28336151d --- /dev/null +++ b/crates/test-files/fixtures/compile_errors/trait_fn_with_generic_params.fe @@ -0,0 +1,5 @@ +trait Bar {} + +trait Foo { + fn generic_fn(self, val: T); +} diff --git a/crates/test-files/fixtures/compile_errors/trait_fn_without_self.fe b/crates/test-files/fixtures/compile_errors/trait_fn_without_self.fe new file mode 100644 index 0000000000..906b15483f --- /dev/null +++ b/crates/test-files/fixtures/compile_errors/trait_fn_without_self.fe @@ -0,0 +1,3 @@ +trait Foo { + fn this_has_no_self(); +} diff --git a/crates/test-files/fixtures/compile_errors/trait_impl_mismatch.fe b/crates/test-files/fixtures/compile_errors/trait_impl_mismatch.fe new file mode 100644 index 0000000000..0344138e69 --- /dev/null +++ b/crates/test-files/fixtures/compile_errors/trait_impl_mismatch.fe @@ -0,0 +1,16 @@ +trait Foo { + fn this_misses_in_impl(self); + fn this_has_wrong_args_in_impl(self, val: u8, val2: bool); + fn this_has_wrong_return_type_in_impl(self) -> bool; +} + +struct Bar {} + +impl Foo for Bar { + + fn this_has_wrong_args_in_impl(self, val: u16, val2: Array) {} + fn this_has_wrong_return_type_in_impl(self) -> u8 { + return 0 + } + fn this_does_not_exist_in_trait() {} +} \ No newline at end of file diff --git a/crates/test-files/fixtures/compile_errors/traits_as_fields.fe b/crates/test-files/fixtures/compile_errors/traits_as_fields.fe new file mode 100644 index 0000000000..b69165b213 --- /dev/null +++ b/crates/test-files/fixtures/compile_errors/traits_as_fields.fe @@ -0,0 +1,9 @@ +trait Foo {} + +struct Bar { + val: Foo +} + +contract Nope { + val: Foo +} \ No newline at end of file diff --git a/crates/test-files/fixtures/compile_errors/traits_with_wrong_bounds.fe b/crates/test-files/fixtures/compile_errors/traits_with_wrong_bounds.fe new file mode 100644 index 0000000000..6276fa1195 --- /dev/null +++ b/crates/test-files/fixtures/compile_errors/traits_with_wrong_bounds.fe @@ -0,0 +1,5 @@ +trait Some {} + +struct Bar { + pub fn no_bounds(val: T) {} +} diff --git a/crates/test-files/fixtures/features/generic_functions.fe b/crates/test-files/fixtures/features/generic_functions.fe index 1e56d96e09..b9bead861a 100644 --- a/crates/test-files/fixtures/features/generic_functions.fe +++ b/crates/test-files/fixtures/features/generic_functions.fe @@ -1,23 +1,42 @@ -use std::traits::IsNumeric +trait Computable { + fn compute(self, val: u256) -> u256; +} + +struct Mac {} -struct Bar { +impl Computable for Mac { + fn compute(self, val: u256) -> u256 { + return 1 + val + } +} - pub fn bar(self, x: T, y: u256) -> bool { - return true +struct Linux { + pub counter: u256 + pub fn get_counter(self) -> u256 { + return self.counter } + pub fn something_static() -> u256 { + return 5 + } +} - pub fn call_me_maybe(self) { - self.bar(x: 1, y: 1) - self.bar(x: i8(-1), y:1) +impl Computable for Linux { + fn compute(self, val: u256) -> u256 { + return val + Linux::something_static() + self.get_counter() } } +struct Runner { -contract Foo { + pub fn run(self, _ val: T) -> u256 { + return val.compute(val: 1000) + } +} - pub fn call_me_maybe(self) { - let bar: Bar = Bar(); - bar.bar(x: 1, y: 1) - bar.bar(x: i8(-1), y:1) +contract Example { + pub fn generic_compute(self) { + let runner: Runner = Runner(); + assert runner.run(Mac()) == 1001 + assert runner.run(Linux(counter: 10)) == 1015 } } diff --git a/crates/tests/src/features.rs b/crates/tests/src/features.rs index c319201456..9d5e2169e5 100644 --- a/crates/tests/src/features.rs +++ b/crates/tests/src/features.rs @@ -2021,7 +2021,7 @@ fn ctx_init_in_call() { #[test] fn generics() { with_executor(&|mut executor| { - // This isn't testing much yet. It soon will when traits and impls are fully implemented - deploy_contract(&mut executor, "generic_functions.fe", "Foo", &[]); + let harness = deploy_contract(&mut executor, "generic_functions.fe", "Example", &[]); + harness.test_function(&mut executor, "generic_compute", &[], None); }); } diff --git a/newsfragments/710.feature.md b/newsfragments/710.feature.md new file mode 100644 index 0000000000..482eca1b7c --- /dev/null +++ b/newsfragments/710.feature.md @@ -0,0 +1,55 @@ +traits and generic function parameter + +Traits can now be defined, e.g: + +``` +trait Computable { + fn compute(self, val: u256) -> u256; +} +``` + +For now, traits can only be implemented for structs. +The mechanism to implement a trait is via an `impl` block e.g: + +``` +struct Linux { + pub counter: u256 + pub fn get_counter(self) -> u256 { + return self.counter + } + pub fn something_static() -> u256 { + return 5 + } +} + +impl Computable for Linux { + fn compute(self, val: u256) -> u256 { + return val + Linux::something_static() + self.get_counter() + } +} +``` + +Traits can only appear as bounds for generic functions e.g.: + +``` +struct Runner { + + pub fn run(self, _ val: T) -> u256 { + return val.compute(val: 1000) + } +} +``` + +Only `struct` functions (not `contract` functions) can have generic parameters. +The `run` method of `Runner` can be called with any type that implements `Computable` e.g. + +``` +contract Example { + + pub fn generic_compute(self) { + let runner: Runner = Runner(); + assert runner.run(Mac()) == 1001 + assert runner.run(Linux(counter: 10)) == 1015 + } +} +```