1 use std::{iter::FromIterator, mem};
2
3 use proc_macro2::{Group, Spacing, Span, TokenStream, TokenTree};
4 use quote::{quote, quote_spanned, ToTokens};
5 use syn::{
6 parse::{Parse, ParseBuffer, ParseStream},
7 parse_quote,
8 punctuated::Punctuated,
9 token,
10 visit_mut::{self, VisitMut},
11 Attribute, ExprPath, ExprStruct, Generics, Ident, Item, Lifetime, LifetimeDef, Macro, PatPath,
12 PatStruct, PatTupleStruct, Path, PathArguments, PredicateType, QSelf, Result, Token, Type,
13 TypeParamBound, TypePath, Variant, Visibility, WherePredicate,
14 };
15
16 pub(crate) type Variants = Punctuated<Variant, Token![,]>;
17
18 macro_rules! error {
19 ($span:expr, $msg:expr) => {
20 syn::Error::new_spanned(&$span, $msg)
21 };
22 ($span:expr, $($tt:tt)*) => {
23 error!($span, format!($($tt)*))
24 };
25 }
26
27 macro_rules! parse_quote_spanned {
28 ($span:expr => $($tt:tt)*) => {
29 syn::parse2(quote::quote_spanned!($span => $($tt)*)).unwrap_or_else(|e| panic!("{}", e))
30 };
31 }
32
33 /// Determines the lifetime names. Ensure it doesn't overlap with any existing
34 /// lifetime names.
determine_lifetime_name(lifetime_name: &mut String, generics: &mut Generics)35 pub(crate) fn determine_lifetime_name(lifetime_name: &mut String, generics: &mut Generics) {
36 struct CollectLifetimes(Vec<String>);
37
38 impl VisitMut for CollectLifetimes {
39 fn visit_lifetime_def_mut(&mut self, def: &mut LifetimeDef) {
40 self.0.push(def.lifetime.to_string());
41 }
42 }
43
44 debug_assert!(lifetime_name.starts_with('\''));
45
46 let mut lifetimes = CollectLifetimes(Vec::new());
47 lifetimes.visit_generics_mut(generics);
48
49 while lifetimes.0.iter().any(|name| name.starts_with(&**lifetime_name)) {
50 lifetime_name.push('_');
51 }
52 }
53
54 /// Like `insert_lifetime`, but also generates a bound of the form
55 /// `OriginalType<A, B>: 'lifetime`. Used when generating the definition
56 /// of a projection type
insert_lifetime_and_bound( generics: &mut Generics, lifetime: Lifetime, orig_generics: &Generics, orig_ident: &Ident, ) -> WherePredicate57 pub(crate) fn insert_lifetime_and_bound(
58 generics: &mut Generics,
59 lifetime: Lifetime,
60 orig_generics: &Generics,
61 orig_ident: &Ident,
62 ) -> WherePredicate {
63 insert_lifetime(generics, lifetime.clone());
64
65 let orig_type: Type = parse_quote!(#orig_ident #orig_generics);
66 let mut punct = Punctuated::new();
67 punct.push(TypeParamBound::Lifetime(lifetime));
68
69 WherePredicate::Type(PredicateType {
70 lifetimes: None,
71 bounded_ty: orig_type,
72 colon_token: <Token![:]>::default(),
73 bounds: punct,
74 })
75 }
76
77 /// Inserts a `lifetime` at position `0` of `generics.params`.
insert_lifetime(generics: &mut Generics, lifetime: Lifetime)78 pub(crate) fn insert_lifetime(generics: &mut Generics, lifetime: Lifetime) {
79 generics.lt_token.get_or_insert_with(<Token![<]>::default);
80 generics.gt_token.get_or_insert_with(<Token![>]>::default);
81 generics.params.insert(0, LifetimeDef::new(lifetime).into());
82 }
83
84 /// Determines the visibility of the projected types and projection methods.
85 ///
86 /// If given visibility is `pub`, returned visibility is `pub(crate)`.
87 /// Otherwise, returned visibility is the same as given visibility.
determine_visibility(vis: &Visibility) -> Visibility88 pub(crate) fn determine_visibility(vis: &Visibility) -> Visibility {
89 if let Visibility::Public(token) = vis {
90 parse_quote_spanned!(token.pub_token.span => pub(crate))
91 } else {
92 vis.clone()
93 }
94 }
95
96 /// Checks if `tokens` is an empty `TokenStream`.
97 ///
98 /// This is almost equivalent to `syn::parse2::<Nothing>()`, but produces
99 /// a better error message and does not require ownership of `tokens`.
parse_as_empty(tokens: &TokenStream) -> Result<()>100 pub(crate) fn parse_as_empty(tokens: &TokenStream) -> Result<()> {
101 if tokens.is_empty() { Ok(()) } else { Err(error!(tokens, "unexpected token: {}", tokens)) }
102 }
103
respan<T>(node: &T, span: Span) -> T where T: ToTokens + Parse,104 pub(crate) fn respan<T>(node: &T, span: Span) -> T
105 where
106 T: ToTokens + Parse,
107 {
108 let tokens = node.to_token_stream();
109 let respanned = respan_tokens(tokens, span);
110 syn::parse2(respanned).unwrap()
111 }
112
respan_tokens(tokens: TokenStream, span: Span) -> TokenStream113 fn respan_tokens(tokens: TokenStream, span: Span) -> TokenStream {
114 tokens
115 .into_iter()
116 .map(|mut token| {
117 token.set_span(span);
118 token
119 })
120 .collect()
121 }
122
123 // =================================================================================================
124 // extension traits
125
126 pub(crate) trait SliceExt {
position_exact(&self, ident: &str) -> Result<Option<usize>>127 fn position_exact(&self, ident: &str) -> Result<Option<usize>>;
find(&self, ident: &str) -> Option<&Attribute>128 fn find(&self, ident: &str) -> Option<&Attribute>;
129 }
130
131 impl SliceExt for [Attribute] {
132 /// # Errors
133 ///
134 /// * There are multiple specified attributes.
135 /// * The `Attribute::tokens` field of the specified attribute is not empty.
position_exact(&self, ident: &str) -> Result<Option<usize>>136 fn position_exact(&self, ident: &str) -> Result<Option<usize>> {
137 self.iter()
138 .try_fold((0, None), |(i, mut prev), attr| {
139 if attr.path.is_ident(ident) {
140 if prev.replace(i).is_some() {
141 return Err(error!(attr, "duplicate #[{}] attribute", ident));
142 }
143 parse_as_empty(&attr.tokens)?;
144 }
145 Ok((i + 1, prev))
146 })
147 .map(|(_, pos)| pos)
148 }
149
find(&self, ident: &str) -> Option<&Attribute>150 fn find(&self, ident: &str) -> Option<&Attribute> {
151 self.iter().position(|attr| attr.path.is_ident(ident)).map(|i| &self[i])
152 }
153 }
154
155 pub(crate) trait ParseBufferExt<'a> {
parenthesized(self) -> Result<ParseBuffer<'a>>156 fn parenthesized(self) -> Result<ParseBuffer<'a>>;
157 }
158
159 impl<'a> ParseBufferExt<'a> for ParseStream<'a> {
parenthesized(self) -> Result<ParseBuffer<'a>>160 fn parenthesized(self) -> Result<ParseBuffer<'a>> {
161 let content;
162 let _: token::Paren = syn::parenthesized!(content in self);
163 Ok(content)
164 }
165 }
166
167 impl<'a> ParseBufferExt<'a> for ParseBuffer<'a> {
parenthesized(self) -> Result<ParseBuffer<'a>>168 fn parenthesized(self) -> Result<ParseBuffer<'a>> {
169 let content;
170 let _: token::Paren = syn::parenthesized!(content in self);
171 Ok(content)
172 }
173 }
174
175 // =================================================================================================
176 // visitors
177
178 // Replace `self`/`Self` with `__self`/`self_ty`.
179 // Based on https://github.com/dtolnay/async-trait/blob/0.1.35/src/receiver.rs
180
181 pub(crate) struct ReplaceReceiver<'a>(pub(crate) &'a TypePath);
182
183 impl ReplaceReceiver<'_> {
self_ty(&self, span: Span) -> TypePath184 fn self_ty(&self, span: Span) -> TypePath {
185 respan(self.0, span)
186 }
187
self_to_qself(&self, qself: &mut Option<QSelf>, path: &mut Path)188 fn self_to_qself(&self, qself: &mut Option<QSelf>, path: &mut Path) {
189 if path.leading_colon.is_some() {
190 return;
191 }
192
193 let first = &path.segments[0];
194 if first.ident != "Self" || !first.arguments.is_empty() {
195 return;
196 }
197
198 if path.segments.len() == 1 {
199 self.self_to_expr_path(path);
200 return;
201 }
202
203 let span = first.ident.span();
204 *qself = Some(QSelf {
205 lt_token: Token![<](span),
206 ty: Box::new(self.self_ty(span).into()),
207 position: 0,
208 as_token: None,
209 gt_token: Token![>](span),
210 });
211
212 path.leading_colon = Some(**path.segments.pairs().next().unwrap().punct().unwrap());
213
214 let segments = mem::replace(&mut path.segments, Punctuated::new());
215 path.segments = segments.into_pairs().skip(1).collect();
216 }
217
self_to_expr_path(&self, path: &mut Path)218 fn self_to_expr_path(&self, path: &mut Path) {
219 if path.leading_colon.is_some() {
220 return;
221 }
222
223 let first = &path.segments[0];
224 if first.ident != "Self" || !first.arguments.is_empty() {
225 return;
226 }
227
228 let self_ty = self.self_ty(first.ident.span());
229 let variant = mem::replace(path, self_ty.path);
230 for segment in &mut path.segments {
231 if let PathArguments::AngleBracketed(bracketed) = &mut segment.arguments {
232 if bracketed.colon2_token.is_none() && !bracketed.args.is_empty() {
233 bracketed.colon2_token = Some(<Token![::]>::default());
234 }
235 }
236 }
237 if variant.segments.len() > 1 {
238 path.segments.push_punct(<Token![::]>::default());
239 path.segments.extend(variant.segments.into_pairs().skip(1));
240 }
241 }
242
visit_token_stream(&self, tokens: &mut TokenStream) -> bool243 fn visit_token_stream(&self, tokens: &mut TokenStream) -> bool {
244 let mut out = Vec::new();
245 let mut modified = false;
246 let mut iter = tokens.clone().into_iter().peekable();
247 while let Some(tt) = iter.next() {
248 match tt {
249 TokenTree::Ident(mut ident) => {
250 modified |= prepend_underscore_to_self(&mut ident);
251 if ident == "Self" {
252 modified = true;
253 let self_ty = self.self_ty(ident.span());
254 match iter.peek() {
255 Some(TokenTree::Punct(p))
256 if p.as_char() == ':' && p.spacing() == Spacing::Joint =>
257 {
258 let next = iter.next().unwrap();
259 match iter.peek() {
260 Some(TokenTree::Punct(p)) if p.as_char() == ':' => {
261 let span = ident.span();
262 out.extend(quote_spanned!(span=> <#self_ty>))
263 }
264 _ => out.extend(quote!(#self_ty)),
265 }
266 out.push(next);
267 }
268 _ => out.extend(quote!(#self_ty)),
269 }
270 } else {
271 out.push(TokenTree::Ident(ident));
272 }
273 }
274 TokenTree::Group(group) => {
275 let mut content = group.stream();
276 modified |= self.visit_token_stream(&mut content);
277 let mut new = Group::new(group.delimiter(), content);
278 new.set_span(group.span());
279 out.push(TokenTree::Group(new));
280 }
281 other => out.push(other),
282 }
283 }
284 if modified {
285 *tokens = TokenStream::from_iter(out);
286 }
287 modified
288 }
289 }
290
291 impl VisitMut for ReplaceReceiver<'_> {
292 // `Self` -> `Receiver`
visit_type_mut(&mut self, ty: &mut Type)293 fn visit_type_mut(&mut self, ty: &mut Type) {
294 if let Type::Path(node) = ty {
295 if node.qself.is_none() && node.path.is_ident("Self") {
296 *ty = self.self_ty(node.path.segments[0].ident.span()).into();
297 } else {
298 self.visit_type_path_mut(node);
299 }
300 } else {
301 visit_mut::visit_type_mut(self, ty);
302 }
303 }
304
305 // `Self::Assoc` -> `<Receiver>::Assoc`
visit_type_path_mut(&mut self, ty: &mut TypePath)306 fn visit_type_path_mut(&mut self, ty: &mut TypePath) {
307 if ty.qself.is_none() {
308 self.self_to_qself(&mut ty.qself, &mut ty.path);
309 }
310 visit_mut::visit_type_path_mut(self, ty);
311 }
312
313 // `Self::method` -> `<Receiver>::method`
visit_expr_path_mut(&mut self, expr: &mut ExprPath)314 fn visit_expr_path_mut(&mut self, expr: &mut ExprPath) {
315 if expr.qself.is_none() {
316 prepend_underscore_to_self(&mut expr.path.segments[0].ident);
317 self.self_to_qself(&mut expr.qself, &mut expr.path);
318 }
319 visit_mut::visit_expr_path_mut(self, expr);
320 }
321
visit_expr_struct_mut(&mut self, expr: &mut ExprStruct)322 fn visit_expr_struct_mut(&mut self, expr: &mut ExprStruct) {
323 self.self_to_expr_path(&mut expr.path);
324 visit_mut::visit_expr_struct_mut(self, expr);
325 }
326
visit_pat_path_mut(&mut self, pat: &mut PatPath)327 fn visit_pat_path_mut(&mut self, pat: &mut PatPath) {
328 if pat.qself.is_none() {
329 self.self_to_qself(&mut pat.qself, &mut pat.path);
330 }
331 visit_mut::visit_pat_path_mut(self, pat);
332 }
333
visit_pat_struct_mut(&mut self, pat: &mut PatStruct)334 fn visit_pat_struct_mut(&mut self, pat: &mut PatStruct) {
335 self.self_to_expr_path(&mut pat.path);
336 visit_mut::visit_pat_struct_mut(self, pat);
337 }
338
visit_pat_tuple_struct_mut(&mut self, pat: &mut PatTupleStruct)339 fn visit_pat_tuple_struct_mut(&mut self, pat: &mut PatTupleStruct) {
340 self.self_to_expr_path(&mut pat.path);
341 visit_mut::visit_pat_tuple_struct_mut(self, pat);
342 }
343
visit_item_mut(&mut self, item: &mut Item)344 fn visit_item_mut(&mut self, item: &mut Item) {
345 match item {
346 // Visit `macro_rules!` because locally defined macros can refer to `self`.
347 Item::Macro(item) if item.mac.path.is_ident("macro_rules") => {
348 self.visit_macro_mut(&mut item.mac)
349 }
350 // Otherwise, do not recurse into nested items.
351 _ => {}
352 }
353 }
354
visit_macro_mut(&mut self, mac: &mut Macro)355 fn visit_macro_mut(&mut self, mac: &mut Macro) {
356 // We can't tell in general whether `self` inside a macro invocation
357 // refers to the self in the argument list or a different self
358 // introduced within the macro. Heuristic: if the macro input contains
359 // `fn`, then `self` is more likely to refer to something other than the
360 // outer function's self argument.
361 if !contains_fn(mac.tokens.clone()) {
362 self.visit_token_stream(&mut mac.tokens);
363 }
364 }
365 }
366
contains_fn(tokens: TokenStream) -> bool367 fn contains_fn(tokens: TokenStream) -> bool {
368 tokens.into_iter().any(|tt| match tt {
369 TokenTree::Ident(ident) => ident == "fn",
370 TokenTree::Group(group) => contains_fn(group.stream()),
371 _ => false,
372 })
373 }
374
prepend_underscore_to_self(ident: &mut Ident) -> bool375 pub(crate) fn prepend_underscore_to_self(ident: &mut Ident) -> bool {
376 let modified = ident == "self";
377 if modified {
378 *ident = Ident::new("__self", ident.span());
379 }
380 modified
381 }
382