1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use super::*;
pub(in crate)
fn collect_lifetime_params (
sig: &'_ mut Signature,
outer_scope: Option<&'_ mut ItemImpl>,
) -> Vec<Lifetime>
{
let mut lifetimes: Vec<Lifetime> =
sig .generics
.lifetimes()
.map(|lt_def| lt_def.lifetime.clone())
.collect()
;
let mut visitor = Visitor {
lifetimes: &mut lifetimes,
next_idx: 0 ..,
params: &mut sig.generics.params,
};
sig.inputs.iter_mut().for_each(|arg| visitor.visit_fn_arg_mut(arg));
if let Some(item_impl) = outer_scope {
let mut impl_lifetimes: Vec<Lifetime> =
item_impl
.generics
.lifetimes()
.map(|lt_def| lt_def.lifetime.clone())
.collect()
;
let mut visitor = Visitor {
lifetimes: &mut impl_lifetimes,
next_idx: visitor.next_idx.next().unwrap() ..,
params: &mut item_impl.generics.params,
};
if let Some((_, ref mut trait_, _)) = item_impl.trait_ {
visitor.visit_path_mut(trait_);
}
visitor.visit_type_mut(&mut *item_impl.self_ty);
impl_lifetimes.extend(lifetimes);
impl_lifetimes
} else {
lifetimes
}
}
struct Visitor<'__> {
lifetimes: &'__ mut Vec<Lifetime>,
next_idx: ::core::ops::RangeFrom<u16>,
params: &'__ mut Punctuated<GenericParam, Token![,]>,
}
impl Visitor<'_> {
fn new_lifetime_param (self: &'_ mut Self, span: Span)
-> Lifetime
{
let mut lifetime;
while {
lifetime = Lifetime::new(
&format!("'_{}", self.next_idx.next().unwrap()),
span,
);
self.lifetimes.contains(&lifetime)
} {}
self.params.insert(self.lifetimes.len(), parse_quote!(#lifetime));
self.lifetimes.push(lifetime.clone());
lifetime
}
}
impl VisitMut for Visitor<'_> {
fn visit_receiver_mut (
self: &'_ mut Self,
receiver: &'_ mut Receiver,
)
{
match receiver.reference {
| Some((ampersand, ref mut elided @ None)) => {
*elided = Some(self.new_lifetime_param(ampersand.span()))
}
| Some((_ampersand, Some(ref mut elided)))
if elided.ident == "_"
=> {
*elided = self.new_lifetime_param(elided.span());
},
| _ => {},
}
}
fn visit_type_reference_mut (
self: &'_ mut Self,
reference: &'_ mut TypeReference,
)
{
visit_mut::visit_type_reference_mut(self, reference);
match reference.lifetime {
| ref mut elided @ None => {
*elided = Some(self.new_lifetime_param(reference.and_token.span()));
},
| Some(ref mut elided)
if elided.ident == "_"
=> {
*elided = self.new_lifetime_param(elided.span());
},
| _ => {},
}
}
fn visit_lifetime_mut (
self: &'_ mut Self,
lifetime: &'_ mut Lifetime,
)
{
if lifetime.ident == "_" {
*lifetime = self.new_lifetime_param(lifetime.span());
}
}
fn visit_path_arguments_mut (
self: &'_ mut Self,
path_arguments: &'_ mut PathArguments,
)
{
if let PathArguments::Parenthesized(_) = path_arguments {
return;
}
visit_mut::visit_path_arguments_mut(self, path_arguments);
}
fn visit_type_trait_object_mut (
self: &'_ mut Self,
dyn_trait: &mut TypeTraitObject,
)
{
visit_mut::visit_type_trait_object_mut(self, dyn_trait);
if dyn_trait
.bounds
.iter()
.any(|bound| match *bound {
| TypeParamBound::Trait(_) => false,
| TypeParamBound::Lifetime(_) => true,
})
.not()
{
let lt = self.new_lifetime_param(dyn_trait.span());
dyn_trait.bounds.push(TypeParamBound::Lifetime(lt));
}
}
}