Skip to content

Commit 49c0861

Browse files
committed
Auto merge of #87234 - cjgillot:lower-mono, r=petrochenkov
Lower only one HIR owner at a time Based on #83723 Additional diff is here: cjgillot/rust@ownernode...lower-mono Lowering is very tangled and has a tendency to intertwine the transformation of different items. This PR aims at simplifying the logic by: - moving global analyses to the resolver (item_generics_num_lifetimes, proc_macros, trait_impls); - removing a few special cases (non-exported macros and use statements); - restricting the amount of available information at any one time; - avoiding back-and-forth between different owners: an item must now be lowered all at once, and its parent cannot refer to its nodes. I also removed the sorting of bodies by span. The diagnostic ordering changes marginally, since definitions are pretty much sorted already according to the AST. This uncovered a subtlety in thir-unsafeck. (While these items could logically be in different PRs, the dependency between commits and the amount of conflicts force a monolithic PR.)
2 parents e7958d3 + 11024b2 commit 49c0861

File tree

19 files changed

+163
-258
lines changed

19 files changed

+163
-258
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
5555
0,
5656
ParenthesizedGenericArgs::Err,
5757
ImplTraitContext::disallowed(),
58-
None,
5958
));
6059
let args = self.lower_exprs(args);
6160
hir::ExprKind::MethodCall(
@@ -328,7 +327,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
328327
let mut generic_args = vec![];
329328
for (idx, arg) in args.into_iter().enumerate() {
330329
if legacy_args_idx.contains(&idx) {
331-
let parent_def_id = self.current_hir_id_owner.0;
330+
let parent_def_id = self.current_hir_id_owner;
332331
let node_id = self.resolver.next_node_id();
333332

334333
// Add a definition for the in-band const def.

compiler/rustc_ast_lowering/src/item.rs

+54-66
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@ impl ItemLowerer<'_, '_, '_> {
4040

4141
impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
4242
fn visit_item(&mut self, item: &'a Item) {
43-
self.lctx.allocate_hir_id_counter(item.id);
4443
let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| {
45-
lctx.without_in_scope_lifetime_defs(|lctx| {
46-
let hir_item = lctx.lower_item(item);
47-
lctx.insert_item(hir_item)
48-
})
44+
let node = lctx.without_in_scope_lifetime_defs(|lctx| lctx.lower_item(item));
45+
hir::OwnerNode::Item(node)
4946
});
5047

5148
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
@@ -72,26 +69,17 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
7269
}
7370

7471
fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
75-
self.lctx.allocate_hir_id_counter(item.id);
7672
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
77-
AssocCtxt::Trait => {
78-
let hir_item = lctx.lower_trait_item(item);
79-
lctx.insert_trait_item(hir_item);
80-
}
81-
AssocCtxt::Impl => {
82-
let hir_item = lctx.lower_impl_item(item);
83-
lctx.insert_impl_item(hir_item);
84-
}
73+
AssocCtxt::Trait => hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)),
74+
AssocCtxt::Impl => hir::OwnerNode::ImplItem(lctx.lower_impl_item(item)),
8575
});
8676

8777
visit::walk_assoc_item(self, item, ctxt);
8878
}
8979

9080
fn visit_foreign_item(&mut self, item: &'a ForeignItem) {
91-
self.lctx.allocate_hir_id_counter(item.id);
9281
self.lctx.with_hir_id_owner(item.id, |lctx| {
93-
let hir_item = lctx.lower_foreign_item(item);
94-
lctx.insert_foreign_item(hir_item);
82+
hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
9583
});
9684

9785
visit::walk_foreign_item(self, item);
@@ -106,12 +94,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
10694
// only used when lowering a child item of a trait or impl.
10795
fn with_parent_item_lifetime_defs<T>(
10896
&mut self,
109-
parent_hir_id: hir::ItemId,
97+
parent_hir_id: LocalDefId,
11098
f: impl FnOnce(&mut Self) -> T,
11199
) -> T {
112100
let old_len = self.in_scope_lifetimes.len();
113101

114-
let parent_generics = match self.owners[parent_hir_id.def_id].unwrap().expect_item().kind {
102+
let parent_generics = match self.owners[parent_hir_id].unwrap().expect_item().kind {
115103
hir::ItemKind::Impl(hir::Impl { ref generics, .. })
116104
| hir::ItemKind::Trait(_, _, ref generics, ..) => generics.params,
117105
_ => &[],
@@ -186,19 +174,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
186174
}
187175
}
188176

189-
pub fn lower_item(&mut self, i: &Item) -> hir::Item<'hir> {
177+
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
190178
let mut ident = i.ident;
191-
let mut vis = self.lower_visibility(&i.vis, None);
179+
let mut vis = self.lower_visibility(&i.vis);
192180
let hir_id = self.lower_node_id(i.id);
193181
let attrs = self.lower_attrs(hir_id, &i.attrs);
194182
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind);
195-
hir::Item {
183+
let item = hir::Item {
196184
def_id: hir_id.expect_owner(),
197185
ident: self.lower_ident(ident),
198186
kind,
199187
vis,
200188
span: self.lower_span(i.span),
201-
}
189+
};
190+
self.arena.alloc(item)
202191
}
203192

