Skip to content

Commit c79d72e

Browse files
authored
Rollup merge of rust-lang#68788 - Centril:unified-fn-bodies, r=petrochenkov
Towards unified `fn` grammar Part of rust-lang#68728. - Syntactically, `fn` items in `extern { ... }` blocks can now have bodies (`fn foo() { ... }` as opposed to `fn foo();`). As above, we use semantic restrictions instead. - Syntactically, `fn` items in free contexts (directly in a file or a module) can now be without bodies (`fn foo();` as opposed to `fn foo() { ... }`. As above, we use semantic restrictions instead, including for non-ident parameter patterns. - We move towards unifying the `fn` front matter; this is fully realized in rust-lang#68728. r? @petrochenkov
2 parents 8f83635 + 67c29ed commit c79d72e

Some content is hidden

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

55 files changed

+1007
-651
lines changed

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,

src/librustc_ast_lowering/lib.rs

+9-19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
3333
#![feature(array_value_iter)]
3434
#![feature(crate_visibility_modifier)]
35+
#![recursion_limit = "256"]
3536

3637
use rustc::arena::Arena;
3738
use rustc::dep_graph::DepGraph;
@@ -63,7 +64,7 @@ use syntax::attr;
6364
use syntax::node_id::NodeMap;
6465
use syntax::token::{self, Nonterminal, Token};
6566
use syntax::tokenstream::{TokenStream, TokenTree};
66-
use syntax::visit::{self, Visitor};
67+
use syntax::visit::{self, AssocCtxt, Visitor};
6768
use syntax::walk_list;
6869

6970
use log::{debug, trace};
@@ -485,25 +486,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
485486
});
486487
}
487488

488-
fn visit_trait_item(&mut self, item: &'tcx AssocItem) {
489+
fn visit_assoc_item(&mut self, item: &'tcx AssocItem, ctxt: AssocCtxt) {
489490
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-
});
491+
let owner = match (&item.kind, ctxt) {
492+
// Ignore patterns in trait methods without bodies.
493+
(AssocItemKind::Fn(_, None), AssocCtxt::Trait) => None,
494+
_ => Some(item.id),
495+
};
496+
self.with_hir_id_owner(owner, |this| visit::walk_assoc_item(this, item, ctxt));
507497
}
508498

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

0 commit comments

Comments
 (0)