Skip to content

Commit b4c19da

Browse files
committed
Improve spans of named lifetimes generated from elided lifetimes
1 parent 8206008 commit b4c19da

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn expand(input: &mut Item, is_local: bool) {
8080
}
8181
}
8282
Item::Impl(input) => {
83-
let mut lifetimes = CollectLifetimes::new("'impl", input.impl_token.span);
83+
let mut lifetimes = CollectLifetimes::new("'impl");
8484
lifetimes.visit_type_mut(&mut *input.self_ty);
8585
lifetimes.visit_path_mut(&mut input.trait_.as_mut().unwrap().1);
8686
let params = &input.generics.params;
@@ -166,7 +166,7 @@ fn transform_sig(
166166
ReturnType::Type(arrow, ret) => (*arrow, quote!(#ret)),
167167
};
168168

169-
let mut lifetimes = CollectLifetimes::new("'life", default_span);
169+
let mut lifetimes = CollectLifetimes::new("'life");
170170
for arg in sig.inputs.iter_mut() {
171171
match arg {
172172
FnArg::Receiver(arg) => lifetimes.visit_receiver_mut(arg),

src/lifetime.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,28 @@ use proc_macro2::{Span, TokenStream};
22
use std::mem;
33
use syn::visit_mut::{self, VisitMut};
44
use syn::{
5-
parse_quote_spanned, token, Expr, GenericArgument, Lifetime, Receiver, ReturnType, Type,
5+
parse_quote_spanned, token, Expr, GenericArgument, Lifetime, Receiver, ReturnType, Token, Type,
66
TypeBareFn, TypeImplTrait, TypeParen, TypePtr, TypeReference,
77
};
88

99
pub struct CollectLifetimes {
1010
pub elided: Vec<Lifetime>,
1111
pub explicit: Vec<Lifetime>,
1212
pub name: &'static str,
13-
pub default_span: Span,
1413
}
1514

1615
impl CollectLifetimes {
17-
pub fn new(name: &'static str, default_span: Span) -> Self {
16+
pub fn new(name: &'static str) -> Self {
1817
CollectLifetimes {
1918
elided: Vec::new(),
2019
explicit: Vec::new(),
2120
name,
22-
default_span,
2321
}
2422
}
2523

26-
fn visit_opt_lifetime(&mut self, lifetime: &mut Option<Lifetime>) {
24+
fn visit_opt_lifetime(&mut self, reference: Token![&], lifetime: &mut Option<Lifetime>) {
2725
match lifetime {
28-
None => *lifetime = Some(self.next_lifetime(None)),
26+
None => *lifetime = Some(self.next_lifetime(reference.span)),
2927
Some(lifetime) => self.visit_lifetime(lifetime),
3028
}
3129
}
@@ -38,9 +36,8 @@ impl CollectLifetimes {
3836
}
3937
}
4038

41-
fn next_lifetime<S: Into<Option<Span>>>(&mut self, span: S) -> Lifetime {
39+
fn next_lifetime(&mut self, span: Span) -> Lifetime {
4240
let name = format!("{}{}", self.name, self.elided.len());
43-
let span = span.into().unwrap_or(self.default_span);
4441
let life = Lifetime::new(&name, span);
4542
self.elided.push(life.clone());
4643
life
@@ -49,13 +46,13 @@ impl CollectLifetimes {
4946

5047
impl VisitMut for CollectLifetimes {
5148
fn visit_receiver_mut(&mut self, arg: &mut Receiver) {
52-
if let Some((_, lifetime)) = &mut arg.reference {
53-
self.visit_opt_lifetime(lifetime);
49+
if let Some((reference, lifetime)) = &mut arg.reference {
50+
self.visit_opt_lifetime(*reference, lifetime);
5451
}
5552
}
5653

5754
fn visit_type_reference_mut(&mut self, ty: &mut TypeReference) {
58-
self.visit_opt_lifetime(&mut ty.lifetime);
55+
self.visit_opt_lifetime(ty.and_token, &mut ty.lifetime);
5956
visit_mut::visit_type_reference_mut(self, ty);
6057
}
6158

tests/ui/lifetime-defined-here.stderr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ error: lifetime may not live long enough
22
--> tests/ui/lifetime-defined-here.rs:12:49
33
|
44
12 | async fn bar(&self, x: &str, y: &'_ str) -> &'static str {
5-
| ----- lifetime `'life0` defined here ^^^^^^^^^^^^ type annotation requires that `'life0` must outlive `'static`
5+
| - ^^^^^^^^^^^^ type annotation requires that `'life0` must outlive `'static`
6+
| |
7+
| lifetime `'life0` defined here
68

79
error: lifetime may not live long enough
810
--> tests/ui/lifetime-defined-here.rs:12:49
911
|
1012
12 | async fn bar(&self, x: &str, y: &'_ str) -> &'static str {
11-
| ----- lifetime `'life1` defined here ^^^^^^^^^^^^ type annotation requires that `'life1` must outlive `'static`
13+
| - ^^^^^^^^^^^^ type annotation requires that `'life1` must outlive `'static`
14+
| |
15+
| lifetime `'life1` defined here
1216

1317
error: lifetime may not live long enough
1418
--> tests/ui/lifetime-defined-here.rs:12:49

0 commit comments

Comments
 (0)