From 69d98b07ff9aec906691853dd76b6eea5f892c37 Mon Sep 17 00:00:00 2001 From: Linwei Shang Date: Fri, 11 Oct 2024 13:18:27 -0700 Subject: [PATCH] fix: update/query macro could not handle function arguments with the same name as the function itself (#525) * add a test case to reveal the bug * fix: update/query macro could not handle function arguments with the same name as the function itself * changelog --- ic-cdk-macros/src/export.rs | 17 ++++++++++------- ic-cdk/CHANGELOG.md | 4 ++++ ic-cdk/tests/pass/method_arg_same_name.rs | 13 +++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 ic-cdk/tests/pass/method_arg_same_name.rs diff --git a/ic-cdk-macros/src/export.rs b/ic-cdk-macros/src/export.rs index 6d087d398..b38c0ed27 100644 --- a/ic-cdk-macros/src/export.rs +++ b/ic-cdk-macros/src/export.rs @@ -81,14 +81,17 @@ fn get_args(method: MethodType, signature: &Signature) -> Result { - if let Pat::Ident(PatIdent { ident, .. }) = pat.as_ref() { - (ident.clone(), ty.clone()) + let ident = if let Pat::Ident(PatIdent { ident, .. }) = pat.as_ref() { + // If the argument is named the same as the function, we need to rename it. + if ident == &signature.ident { + format_ident!("__arg_{}", ident, span = pat.span()) + } else { + ident.clone() + } } else { - ( - format_ident!("__unnamed_arg_{i}", span = pat.span()), - ty.clone(), - ) - } + format_ident!("__unnamed_arg_{i}", span = pat.span()) + }; + (ident, ty.clone()) } }; diff --git a/ic-cdk/CHANGELOG.md b/ic-cdk/CHANGELOG.md index 2c58efa9d..9476bd1dc 100644 --- a/ic-cdk/CHANGELOG.md +++ b/ic-cdk/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +### Fixed + +- Fix update/query macro could not handle function arguments with the same name as the function itself. (#525) + ### Changed - Add `AllowedViewers` variant to `LogVisibility` enum. (#512) diff --git a/ic-cdk/tests/pass/method_arg_same_name.rs b/ic-cdk/tests/pass/method_arg_same_name.rs new file mode 100644 index 000000000..749db8d54 --- /dev/null +++ b/ic-cdk/tests/pass/method_arg_same_name.rs @@ -0,0 +1,13 @@ +use ic_cdk::{query, update}; + +#[update] +fn foo(foo: i32) -> i32 { + foo +} + +#[query] +fn bar(bar: i32) -> i32 { + bar +} + +fn main() {}