Skip to content

Commit

Permalink
fix: update/query macro could not handle function arguments with the …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
lwshang authored Oct 11, 2024
1 parent 69a459d commit 69d98b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
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() {}

0 comments on commit 69d98b0

Please sign in to comment.