From f81548d92652cb477d8dabde87f7310412675aac Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 3 Mar 2023 21:30:54 -0800 Subject: [PATCH 1/2] Add regression test for issue 238 Currently fails: error: lifetime parameter `'impl0` only used once --> tests/test.rs:1603:20 | 1603 | impl Trait for &Struct { | ^ | | | this lifetime... | ...is used only here | note: the lint level is defined here --> tests/test.rs:1591:13 | 1591 | #![deny(single_use_lifetimes)] | ^^^^^^^^^^^^^^^^^^^^ help: elide the single-use lifetime | 1602 ~ 1603 ~ impl Trait for Struct { | --- tests/test.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test.rs b/tests/test.rs index 102f97e..4fc46f0 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1585,3 +1585,22 @@ pub mod issue236 { } } } + +// https://github.com/dtolnay/async-trait/issues/238 +pub mod issue238 { + #![deny(single_use_lifetimes)] + + use async_trait::async_trait; + + #[async_trait] + pub trait Trait { + async fn f(); + } + + pub struct Struct; + + #[async_trait] + impl Trait for &Struct { + async fn f() {} + } +} From 5883ac897efdc706cc5a256a0fa4aa3b108f1dd9 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 3 Mar 2023 22:49:19 -0800 Subject: [PATCH 2/2] Delete replacement of elided lifetimes in impl heading --- src/expand.rs | 9 +-------- src/lifetime.rs | 6 ++---- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/expand.rs b/src/expand.rs index 88338db..2499177 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -80,13 +80,6 @@ pub fn expand(input: &mut Item, is_local: bool) { } } Item::Impl(input) => { - let mut lifetimes = CollectLifetimes::new("'impl"); - lifetimes.visit_type_mut(&mut *input.self_ty); - lifetimes.visit_path_mut(&mut input.trait_.as_mut().unwrap().1); - let params = &input.generics.params; - let elided = lifetimes.elided; - input.generics.params = parse_quote!(#(#elided,)* #params); - let mut associated_type_impl_traits = Set::new(); for inner in &input.items { if let ImplItem::Type(assoc) = inner { @@ -167,7 +160,7 @@ fn transform_sig( ReturnType::Type(arrow, ret) => (*arrow, quote!(#ret)), }; - let mut lifetimes = CollectLifetimes::new("'life"); + let mut lifetimes = CollectLifetimes::new(); for arg in sig.inputs.iter_mut() { match arg { FnArg::Receiver(arg) => lifetimes.visit_receiver_mut(arg), diff --git a/src/lifetime.rs b/src/lifetime.rs index 86eac24..1e5a658 100644 --- a/src/lifetime.rs +++ b/src/lifetime.rs @@ -9,15 +9,13 @@ use syn::{ pub struct CollectLifetimes { pub elided: Vec, pub explicit: Vec, - pub name: &'static str, } impl CollectLifetimes { - pub fn new(name: &'static str) -> Self { + pub fn new() -> Self { CollectLifetimes { elided: Vec::new(), explicit: Vec::new(), - name, } } @@ -37,7 +35,7 @@ impl CollectLifetimes { } fn next_lifetime(&mut self, span: Span) -> Lifetime { - let name = format!("{}{}", self.name, self.elided.len()); + let name = format!("'life{}", self.elided.len()); let life = Lifetime::new(&name, span); self.elided.push(life.clone()); life