Skip to content

Commit 2093d83

Browse files
committed
def_collector: Fully visit async functions
1 parent 7cdbc87 commit 2093d83

File tree

2 files changed

+35
-61
lines changed

2 files changed

+35
-61
lines changed

src/librustc_resolve/def_collector.rs

+22-61
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use log::debug;
22
use rustc::hir::map::definitions::*;
33
use rustc_ast::ast::*;
44
use rustc_ast::token::{self, Token};
5-
use rustc_ast::visit;
5+
use rustc_ast::visit::{self, FnKind};
66
use rustc_expand::expand::AstFragment;
77
use rustc_hir::def_id::DefIndex;
88
use 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),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// check-pass
2+
// edition:2018
3+
4+
macro_rules! with_doc {
5+
($doc: expr) => {
6+
#[doc = $doc]
7+
async fn f() {}
8+
};
9+
}
10+
11+
with_doc!(concat!(""));
12+
13+
fn main() {}

0 commit comments

Comments
 (0)