|
| 1 | +use super::respan::respan; |
| 2 | +use proc_macro2::{Group, Spacing, Span, TokenStream, TokenTree}; |
| 3 | +use quote::{quote, quote_spanned}; |
| 4 | +use std::{iter::FromIterator, mem}; |
| 5 | +use syn::{ |
| 6 | + parse_quote, |
| 7 | + punctuated::Punctuated, |
| 8 | + visit_mut::{self, VisitMut}, |
| 9 | + DeriveInput, ExprPath, Macro, Path, PathArguments, QSelf, Type, TypePath, |
| 10 | +}; |
| 11 | + |
| 12 | +pub fn replace_receiver(input: &mut DeriveInput) { |
| 13 | + let self_ty = { |
| 14 | + let ident = &input.ident; |
| 15 | + let ty_generics = input.generics.split_for_impl().1; |
| 16 | + parse_quote!(#ident #ty_generics) |
| 17 | + }; |
| 18 | + let mut visitor = ReplaceReceiver(&self_ty); |
| 19 | + visitor.visit_generics_mut(&mut input.generics); |
| 20 | + visitor.visit_data_mut(&mut input.data); |
| 21 | +} |
| 22 | + |
| 23 | +struct ReplaceReceiver<'a>(&'a TypePath); |
| 24 | + |
| 25 | +impl ReplaceReceiver<'_> { |
| 26 | + fn self_ty(&self, span: Span) -> TypePath { |
| 27 | + respan(self.0, span) |
| 28 | + } |
| 29 | + |
| 30 | + fn self_to_qself(&self, qself: &mut Option<QSelf>, path: &mut Path) { |
| 31 | + if path.leading_colon.is_some() { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + // Make borrow checker happy |
| 36 | + { |
| 37 | + let first = &path.segments[0]; |
| 38 | + if first.ident != "Self" || !first.arguments.is_empty() { |
| 39 | + return; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if path.segments.len() == 1 { |
| 44 | + self.self_to_expr_path(path); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + let span = path.segments[0].ident.span(); |
| 49 | + *qself = Some(QSelf { |
| 50 | + lt_token: Token, |
| 51 | + ty: Box::new(self.self_ty(span).into()), |
| 52 | + position: 0, |
| 53 | + as_token: None, |
| 54 | + gt_token: Token, |
| 55 | + }); |
| 56 | + |
| 57 | + path.leading_colon = Some(**path.segments.pairs().next().unwrap().punct().unwrap()); |
| 58 | + |
| 59 | + let segments = mem::replace(&mut path.segments, Punctuated::new()); |
| 60 | + path.segments = segments.into_pairs().skip(1).collect(); |
| 61 | + } |
| 62 | + |
| 63 | + fn self_to_expr_path(&self, path: &mut Path) { |
| 64 | + if path.leading_colon.is_some() { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Make borrow checker happy |
| 69 | + { |
| 70 | + let first = &path.segments[0]; |
| 71 | + if first.ident != "Self" || !first.arguments.is_empty() { |
| 72 | + return; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + let self_ty = self.self_ty(path.segments[0].ident.span()); |
| 77 | + let variant = mem::replace(path, self_ty.path); |
| 78 | + for segment in &mut path.segments { |
| 79 | + if let PathArguments::AngleBracketed(bracketed) = &mut segment.arguments { |
| 80 | + if bracketed.colon2_token.is_none() && !bracketed.args.is_empty() { |
| 81 | + bracketed.colon2_token = Some(<Token![::]>::default()); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + if variant.segments.len() > 1 { |
| 86 | + path.segments.push_punct(<Token![::]>::default()); |
| 87 | + path.segments.extend(variant.segments.into_pairs().skip(1)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + fn visit_token_stream(&self, tokens: &mut TokenStream) -> bool { |
| 92 | + let mut out = Vec::new(); |
| 93 | + let mut modified = false; |
| 94 | + let mut iter = tokens.clone().into_iter().peekable(); |
| 95 | + while let Some(tt) = iter.next() { |
| 96 | + match tt { |
| 97 | + TokenTree::Ident(ident) => { |
| 98 | + if ident == "Self" { |
| 99 | + modified = true; |
| 100 | + let self_ty = self.self_ty(ident.span()); |
| 101 | + match iter.peek() { |
| 102 | + Some(TokenTree::Punct(p)) |
| 103 | + if p.as_char() == ':' && p.spacing() == Spacing::Joint => {} |
| 104 | + _ => { |
| 105 | + out.extend(quote!(#self_ty)); |
| 106 | + continue; |
| 107 | + } |
| 108 | + } |
| 109 | + let next = iter.next().unwrap(); |
| 110 | + match iter.peek() { |
| 111 | + Some(TokenTree::Punct(p)) if p.as_char() == ':' => { |
| 112 | + let span = ident.span(); |
| 113 | + out.extend(quote_spanned!(span=> <#self_ty>)); |
| 114 | + } |
| 115 | + _ => out.extend(quote!(#self_ty)), |
| 116 | + } |
| 117 | + out.push(next); |
| 118 | + } else { |
| 119 | + out.push(TokenTree::Ident(ident)); |
| 120 | + } |
| 121 | + } |
| 122 | + TokenTree::Group(group) => { |
| 123 | + let mut content = group.stream(); |
| 124 | + modified |= self.visit_token_stream(&mut content); |
| 125 | + let mut new = Group::new(group.delimiter(), content); |
| 126 | + new.set_span(group.span()); |
| 127 | + out.push(TokenTree::Group(new)); |
| 128 | + } |
| 129 | + other => out.push(other), |
| 130 | + } |
| 131 | + } |
| 132 | + if modified { |
| 133 | + *tokens = TokenStream::from_iter(out); |
| 134 | + } |
| 135 | + modified |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl VisitMut for ReplaceReceiver<'_> { |
| 140 | + // `Self` -> `Receiver` |
| 141 | + fn visit_type_mut(&mut self, ty: &mut Type) { |
| 142 | + let span = if let Type::Path(node) = ty { |
| 143 | + if node.qself.is_none() && node.path.is_ident("Self") { |
| 144 | + node.path.segments[0].ident.span() |
| 145 | + } else { |
| 146 | + self.visit_type_path_mut(node); |
| 147 | + return; |
| 148 | + } |
| 149 | + } else { |
| 150 | + visit_mut::visit_type_mut(self, ty); |
| 151 | + return; |
| 152 | + }; |
| 153 | + *ty = self.self_ty(span).into(); |
| 154 | + } |
| 155 | + |
| 156 | + // `Self::Assoc` -> `<Receiver>::Assoc` |
| 157 | + fn visit_type_path_mut(&mut self, ty: &mut TypePath) { |
| 158 | + if ty.qself.is_none() { |
| 159 | + self.self_to_qself(&mut ty.qself, &mut ty.path); |
| 160 | + } |
| 161 | + visit_mut::visit_type_path_mut(self, ty); |
| 162 | + } |
| 163 | + |
| 164 | + // `Self::method` -> `<Receiver>::method` |
| 165 | + fn visit_expr_path_mut(&mut self, expr: &mut ExprPath) { |
| 166 | + if expr.qself.is_none() { |
| 167 | + self.self_to_qself(&mut expr.qself, &mut expr.path); |
| 168 | + } |
| 169 | + visit_mut::visit_expr_path_mut(self, expr); |
| 170 | + } |
| 171 | + |
| 172 | + fn visit_macro_mut(&mut self, mac: &mut Macro) { |
| 173 | + // We can't tell in general whether `self` inside a macro invocation |
| 174 | + // refers to the self in the argument list or a different self |
| 175 | + // introduced within the macro. Heuristic: if the macro input contains |
| 176 | + // `fn`, then `self` is more likely to refer to something other than the |
| 177 | + // outer function's self argument. |
| 178 | + if !contains_fn(mac.tokens.clone()) { |
| 179 | + self.visit_token_stream(&mut mac.tokens); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +fn contains_fn(tokens: TokenStream) -> bool { |
| 185 | + tokens.into_iter().any(|tt| match tt { |
| 186 | + TokenTree::Ident(ident) => ident == "fn", |
| 187 | + TokenTree::Group(group) => contains_fn(group.stream()), |
| 188 | + _ => false, |
| 189 | + }) |
| 190 | +} |
0 commit comments