-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
expand.rs
345 lines (323 loc) · 10.9 KB
/
expand.rs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use crate::lifetime::{has_async_lifetime, CollectLifetimes};
use crate::parse::Item;
use crate::receiver::{
mut_pat, has_self_in_block, has_self_in_sig, has_self_in_where_predicate, ReplaceReceiver,
ReplaceSelf,
};
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use std::mem;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::visit_mut::VisitMut;
use syn::{
parse_quote, Block, FnArg, GenericParam, Generics, Ident, ImplItem, Lifetime, Pat, PatIdent,
Path, Receiver, ReturnType, Signature, Stmt, Token, TraitItem, Type, TypeParam, TypeParamBound,
WhereClause,
};
impl ToTokens for Item {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Item::Trait(item) => item.to_tokens(tokens),
Item::Impl(item) => item.to_tokens(tokens),
}
}
}
#[derive(Clone, Copy)]
enum Context<'a> {
Trait {
name: &'a Ident,
generics: &'a Generics,
supertraits: &'a Supertraits,
},
Impl {
impl_generics: &'a Generics,
receiver: &'a Type,
as_trait: &'a Path,
},
}
impl Context<'_> {
fn lifetimes<'a>(&'a self, used: &'a [Lifetime]) -> impl Iterator<Item = &'a GenericParam> {
let generics = match self {
Context::Trait { generics, .. } => generics,
Context::Impl { impl_generics, .. } => impl_generics,
};
generics.params.iter().filter(move |param| {
if let GenericParam::Lifetime(param) = param {
used.contains(¶m.lifetime)
} else {
false
}
})
}
}
type Supertraits = Punctuated<TypeParamBound, Token![+]>;
pub fn expand(input: &mut Item, is_local: bool) {
match input {
Item::Trait(input) => {
let context = Context::Trait {
name: &input.ident,
generics: &input.generics,
supertraits: &input.supertraits,
};
for inner in &mut input.items {
if let TraitItem::Method(method) = inner {
let sig = &mut method.sig;
if sig.asyncness.is_some() {
let block = &mut method.default;
let mut has_self = has_self_in_sig(sig);
if let Some(block) = block {
has_self |= has_self_in_block(block);
transform_block(sig, block);
method
.attrs
.push(parse_quote!(#[allow(clippy::used_underscore_binding)]));
}
let has_default = method.default.is_some();
transform_sig(context, sig, has_self, has_default, is_local);
method.attrs.push(parse_quote!(#[must_use]));
}
}
}
}
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 context = Context::Impl {
impl_generics: &input.generics,
receiver: &input.self_ty,
as_trait: &input.trait_.as_ref().unwrap().1,
};
for inner in &mut input.items {
if let ImplItem::Method(method) = inner {
let sig = &mut method.sig;
if sig.asyncness.is_some() {
let block = &mut method.block;
let has_self = has_self_in_sig(sig) || has_self_in_block(block);
transform_block(sig, block);
transform_sig(context, sig, has_self, false, is_local);
method
.attrs
.push(parse_quote!(#[allow(clippy::used_underscore_binding)]));
}
}
}
}
}
}
// Input:
// async fn f<T>(&self, x: &T) -> Ret;
//
// Output:
// fn f<'life0, 'life1, 'async_trait, T>(
// &'life0 self,
// x: &'life1 T,
// ) -> Pin<Box<dyn Future<Output = Ret> + Send + 'async_trait>>
// where
// 'life0: 'async_trait,
// 'life1: 'async_trait,
// T: 'async_trait,
// Self: Sync + 'async_trait;
fn transform_sig(
context: Context,
sig: &mut Signature,
has_self: bool,
has_default: bool,
is_local: bool,
) {
sig.fn_token.span = sig.asyncness.take().unwrap().span;
let ret = match &sig.output {
ReturnType::Default => quote!(()),
ReturnType::Type(_, ret) => quote!(#ret),
};
let mut lifetimes = CollectLifetimes::new("'life");
for arg in sig.inputs.iter_mut() {
match arg {
FnArg::Receiver(arg) => lifetimes.visit_receiver_mut(arg),
FnArg::Typed(arg) => lifetimes.visit_type_mut(&mut arg.ty),
}
}
let where_clause = sig
.generics
.where_clause
.get_or_insert_with(|| WhereClause {
where_token: Default::default(),
predicates: Punctuated::new(),
});
for param in sig
.generics
.params
.iter()
.chain(context.lifetimes(&lifetimes.explicit))
{
match param {
GenericParam::Type(param) => {
let param = ¶m.ident;
where_clause
.predicates
.push(parse_quote!(#param: 'async_trait));
}
GenericParam::Lifetime(param) => {
let param = ¶m.lifetime;
where_clause
.predicates
.push(parse_quote!(#param: 'async_trait));
}
GenericParam::Const(_) => {}
}
}
for elided in lifetimes.elided {
sig.generics.params.push(parse_quote!(#elided));
where_clause
.predicates
.push(parse_quote!(#elided: 'async_trait));
}
sig.generics.params.push(parse_quote!('async_trait));
if has_self {
let bound: Ident = match sig.inputs.iter().next() {
Some(FnArg::Receiver(Receiver {
reference: Some(_),
mutability: None,
..
})) => parse_quote!(Sync),
Some(FnArg::Typed(arg))
if match (arg.pat.as_ref(), arg.ty.as_ref()) {
(Pat::Ident(pat), Type::Reference(ty)) => {
pat.ident == "self" && ty.mutability.is_none()
}
_ => false,
} =>
{
parse_quote!(Sync)
}
_ => parse_quote!(Send),
};
let assume_bound = match context {
Context::Trait { supertraits, .. } => !has_default || has_bound(supertraits, &bound),
Context::Impl { .. } => true,
};
where_clause.predicates.push(if assume_bound || is_local {
parse_quote!(Self: 'async_trait)
} else {
parse_quote!(Self: ::core::marker::#bound + 'async_trait)
});
}
for (i, arg) in sig.inputs.iter_mut().enumerate() {
match arg {
FnArg::Receiver(Receiver {
reference: Some(_), ..
}) => {}
FnArg::Receiver(arg) => arg.mutability = None,
FnArg::Typed(arg) => {
if let Pat::Ident(ident) = &mut *arg.pat {
ident.by_ref = None;
ident.mutability = None;
} else {
let span = arg.pat.span();
let positional = positional_arg(i, span);
let m = mut_pat(&mut arg.pat);
arg.pat = parse_quote!(#m #positional);
}
}
}
}
let bounds = if is_local {
quote!('async_trait)
} else {
quote!(::core::marker::Send + 'async_trait)
};
sig.output = parse_quote! {
-> ::core::pin::Pin<Box<
dyn ::core::future::Future<Output = #ret> + #bounds
>>
};
}
// Input:
// async fn f<T>(&self, x: &T, (a, b): (A, B)) -> Ret {
// self + x + a + b
// }
//
// Output:
// Box::pin(async move {
// let ___ret: Ret = {
// let __self = self;
// let x = x;
// let (a, b) = __arg1;
//
// __self + x + a + b
// };
//
// ___ret
// })
fn transform_block(
sig: &mut Signature,
block: &mut Block,
) {
if let Some(Stmt::Item(syn::Item::Verbatim(item))) = block.stmts.first() {
if block.stmts.len() == 1 && item.to_string() == ";" {
return;
}
}
let self_prefix = "__";
let mut self_span = None;
let decls = sig.inputs.iter().enumerate().map(|(i, arg)| match arg {
FnArg::Receiver(Receiver { self_token, mutability, .. }) => {
let mut ident = format_ident!("{}self", self_prefix);
ident.set_span(self_token.span());
self_span = Some(self_token.span());
quote!(let #mutability #ident = #self_token;)
}
FnArg::Typed(arg) => {
if let Pat::Ident(PatIdent { ident, mutability, .. }) = &*arg.pat {
if ident == "self" {
self_span = Some(ident.span());
let prefixed = format_ident!("{}{}", self_prefix, ident);
quote!(let #mutability #prefixed = #ident;)
} else {
quote!(let #mutability #ident = #ident;)
}
} else {
let pat = &arg.pat;
let ident = positional_arg(i, pat.span());
quote!(let #pat = #ident;)
}
}
}).collect::<Vec<_>>();
if let Some(span) = self_span {
let mut replace_self = ReplaceSelf(self_prefix, span);
replace_self.visit_block_mut(block);
}
let stmts = &block.stmts;
let ret_ty = match &sig.output {
ReturnType::Default => quote_spanned!(block.span()=>()),
ReturnType::Type(_, ret) => quote!(#ret),
};
let box_pin = quote_spanned!(ret_ty.span()=>
Box::pin(async move {
let __ret: #ret_ty = {
#(#decls)*
#(#stmts)*
};
#[allow(unreachable_code)]
__ret
})
);
block.stmts = parse_quote!(#box_pin);
}
fn positional_arg(i: usize, span: Span) -> Ident {
format_ident!("__arg{}", i, span = span)
}
fn has_bound(supertraits: &Supertraits, marker: &Ident) -> bool {
for bound in supertraits {
if let TypeParamBound::Trait(bound) = bound {
if bound.path.is_ident(marker) {
return true;
}
}
}
false
}