Skip to content

Commit b2c6eeb

Browse files
committed
parser: merge fn grammars wrt. bodies & headers
also refactor `FnKind` and `visit_assoc_item` visitors
1 parent c0b7b41 commit b2c6eeb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+961
-629
lines changed

Diff for: src/librustc_ast_lowering/item.rs

+22-26
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_target::spec::abi;
1414
use syntax::ast::*;
1515
use syntax::attr;
1616
use syntax::node_id::NodeMap;
17-
use syntax::visit::{self, Visitor};
17+
use syntax::visit::{self, AssocCtxt, Visitor};
1818

1919
use log::debug;
2020
use smallvec::{smallvec, SmallVec};
@@ -81,25 +81,23 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
8181
}
8282
}
8383

84-
fn visit_trait_item(&mut self, item: &'a AssocItem) {
85-
self.lctx.with_hir_id_owner(item.id, |lctx| {
86-
let hir_item = lctx.lower_trait_item(item);
87-
let id = hir::TraitItemId { hir_id: hir_item.hir_id };
88-
lctx.trait_items.insert(id, hir_item);
89-
lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id);
84+
fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
85+
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
86+
AssocCtxt::Trait => {
87+
let hir_item = lctx.lower_trait_item(item);
88+
let id = hir::TraitItemId { hir_id: hir_item.hir_id };
89+
lctx.trait_items.insert(id, hir_item);
90+
lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id);
91+
}
92+
AssocCtxt::Impl => {
93+
let hir_item = lctx.lower_impl_item(item);
94+
let id = hir::ImplItemId { hir_id: hir_item.hir_id };
95+
lctx.impl_items.insert(id, hir_item);
96+
lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id);
97+
}
9098
});
9199

92-
visit::walk_trait_item(self, item);
93-
}
94-
95-
fn visit_impl_item(&mut self, item: &'a AssocItem) {
96-
self.lctx.with_hir_id_owner(item.id, |lctx| {
97-
let hir_item = lctx.lower_impl_item(item);
98-
let id = hir::ImplItemId { hir_id: hir_item.hir_id };
99-
lctx.impl_items.insert(id, hir_item);
100-
lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id);
101-
});
102-
visit::walk_impl_item(self, item);
100+
visit::walk_assoc_item(self, item, ctxt);
103101
}
104102
}
105103

@@ -299,20 +297,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
299297
// `impl Future<Output = T>` here because lower_body
300298
// only cares about the input argument patterns in the function
301299
// declaration (decl), not the return types.
300+
let asyncness = header.asyncness.node;
302301
let body_id =
303-
this.lower_maybe_async_body(span, &decl, header.asyncness.node, Some(body));
302+
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
304303

305304
let (generics, decl) = this.add_in_band_defs(
306305
generics,
307306
fn_def_id,
308307
AnonymousLifetimeMode::PassThrough,
309308
|this, idty| {
310-
this.lower_fn_decl(
311-
&decl,
312-
Some((fn_def_id, idty)),
313-
true,
314-
header.asyncness.node.opt_return_id(),
315-
)
309+
let ret_id = asyncness.opt_return_id();
310+
this.lower_fn_decl(&decl, Some((fn_def_id, idty)), true, ret_id)
316311
},
317312
);
318313
let sig = hir::FnSig { decl, header: this.lower_fn_header(header) };
@@ -658,7 +653,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
658653
ident: i.ident,
659654
attrs: self.lower_attrs(&i.attrs),
660655
kind: match i.kind {
661-
ForeignItemKind::Fn(ref fdec, ref generics) => {
656+
ForeignItemKind::Fn(ref sig, ref generics, _) => {
657+
let fdec = &sig.decl;
662658
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
663659
generics,
664660
def_id,

Diff for: src/librustc_ast_lowering/lib.rs

+8-19
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use syntax::attr;
6363
use syntax::node_id::NodeMap;
6464
use syntax::token::{self, Nonterminal, Token};
6565
use syntax::tokenstream::{TokenStream, TokenTree};
66-
use syntax::visit::{self, Visitor};
66+
use syntax::visit::{self, AssocCtxt, Visitor};
6767
use syntax::walk_list;
6868

6969
use log::{debug, trace};
@@ -485,25 +485,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
485485
});
486486
}
487487

488-
fn visit_trait_item(&mut self, item: &'tcx AssocItem) {
488+
fn visit_assoc_item(&mut self, item: &'tcx AssocItem, ctxt: AssocCtxt) {
489489
self.lctx.allocate_hir_id_counter(item.id);
490-
491-
match item.kind {
492-
AssocItemKind::Fn(_, None) => {
493-
// Ignore patterns in trait methods without bodies
494-
self.with_hir_id_owner(None, |this| visit::walk_trait_item(this, item));
495-
}
496-
_ => self.with_hir_id_owner(Some(item.id), |this| {
497-
visit::walk_trait_item(this, item);
498-
}),
499-
}
500-
}
501-
502-
fn visit_impl_item(&mut self, item: &'tcx AssocItem) {
503-
self.lctx.allocate_hir_id_counter(item.id);
504-
self.with_hir_id_owner(Some(item.id), |this| {
505-
visit::walk_impl_item(this, item);
506-
});
490+
let owner = match (&item.kind, ctxt) {
491+
// Ignore patterns in trait methods without bodies.
492+
(AssocItemKind::Fn(_, None), AssocCtxt::Trait) => None,
493+
_ => Some(item.id),
494+
};
495+
self.with_hir_id_owner(owner, |this| visit::walk_assoc_item(this, item, ctxt));
507496
}
508497

509498
fn visit_foreign_item(&mut self, i: &'tcx ForeignItem) {

0 commit comments

Comments
 (0)