Skip to content

Commit 147f74a

Browse files
committedAug 30, 2021
Clean up the lowering of AST items
1 parent 9556d7a commit 147f74a

File tree

1 file changed

+29
-33
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+29
-33
lines changed
 

‎compiler/rustc_ast_lowering/src/item.rs

+29-33
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,43 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
2626
}
2727

2828
impl ItemLowerer<'_, '_, '_> {
29-
fn with_trait_impl_ref(&mut self, impl_ref: &Option<TraitRef>, f: impl FnOnce(&mut Self)) {
29+
fn with_trait_impl_ref<T>(
30+
&mut self,
31+
impl_ref: &Option<TraitRef>,
32+
f: impl FnOnce(&mut Self) -> T,
33+
) -> T {
3034
let old = self.lctx.is_in_trait_impl;
3135
self.lctx.is_in_trait_impl = impl_ref.is_some();
32-
f(self);
36+
let ret = f(self);
3337
self.lctx.is_in_trait_impl = old;
38+
ret
3439
}
3540
}
3641

3742
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
3843
fn visit_item(&mut self, item: &'a Item) {
39-
let mut item_hir_id = None;
40-
self.lctx.with_hir_id_owner(item.id, |lctx| {
44+
let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| {
4145
lctx.without_in_scope_lifetime_defs(|lctx| {
42-
if let Some(hir_item) = lctx.lower_item(item) {
43-
let id = lctx.insert_item(hir_item);
44-
item_hir_id = Some(id);
45-
}
46+
let hir_item = lctx.lower_item(item);
47+
lctx.insert_item(hir_item)
4648
})
4749
});
4850

49-
if let Some(hir_id) = item_hir_id {
50-
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
51-
let this = &mut ItemLowerer { lctx: this };
52-
match item.kind {
53-
ItemKind::Mod(..) => {
54-
let def_id = this.lctx.lower_node_id(item.id).expect_owner();
55-
let old_current_module =
56-
mem::replace(&mut this.lctx.current_module, def_id);
57-
visit::walk_item(this, item);
58-
this.lctx.current_module = old_current_module;
59-
}
60-
ItemKind::Impl(box ImplKind { ref of_trait, .. }) => {
61-
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
62-
}
63-
_ => visit::walk_item(this, item),
51+
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
52+
let this = &mut ItemLowerer { lctx: this };
53+
match item.kind {
54+
ItemKind::Mod(..) => {
55+
let def_id = this.lctx.lower_node_id(item.id).expect_owner();
56+
let old_current_module = mem::replace(&mut this.lctx.current_module, def_id);
57+
visit::walk_item(this, item);
58+
this.lctx.current_module = old_current_module;
6459
}
65-
});
66-
}
60+
ItemKind::Impl(box ImplKind { ref of_trait, .. }) => {
61+
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
62+
}
63+
_ => visit::walk_item(this, item),
64+
}
65+
});
6766
}
6867

6968
fn visit_fn(&mut self, fk: FnKind<'a>, sp: Span, _: NodeId) {
@@ -113,7 +112,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
113112
fn with_parent_item_lifetime_defs<T>(
114113
&mut self,
115114
parent_hir_id: hir::ItemId,
116-
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
115+
f: impl FnOnce(&mut Self) -> T,
117116
) -> T {
118117
let old_len = self.in_scope_lifetimes.len();
119118

@@ -137,10 +136,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
137136
// Clears (and restores) the `in_scope_lifetimes` field. Used when
138137
// visiting nested items, which never inherit in-scope lifetimes
139138
// from their surrounding environment.
140-
fn without_in_scope_lifetime_defs<T>(
141-
&mut self,
142-
f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T,
143-
) -> T {
139+
fn without_in_scope_lifetime_defs<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
144140
let old_in_scope_lifetimes = mem::replace(&mut self.in_scope_lifetimes, vec![]);
145141

146142
// this vector is only used when walking over impl headers,
@@ -208,19 +204,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
208204
}
209205
}
210206

211-
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> {
207+
pub fn lower_item(&mut self, i: &Item) -> hir::Item<'hir> {
212208
let mut ident = i.ident;
213209
let mut vis = self.lower_visibility(&i.vis, None);
214210
let hir_id = self.lower_node_id(i.id);
215211
let attrs = self.lower_attrs(hir_id, &i.attrs);
216212
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind);
217-
Some(hir::Item {
213+
hir::Item {
218214
def_id: hir_id.expect_owner(),
219215
ident: self.lower_ident(ident),
220216
kind,
221217
vis,
222218
span: self.lower_span(i.span),
223-
})
219+
}
224220
}
225221

226222
fn lower_item_kind(

0 commit comments

Comments
 (0)
Please sign in to comment.