Skip to content

Commit 6cf682f

Browse files
committed
Forward attributes on impl function parameters to variable declaration.
1 parent 6050c94 commit 6cf682f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/expand.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
362362
quote!(let #mutability #ident = #self_token;)
363363
}
364364
FnArg::Typed(arg) => {
365+
// If there is a `#[cfg(..)]` attribute that selectively
366+
// enables the parameter, forward it to the variable.
367+
//
368+
// This is currently not applied to the `self` parameter
369+
let attrs = arg.attrs.iter();
370+
365371
if let Pat::Ident(PatIdent {
366372
ident, mutability, ..
367373
}) = &*arg.pat
@@ -371,15 +377,24 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
371377
let prefixed = Ident::new("__self", ident.span());
372378
quote!(let #mutability #prefixed = #ident;)
373379
} else {
374-
quote!(let #mutability #ident = #ident;)
380+
quote!(
381+
#(#attrs)*
382+
let #mutability #ident = #ident;
383+
)
375384
}
376385
} else {
377386
let pat = &arg.pat;
378387
let ident = positional_arg(i, pat);
379388
if let Pat::Wild(_) = **pat {
380-
quote!(let #ident = #ident;)
389+
quote!(
390+
#(#attrs)*
391+
let #ident = #ident;
392+
)
381393
} else {
382-
quote!(let #pat = #ident;)
394+
quote!(
395+
#(#attrs)*
396+
let #pat = #ident;
397+
)
383398
}
384399
}
385400
}

tests/test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ trait Trait {
5454
async fn calls_mut(&mut self) {
5555
self.selfmut().await;
5656
}
57+
58+
async fn cfg_param(&self, param: u8);
59+
async fn cfg_param_wildcard(&self, _: u8);
60+
async fn cfg_param_tuple(&self, (left, right): (u8, u8));
5761
}
5862

5963
struct Struct;
@@ -87,6 +91,17 @@ impl Trait for Struct {
8791
async fn calls_mut(&mut self) {
8892
self.selfmut().await;
8993
}
94+
95+
async fn cfg_param(&self, #[cfg(any())] param: u8, #[cfg(all())] _unused: u8) {}
96+
97+
async fn cfg_param_wildcard(&self, #[cfg(any())] _: u8, #[cfg(all())] _: u8) {}
98+
99+
async fn cfg_param_tuple(
100+
&self,
101+
#[cfg(any())] (left, right): (u8, u8),
102+
#[cfg(all())] (_left, _right): (u8, u8),
103+
) {
104+
}
90105
}
91106

92107
pub async fn test() {

0 commit comments

Comments
 (0)