204193
fn lower_item_kind(
@@ -480,10 +469,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
480469
// Essentially a single `use` which imports two names is desugared into
481470
// two imports.
482471
for new_node_id in [id1, id2] {
483-
// Associate an HirId to both ids even if there is no resolution.
484-
let new_id = self.allocate_hir_id_counter(new_node_id);
485-
486-
let res = if let Some(res) = resolutions.next() { res } else { continue };
472+
let new_id = self.resolver.local_def_id(new_node_id);
473+
let res = if let Some(res) = resolutions.next() {
474+
res
475+
} else {
476+
// Associate an HirId to both ids even if there is no resolution.
477+
self.node_id_to_hir_id.ensure_contains_elem(new_node_id, || None);
478+
debug_assert!(self.node_id_to_hir_id[new_node_id].is_none());
479+
self.node_id_to_hir_id[new_node_id] = Some(hir::HirId::make_owner(new_id));
480+
continue;
481+
};
487482
let ident = *ident;
488483
let mut path = path.clone();
489484
for seg in &mut path.segments {
@@ -493,24 +488,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
493488

494489
self.with_hir_id_owner(new_node_id, |this| {
495490
let res = this.lower_res(res);
496-
let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None);
491+
let path = this.lower_path_extra(res, &path, ParamMode::Explicit);
497492
let kind = hir::ItemKind::Use(path, hir::UseKind::Single);
498493
let vis = this.rebuild_vis(&vis);
499494
if let Some(attrs) = attrs {
500495
this.attrs.insert(hir::HirId::make_owner(new_id), attrs);
501496
}
502497

503-
this.insert_item(hir::Item {
498+
let item = hir::Item {
504499
def_id: new_id,
505500
ident: this.lower_ident(ident),
506501
kind,
507502
vis,
508503
span: this.lower_span(span),
509-
});
504+
};
505+
hir::OwnerNode::Item(this.arena.alloc(item))
510506
});
511507
}
512508

513-
let path = self.lower_path_extra(ret_res, &path, ParamMode::Explicit, None);
509+
let path = self.lower_path_extra(ret_res, &path, ParamMode::Explicit);
514510
hir::ItemKind::Use(path, hir::UseKind::Single)
515511
}
516512
UseTreeKind::Glob => {
@@ -550,7 +546,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
550546

551547
// Add all the nested `PathListItem`s to the HIR.
552548
for &(ref use_tree, id) in trees {
553-
let new_hir_id = self.allocate_hir_id_counter(id);
549+
let new_hir_id = self.resolver.local_def_id(id);
554550

555551
let mut prefix = prefix.clone();
556552

@@ -574,13 +570,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
574570
this.attrs.insert(hir::HirId::make_owner(new_hir_id), attrs);
575571
}
576572

577-
this.insert_item(hir::Item {
573+
let item = hir::Item {
578574
def_id: new_hir_id,
579575
ident: this.lower_ident(ident),
580576
kind,
581577
vis,
582578
span: this.lower_span(use_tree.span),
583-
});
579+
};
580+
hir::OwnerNode::Item(this.arena.alloc(item))
584581
});
585582
}
586583

@@ -610,7 +607,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
610607

611608
let res = self.expect_full_res_from_use(id).next().unwrap_or(Res::Err);
612609
let res = self.lower_res(res);
613-
let path = self.lower_path_extra(res, &prefix, ParamMode::Explicit, None);
610+
let path = self.lower_path_extra(res, &prefix, ParamMode::Explicit);
614611
hir::ItemKind::Use(path, hir::UseKind::ListStem)
615612
}
616613
}
@@ -647,11 +644,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
647644
respan(self.lower_span(vis.span), vis_kind)
648645
}
649646

650-
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> {
647+
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
651648
let hir_id = self.lower_node_id(i.id);
652649
let def_id = hir_id.expect_owner();
653650
self.lower_attrs(hir_id, &i.attrs);
654-
hir::ForeignItem {
651+
let item = hir::ForeignItem {
655652
def_id,
656653
ident: self.lower_ident(i.ident),
657654
kind: match i.kind {
@@ -679,17 +676,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
679676
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
680677
ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),
681678
},
682-
vis: self.lower_visibility(&i.vis, None),
679+
vis: self.lower_visibility(&i.vis),
683680
span: self.lower_span(i.span),
684-
}
681+
};
682+
self.arena.alloc(item)
685683
}
686684

