Debian Codegraph @

third_party/rust/async-trait/src/receiver.rs

raw
1 use proc_macro2::{Group, Span, TokenStream, TokenTree};
2 use std::iter::FromIterator;
3 use syn::visit_mut::{self, VisitMut};
4 use syn::{
5 Block, ExprPath, Ident, Item, Macro, Pat, PatIdent, Path, Receiver, Signature, Token, TypePath,
6 };
7
8 pub fn has_self_in_sig(sig: &mut Signature) -> bool {
9 let mut visitor = HasSelf(false);
10 visitor.visit_signature_mut(sig);
11 visitor.0
12 }
13
14 pub fn has_self_in_block(block: &mut Block) -> bool {
15 let mut visitor = HasSelf(false);
16 visitor.visit_block_mut(block);
17 visitor.0
18 }
19
20 fn has_self_in_token_stream(tokens: TokenStream) -> bool {
21 tokens.into_iter().any(|tt| match tt {
22 TokenTree::Ident(ident) => ident == "Self",
23 TokenTree::Group(group) => has_self_in_token_stream(group.stream()),
24 _ => false,
25 })
26 }
27
28 pub fn mut_pat(pat: &mut Pat) -> Option<Token![mut]> {
29 let mut visitor = HasMutPat(None);
30 visitor.visit_pat_mut(pat);
31 visitor.0
32 }
33
34 fn contains_fn(tokens: TokenStream) -> bool {
35 tokens.into_iter().any(|tt| match tt {
36 TokenTree::Ident(ident) => ident == "fn",
37 TokenTree::Group(group) => contains_fn(group.stream()),
38 _ => false,
39 })
40 }
41
42 struct HasMutPat(Option<Token![mut]>);
43
44 impl VisitMut for HasMutPat {
45 fn visit_pat_ident_mut(&mut self, i: &mut PatIdent) {
46 if let Some(m) = i.mutability {
47 self.0 = Some(m);
48 } else {
49 visit_mut::visit_pat_ident_mut(self, i);
50 }
51 }
52 }
53
54 struct HasSelf(bool);
55
56 impl VisitMut for HasSelf {
57 fn visit_expr_path_mut(&mut self, expr: &mut ExprPath) {
58 self.0 |= expr.path.segments[0].ident == "Self";
59 visit_mut::visit_expr_path_mut(self, expr);
60 }
61
62 fn visit_type_path_mut(&mut self, ty: &mut TypePath) {
63 self.0 |= ty.path.segments[0].ident == "Self";
64 visit_mut::visit_type_path_mut(self, ty);
65 }
66
67 fn visit_receiver_mut(&mut self, _arg: &mut Receiver) {
68 self.0 = true;
69 }
70
71 fn visit_item_mut(&mut self, _: &mut Item) {
72 // Do not recurse into nested items.
73 }
74
75 fn visit_macro_mut(&mut self, mac: &mut Macro) {
76 if !contains_fn(mac.tokens.clone()) {
77 self.0 |= has_self_in_token_stream(mac.tokens.clone());
78 }
79 }
80 }
81
82 pub struct ReplaceSelf(pub Span);
83
84 impl ReplaceSelf {
85 #[cfg_attr(not(self_span_hack), allow(clippy::unused_self))]
86 fn prepend_underscore_to_self(&self, ident: &mut Ident) -> bool {
87 let modified = ident == "self";
88 if modified {
89 *ident = Ident::new("__self", ident.span());
90 #[cfg(self_span_hack)]
91 ident.set_span(self.0);
92 }
93 modified
94 }
95
96 fn visit_token_stream(&mut self, tokens: &mut TokenStream) -> bool {
97 let mut out = Vec::new();
98 let mut modified = false;
99 visit_token_stream_impl(self, tokens.clone(), &mut modified, &mut out);
100 if modified {
101 *tokens = TokenStream::from_iter(out);
102 }
103 return modified;
104
105 fn visit_token_stream_impl(
106 visitor: &mut ReplaceSelf,
107 tokens: TokenStream,
108 modified: &mut bool,
109 out: &mut Vec<TokenTree>,
110 ) {
111 for tt in tokens {
112 match tt {
113 TokenTree::Ident(mut ident) => {
114 *modified |= visitor.prepend_underscore_to_self(&mut ident);
115 out.push(TokenTree::Ident(ident));
116 }
117 TokenTree::Group(group) => {
118 let mut content = group.stream();
119 *modified |= visitor.visit_token_stream(&mut content);
120 let mut new = Group::new(group.delimiter(), content);
121 new.set_span(group.span());
122 out.push(TokenTree::Group(new));
123 }
124 other => out.push(other),
125 }
126 }
127 }
128 }
129 }
130
131 impl VisitMut for ReplaceSelf {
132 fn visit_ident_mut(&mut self, i: &mut Ident) {
133 self.prepend_underscore_to_self(i);
134 }
135
136 fn visit_path_mut(&mut self, p: &mut Path) {
137 if p.segments.len() == 1 {
138 // Replace `self`, but not `self::function`.
139 self.visit_ident_mut(&mut p.segments[0].ident);
140 }
141 for segment in &mut p.segments {
142 self.visit_path_arguments_mut(&mut segment.arguments);
143 }
144 }
145
146 fn visit_item_mut(&mut self, i: &mut Item) {
147 // Visit `macro_rules!` because locally defined macros can refer to
148 // `self`.
149 //
150 // Visit `futures::select` and similar select macros, which commonly
151 // appear syntactically like an item despite expanding to an expression.
152 //
153 // Otherwise, do not recurse into nested items.
154 if let Item::Macro(i) = i {
155 if i.mac.path.is_ident("macro_rules")
156 || i.mac.path.segments.last().unwrap().ident == "select"
157 {
158 self.visit_macro_mut(&mut i.mac);
159 }
160 }
161 }
162
163 fn visit_macro_mut(&mut self, mac: &mut Macro) {
164 // We can't tell in general whether `self` inside a macro invocation
165 // refers to the self in the argument list or a different self
166 // introduced within the macro. Heuristic: if the macro input contains
167 // `fn`, then `self` is more likely to refer to something other than the
168 // outer function's self argument.
169 if !contains_fn(mac.tokens.clone()) {
170 self.visit_token_stream(&mut mac.tokens);
171 }
172 }
173 }