@@ -3,8 +3,10 @@ use proc_macro2::Span;
33use quote:: ToTokens ;
44use std:: mem;
55use syn:: punctuated:: Punctuated ;
6- use syn:: visit_mut:: { self , VisitMut } ;
7- use syn:: { parse_quote, DeriveInput , ExprPath , Path , PathArguments , QSelf , Type , TypePath } ;
6+ use syn:: {
7+ parse_quote, Data , DeriveInput , Expr , ExprPath , GenericArgument , GenericParam , Generics , Macro ,
8+ Path , PathArguments , QSelf , ReturnType , Type , TypeParamBound , TypePath , WherePredicate ,
9+ } ;
810
911pub fn replace_receiver ( input : & mut DeriveInput ) {
1012 let self_ty = {
@@ -68,7 +70,7 @@ impl ReplaceReceiver<'_> {
6870 }
6971}
7072
71- impl VisitMut for ReplaceReceiver < ' _ > {
73+ impl ReplaceReceiver < ' _ > {
7274 // `Self` -> `Receiver`
7375 fn visit_type_mut ( & mut self , ty : & mut Type ) {
7476 let span = if let Type :: Path ( node) = ty {
@@ -79,7 +81,7 @@ impl VisitMut for ReplaceReceiver<'_> {
7981 return ;
8082 }
8183 } else {
82- visit_mut :: visit_type_mut ( self , ty) ;
84+ self . visit_type_mut_impl ( ty) ;
8385 return ;
8486 } ;
8587 * ty = self . self_ty ( span) . into ( ) ;
@@ -90,14 +92,196 @@ impl VisitMut for ReplaceReceiver<'_> {
9092 if ty. qself . is_none ( ) {
9193 self . self_to_qself ( & mut ty. qself , & mut ty. path ) ;
9294 }
93- visit_mut :: visit_type_path_mut ( self , ty) ;
95+ self . visit_type_path_mut_impl ( ty) ;
9496 }
9597
9698 // `Self::method` -> `<Receiver>::method`
9799 fn visit_expr_path_mut ( & mut self , expr : & mut ExprPath ) {
98100 if expr. qself . is_none ( ) {
99101 self . self_to_qself ( & mut expr. qself , & mut expr. path ) ;
100102 }
101- visit_mut :: visit_expr_path_mut ( self , expr) ;
103+ self . visit_expr_path_mut_impl ( expr) ;
102104 }
105+
106+ // Everything below is simply traversing the syntax tree.
107+
108+ fn visit_type_mut_impl ( & mut self , ty : & mut Type ) {
109+ match ty {
110+ Type :: Array ( ty) => {
111+ self . visit_type_mut ( & mut ty. elem ) ;
112+ self . visit_expr_mut ( & mut ty. len ) ;
113+ }
114+ Type :: BareFn ( ty) => {
115+ for arg in & mut ty. inputs {
116+ self . visit_type_mut ( & mut arg. ty ) ;
117+ }
118+ self . visit_return_type_mut ( & mut ty. output ) ;
119+ }
120+ Type :: Group ( ty) => self . visit_type_mut ( & mut ty. elem ) ,
121+ Type :: ImplTrait ( ty) => {
122+ for bound in & mut ty. bounds {
123+ self . visit_type_param_bound_mut ( bound) ;
124+ }
125+ }
126+ Type :: Macro ( ty) => self . visit_macro_mut ( & mut ty. mac ) ,
127+ Type :: Paren ( ty) => self . visit_type_mut ( & mut ty. elem ) ,
128+ Type :: Path ( ty) => {
129+ if let Some ( qself) = & mut ty. qself {
130+ self . visit_type_mut ( & mut qself. ty ) ;
131+ }
132+ self . visit_path_mut ( & mut ty. path ) ;
133+ }
134+ Type :: Ptr ( ty) => self . visit_type_mut ( & mut ty. elem ) ,
135+ Type :: Reference ( ty) => self . visit_type_mut ( & mut ty. elem ) ,
136+ Type :: Slice ( ty) => self . visit_type_mut ( & mut ty. elem ) ,
137+ Type :: TraitObject ( ty) => {
138+ for bound in & mut ty. bounds {
139+ self . visit_type_param_bound_mut ( bound) ;
140+ }
141+ }
142+ Type :: Tuple ( ty) => {
143+ for elem in & mut ty. elems {
144+ self . visit_type_mut ( elem) ;
145+ }
146+ }
147+
148+ Type :: Infer ( _) | Type :: Never ( _) | Type :: Verbatim ( _) => { }
149+
150+ #[ cfg( test) ]
151+ Type :: __TestExhaustive( _) => unimplemented ! ( ) ,
152+ #[ cfg( not( test) ) ]
153+ _ => { }
154+ }
155+ }
156+
157+ fn visit_type_path_mut_impl ( & mut self , ty : & mut TypePath ) {
158+ if let Some ( qself) = & mut ty. qself {
159+ self . visit_type_mut ( & mut qself. ty ) ;
160+ }
161+ self . visit_path_mut ( & mut ty. path ) ;
162+ }
163+
164+ fn visit_expr_path_mut_impl ( & mut self , expr : & mut ExprPath ) {
165+ if let Some ( qself) = & mut expr. qself {
166+ self . visit_type_mut ( & mut qself. ty ) ;
167+ }
168+ self . visit_path_mut ( & mut expr. path ) ;
169+ }
170+
171+ fn visit_path_mut ( & mut self , path : & mut Path ) {
172+ for segment in & mut path. segments {
173+ self . visit_path_arguments_mut ( & mut segment. arguments ) ;
174+ }
175+ }
176+
177+ fn visit_path_arguments_mut ( & mut self , arguments : & mut PathArguments ) {
178+ match arguments {
179+ PathArguments :: None => { }
180+ PathArguments :: AngleBracketed ( arguments) => {
181+ for arg in & mut arguments. args {
182+ match arg {
183+ GenericArgument :: Type ( arg) => self . visit_type_mut ( arg) ,
184+ GenericArgument :: Binding ( arg) => self . visit_type_mut ( & mut arg. ty ) ,
185+ GenericArgument :: Lifetime ( _)
186+ | GenericArgument :: Constraint ( _)
187+ | GenericArgument :: Const ( _) => { }
188+ }
189+ }
190+ }
191+ PathArguments :: Parenthesized ( arguments) => {
192+ for argument in & mut arguments. inputs {
193+ self . visit_type_mut ( argument) ;
194+ }
195+ self . visit_return_type_mut ( & mut arguments. output ) ;
196+ }
197+ }
198+ }
199+
200+ fn visit_return_type_mut ( & mut self , return_type : & mut ReturnType ) {
201+ match return_type {
202+ ReturnType :: Default => { }
203+ ReturnType :: Type ( _, output) => self . visit_type_mut ( output) ,
204+ }
205+ }
206+
207+ fn visit_type_param_bound_mut ( & mut self , bound : & mut TypeParamBound ) {
208+ match bound {
209+ TypeParamBound :: Trait ( bound) => self . visit_path_mut ( & mut bound. path ) ,
210+ TypeParamBound :: Lifetime ( _) => { }
211+ }
212+ }
213+
214+ fn visit_generics_mut ( & mut self , generics : & mut Generics ) {
215+ for param in & mut generics. params {
216+ match param {
217+ GenericParam :: Type ( param) => {
218+ for bound in & mut param. bounds {
219+ self . visit_type_param_bound_mut ( bound) ;
220+ }
221+ }
222+ GenericParam :: Lifetime ( _) | GenericParam :: Const ( _) => { }
223+ }
224+ }
225+ if let Some ( where_clause) = & mut generics. where_clause {
226+ for predicate in & mut where_clause. predicates {
227+ match predicate {
228+ WherePredicate :: Type ( predicate) => {
229+ self . visit_type_mut ( & mut predicate. bounded_ty ) ;
230+ for bound in & mut predicate. bounds {
231+ self . visit_type_param_bound_mut ( bound) ;
232+ }
233+ }
234+ WherePredicate :: Lifetime ( _) | WherePredicate :: Eq ( _) => { }
235+ }
236+ }
237+ }
238+ }
239+
240+ fn visit_data_mut ( & mut self , data : & mut Data ) {
241+ match data {
242+ Data :: Struct ( data) => {
243+ for field in & mut data. fields {
244+ self . visit_type_mut ( & mut field. ty ) ;
245+ }
246+ }
247+ Data :: Enum ( data) => {
248+ for variant in & mut data. variants {
249+ for field in & mut variant. fields {
250+ self . visit_type_mut ( & mut field. ty ) ;
251+ }
252+ }
253+ }
254+ Data :: Union ( _) => { }
255+ }
256+ }
257+
258+ fn visit_expr_mut ( & mut self , expr : & mut Expr ) {
259+ match expr {
260+ Expr :: Binary ( expr) => {
261+ self . visit_expr_mut ( & mut expr. left ) ;
262+ self . visit_expr_mut ( & mut expr. right ) ;
263+ }
264+ Expr :: Call ( expr) => {
265+ self . visit_expr_mut ( & mut expr. func ) ;
266+ for arg in & mut expr. args {
267+ self . visit_expr_mut ( arg) ;
268+ }
269+ }
270+ Expr :: Cast ( expr) => {
271+ self . visit_expr_mut ( & mut expr. expr ) ;
272+ self . visit_type_mut ( & mut expr. ty ) ;
273+ }
274+ Expr :: Field ( expr) => self . visit_expr_mut ( & mut expr. base ) ,
275+ Expr :: Index ( expr) => {
276+ self . visit_expr_mut ( & mut expr. expr ) ;
277+ self . visit_expr_mut ( & mut expr. index ) ;
278+ }
279+ Expr :: Paren ( expr) => self . visit_expr_mut ( & mut expr. expr ) ,
280+ Expr :: Path ( expr) => self . visit_expr_path_mut ( expr) ,
281+ Expr :: Unary ( expr) => self . visit_expr_mut ( & mut expr. expr ) ,
282+ _ => { }
283+ }
284+ }
285+
286+ fn visit_macro_mut ( & mut self , _mac : & mut Macro ) { }
103287}
0 commit comments