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 25, 2022
1 parent 80fc0ac commit 0ff6b2c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 2 deletions.
16 changes: 15 additions & 1 deletion crates/analyzer/src/traversal/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,21 @@ fn expr_call_builtin_value_method(
vec![],
);
}
Location::Storage { .. } => {}
Location::Storage { .. } => {
if value_attrs.typ.is_base(context.db()) {
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()
}
}
17 changes: 17 additions & 0 deletions foo.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::context::Context


contract WavePortal {
foo: u256
totalWaves: Map<address,u256>

pub fn wave(self,ctx:Context) {
self.totalWaves[ctx.msg_sender()] +=1;
}

pub fn getTotalWaves(self, addr: address)-> u256{
return self.foo.to_mem()
//return self.totalWaves[addr].to_mem()
}

}
1 change: 1 addition & 0 deletions newsfragments/4711.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 0ff6b2c

Please sign in to comment.