Skip to content

Commit

Permalink
Merge pull request #873 from Y-Nak/name-resolution
Browse files Browse the repository at this point in the history
Name resolution
  • Loading branch information
Y-Nak authored Aug 8, 2023
2 parents cadea40 + a61a20e commit 0f49db0
Show file tree
Hide file tree
Showing 344 changed files with 17,613 additions and 6,241 deletions.
367 changes: 334 additions & 33 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ Run `fe --help` to explore further options.

The following is a simple contract implemented in Fe.

```rust
```fe
struct Signed {
book_msg: String<100>
pub book_msg: String<100>
}
contract GuestBook {
messages: Map<address, String<100>>
pub fn sign(self, ctx: Context, book_msg: String<100>) {
pub fn sign(mut self, mut ctx: Context, book_msg: String<100>) {
self.messages[ctx.msg_sender()] = book_msg
ctx.emit(Signed(book_msg: book_msg))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/abi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "fe-abi"
version = "0.22.0"
version = "0.23.0"
authors = ["The Fe Developers <snakecharmers@ethereum.org>"]
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/ethereum/fe"

[dependencies]
fe-common = { path = "../common", version = "^0.22.0"}
fe-common = { path = "../common", version = "^0.23.0"}
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
Expand Down
8 changes: 4 additions & 4 deletions crates/analyzer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
name = "fe-analyzer"
version = "0.22.0"
version = "0.23.0"
authors = ["The Fe Developers <snakecharmers@ethereum.org>"]
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/ethereum/fe"

[dependencies]

fe-common = {path = "../common", version = "^0.22.0"}
fe-parser = {path = "../parser", version = "^0.22.0"}
fe-library = {path = "../library", version = "^0.22.0"}
fe-common = {path = "../common", version = "^0.23.0"}
fe-parser = {path = "../parser", version = "^0.23.0"}
fe-library = {path = "../library", version = "^0.23.0"}
hex = "0.4"
num-bigint = "0.4.3"
num-traits = "0.2.14"
Expand Down
3 changes: 1 addition & 2 deletions crates/analyzer/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use fe_analyzer::namespace::items::ModuleId;
use fe_analyzer::TestDb;
use fe_analyzer::{namespace::items::ModuleId, TestDb};

fn criterion_benchmark(c: &mut Criterion) {
let path = "demos/uniswap.fe";
Expand Down
41 changes: 21 additions & 20 deletions crates/analyzer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@ use crate::{
pattern_analysis::PatternMatrix,
};

use crate::namespace::items::{
ContractId, DiagnosticSink, FunctionId, FunctionSigId, Item, TraitId,
};
use crate::namespace::types::{Generic, SelfDecl, Type, TypeId};
use crate::AnalyzerDb;
use crate::{
builtins::{ContractTypeMethod, GlobalFunction, Intrinsic, ValueMethod},
namespace::scopes::BlockScopeType,
};
use crate::{
errors::{self, IncompleteItem, TypeError},
namespace::items::ModuleId,
namespace::{
items::{ContractId, DiagnosticSink, FunctionId, FunctionSigId, Item, ModuleId, TraitId},
scopes::BlockScopeType,
types::{Generic, SelfDecl, Type, TypeId},
},
AnalyzerDb,
};
use fe_common::diagnostics::Diagnostic;
pub use fe_common::diagnostics::Label;
use fe_common::Span;
use fe_parser::ast;
use fe_parser::node::{Node, NodeId};
use fe_common::{diagnostics::Diagnostic, Span};
use fe_parser::{
ast,
node::{Node, NodeId},
};

use indexmap::IndexMap;
use num_bigint::BigInt;
use smol_str::SmolStr;
use std::fmt::{self, Debug};
use std::hash::Hash;
use std::marker::PhantomData;
use std::rc::Rc;
use std::{cell::RefCell, collections::HashMap};
use std::{
cell::RefCell,
collections::HashMap,
fmt::{self, Debug},
hash::Hash,
marker::PhantomData,
rc::Rc,
};

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Analysis<T> {
Expand Down Expand Up @@ -222,7 +223,7 @@ pub trait AnalyzerContext {

fn register_diag(&self, diag: Diagnostic) -> DiagnosticVoucher {
self.add_diagnostic(diag);
DiagnosticVoucher(PhantomData::default())
DiagnosticVoucher(PhantomData)
}
}

Expand Down Expand Up @@ -313,7 +314,7 @@ pub struct DiagnosticVoucher(PhantomData<()>);

impl DiagnosticVoucher {
pub fn assume_the_parser_handled_it() -> Self {
Self(PhantomData::default())
Self(PhantomData)
}
}

Expand Down
24 changes: 12 additions & 12 deletions crates/analyzer/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::namespace::items::{
self, AttributeId, ContractFieldId, ContractId, DepGraphWrapper, EnumVariantKind, FunctionId,
FunctionSigId, ImplId, IngotId, Item, ModuleConstantId, ModuleId, StructFieldId, StructId,
TraitId, TypeAliasId,
};
use crate::namespace::types::{self, Type, TypeId};
use crate::{
context::{Analysis, Constant, FunctionBody},
namespace::items::EnumId,
};
use crate::{
errors::{ConstEvalError, TypeError},
namespace::items::EnumVariantId,
namespace::{
items::{
self, AttributeId, ContractFieldId, ContractId, DepGraphWrapper, EnumId, EnumVariantId,
EnumVariantKind, FunctionId, FunctionSigId, ImplId, IngotId, Item, ModuleConstantId,
ModuleId, StructFieldId, StructId, TraitId, TypeAliasId,
},
types::{self, Type, TypeId},
},
};
use fe_common::{
db::{SourceDb, SourceDbStorage, Upcast, UpcastMut},
SourceFileId, Span,
};
use fe_common::db::{SourceDb, SourceDbStorage, Upcast, UpcastMut};
use fe_common::{SourceFileId, Span};
use fe_parser::ast;
use indexmap::map::IndexMap;
use smol_str::SmolStr;
Expand Down
22 changes: 13 additions & 9 deletions crates/analyzer/src/db/queries/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use crate::context::AnalyzerContext;
use crate::db::{Analysis, AnalyzerDb};
use crate::errors;
use crate::namespace::items::{
self, ContractFieldId, ContractId, DepGraph, DepGraphWrapper, DepLocality, FunctionId, Item,
TypeDef,
use crate::{
context::AnalyzerContext,
db::{Analysis, AnalyzerDb},
errors,
namespace::{
items::{
self, ContractFieldId, ContractId, DepGraph, DepGraphWrapper, DepLocality, FunctionId,
Item, TypeDef,
},
scopes::ItemScope,
types::{self, Type},
},
traversal::types::type_desc,
};
use crate::namespace::scopes::ItemScope;
use crate::namespace::types::{self, Type};
use crate::traversal::types::type_desc;
use fe_common::diagnostics::Label;
use fe_parser::ast;
use indexmap::map::{Entry, IndexMap};
Expand Down
41 changes: 27 additions & 14 deletions crates/analyzer/src/db/queries/functions.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
use crate::context::{AnalyzerContext, CallType, FunctionBody};
use crate::db::{Analysis, AnalyzerDb};
use crate::display::Displayable;
use crate::errors::TypeError;
use crate::namespace::items::{
DepGraph, DepGraphWrapper, DepLocality, FunctionId, FunctionSigId, Item, TypeDef,
use crate::{
context::{AnalyzerContext, CallType, FunctionBody},
db::{Analysis, AnalyzerDb},
display::Displayable,
errors::TypeError,
namespace::{
items::{DepGraph, DepGraphWrapper, DepLocality, FunctionId, FunctionSigId, Item, TypeDef},
scopes::{BlockScope, BlockScopeType, FunctionScope, ItemScope},
types::{self, CtxDecl, Generic, SelfDecl, Type, TypeId},
},
traversal::{
functions::traverse_statements,
types::{type_desc, type_desc_to_trait},
},
};
use crate::namespace::scopes::{BlockScope, BlockScopeType, FunctionScope, ItemScope};
use crate::namespace::types::{self, CtxDecl, Generic, SelfDecl, Type, TypeId};
use crate::traversal::functions::traverse_statements;
use crate::traversal::types::{type_desc, type_desc_to_trait};
use fe_common::diagnostics::Label;
use fe_parser::ast::{self, GenericParameter};
use fe_parser::node::Node;
use fe_parser::{
ast::{self, GenericParameter},
node::Node,
};
use if_chain::if_chain;
use smol_str::SmolStr;
use std::collections::HashMap;
use std::rc::Rc;
use std::{collections::HashMap, rc::Rc};

/// Gather context information for a function definition and check for type
/// errors. Does not inspect the function body.
Expand Down Expand Up @@ -131,6 +136,14 @@ pub fn function_signature(
});

if let Some(context_type) = scope.get_context_type() {
if arg.name() == "ctx" && typ.as_ref().map(|val| val.deref(db)) != Ok(context_type) {
scope.error(
"`ctx` is reserved for instances of `Context`",
arg.span,
"`ctx` must be an instance of `Context`",
);
};

if typ.as_ref().map(|val| val.deref(db)) == Ok(context_type) {
if arg.name() != "ctx" {
scope.error(
Expand Down
15 changes: 9 additions & 6 deletions crates/analyzer/src/db/queries/impls.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use indexmap::map::Entry;
use indexmap::IndexMap;
use indexmap::{map::Entry, IndexMap};
use smol_str::SmolStr;

use crate::context::{Analysis, AnalyzerContext};
use crate::namespace::items::{Function, FunctionId, ImplId, Item};
use crate::namespace::scopes::ItemScope;
use crate::AnalyzerDb;
use crate::{
context::{Analysis, AnalyzerContext},
namespace::{
items::{Function, FunctionId, ImplId, Item},
scopes::ItemScope,
},
AnalyzerDb,
};
use std::rc::Rc;

pub fn impl_all_functions(db: &dyn AnalyzerDb, impl_: ImplId) -> Rc<[FunctionId]> {
Expand Down
30 changes: 20 additions & 10 deletions crates/analyzer/src/db/queries/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,33 @@ pub fn module_item_map(
if function.is_test(db) {
if !sig_ast.generic_params.kind.is_empty() {
diagnostics.push(errors::fancy_error(
"generic function parameters are not supported on test functions",
"generic parameters are not supported on test functions",
vec![Label::primary(
sig_ast.generic_params.span,
"this cannot appear here",
"invalid generic parameters",
)],
vec!["Hint: remove the generic parameters".into()],
));
}

if !sig_ast.args.is_empty() {
let span =
sig_ast.args.first().unwrap().span + sig_ast.args.last().unwrap().span;
diagnostics.push(errors::fancy_error(
"function parameters are not supported on test functions",
vec![Label::primary(span, "this cannot appear here")],
vec!["Hint: remove the parameters".into()],
));
if let Some(arg) = sig_ast.args.first() {
if arg.name() != "ctx" {
diagnostics.push(errors::fancy_error(
"function parameters other than `ctx` are not supported on test functions",
vec![Label::primary(arg.span, "invalid function parameter")],
vec!["Hint: remove the parameter".into()],
));
}
}

for arg in sig_ast.args.iter().skip(1) {
if arg.name() != "ctx" {
diagnostics.push(errors::fancy_error(
"function parameters other than `ctx` are not supported on test functions",
vec![Label::primary(arg.span, "invalid function parameter")],
vec!["Hint: remove the parameter".into()],
));
}
}
}
}
Expand Down
31 changes: 17 additions & 14 deletions crates/analyzer/src/db/queries/structs.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use crate::builtins;
use crate::constants::MAX_INDEXED_EVENT_FIELDS;
use crate::context::AnalyzerContext;
use crate::db::Analysis;
use crate::errors::TypeError;
use crate::namespace::items::{
self, DepGraph, DepGraphWrapper, DepLocality, FunctionId, Item, StructField, StructFieldId,
StructId, TypeDef,
use crate::{
builtins,
constants::MAX_INDEXED_EVENT_FIELDS,
context::AnalyzerContext,
db::Analysis,
errors::TypeError,
namespace::{
items::{
self, DepGraph, DepGraphWrapper, DepLocality, FunctionId, Item, StructField,
StructFieldId, StructId, TypeDef,
},
scopes::ItemScope,
types::{Type, TypeId},
},
traversal::types::type_desc,
AnalyzerDb,
};
use crate::namespace::scopes::ItemScope;
use crate::namespace::types::{Type, TypeId};
use crate::traversal::types::type_desc;
use crate::AnalyzerDb;
use fe_common::utils::humanize::pluralize_conditionally;
use fe_parser::{ast, Label};
use indexmap::map::{Entry, IndexMap};
use smol_str::SmolStr;
use std::rc::Rc;
use std::str::FromStr;
use std::{rc::Rc, str::FromStr};

pub fn struct_all_fields(db: &dyn AnalyzerDb, struct_: StructId) -> Rc<[StructFieldId]> {
struct_
Expand Down
17 changes: 10 additions & 7 deletions crates/analyzer/src/db/queries/traits.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use indexmap::map::Entry;
use indexmap::IndexMap;
use indexmap::{map::Entry, IndexMap};
use smol_str::SmolStr;

use crate::context::{Analysis, AnalyzerContext};
use crate::namespace::items::{FunctionSig, FunctionSigId, Item, TraitId};
use crate::namespace::scopes::ItemScope;
use crate::namespace::types::TypeId;
use crate::AnalyzerDb;
use crate::{
context::{Analysis, AnalyzerContext},
namespace::{
items::{FunctionSig, FunctionSigId, Item, TraitId},
scopes::ItemScope,
types::TypeId,
},
AnalyzerDb,
};
use std::rc::Rc;

pub fn trait_all_functions(db: &dyn AnalyzerDb, trait_: TraitId) -> Rc<[FunctionSigId]> {
Expand Down
Loading

0 comments on commit 0f49db0

Please sign in to comment.