Repository: dtolnay/async-trait (Rust)

Issue:
Outer attributes of generic type / lifetime parameter not respected in async trait async function definition.
Hi sir(s)

Thank you for the awesome crate and also for many other crates you (and your team) had contributed to the Rust ecosystem!

Consider this snippet:

```rust
use {async_trait::async_trait}; // 0.1.86

#[async_trait]
trait AsyncTrait {
    
    async fn async_fn<#[cfg(test)] T>(#[cfg(test)] t: T) {
        #[cfg(test)]
        let _ = t;
    }
}

trait SyncTrait {
    
    fn async_fn<#[cfg(test)] T>(#[cfg(test)] t: T) {
        #[cfg(test)]
        let _ = t;
    }
}
```

[Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0beb91c888a22032c7a4cf683d29a6ac)

For the async function definition `async fn async_fn<#[cfg(test)] T>(#[cfg(test)] t: T) {`, it seems that `#[async_trait]` attribute macro does not respect the outer attribute `#[cfg(test)]` in the generic type parameter 'slot', i.e. `<#[cfg(test)] T>`.

I am not sure if this is a bug or intended.

Thank you.


Discussion:

Patch:
diff --git a/src/expand.rs b/src/expand.rs
index cfaa11c..573ad1d 100644
--- a/src/expand.rs
+++ b/src/expand.rs
@@ -192,10 +192,14 @@ fn transform_sig(
                     Some(colon_token) => colon_token.span,
                     None => param_name.span(),
                 };
-                let bounds = mem::take(&mut param.bounds);
-                where_clause_or_default(&mut sig.generics.where_clause)
-                    .predicates
-                    .push(parse_quote_spanned!(span=> #param_name: 'async_trait + #bounds));
+                if param.attrs.is_empty() {
+                    let bounds = mem::take(&mut param.bounds);
+                    where_clause_or_default(&mut sig.generics.where_clause)
+                        .predicates
+                        .push(parse_quote_spanned!(span=> #param_name: 'async_trait + #bounds));
+                } else {
+                    param.bounds.push(parse_quote!('async_trait));
+                }
             }
             GenericParam::Lifetime(param) => {
                 let param_name = &param.lifetime;
@@ -203,10 +207,14 @@ fn transform_sig(
                     Some(colon_token) => colon_token.span,
                     None => param_name.span(),
                 };
-                let bounds = mem::take(&mut param.bounds);
-                where_clause_or_default(&mut sig.generics.where_clause)
-                    .predicates
-                    .push(parse_quote_spanned!(span=> #param: 'async_trait + #bounds));
+                if param.attrs.is_empty() {
+                    let bounds = mem::take(&mut param.bounds);
+                    where_clause_or_default(&mut sig.generics.where_clause)
+                        .predicates
+                        .push(parse_quote_spanned!(span=> #param: 'async_trait + #bounds));
+                } else {
+                    param.bounds.push(parse_quote!('async_trait));
+                }
             }
             GenericParam::Const(_) => {}
         }


Test patch:
diff --git a/tests/test.rs b/tests/test.rs
index 1772e75..0b6a322 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -1705,3 +1705,24 @@ pub mod issue283 {
         }
     }
 }
+
+// https://github.com/dtolnay/async-trait/issues/288
+pub mod issue288 {
+    use async_trait::async_trait;
+
+    #[async_trait]
+    pub trait Trait {
+        async fn f<#[cfg(any())] T: Send>(#[cfg(any())] t: T);
+        async fn g<#[cfg(all())] T: Send>(#[cfg(all())] t: T);
+    }
+
+    pub struct Struct;
+
+    #[async_trait]
+    impl Trait for Struct {
+        async fn f<#[cfg(any())] T: Send>(#[cfg(any())] t: T) {}
+        async fn g<#[cfg(all())] T: Send>(#[cfg(all())] t: T) {
+            let _ = t;
+        }
+    }
+}