Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject to_mem on primitive types in storage #801

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 │ return 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