Skip to content

Commit 95ef5eb

Browse files
authored
Rollup merge of rust-lang#59413 - Zoxc:hirid, r=oli-obk
HirIdify hir::ItemId Version of rust-lang#59092. r? @oli-obk
2 parents e04b7b8 + f7c66fb commit 95ef5eb

File tree

13 files changed

+106
-81
lines changed

13 files changed

+106
-81
lines changed

src/librustc/hir/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub trait Visitor<'v> : Sized {
163163
/// but cannot supply a `Map`; see `nested_visit_map` for advice.
164164
#[allow(unused_variables)]
165165
fn visit_nested_item(&mut self, id: ItemId) {
166-
let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item(id.id));
166+
let opt_item = self.nested_visit_map().inter().map(|map| map.expect_item_by_hir_id(id.id));
167167
if let Some(item) = opt_item {
168168
self.visit_item(item);
169169
}

src/librustc/hir/lowering.rs

+75-50
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ use rustc_data_structures::thin_vec::ThinVec;
5151
use rustc_data_structures::sync::Lrc;
5252

5353
use std::collections::{BTreeSet, BTreeMap};
54-
use std::fmt::Debug;
5554
use std::mem;
5655
use smallvec::SmallVec;
5756
use syntax::attr;
@@ -82,7 +81,7 @@ pub struct LoweringContext<'a> {
8281
resolver: &'a mut dyn Resolver,
8382

8483
/// The items being lowered are collected here.
85-
items: BTreeMap<NodeId, hir::Item>,
84+
items: BTreeMap<hir::HirId, hir::Item>,
8685

8786
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
8887
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
@@ -321,7 +320,7 @@ enum AnonymousLifetimeMode {
321320
PassThrough,
322321
}
323322

324-
struct ImplTraitTypeIdVisitor<'a> { ids: &'a mut SmallVec<[hir::ItemId; 1]> }
323+
struct ImplTraitTypeIdVisitor<'a> { ids: &'a mut SmallVec<[NodeId; 1]> }
325324

326325
impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
327326
fn visit_ty(&mut self, ty: &'a Ty) {
@@ -330,7 +329,7 @@ impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
330329
| TyKind::BareFn(_)
331330
=> return,
332331

333-
TyKind::ImplTrait(id, _) => self.ids.push(hir::ItemId { id }),
332+
TyKind::ImplTrait(id, _) => self.ids.push(id),
334333
_ => {},
335334
}
336335
visit::walk_ty(self, ty);
@@ -361,9 +360,40 @@ impl<'a> LoweringContext<'a> {
361360
lctx: &'lcx mut LoweringContext<'interner>,
362361
}
363362

363+
impl MiscCollector<'_, '_> {
364+
fn allocate_use_tree_hir_id_counters(
365+
&mut self,
366+
tree: &UseTree,
367+
owner: DefIndex,
368+
) {
369+
match tree.kind {
370+
UseTreeKind::Simple(_, id1, id2) => {
371+
for &id in &[id1, id2] {
372+
self.lctx.resolver.definitions().create_def_with_parent(
373+
owner,
374+
id,
375+
DefPathData::Misc,
376+
DefIndexAddressSpace::High,
377+
Mark::root(),
378+
tree.prefix.span,
379+
);
380+
self.lctx.allocate_hir_id_counter(id);
381+
}
382+
}
383+
UseTreeKind::Glob => (),
384+
UseTreeKind::Nested(ref trees) => {
385+
for &(ref use_tree, id) in trees {
386+
let hir_id = self.lctx.allocate_hir_id_counter(id).hir_id;
387+
self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner);
388+
}
389+
}
390+
}
391+
}
392+
}
393+
364394
impl<'lcx, 'interner> Visitor<'lcx> for MiscCollector<'lcx, 'interner> {
365395
fn visit_item(&mut self, item: &'lcx Item) {
366-
self.lctx.allocate_hir_id_counter(item.id, item);
396+
let hir_id = self.lctx.allocate_hir_id_counter(item.id).hir_id;
367397

368398
match item.node {
369399
ItemKind::Struct(_, ref generics)
@@ -383,18 +413,21 @@ impl<'a> LoweringContext<'a> {
383413
.count();
384414
self.lctx.type_def_lifetime_params.insert(def_id, count);
385415
}
416+
ItemKind::Use(ref use_tree) => {
417+
self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner);
418+
}
386419
_ => {}
387420
}
388421
visit::walk_item(self, item);
389422
}
390423

