Skip to content

Commit

Permalink
Merge pull request #227 from azriel91/bugfix/226/forward-param-attrib…
Browse files Browse the repository at this point in the history
…utes

Forward attributes on `impl` function parameters to variable declaration.
  • Loading branch information
dtolnay committed Jan 6, 2023
2 parents 6050c94 + 6cf682f commit 2866cb9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/expand.rs
Expand Up @@ -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
Expand All @@ -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;
)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/test.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 2866cb9

Please sign in to comment.