Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lifetime bounding on generic parameters that have cfg #289

Merged
merged 2 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,29 @@ 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;
let span = match param.colon_token.take() {
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(_) => {}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}