Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sbillig committed Aug 11, 2022
1 parent 9300ef4 commit 80627e8
Show file tree
Hide file tree
Showing 78 changed files with 199 additions and 259 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ Run `fe --help` to explore further options.
The following is a simple contract implemented in Fe.

```rust
use std::context::Context

contract GuestBook {
messages: Map<address, String<100>>

Expand Down
25 changes: 24 additions & 1 deletion crates/analyzer/src/db/queries/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ pub fn module_used_item_map(
let mut diagnostics = vec![];
let body = &module.ast(db).body;

let items = body
let mut items = body
.iter()
.fold(indexmap! {}, |mut accum, stmt| {
if let ast::ModuleStmt::Use(use_stmt) = stmt {
Expand Down Expand Up @@ -481,6 +481,29 @@ pub fn module_used_item_map(
})
.collect::<IndexMap<_, _>>();

// std prelude
if !module.is_in_std(db) {
let prelude_items = resolve_use_tree(
db,
module,
&Node::new(
ast::UseTree::Glob {
prefix: ast::Path {
segments: vec![
Node::new("std".into(), Span::dummy()),
Node::new("prelude".into(), Span::dummy()),
],
},
},
Span::dummy(),
),
true,
)
.value;

items.extend(Rc::try_unwrap(prelude_items).unwrap().into_iter());
}

Analysis::new(Rc::new(items), diagnostics.into())
}

Expand Down
5 changes: 2 additions & 3 deletions crates/analyzer/src/namespace/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ impl Item {
}
}

// Placeholder; someday std::prelude will be a proper module.
pub fn std_prelude_items() -> IndexMap<SmolStr, Item> {
pub fn builtin_items() -> IndexMap<SmolStr, Item> {
let mut items = indexmap! {
SmolStr::new("bool") => Item::Type(TypeDef::Primitive(types::Base::Bool)),
SmolStr::new("address") => Item::Type(TypeDef::Primitive(types::Base::Address)),
Expand Down Expand Up @@ -689,7 +688,7 @@ impl ModuleId {
.external_ingots(db)
.iter()
.map(|(name, ingot)| (name.clone(), Item::Ingot(*ingot)))
.chain(std_prelude_items())
.chain(builtin_items())
.collect::<IndexMap<_, _>>();

if ingot.data(db).mode != IngotMode::StandaloneModule {
Expand Down
7 changes: 2 additions & 5 deletions crates/analyzer/src/traversal/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,7 @@ fn expr_call_type_attribute(
vec![
Label::primary(
args.span,
"`ctx` must be defined and passed into the function",
"`ctx` must be passed into the function",
),
Label::secondary(
context.parent_function().name_span(context.db()),
Expand All @@ -1906,10 +1906,7 @@ fn expr_call_type_attribute(
"Example: `pub fn foo(ctx: Context, ...)`",
),
],
vec![
"Note: import context with `use std::context::Context`".into(),
"Example: `MyContract.create(ctx, 0)`".into(),
],
vec!["Example: `MyContract.create(ctx, 0)`".into()],
);
}
} else if !attrs.typ.is_integer(context.db()) {
Expand Down
5 changes: 1 addition & 4 deletions crates/analyzer/src/traversal/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ fn emit(scope: &mut BlockScope, stmt: &Node<fe::FuncStmt>) -> Result<(), FatalEr
"Example: `pub fn foo(ctx: Context, ...)`",
),
],
vec![
"Note: import context with `use std::context::Context`".into(),
"Example: emit MyEvent(ctx, ...)".into(),
],
vec!["Example: emit MyEvent(ctx, ...)".into()],
);
}
}
Expand Down
14 changes: 3 additions & 11 deletions crates/analyzer/tests/snapshots/errors__ctx_undefined_create.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,10 @@ error: `create` expects 2 arguments, but 1 was provided
│ │
expects 2 arguments

error: `Context` is not defined
┌─ compile_errors/ctx_undefined_create.fe:5:19
error: incorrect type for argument to `Bar.create`
┌─ compile_errors/ctx_undefined_create.fe:5:20
4pub fn baz() {
---
│ │
Note: declare `ctx` in this function signature
│ Example: `pub fn foo(ctx: Context, ...)`
5Bar.create(0)
│ ^^^ `ctx` must be defined and passed into the function
= Note: import context with `use std::context::Context`
= Example: `MyContract.create(ctx, 0)`
│ ^ this has type `u256`; expected `Context`


14 changes: 3 additions & 11 deletions crates/analyzer/tests/snapshots/errors__ctx_undefined_create2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,10 @@ error: `create2` expects 3 arguments, but 2 were provided
│ │
expects 3 arguments

error: `Context` is not defined
┌─ compile_errors/ctx_undefined_create2.fe:5:20
error: incorrect type for argument to `Bar.create2`
┌─ compile_errors/ctx_undefined_create2.fe:5:21
4pub fn baz() {
---
│ │
Note: declare `ctx` in this function signature
│ Example: `pub fn foo(ctx: Context, ...)`
5Bar.create2(0, 0)
│ ^^^^^^ `ctx` must be defined and passed into the function
= Note: import context with `use std::context::Context`
= Example: `MyContract.create(ctx, 0)`
│ ^ this has type `u256`; expected `Context`


28 changes: 18 additions & 10 deletions crates/analyzer/tests/snapshots/errors__ctx_undefined_event.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ source: crates/analyzer/tests/errors.rs
expression: "error_string(&path, test_files::fixture(path))"

---
error: `Context` is not defined
┌─ compile_errors/ctx_undefined_event.fe:7:9
error: `YouWin` expects 2 arguments, but 1 was provided
┌─ compile_errors/ctx_undefined_event.fe:7:14
6pub fn bar() {
---
│ │
Note: declare `ctx` in this function signature
│ Example: `pub fn foo(ctx: Context, ...)`
7emit YouWin(amount: 42)
│ ^^^^^^^^^^^^^^^^^^^^^^^ `ctx` must be defined and passed into the event
^^^^^^ ---------- supplied 1 argument
│ │
expects 2 arguments

error: argument label mismatch
┌─ compile_errors/ctx_undefined_event.fe:7:21
7emit YouWin(amount: 42)
^^^^^^ expected `ctx`
= Note: import context with `use std::context::Context`
= Example: emit MyEvent(ctx, ...)
= Note: arguments must be provided in order.

error: incorrect type for `YouWin` argument `ctx`
┌─ compile_errors/ctx_undefined_event.fe:7:29
7emit YouWin(amount: 42)
│ ^^ this has type `u256`; expected type `Context`


2 changes: 0 additions & 2 deletions crates/parser/tests/cases/parse_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ contract GuestBook {
}"# }

test_parse! { module_level_events, try_parse_module, r#"
use std::context::Context
event Transfer {
idx sender: address
idx receiver: address
Expand Down
Loading

0 comments on commit 80627e8

Please sign in to comment.