@@ -2,7 +2,7 @@ use log::debug;
22use rustc:: hir:: map:: definitions:: * ;
33use rustc_ast:: ast:: * ;
44use rustc_ast:: token:: { self , Token } ;
5- use rustc_ast:: visit;
5+ use rustc_ast:: visit:: { self , FnKind } ;
66use rustc_expand:: expand:: AstFragment ;
77use rustc_hir:: def_id:: DefIndex ;
88use rustc_span:: hygiene:: ExpnId ;
@@ -38,42 +38,6 @@ impl<'a> DefCollector<'a> {
3838 self . parent_def = orig_parent_def;
3939 }
4040
41- fn visit_async_fn (
42- & mut self ,
43- id : NodeId ,
44- name : Name ,
45- span : Span ,
46- header : & FnHeader ,
47- generics : & ' a Generics ,
48- decl : & ' a FnDecl ,
49- body : Option < & ' a Block > ,
50- ) {
51- let ( closure_id, return_impl_trait_id) = match header. asyncness {
52- Async :: Yes { span : _, closure_id, return_impl_trait_id } => {
53- ( closure_id, return_impl_trait_id)
54- }
55- _ => unreachable ! ( ) ,
56- } ;
57-
58- // For async functions, we need to create their inner defs inside of a
59- // closure to match their desugared representation.
60- let fn_def_data = DefPathData :: ValueNs ( name) ;
61- let fn_def = self . create_def ( id, fn_def_data, span) ;
62- return self . with_parent ( fn_def, |this| {
63- this. create_def ( return_impl_trait_id, DefPathData :: ImplTrait , span) ;
64-
65- visit:: walk_generics ( this, generics) ;
66- visit:: walk_fn_decl ( this, decl) ;
67-
68- let closure_def = this. create_def ( closure_id, DefPathData :: ClosureExpr , span) ;
69- this. with_parent ( closure_def, |this| {
70- if let Some ( body) = body {
71- visit:: walk_block ( this, body) ;
72- }
73- } )
74- } ) ;
75- }
76-
7741 fn collect_field ( & mut self , field : & ' a StructField , index : Option < usize > ) {
7842 let index = |this : & Self | {
7943 index. unwrap_or_else ( || {
@@ -117,17 +81,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
11781 | ItemKind :: ExternCrate ( ..)
11882 | ItemKind :: ForeignMod ( ..)
11983 | ItemKind :: TyAlias ( ..) => DefPathData :: TypeNs ( i. ident . name ) ,
120- ItemKind :: Fn ( _, sig, generics, body) if sig. header . asyncness . is_async ( ) => {
121- return self . visit_async_fn (
122- i. id ,
123- i. ident . name ,
124- i. span ,
125- & sig. header ,
126- generics,
127- & sig. decl ,
128- body. as_deref ( ) ,
129- ) ;
130- }
13184 ItemKind :: Static ( ..) | ItemKind :: Const ( ..) | ItemKind :: Fn ( ..) => {
13285 DefPathData :: ValueNs ( i. ident . name )
13386 }
@@ -154,6 +107,27 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
154107 } ) ;
155108 }
156109
110+ fn visit_fn ( & mut self , fn_kind : FnKind < ' a > , span : Span , _: NodeId ) {
111+ if let FnKind :: Fn ( _, _, sig, _, body) = fn_kind {
112+ if let Async :: Yes { closure_id, return_impl_trait_id, .. } = sig. header . asyncness {
113+ self . create_def ( return_impl_trait_id, DefPathData :: ImplTrait , span) ;
114+
115+ // For async functions, we need to create their inner defs inside of a
116+ // closure to match their desugared representation. Besides that,
117+ // we must mirror everything that `visit::walk_fn` below does.
118+ self . visit_fn_header ( & sig. header ) ;
119+ visit:: walk_fn_decl ( self , & sig. decl ) ;
120+ if let Some ( body) = body {
121+ let closure_def = self . create_def ( closure_id, DefPathData :: ClosureExpr , span) ;
122+ self . with_parent ( closure_def, |this| this. visit_block ( body) ) ;
123+ }
124+ return ;
125+ }
126+ }
127+
128+ visit:: walk_fn ( self , fn_kind, span) ;
129+ }
130+
157131 fn visit_use_tree ( & mut self , use_tree : & ' a UseTree , id : NodeId , _nested : bool ) {
158132 self . create_def ( id, DefPathData :: Misc , use_tree. span ) ;
159133 visit:: walk_use_tree ( self , use_tree, id) ;
@@ -215,19 +189,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
215189
216190 fn visit_assoc_item ( & mut self , i : & ' a AssocItem , ctxt : visit:: AssocCtxt ) {
217191 let def_data = match & i. kind {
218- AssocItemKind :: Fn ( _, FnSig { header, decl } , generics, body)
219- if header. asyncness . is_async ( ) =>
220- {
221- return self . visit_async_fn (
222- i. id ,
223- i. ident . name ,
224- i. span ,
225- header,
226- generics,
227- decl,
228- body. as_deref ( ) ,
229- ) ;
230- }
231192 AssocItemKind :: Fn ( ..) | AssocItemKind :: Const ( ..) => DefPathData :: ValueNs ( i. ident . name ) ,
232193 AssocItemKind :: TyAlias ( ..) => DefPathData :: TypeNs ( i. ident . name ) ,
233194 AssocItemKind :: Macro ( ..) => return self . visit_macro_invoc ( i. id ) ,
0 commit comments