From 6cf682f3a252d71a3f0b93be0995504844aec897 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Fri, 6 Jan 2023 14:42:45 +1300 Subject: [PATCH] Forward attributes on `impl` function parameters to variable declaration. --- src/expand.rs | 21 ++++++++++++++++++--- tests/test.rs | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/expand.rs b/src/expand.rs index 53918cb..8e6cd28 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -362,6 +362,12 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) { quote!(let #mutability #ident = #self_token;) } FnArg::Typed(arg) => { + // If there is a `#[cfg(..)]` attribute that selectively + // enables the parameter, forward it to the variable. + // + // This is currently not applied to the `self` parameter + let attrs = arg.attrs.iter(); + if let Pat::Ident(PatIdent { ident, mutability, .. }) = &*arg.pat @@ -371,15 +377,24 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) { let prefixed = Ident::new("__self", ident.span()); quote!(let #mutability #prefixed = #ident;) } else { - quote!(let #mutability #ident = #ident;) + quote!( + #(#attrs)* + let #mutability #ident = #ident; + ) } } else { let pat = &arg.pat; let ident = positional_arg(i, pat); if let Pat::Wild(_) = **pat { - quote!(let #ident = #ident;) + quote!( + #(#attrs)* + let #ident = #ident; + ) } else { - quote!(let #pat = #ident;) + quote!( + #(#attrs)* + let #pat = #ident; + ) } } } diff --git a/tests/test.rs b/tests/test.rs index 9dcc673..7b9078d 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -54,6 +54,10 @@ trait Trait { async fn calls_mut(&mut self) { self.selfmut().await; } + + async fn cfg_param(&self, param: u8); + async fn cfg_param_wildcard(&self, _: u8); + async fn cfg_param_tuple(&self, (left, right): (u8, u8)); } struct Struct; @@ -87,6 +91,17 @@ impl Trait for Struct { async fn calls_mut(&mut self) { self.selfmut().await; } + + async fn cfg_param(&self, #[cfg(any())] param: u8, #[cfg(all())] _unused: u8) {} + + async fn cfg_param_wildcard(&self, #[cfg(any())] _: u8, #[cfg(all())] _: u8) {} + + async fn cfg_param_tuple( + &self, + #[cfg(any())] (left, right): (u8, u8), + #[cfg(all())] (_left, _right): (u8, u8), + ) { + } } pub async fn test() {