Skip to content

Commit

Permalink
Reject to_mem on primitive types in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
cburgdorf committed Oct 26, 2022
1 parent 80fc0ac commit a696cf7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
18 changes: 17 additions & 1 deletion crates/analyzer/src/traversal/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,23 @@ fn expr_call_builtin_value_method(
vec![],
);
}
Location::Storage { .. } => {}
Location::Storage { .. } => {
if value_attrs.typ.is_base(context.db())
|| matches!(value_attrs.typ.typ(context.db()), Type::Contract(_))
{
context.fancy_error(
"`to_mem()` called on primitive type",
vec![
Label::primary(
value.span,
"this value does not need to be copied to memory",
),
Label::secondary(method_name.span, "hint: remove `.to_mem()`"),
],
vec![],
);
}
}
Location::Value => {
context.fancy_error(
"`to_mem()` called on primitive type",
Expand Down
2 changes: 1 addition & 1 deletion crates/analyzer/tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ test_file! { abi_encode_from_storage }
test_file! { assert_sto_msg_no_copy }
test_file! { for_loop_sto_iter_no_copy }
test_file! { revert_sto_error_no_copy }

test_file! { call_to_mem_on_primitive }
test_file! { call_to_mut_fn_without_self }
test_file! { call_to_pure_fn_on_self }
test_file! { call_to_pure_struct_fn_on_instance }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: crates/analyzer/tests/errors.rs
expression: "error_string(&path, test_files::fixture(path))"

---
error: `to_mem()` called on primitive type
┌─ compile_errors/call_to_mem_on_primitive.fe:5:16
5return self.bar.to_mem()
^^^^^^^^ ------ hint: remove `.to_mem()`
│ │
this value does not need to be copied to memory


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract Foo {
bar: u256

fn call_me(self) -> u256 {
return self.bar.to_mem()
}
}
1 change: 1 addition & 0 deletions newsfragments/801.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reject calling `to_mem()` on primitive types in storage

0 comments on commit a696cf7

Please sign in to comment.