Rollup merge of #88445 - inquisitivecrystal:ast-lowering, r=cjgillot

Clean up the lowering of AST items

This PR simplifies and improves `rustc_ast_lowering::item` in various minor ways. The reasons for the changes should mostly be self evident, though I'm happy to specifically explain anything if needed.

These changes used to be part of #88019, but I removed them after it was pointed out that some of my other changes to `rustc_ast_lowering` were unnecessary. It felt like a bad idea to clean up code which I didn't even need to touch anymore.

r? `@cjgillot`
This commit is contained in:
Mara Bos 2021-08-31 17:54:57 +02:00 committed by GitHub
commit 41249cad91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -26,34 +26,34 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
} }
impl ItemLowerer<'_, '_, '_> { impl ItemLowerer<'_, '_, '_> {
fn with_trait_impl_ref(&mut self, impl_ref: &Option<TraitRef>, f: impl FnOnce(&mut Self)) { fn with_trait_impl_ref<T>(
&mut self,
impl_ref: &Option<TraitRef>,
f: impl FnOnce(&mut Self) -> T,
) -> T {
let old = self.lctx.is_in_trait_impl; let old = self.lctx.is_in_trait_impl;
self.lctx.is_in_trait_impl = impl_ref.is_some(); self.lctx.is_in_trait_impl = impl_ref.is_some();
f(self); let ret = f(self);
self.lctx.is_in_trait_impl = old; self.lctx.is_in_trait_impl = old;
ret
} }
} }
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
fn visit_item(&mut self, item: &'a Item) { fn visit_item(&mut self, item: &'a Item) {
let mut item_hir_id = None; let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| {
self.lctx.with_hir_id_owner(item.id, |lctx| {
lctx.without_in_scope_lifetime_defs(|lctx| { lctx.without_in_scope_lifetime_defs(|lctx| {
if let Some(hir_item) = lctx.lower_item(item) { let hir_item = lctx.lower_item(item);
let id = lctx.insert_item(hir_item); lctx.insert_item(hir_item)
item_hir_id = Some(id);
}
}) })
}); });
if let Some(hir_id) = item_hir_id {
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| { self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
let this = &mut ItemLowerer { lctx: this }; let this = &mut ItemLowerer { lctx: this };
match item.kind { match item.kind {
ItemKind::Mod(..) => { ItemKind::Mod(..) => {
let def_id = this.lctx.lower_node_id(item.id).expect_owner(); let def_id = this.lctx.lower_node_id(item.id).expect_owner();
let old_current_module = let old_current_module = mem::replace(&mut this.lctx.current_module, def_id);
mem::replace(&mut this.lctx.current_module, def_id);
visit::walk_item(this, item); visit::walk_item(this, item);
this.lctx.current_module = old_current_module; this.lctx.current_module = old_current_module;
} }
@ -64,7 +64,6 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
} }
}); });
} }
}
fn visit_fn(&mut self, fk: FnKind<'a>, sp: Span, _: NodeId) { fn visit_fn(&mut self, fk: FnKind<'a>, sp: Span, _: NodeId) {
match fk { match fk {
@ -113,7 +112,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn with_parent_item_lifetime_defs<T>( fn with_parent_item_lifetime_defs<T>(
&mut self, &mut self,
parent_hir_id: hir::ItemId, parent_hir_id: hir::ItemId,
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T, f: impl FnOnce(&mut Self) -> T,
) -> T { ) -> T {
let old_len = self.in_scope_lifetimes.len(); let old_len = self.in_scope_lifetimes.len();
@ -137,10 +136,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// Clears (and restores) the `in_scope_lifetimes` field. Used when // Clears (and restores) the `in_scope_lifetimes` field. Used when
// visiting nested items, which never inherit in-scope lifetimes // visiting nested items, which never inherit in-scope lifetimes
// from their surrounding environment. // from their surrounding environment.
fn without_in_scope_lifetime_defs<T>( fn without_in_scope_lifetime_defs<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
&mut self,
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
) -> T {
let old_in_scope_lifetimes = mem::replace(&mut self.in_scope_lifetimes, vec![]); let old_in_scope_lifetimes = mem::replace(&mut self.in_scope_lifetimes, vec![]);
// this vector is only used when walking over impl headers, // this vector is only used when walking over impl headers,
@ -208,19 +204,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
} }
} }
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> { pub fn lower_item(&mut self, i: &Item) -> hir::Item<'hir> {
let mut ident = i.ident; let mut ident = i.ident;
let mut vis = self.lower_visibility(&i.vis, None); let mut vis = self.lower_visibility(&i.vis, None);
let hir_id = self.lower_node_id(i.id); let hir_id = self.lower_node_id(i.id);
let attrs = self.lower_attrs(hir_id, &i.attrs); let attrs = self.lower_attrs(hir_id, &i.attrs);
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind); let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind);
Some(hir::Item { hir::Item {
def_id: hir_id.expect_owner(), def_id: hir_id.expect_owner(),
ident: self.lower_ident(ident), ident: self.lower_ident(ident),
kind, kind,
vis, vis,
span: self.lower_span(i.span), span: self.lower_span(i.span),
}) }
} }
fn lower_item_kind( fn lower_item_kind(