Skip to content

Commit

Permalink
Merge aad4c4b into aa4a91e
Browse files Browse the repository at this point in the history
  • Loading branch information
g-r-a-n-t authored Jun 2, 2021
2 parents aa4a91e + aad4c4b commit 62783fa
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 14 deletions.
2 changes: 0 additions & 2 deletions analyzer/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub enum ValueMethod {
Clone,
ToMem,
AbiEncode,
AbiEncodePacked,
}

#[derive(Clone, Debug, PartialEq, EnumString, IntoStaticStr, Hash)]
Expand Down Expand Up @@ -51,7 +50,6 @@ pub enum ChainField {
#[derive(Debug, PartialEq, EnumString)]
#[strum(serialize_all = "snake_case")]
pub enum MsgField {
Data,
Sender,
Sig,
Value,
Expand Down
8 changes: 8 additions & 0 deletions analyzer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,14 @@ impl Context {
)
}

pub fn not_yet_implemented(&mut self, feature: impl Display, span: Span) {
self.error(
"feature not yet implemented",
span,
format!("{} is not yet implemented", feature),
)
}

pub fn fancy_error(
&mut self,
message: impl Into<String>,
Expand Down
3 changes: 2 additions & 1 deletion analyzer/src/traversal/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ fn event_def(
}

// check if they are trying to index an array type
// todo clean all this up
for index in indexed_fields.clone() {
match fields[index].1.to_owned() {
FixedSize::Base(_) => {}
_ => unimplemented!("non-base type indexed event params"),
_ => context.not_yet_implemented("non-base type indexed event fields", stmt.span),
}
}

Expand Down
47 changes: 41 additions & 6 deletions analyzer/src/traversal/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use fe_common::diagnostics::Label;
use fe_common::numeric;
use fe_common::{Span, Spanned};
use fe_parser::ast as fe;
use fe_parser::ast::UnaryOperator;
use fe_parser::node::Node;
use num_bigint::BigInt;
use std::convert::TryInto;
Expand Down Expand Up @@ -398,7 +399,6 @@ fn expr_attribute(
}
Ok(Object::Msg) => {
return match MsgField::from_str(&attr.kind) {
Ok(MsgField::Data) => todo!(),
Ok(MsgField::Sender) => base_type(Base::Address),
Ok(MsgField::Sig) => array_type(Array {
size: 32,
Expand Down Expand Up @@ -556,7 +556,14 @@ fn expr_unary_operation(
Location::Value,
))
}
_ => todo!(),
UnaryOperator::Invert => {
context.not_yet_implemented("unary invert", exp.span);
Ok(ExpressionAttributes::new(Type::unit(), Location::Value))
}
UnaryOperator::UAdd => {
context.not_yet_implemented("unary add", exp.span);
Ok(ExpressionAttributes::new(Type::unit(), Location::Value))
}
};
}

Expand Down Expand Up @@ -983,7 +990,13 @@ fn expr_call_value_attribute(
ValueMethod::AbiEncode => match &value_attributes.typ {
Type::Struct(struct_) => {
if value_attributes.final_location() != Location::Memory {
todo!("encode structs from storage")
context.fancy_error(
"value must be copied to memory",
vec![Label::primary(value.span, "this value is in storage")],
vec!["Hint: values located in storage can be copied to memory using the `to_mem` function.".into(),
"Example: `self.my_array.to_mem().abi_encode()`".into(),
],
);
}

Ok(ExpressionAttributes::new(
Expand All @@ -996,7 +1009,13 @@ fn expr_call_value_attribute(
}
Type::Tuple(tuple) => {
if value_attributes.final_location() != Location::Memory {
todo!("encode tuple from storage")
context.fancy_error(
"value must be copied to memory",
vec![Label::primary(value.span, "this value is in storage")],
vec!["Hint: values located in storage can be copied to memory using the `to_mem` function.".into(),
"Example: `self.my_array.to_mem().abi_encode()`".into(),
],
);
}

Ok(ExpressionAttributes::new(
Expand All @@ -1007,9 +1026,25 @@ fn expr_call_value_attribute(
Location::Memory,
))
}
_ => todo!(),
_ => {
context.fancy_error(
format!(
"value of type {} does not support `abi_encode()`",
value_attributes.typ
),
vec![Label::primary(
value.span,
"this value cannot be encoded using `abi_encode()`",
)],
vec![
"Hint: struct and tuple values can be encoded.".into(),
"Example: `(42,).abi_encode()`".into(),
],
);

Ok(ExpressionAttributes::new(Type::unit(), Location::Value))
}
},
ValueMethod::AbiEncodePacked => todo!(),
};
}

Expand Down
8 changes: 6 additions & 2 deletions analyzer/src/traversal/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ pub fn module(context: &mut Context, module: &fe::Module) -> Result<(), Semantic
let contract_scope = contracts::contract_def(Rc::clone(&scope), context, stmt)?;
contracts.push((stmt, contract_scope))
}
fe::ModuleStmt::FromImport { .. } => unimplemented!(),
fe::ModuleStmt::SimpleImport { .. } => unimplemented!(),
fe::ModuleStmt::FromImport { .. } => {
context.not_yet_implemented("from import", stmt.span)
}
fe::ModuleStmt::SimpleImport { .. } => {
context.not_yet_implemented("simple import", stmt.span)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion analyzer/src/traversal/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn struct_def(
if let Type::Base(base_typ) = field_type {
val.add_field(&name.kind, &FixedSize::Base(base_typ))?;
} else {
todo!("Non-Base type fields aren't yet supported")
context.not_yet_implemented("non-base type struct fields", field.span)
}
}
module_scope
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/yul/mappers/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ fn expr_call(context: &Context, exp: &Node<fe::Expr>) -> yul::Expression {
),
_ => panic!("invalid attributes"),
},
builtins::ValueMethod::AbiEncodePacked => todo!(),
}
}
};
Expand Down Expand Up @@ -383,7 +382,6 @@ fn expr_attribute(context: &Context, exp: &Node<fe::Expr>) -> yul::Expression {
Err(_) => panic!("invalid `chain` attribute name"),
},
Ok(Object::Msg) => match MsgField::from_str(&attr.kind) {
Ok(MsgField::Data) => todo!(),
Ok(MsgField::Sender) => expression! { caller() },
Ok(MsgField::Sig) => expression! {
and(
Expand Down
1 change: 1 addition & 0 deletions newsfragments/437.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactored front-end "not implemented" errors into analyzer errors and removed questionable variants. Any panic is now considered to be a bug.
3 changes: 3 additions & 0 deletions test.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract Foo:
pub def bar():
(42,).abi_encode()
5 changes: 5 additions & 0 deletions tests/fixtures/compile_errors/abi_encode_from_storage.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
contract Foo:
my_field: (u256, bool)

pub def bar():
self.my_field.abi_encode()
3 changes: 3 additions & 0 deletions tests/fixtures/compile_errors/abi_encode_u256.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract Foo:
pub def foo():
42.abi_encode()
2 changes: 2 additions & 0 deletions tests/src/compile_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,5 @@ test_file! { strict_boolean_if_else }
test_file! { struct_call_bad_args }
test_file! { struct_call_without_kw_args }
test_file! { non_pub_init }
test_file! { abi_encode_u256 }
test_file! { abi_encode_from_storage }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/src/compile_errors.rs
expression: "error_string(&path, &src)"

---
error: value must be copied to memory
┌─ fixtures/compile_errors/abi_encode_from_storage.fe:5:9
5self.my_field.abi_encode()
^^^^^^^^^^^^^ this value is in storage
= Hint: values located in storage can be copied to memory using the `to_mem` function.
= Example: `self.my_array.to_mem().abi_encode()`


Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/src/compile_errors.rs
expression: "error_string(&path, &src)"

---
error: value of type u256 does not support `abi_encode()`
┌─ fixtures/compile_errors/abi_encode_u256.fe:3:9
3 │ 42.abi_encode()
│ ^^ this value cannot be encoded using `abi_encode()`
= Hint: struct and tuple values can be encoded.
= Example: `(42,).abi_encode()`


0 comments on commit 62783fa

Please sign in to comment.