Skip to content

Commit 7dd8b77

Browse files
ljedrzZoxc
authored andcommitted
hir: replace NodeId with HirId in ItemId
1 parent 60eca54 commit 7dd8b77

File tree

12 files changed

+59
-56
lines changed

12 files changed

+59
-56
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

+29-26
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub struct LoweringContext<'a> {
8282
resolver: &'a mut dyn Resolver,
8383

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

8787
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
8888
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
@@ -321,7 +321,7 @@ enum AnonymousLifetimeMode {
321321
PassThrough,
322322
}
323323

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

326326
impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
327327
fn visit_ty(&mut self, ty: &'a Ty) {
@@ -330,7 +330,7 @@ impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
330330
| TyKind::BareFn(_)
331331
=> return,
332332

333-
TyKind::ImplTrait(id, _) => self.ids.push(hir::ItemId { id }),
333+
TyKind::ImplTrait(id, _) => self.ids.push(id),
334334
_ => {},
335335
}
336336
visit::walk_ty(self, ty);
@@ -434,17 +434,16 @@ impl<'a> LoweringContext<'a> {
434434
}
435435

436436
fn visit_item(&mut self, item: &'lcx Item) {
437-
let mut item_lowered = true;
437+
let mut item_hir_id = None;
438438
self.lctx.with_hir_id_owner(item.id, |lctx| {
439439
if let Some(hir_item) = lctx.lower_item(item) {
440-
lctx.insert_item(item.id, hir_item);
441-
} else {
442-
item_lowered = false;
440+
item_hir_id = Some(hir_item.hir_id);
441+
lctx.insert_item(hir_item);
443442
}
444443
});
445444

446-
if item_lowered {
447-
let item_generics = match self.lctx.items.get(&item.id).unwrap().node {
445+
if let Some(hir_id) = item_hir_id {
446+
let item_generics = match self.lctx.items.get(&hir_id).unwrap().node {
448447
hir::ItemKind::Impl(_, _, _, ref generics, ..)
449448
| hir::ItemKind::Trait(_, _, ref generics, ..) => {
450449
generics.params.clone()
@@ -516,7 +515,8 @@ impl<'a> LoweringContext<'a> {
516515
}
517516
}
518517

519-
fn insert_item(&mut self, id: NodeId, item: hir::Item) {
518+
fn insert_item(&mut self, item: hir::Item) {
519+
let id = item.hir_id;
520520
self.items.insert(id, item);
521521
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
522522
}
@@ -1422,10 +1422,10 @@ impl<'a> LoweringContext<'a> {
14221422
// Insert the item into the global list. This usually happens
14231423
// automatically for all AST items. But this existential type item
14241424
// does not actually exist in the AST.
1425-
lctx.insert_item(exist_ty_id.node_id, exist_ty_item);
1425+
lctx.insert_item(exist_ty_item);
14261426

14271427
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1428-
hir::TyKind::Def(hir::ItemId { id: exist_ty_id.node_id }, lifetimes)
1428+
hir::TyKind::Def(hir::ItemId { id: exist_ty_id.hir_id }, lifetimes)
14291429
})
14301430
}
14311431

@@ -2002,9 +2002,9 @@ impl<'a> LoweringContext<'a> {
20022002
)
20032003
}
20042004

2005-
fn lower_local(&mut self, l: &Local) -> (hir::Local, SmallVec<[hir::ItemId; 1]>) {
2005+
fn lower_local(&mut self, l: &Local) -> (hir::Local, SmallVec<[NodeId; 1]>) {
20062006
let LoweredNodeId { node_id: _, hir_id } = self.lower_node_id(l.id);
2007-
let mut ids = SmallVec::<[hir::ItemId; 1]>::new();
2007+
let mut ids = SmallVec::<[NodeId; 1]>::new();
20082008
if self.sess.features_untracked().impl_trait_in_bindings {
20092009
if let Some(ref ty) = l.ty {
20102010
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
@@ -3114,7 +3114,6 @@ impl<'a> LoweringContext<'a> {
31143114
let vis = respan(vis.span, vis_kind);
31153115

31163116
this.insert_item(
3117-
new_id.node_id,
31183117
hir::Item {
31193118
hir_id: new_id.hir_id,
31203119
ident,
@@ -3219,7 +3218,6 @@ impl<'a> LoweringContext<'a> {
32193218
let vis = respan(vis.span, vis_kind);
32203219

32213220
this.insert_item(
3222-
new_id,
32233221
hir::Item {
32243222
hir_id: new_hir_id,
32253223
ident,
@@ -3443,43 +3441,47 @@ impl<'a> LoweringContext<'a> {
34433441
}
34443442

34453443
fn lower_item_id(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
3446-
match i.node {
3444+
let node_ids = match i.node {
34473445
ItemKind::Use(ref use_tree) => {
3448-
let mut vec = smallvec![hir::ItemId { id: i.id }];
3446+
let mut vec = smallvec![i.id];
34493447
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
34503448
vec
34513449
}
34523450
ItemKind::MacroDef(..) => SmallVec::new(),
34533451
ItemKind::Fn(..) |
3454-
ItemKind::Impl(.., None, _, _) => smallvec![hir::ItemId { id: i.id }],
3452+
ItemKind::Impl(.., None, _, _) => smallvec![i.id],
34553453
ItemKind::Static(ref ty, ..) => {
3456-
let mut ids = smallvec![hir::ItemId { id: i.id }];
3454+
let mut ids = smallvec![i.id];
34573455
if self.sess.features_untracked().impl_trait_in_bindings {
34583456
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
34593457
visitor.visit_ty(ty);
34603458
}
34613459
ids
34623460
},
34633461
ItemKind::Const(ref ty, ..) => {
3464-
let mut ids = smallvec![hir::ItemId { id: i.id }];
3462+
let mut ids = smallvec![i.id];
34653463
if self.sess.features_untracked().impl_trait_in_bindings {
34663464
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
34673465
visitor.visit_ty(ty);
34683466
}
34693467
ids
34703468
},
3471-
_ => smallvec![hir::ItemId { id: i.id }],
3472-
}
3469+
_ => smallvec![i.id],
3470+
};
3471+
3472+
node_ids.into_iter()
3473+
.map(|node_id| hir::ItemId { id: self.lower_node_id(node_id).hir_id })
3474+
.collect()
34733475
}
34743476

34753477
fn lower_item_id_use_tree(&mut self,
34763478
tree: &UseTree,
34773479
base_id: NodeId,
3478-
vec: &mut SmallVec<[hir::ItemId; 1]>)
3480+
vec: &mut SmallVec<[NodeId; 1]>)
34793481
{
34803482
match tree.kind {
34813483
UseTreeKind::Nested(ref nested_vec) => for &(ref nested, id) in nested_vec {
3482-
vec.push(hir::ItemId { id });
3484+
vec.push(id);
34833485
self.lower_item_id_use_tree(nested, id, vec);
34843486
},
34853487
UseTreeKind::Glob => {}
@@ -3488,7 +3490,7 @@ impl<'a> LoweringContext<'a> {
34883490
.skip(1)
34893491
.zip([id1, id2].iter())
34903492
{
3491-
vec.push(hir::ItemId { id });
3493+
vec.push(id);
34923494
}
34933495
},
34943496
}
@@ -4604,6 +4606,7 @@ impl<'a> LoweringContext<'a> {
46044606
let mut ids: SmallVec<[hir::Stmt; 1]> = item_ids
46054607
.into_iter()
46064608
.map(|item_id| {
4609+
let item_id = hir::ItemId { id: self.lower_node_id(item_id).hir_id };
46074610
let LoweredNodeId { node_id: _, hir_id } = self.next_id();
46084611

46094612
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 {
@@ -1292,7 +1292,7 @@ pub fn map_crate<'hir>(sess: &crate::session::Session,
12921292
impl<'hir> print::PpAnn for Map<'hir> {
12931293
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) -> io::Result<()> {
12941294
match nested {
1295-
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
1295+
Nested::Item(id) => state.print_item(self.expect_item_by_hir_id(id.id)),
12961296
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
12971297
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
12981298
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<()> {

src/librustc/middle/resolve_lifetime.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
638638
// `abstract type MyAnonTy<'b>: MyTrait<'b>;`
639639
// ^ ^ this gets resolved in the scope of
640640
// the exist_ty generics
641-
let (generics, bounds) = match self.tcx.hir().expect_item(item_id.id).node {
641+
let (generics, bounds) = match self.tcx.hir().expect_item_by_hir_id(item_id.id).node
642+
{
642643
// named existential types are reached via TyKind::Path
643644
// this arm is for `impl Trait` in the types of statics, constants and locals
644645
hir::ItemKind::Existential(hir::ExistTy {
@@ -678,8 +679,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
678679
let parent_impl_id = hir::ImplItemId { hir_id: parent_id };
679680
let parent_trait_id = hir::TraitItemId { hir_id: parent_id };
680681
let krate = self.tcx.hir().forest.krate();
681-
let parent_node_id = self.tcx.hir().hir_to_node_id(parent_id);
682-
if !(krate.items.contains_key(&parent_node_id)
682+
683+
if !(krate.items.contains_key(&parent_id)
683684
|| krate.impl_items.contains_key(&parent_impl_id)
684685
|| krate.trait_items.contains_key(&parent_trait_id))
685686
{

src/librustc_metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
692692
span: self.lazy(&tcx.def_span(def_id)),
693693
attributes: self.encode_attributes(attrs),
694694
children: self.lazy_seq(md.item_ids.iter().map(|item_id| {
695-
tcx.hir().local_def_id(item_id.id).index
695+
tcx.hir().local_def_id_from_hir_id(item_id.id).index
696696
})),
697697
stability: self.encode_stability(def_id),
698698
deprecation: self.encode_deprecation(def_id),

src/librustc_privacy/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,8 @@ impl<'a, 'tcx> EmbargoVisitor<'a, 'tcx> {
472472
{
473473
if let hir::ItemKind::Mod(m) = &item.node {
474474
for item_id in m.item_ids.as_ref() {
475-
let item = self.tcx.hir().expect_item(item_id.id);
476-
let def_id = self.tcx.hir().local_def_id(item_id.id);
475+
let item = self.tcx.hir().expect_item_by_hir_id(item_id.id);
476+
let def_id = self.tcx.hir().local_def_id_from_hir_id(item_id.id);
477477
if !self.tcx.hygienic_eq(segment.ident, item.ident, def_id) { continue; }
478478
if let hir::ItemKind::Use(..) = item.node {
479479
self.update(item.hir_id, Some(AccessLevel::Exported));
@@ -737,8 +737,7 @@ impl<'a, 'tcx> Visitor<'tcx> for EmbargoVisitor<'a, 'tcx> {
737737
unreachable!()
738738
};
739739
for id in &module.item_ids {
740-
let hir_id = self.tcx.hir().node_to_hir_id(id.id);
741-
self.update(hir_id, level);
740+
self.update(id.id, level);
742741
}
743742
let def_id = self.tcx.hir().local_def_id_from_hir_id(module_id);
744743
if let Some(exports) = self.tcx.module_exports(def_id) {

src/librustc_typeck/astconv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1817,7 +1817,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
18171817
self.def_to_ty(opt_self_ty, path, false)
18181818
}
18191819
hir::TyKind::Def(item_id, ref lifetimes) => {
1820-
let did = tcx.hir().local_def_id(item_id.id);
1820+
let did = tcx.hir().local_def_id_from_hir_id(item_id.id);
18211821
self.impl_trait_ty_to_ty(did, lifetimes)
18221822
},
18231823
hir::TyKind::Path(hir::QPath::TypeRelative(ref qself, ref segment)) => {

src/librustc_typeck/check/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ impl<'a, 'tcx, 'gcx> hir::intravisit::Visitor<'tcx> for UsePlacementFinder<'a, '
853853
}
854854
// Find a `use` statement.
855855
for item_id in &module.item_ids {
856-
let item = self.tcx.hir().expect_item(item_id.id);
856+
let item = self.tcx.hir().expect_item_by_hir_id(item_id.id);
857857
match item.node {
858858
hir::ItemKind::Use(..) => {
859859
// Don't suggest placing a `use` before the prelude

src/librustdoc/clean/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,16 @@ impl Clean<ExternalCrate> for CrateNum {
278278
};
279279
let primitives = if root.is_local() {
280280
cx.tcx.hir().krate().module.item_ids.iter().filter_map(|&id| {
281-
let item = cx.tcx.hir().expect_item(id.id);
281+
let item = cx.tcx.hir().expect_item_by_hir_id(id.id);
282282
match item.node {
283283
hir::ItemKind::Mod(_) => {
284-
as_primitive(Def::Mod(cx.tcx.hir().local_def_id(id.id)))
284+
as_primitive(Def::Mod(cx.tcx.hir().local_def_id_from_hir_id(id.id)))
285285
}
286286
hir::ItemKind::Use(ref path, hir::UseKind::Single)
287287
if item.vis.node.is_pub() => {
288288
as_primitive(path.def).map(|(_, prim, attrs)| {
289289
// Pretend the primitive is local.
290-
(cx.tcx.hir().local_def_id(id.id), prim, attrs)
290+
(cx.tcx.hir().local_def_id_from_hir_id(id.id), prim, attrs)
291291
})
292292
}
293293
_ => None
@@ -320,15 +320,15 @@ impl Clean<ExternalCrate> for CrateNum {
320320
};
321321
let keywords = if root.is_local() {
322322
cx.tcx.hir().krate().module.item_ids.iter().filter_map(|&id| {
323-
let item = cx.tcx.hir().expect_item(id.id);
323+
let item = cx.tcx.hir().expect_item_by_hir_id(id.id);
324324
match item.node {
325325
hir::ItemKind::Mod(_) => {
326-
as_keyword(Def::Mod(cx.tcx.hir().local_def_id(id.id)))
326+
as_keyword(Def::Mod(cx.tcx.hir().local_def_id_from_hir_id(id.id)))
327327
}
328328
hir::ItemKind::Use(ref path, hir::UseKind::Single)
329329
if item.vis.node.is_pub() => {
330330
as_keyword(path.def).map(|(_, prim, attrs)| {
331-
(cx.tcx.hir().local_def_id(id.id), prim, attrs)
331+
(cx.tcx.hir().local_def_id_from_hir_id(id.id), prim, attrs)
332332
})
333333
}
334334
_ => None
@@ -2584,7 +2584,7 @@ impl Clean<Type> for hir::Ty {
25842584
},
25852585
TyKind::Tup(ref tys) => Tuple(tys.clean(cx)),
25862586
TyKind::Def(item_id, _) => {
2587-
let item = cx.tcx.hir().expect_item(item_id.id);
2587+
let item = cx.tcx.hir().expect_item_by_hir_id(item_id.id);
25882588
if let hir::ItemKind::Existential(ref ty) = item.node {
25892589
ImplTrait(ty.bounds.clean(cx))
25902590
} else {
@@ -4212,10 +4212,10 @@ pub fn path_to_def_local(tcx: &TyCtxt<'_, '_, '_>, path: &[&str]) -> Option<DefI
42124212
let segment = path_it.next()?;
42134213

42144214
for item_id in mem::replace(&mut items, HirVec::new()).iter() {
4215-
let item = tcx.hir().expect_item(item_id.id);
4215+
let item = tcx.hir().expect_item_by_hir_id(item_id.id);
42164216
if item.ident.name == *segment {
42174217
if path_it.peek().is_none() {
4218-
return Some(tcx.hir().local_def_id(item_id.id))
4218+
return Some(tcx.hir().local_def_id_from_hir_id(item_id.id))
42194219
}
42204220

42214221
items = match &item.node {

0 commit comments

Comments
 (0)