687-
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
685+
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
688686
hir::ForeignItemRef {
689-
id: hir::ForeignItemId { def_id: self.allocate_hir_id_counter(i.id) },
687+
id: hir::ForeignItemId { def_id: self.resolver.local_def_id(i.id) },
690688
ident: self.lower_ident(i.ident),
691689
span: self.lower_span(i.span),
692-
vis: self.lower_visibility(&i.vis, Some(i.id)),
693690
}
694691
}
695692

@@ -757,12 +754,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
757754
// FIXME(jseyfried): positional field hygiene.
758755
None => Ident::new(sym::integer(index), self.lower_span(f.span)),
759756
},
760-
vis: self.lower_visibility(&f.vis, None),
757+
vis: self.lower_visibility(&f.vis),
761758
ty,
762759
}
763760
}
764761

765-
fn lower_trait_item(&mut self, i: &AssocItem) -> hir::TraitItem<'hir> {
762+
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
766763
let hir_id = self.lower_node_id(i.id);
767764
let trait_item_def_id = hir_id.expect_owner();
768765

@@ -805,13 +802,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
805802
};
806803

807804
self.lower_attrs(hir_id, &i.attrs);
808-
hir::TraitItem {
805+
let item = hir::TraitItem {
809806
def_id: trait_item_def_id,
810807
ident: self.lower_ident(i.ident),
811808
generics,
812809
kind,
813810
span: self.lower_span(i.span),
814-
}
811+
};
812+
self.arena.alloc(item)
815813
}
816814

817815
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
@@ -841,7 +839,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
841839
self.expr(span, hir::ExprKind::Err, AttrVec::new())
842840
}
843841

844-
fn lower_impl_item(&mut self, i: &AssocItem) -> hir::ImplItem<'hir> {
842+
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
845843
let impl_item_def_id = self.resolver.local_def_id(i.id);
846844

847845
let (generics, kind) = match &i.kind {
@@ -895,26 +893,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
895893
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
896894
let hir_id = self.lower_node_id(i.id);
897895
self.lower_attrs(hir_id, &i.attrs);
898-
hir::ImplItem {
896+
let item = hir::ImplItem {
899897
def_id: hir_id.expect_owner(),
900898
ident: self.lower_ident(i.ident),
901899
generics,
902-
vis: self.lower_visibility(&i.vis, None),
900+
vis: self.lower_visibility(&i.vis),
903901
defaultness,
904902
kind,
905903
span: self.lower_span(i.span),
906-
}
904+
};
905+
self.arena.alloc(item)
907906
}
908907

909-
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef<'hir> {
908+
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
910909
// Since `default impl` is not yet implemented, this is always true in impls.
911910
let has_value = true;
912911
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
913912
hir::ImplItemRef {
914-
id: hir::ImplItemId { def_id: self.allocate_hir_id_counter(i.id) },
913+
id: hir::ImplItemId { def_id: self.resolver.local_def_id(i.id) },
915914
ident: self.lower_ident(i.ident),
916915
span: self.lower_span(i.span),
917-
vis: self.lower_visibility(&i.vis, Some(i.id)),
918916
defaultness,
919917
kind: match &i.kind {
920918
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
@@ -932,25 +930,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
932930
/// lowered. This can happen during `lower_impl_item_ref()` where we need to
933931
/// lower a `Visibility` value although we haven't lowered the owning
934932
/// `ImplItem` in question yet.
935-
fn lower_visibility(
936-
&mut self,
937-
v: &Visibility,
938-
explicit_owner: Option<NodeId>,
939-
) -> hir::Visibility<'hir> {
933+
fn lower_visibility(&mut self, v: &Visibility) -> hir::Visibility<'hir> {
940934
let node = match v.kind {
941935
VisibilityKind::Public => hir::VisibilityKind::Public,
942936
VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
943937
VisibilityKind::Restricted { ref path, id } => {
944938
debug!("lower_visibility: restricted path id = {:?}", id);
945-
let lowered_id = if let Some(owner) = explicit_owner {
946-
self.lower_node_id_with_owner(id, owner)
947-
} else {
948-
self.lower_node_id(id)
949-
};
950-
let res = self.expect_full_res(id);
951-
let res = self.lower_res(res);
939+
let lowered_id = self.lower_node_id(id);
952940
hir::VisibilityKind::Restricted {
953-
path: self.lower_path_extra(res, path, ParamMode::Explicit, explicit_owner),
941+
path: self.lower_path(id, path, ParamMode::Explicit),
954942
hir_id: lowered_id,
955943
}
956944
}

0 commit comments

Comments
 (0)