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

fix: code generation for mut self methods #616

Merged
merged 2 commits into from
Nov 3, 2021
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
37 changes: 37 additions & 0 deletions near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,43 @@ mod tests {
assert_eq!(expected.to_string(), actual.to_string());
}

#[test]
fn owned_no_args_no_return_no_mut() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
let mut method: ImplItemMethod = syn::parse_str("pub fn method(self) { }").unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, impl_type).unwrap();
let actual = method_info.method_wrapper();
let expected = quote!(
#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn method() {
near_sdk::env::setup_panic_hook();
let contract: Hello = near_sdk::env::state_read().unwrap_or_default();
contract.method();
}
);
assert_eq!(expected.to_string(), actual.to_string());
}


#[test]
fn mut_owned_no_args_no_return() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
let mut method: ImplItemMethod = syn::parse_str("pub fn method(mut self) { }").unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, impl_type).unwrap();
let actual = method_info.method_wrapper();
let expected = quote!(
#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn method() {
near_sdk::env::setup_panic_hook();
let mut contract: Hello = near_sdk::env::state_read().unwrap_or_default();
contract.method();
}
);
assert_eq!(expected.to_string(), actual.to_string());
}

#[test]
fn no_args_no_return_mut() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl AttrSigInfo {

if let Some(ref receiver) = receiver {
if matches!(method_type, MethodType::Regular) {
if receiver.mutability.is_none() {
if receiver.mutability.is_none() || receiver.reference.is_none() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why we can't just have

if receiver.reference.is_none() {

but compilation_tests/payable_view.rs successfully compiles even though its expected not to.

This conditional should just only be checking for mut self and self signatures, where checking for mutability shouldn't matter at all. But somehow also related to pay(&self) signature in payable_vew.rs? Maybe there's some sort of complexity somewhere we're not noticing or I'm probably missing something here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah this is intentional. This was the point of the PR that mut self was treated as mutating state and tries to write the data after. If you check only for reference, you don't differentiate between mutable and immutable references

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, so I was confused what this whole if block was checking. Thought it was for self and mut self but was actually for the reference versions &self

method_type = MethodType::View;
}
} else {
Expand Down