391424
fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
392-
self.lctx.allocate_hir_id_counter(item.id, item);
425+
self.lctx.allocate_hir_id_counter(item.id);
393426
visit::walk_trait_item(self, item);
394427
}
395428

396429
fn visit_impl_item(&mut self, item: &'lcx ImplItem) {
397-
self.lctx.allocate_hir_id_counter(item.id, item);
430+
self.lctx.allocate_hir_id_counter(item.id);
398431
visit::walk_impl_item(self, item);
399432
}
400433
}
@@ -434,17 +467,16 @@ impl<'a> LoweringContext<'a> {
434467
}
435468

436469
fn visit_item(&mut self, item: &'lcx Item) {
437-
let mut item_lowered = true;
470+
let mut item_hir_id = None;
438471
self.lctx.with_hir_id_owner(item.id, |lctx| {
439472
if let Some(hir_item) = lctx.lower_item(item) {
440-
lctx.insert_item(item.id, hir_item);
441-
} else {
442-
item_lowered = false;
473+
item_hir_id = Some(hir_item.hir_id);
474+
lctx.insert_item(hir_item);
443475
}
444476
});
445477

446-
if item_lowered {
447-
let item_generics = match self.lctx.items.get(&item.id).unwrap().node {
478+
if let Some(hir_id) = item_hir_id {
479+
let item_generics = match self.lctx.items.get(&hir_id).unwrap().node {
448480
hir::ItemKind::Impl(_, _, _, ref generics, ..)
449481
| hir::ItemKind::Trait(_, _, ref generics, ..) => {
450482
generics.params.clone()
@@ -516,20 +548,21 @@ impl<'a> LoweringContext<'a> {
516548
}
517549
}
518550

519-
fn insert_item(&mut self, id: NodeId, item: hir::Item) {
551+
fn insert_item(&mut self, item: hir::Item) {
552+
let id = item.hir_id;
553+
// FIXME: Use debug_asset-rt
554+
assert_eq!(id.local_id, hir::ItemLocalId::from_u32(0));
520555
self.items.insert(id, item);
521556
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
522557
}
523558

524-
fn allocate_hir_id_counter<T: Debug>(&mut self, owner: NodeId, debug: &T) -> LoweredNodeId {
525-
if self.item_local_id_counters.insert(owner, 0).is_some() {
526-
bug!(
527-
"Tried to allocate item_local_id_counter for {:?} twice",
528-
debug
529-
);
530-
}
559+
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> LoweredNodeId {
560+
// Setup the counter if needed
561+
self.item_local_id_counters.entry(owner).or_insert(0);
531562
// Always allocate the first `HirId` for the owner itself.
532-
self.lower_node_id_with_owner(owner, owner)
563+
let lowered = self.lower_node_id_with_owner(owner, owner);
564+
debug_assert_eq!(lowered.hir_id.local_id.as_u32(), 0);
565+
lowered
533566
}
534567

535568
fn lower_node_id_generic<F>(&mut self, ast_node_id: NodeId, alloc_hir_id: F) -> LoweredNodeId
@@ -1381,7 +1414,7 @@ impl<'a> LoweringContext<'a> {
13811414
.opt_def_index(exist_ty_node_id)
13821415
.unwrap();
13831416

1384-
self.allocate_hir_id_counter(exist_ty_node_id, &"existential impl trait");
1417+
self.allocate_hir_id_counter(exist_ty_node_id);
13851418

13861419
let hir_bounds = self.with_hir_id_owner(exist_ty_node_id, lower_bounds);
13871420

@@ -1422,10 +1455,10 @@ impl<'a> LoweringContext<'a> {
14221455
// Insert the item into the global list. This usually happens
14231456
// automatically for all AST items. But this existential type item
14241457
// does not actually exist in the AST.
1425-
lctx.insert_item(exist_ty_id.node_id, exist_ty_item);
1458+
lctx.insert_item(exist_ty_item);
14261459

14271460
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1428-
hir::TyKind::Def(hir::ItemId { id: exist_ty_id.node_id }, lifetimes)
1461+
hir::TyKind::Def(hir::ItemId { id: exist_ty_id.hir_id }, lifetimes)
14291462
})
14301463
}
14311464

@@ -2002,9 +2035,9 @@ impl<'a> LoweringContext<'a> {
20022035
)
20032036
}
20042037

2005-
fn lower_local(&mut self, l: &Local) -> (hir::Local, SmallVec<[hir::ItemId; 1]>) {
2038+
fn lower_local(&mut self, l: &Local) -> (hir::Local, SmallVec<[NodeId; 1]>) {
20062039
let LoweredNodeId { node_id: _, hir_id } = self.lower_node_id(l.id);
2007-
let mut ids = SmallVec::<[hir::ItemId; 1]>::new();
2040+
let mut ids = SmallVec::<[NodeId; 1]>::new();
20082041
if self.sess.features_untracked().impl_trait_in_bindings {
20092042
if let Some(ref ty) = l.ty {
20102043
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
@@ -3065,7 +3098,6 @@ impl<'a> LoweringContext<'a> {
30653098
}
30663099
}
30673100

3068-
let parent_def_index = self.current_hir_id_owner.last().unwrap().0;
30693101
let mut defs = self.expect_full_def_from_use(id);
30703102
// We want to return *something* from this function, so hold onto the first item
30713103
// for later.
@@ -3084,14 +3116,6 @@ impl<'a> LoweringContext<'a> {
30843116
seg.id = self.sess.next_node_id();
30853117
}
30863118
let span = path.span;
3087-
self.resolver.definitions().create_def_with_parent(
3088-
parent_def_index,
3089-
new_node_id,
3090-
DefPathData::Misc,
3091-
DefIndexAddressSpace::High,
3092-
Mark::root(),
3093-
span);
3094-
self.allocate_hir_id_counter(new_node_id, &path);
30953119

30963120
self.with_hir_id_owner(new_node_id, |this| {
30973121
let new_id = this.lower_node_id(new_node_id);
@@ -3114,7 +3138,6 @@ impl<'a> LoweringContext<'a> {
31143138
let vis = respan(vis.span, vis_kind);
31153139

31163140
this.insert_item(
3117-
new_id.node_id,
31183141
hir::Item {
31193142
hir_id: new_id.hir_id,
31203143
ident,
@@ -3174,8 +3197,6 @@ impl<'a> LoweringContext<'a> {
31743197

31753198
// Add all the nested `PathListItem`s to the HIR.
31763199
for &(ref use_tree, id) in trees {
3177-
self.allocate_hir_id_counter(id, &use_tree);
3178-
31793200
let LoweredNodeId {
31803201
node_id: new_id,
31813202
hir_id: new_hir_id,
@@ -3219,7 +3240,6 @@ impl<'a> LoweringContext<'a> {
32193240
let vis = respan(vis.span, vis_kind);
32203241

32213242
this.insert_item(
3222-
new_id,
32233243
hir::Item {
32243244
hir_id: new_hir_id,
32253245
ident,
@@ -3443,43 +3463,47 @@ impl<'a> LoweringContext<'a> {
34433463
}
34443464

34453465
fn lower_item_id(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
3446-
match i.node {
3466+
let node_ids = match i.node {
34473467
ItemKind::Use(ref use_tree) => {
3448-
let mut vec = smallvec![hir::ItemId { id: i.id }];
3468+
let mut vec = smallvec![i.id];
34493469
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
34503470
vec
34513471
}
34523472
ItemKind::MacroDef(..) => SmallVec::new(),
34533473
ItemKind::Fn(..) |
3454-
ItemKind::Impl(.., None, _, _) => smallvec![hir::ItemId { id: i.id }],
3474+
ItemKind::Impl(.., None, _, _) => smallvec![i.id],
34553475
ItemKind::Static(ref ty, ..) => {
3456-
let mut ids = smallvec![hir::ItemId { id: i.id }];
3476+
let mut ids = smallvec![i.id];
34573477
if self.sess.features_untracked().impl_trait_in_bindings {
34583478
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
34593479
visitor.visit_ty(ty);
34603480
}
34613481
ids
34623482
},
34633483
ItemKind::Const(ref ty, ..) => {
3464-
let mut ids = smallvec![hir::ItemId { id: i.id }];
3484+
let mut ids = smallvec![i.id];
34653485
if self.sess.features_untracked().impl_trait_in_bindings {
34663486
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
34673487
visitor.visit_ty(ty);
34683488
}
34693489
ids
34703490
},
3471-
_ => smallvec![hir::ItemId { id: i.id }],
3472-
}
3491+
_ => smallvec![i.id],
3492+
};
3493+
3494+
node_ids.into_iter().map(|node_id| hir::ItemId {
3495+
id: self.allocate_hir_id_counter(node_id).hir_id
3496+
}).collect()
34733497
}
34743498

34753499
fn lower_item_id_use_tree(&mut self,
34763500
tree: &UseTree,
34773501
base_id: NodeId,
3478-
vec: &mut SmallVec<[hir::ItemId; 1]>)
3502+
vec: &mut SmallVec<[NodeId; 1]>)
34793503
{
34803504
match tree.kind {
34813505
UseTreeKind::Nested(ref nested_vec) => for &(ref nested, id) in nested_vec {
3482-
vec.push(hir::ItemId { id });
3506+
vec.push(id);
34833507
self.lower_item_id_use_tree(nested, id, vec);
34843508
},
34853509
UseTreeKind::Glob => {}
@@ -3488,7 +3512,7 @@ impl<'a> LoweringContext<'a> {
34883512
.skip(1)
34893513
.zip([id1, id2].iter())
34903514
{
3491-
vec.push(hir::ItemId { id });
3515+
vec.push(id);
34923516
}
34933517
},
34943518
}
@@ -4604,6 +4628,7 @@ impl<'a> LoweringContext<'a> {
46044628
let mut ids: SmallVec<[hir::Stmt; 1]> = item_ids
46054629
.into_iter()
46064630
.map(|item_id| {
4631+
let item_id = hir::ItemId { id: self.lower_node_id(item_id).hir_id };
46074632
let LoweredNodeId { node_id: _, hir_id } = self.next_id();
46084633

46094634
hir::Stmt {

src/librustc/hir/map/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ impl<'hir> Map<'hir> {
609609
let module = &self.forest.krate.modules[&node_id];
610610

611611
for id in &module.items {
612-
visitor.visit_item(self.expect_item(*id));
612+
visitor.visit_item(self.expect_item_by_hir_id(*id));
613613
}
614614

615615
for id in &module.trait_items {
@@ -1293,7 +1293,7 @@ pub fn map_crate<'hir>(sess: &crate::session::Session,
12931293
impl<'hir> print::PpAnn for Map<'hir> {
12941294
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) -> io::Result<()> {
12951295
match nested {
1296-
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
1296+
Nested::Item(id) => state.print_item(self.expect_item_by_hir_id(id.id)),
12971297
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
12981298
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
12991299
Nested::Body(id) => state.print_expr(&self.body(id).value),

src/librustc/hir/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ pub struct WhereEqPredicate {
698698
pub struct ModuleItems {
699699
// Use BTreeSets here so items are in the same order as in the
700700
// list of all items in Crate
701-
pub items: BTreeSet<NodeId>,
701+
pub items: BTreeSet<HirId>,
702702
pub trait_items: BTreeSet<TraitItemId>,
703703
pub impl_items: BTreeSet<ImplItemId>,
704704
}
@@ -722,7 +722,7 @@ pub struct Crate {
722722
// does, because it can affect the order in which errors are
723723
// detected, which in turn can make compile-fail tests yield
724724
// slightly different results.
725-
pub items: BTreeMap<NodeId, Item>,
725+
pub items: BTreeMap<HirId, Item>,
726726

727727
pub trait_items: BTreeMap<TraitItemId, TraitItem>,
728728
pub impl_items: BTreeMap<ImplItemId, ImplItem>,
@@ -741,7 +741,7 @@ pub struct Crate {
741741
}
742742

743743
impl Crate {
744-
pub fn item(&self, id: NodeId) -> &Item {
744+
pub fn item(&self, id: HirId) -> &Item {
745745
&self.items[&id]
746746
}
747747

@@ -2215,7 +2215,7 @@ impl VariantData {
22152215
// so it can fetched later.
22162216
#[derive(Copy, Clone, RustcEncodable, RustcDecodable, Debug)]
22172217
pub struct ItemId {
2218-
pub id: NodeId,
2218+
pub id: HirId,
22192219
}
22202220

22212221
/// An item

src/librustc/hir/print.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub trait PpAnn {
4848
fn post(&self, _state: &mut State<'_>, _node: AnnNode<'_>) -> io::Result<()> {
4949
Ok(())
5050
}
51-
fn try_fetch_item(&self, _: ast::NodeId) -> Option<&hir::Item> {
51+
fn try_fetch_item(&self, _: hir::HirId) -> Option<&hir::Item> {
5252
None
5353
}
5454
}
@@ -58,7 +58,7 @@ impl PpAnn for NoAnn {}
5858
pub const NO_ANN: &dyn PpAnn = &NoAnn;
5959

6060
impl PpAnn for hir::Crate {
61-
fn try_fetch_item(&self, item: ast::NodeId) -> Option<&hir::Item> {
61+
fn try_fetch_item(&self, item: hir::HirId) -> Option<&hir::Item> {
6262
Some(self.item(item))
6363
}
6464
fn nested(&self, state: &mut State<'_>, nested: Nested) -> io::Result<()> {

0 commit comments

Comments
 (0)