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: update/query macro could not handle function arguments with the same name as the function itself #525

Merged
merged 3 commits into from
Oct 11, 2024
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
17 changes: 10 additions & 7 deletions ic-cdk-macros/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ fn get_args(method: MethodType, signature: &Signature) -> Result<Vec<(Ident, Box
));
}
FnArg::Typed(PatType { pat, ty, .. }) => {
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())
}
};

Expand Down
4 changes: 4 additions & 0 deletions ic-cdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions ic-cdk/tests/pass/method_arg_same_name.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Loading