Skip to content

Commit 6536dbb

Browse files
committed
hir: HirId-ify intravisit
1 parent 74e35d2 commit 6536dbb

File tree

20 files changed

+216
-211
lines changed

20 files changed

+216
-211
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 66 additions & 67 deletions
Large diffs are not rendered by default.

src/librustc/hir/lowering.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3301,7 +3301,9 @@ impl<'a> LoweringContext<'a> {
33013301
let mut path = path.clone();
33023302
for seg in path.segments.iter_mut() {
33033303
if seg.id.is_some() {
3304-
seg.id = Some(self.next_id().node_id);
3304+
let next_id = self.next_id();
3305+
seg.id = Some(next_id.node_id);
3306+
seg.hir_id = Some(next_id.hir_id);
33053307
}
33063308
}
33073309
path

src/librustc/hir/map/collector.rs

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
228228
self.map[id.as_usize()] = Some(entry);
229229
}
230230

231-
fn insert(&mut self, span: Span, id: NodeId, node: Node<'hir>) {
231+
// FIXME(ljedrz): devise a way to get rid of this NodeId
232+
fn insert(&mut self, span: Span, node_id: NodeId, hir_id: HirId, node: Node<'hir>) {
232233
let entry = Entry {
233234
parent: self.parent_node,
234235
parent_hir: self.parent_hir,
@@ -243,18 +244,18 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
243244
// Make sure that the DepNode of some node coincides with the HirId
244245
// owner of that node.
245246
if cfg!(debug_assertions) {
246-
let hir_id = self.definitions.node_to_hir_id(id);
247+
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);
247248

248249
if hir_id.owner != self.current_dep_node_owner {
249-
let node_str = match self.definitions.opt_def_index(id) {
250+
let node_str = match self.definitions.opt_def_index(node_id) {
250251
Some(def_index) => {
251252
self.definitions.def_path(def_index).to_string_no_crate()
252253
}
253254
None => format!("{:?}", node)
254255
};
255256

256257
let forgot_str = if hir_id == crate::hir::DUMMY_HIR_ID {
257-
format!("\nMaybe you forgot to lower the node id {:?}?", id)
258+
format!("\nMaybe you forgot to lower the node id {:?}?", node_id)
258259
} else {
259260
String::new()
260261
};
@@ -276,13 +277,21 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
276277
}
277278
}
278279

279-
self.insert_entry(id, entry);
280+
self.insert_entry(node_id, entry);
280281
}
281282

282-
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_id: NodeId, f: F) {
283+
fn with_parent<F: FnOnce(&mut Self)>(
284+
&mut self,
285+
parent_node_id: NodeId,
286+
parent_hir_id: HirId,
287+
f: F,
288+
) {
283289
let parent_node = self.parent_node;
284-
self.parent_node = parent_id;
290+
self.parent_node = parent_node_id;
291+
let parent_hir = self.parent_hir;
292+
self.parent_hir = parent_hir_id;
285293
f(self);
294+
self.parent_hir = parent_hir;
286295
self.parent_node = parent_node;
287296
}
288297

@@ -352,12 +361,13 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
352361
debug_assert_eq!(i.hir_id.owner,
353362
self.definitions.opt_def_index(i.id).unwrap());
354363
self.with_dep_node_owner(i.hir_id.owner, i, |this| {
355-
this.insert(i.span, i.id, Node::Item(i));
356-
this.with_parent(i.id, |this| {
364+
this.insert(i.span, i.id, i.hir_id, Node::Item(i));
365+
this.with_parent(i.id, i.hir_id, |this| {
357366
if let ItemKind::Struct(ref struct_def, _) = i.node {
358367
// If this is a tuple-like struct, register the constructor.
359368
if !struct_def.is_struct() {
360-
this.insert(i.span, struct_def.id(), Node::StructCtor(struct_def));
369+
this.insert(i.span, struct_def.id(), struct_def.hir_id(),
370+
Node::StructCtor(struct_def));
361371
}
362372
}
363373
intravisit::walk_item(this, i);
@@ -366,25 +376,26 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
366376
}
367377

368378
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem) {
369-
self.insert(foreign_item.span, foreign_item.id, Node::ForeignItem(foreign_item));
379+
self.insert(foreign_item.span, foreign_item.id, foreign_item.hir_id,
380+
Node::ForeignItem(foreign_item));
370381

371-
self.with_parent(foreign_item.id, |this| {
382+
self.with_parent(foreign_item.id, foreign_item.hir_id, |this| {
372383
intravisit::walk_foreign_item(this, foreign_item);
373384
});
374385
}
375386

376387
fn visit_generic_param(&mut self, param: &'hir GenericParam) {
377-
self.insert(param.span, param.id, Node::GenericParam(param));
388+
self.insert(param.span, param.id, param.hir_id, Node::GenericParam(param));
378389
intravisit::walk_generic_param(self, param);
379390
}
380391

381392
fn visit_trait_item(&mut self, ti: &'hir TraitItem) {
382393
debug_assert_eq!(ti.hir_id.owner,
383394
self.definitions.opt_def_index(ti.id).unwrap());
384395
self.with_dep_node_owner(ti.hir_id.owner, ti, |this| {
385-
this.insert(ti.span, ti.id, Node::TraitItem(ti));
396+
this.insert(ti.span, ti.id, ti.hir_id, Node::TraitItem(ti));
386397

387-
this.with_parent(ti.id, |this| {
398+
this.with_parent(ti.id, ti.hir_id, |this| {
388399
intravisit::walk_trait_item(this, ti);
389400
});
390401
});
@@ -394,9 +405,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
394405
debug_assert_eq!(ii.hir_id.owner,
395406
self.definitions.opt_def_index(ii.id).unwrap());
396407
self.with_dep_node_owner(ii.hir_id.owner, ii, |this| {
397-
this.insert(ii.span, ii.id, Node::ImplItem(ii));
408+
this.insert(ii.span, ii.id, ii.hir_id, Node::ImplItem(ii));
398409

399-
this.with_parent(ii.id, |this| {
410+
this.with_parent(ii.id, ii.hir_id, |this| {
400411
intravisit::walk_impl_item(this, ii);
401412
});
402413
});
@@ -408,93 +419,93 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
408419
} else {
409420
Node::Pat(pat)
410421
};
411-
self.insert(pat.span, pat.id, node);
422+
self.insert(pat.span, pat.id, pat.hir_id, node);
412423

413-
self.with_parent(pat.id, |this| {
424+
self.with_parent(pat.id, pat.hir_id, |this| {
414425
intravisit::walk_pat(this, pat);
415426
});
416427
}
417428

418429
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
419-
self.insert(DUMMY_SP, constant.id, Node::AnonConst(constant));
430+
self.insert(DUMMY_SP, constant.id, constant.hir_id, Node::AnonConst(constant));
420431

421-
self.with_parent(constant.id, |this| {
432+
self.with_parent(constant.id, constant.hir_id, |this| {
422433
intravisit::walk_anon_const(this, constant);
423434
});
424435
}
425436

426437
fn visit_expr(&mut self, expr: &'hir Expr) {
427-
self.insert(expr.span, expr.id, Node::Expr(expr));
438+
self.insert(expr.span, expr.id, expr.hir_id, Node::Expr(expr));
428439

429-
self.with_parent(expr.id, |this| {
440+
self.with_parent(expr.id, expr.hir_id, |this| {
430441
intravisit::walk_expr(this, expr);
431442
});
432443
}
433444

434445
fn visit_stmt(&mut self, stmt: &'hir Stmt) {
435-
let id = stmt.id;
436-
self.insert(stmt.span, id, Node::Stmt(stmt));
446+
self.insert(stmt.span, stmt.id, stmt.hir_id, Node::Stmt(stmt));
437447

438-
self.with_parent(id, |this| {
448+
self.with_parent(stmt.id, stmt.hir_id, |this| {
439449
intravisit::walk_stmt(this, stmt);
440450
});
441451
}
442452

443453
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment) {
444-
if let Some(id) = path_segment.id {
445-
self.insert(path_span, id, Node::PathSegment(path_segment));
454+
if let Some(node_id) = path_segment.id {
455+
let hir_id = path_segment.hir_id.unwrap();
456+
self.insert(path_span, node_id, hir_id, Node::PathSegment(path_segment));
446457
}
447458
intravisit::walk_path_segment(self, path_span, path_segment);
448459
}
449460

450461
fn visit_ty(&mut self, ty: &'hir Ty) {
451-
self.insert(ty.span, ty.id, Node::Ty(ty));
462+
self.insert(ty.span, ty.id, ty.hir_id, Node::Ty(ty));
452463

453-
self.with_parent(ty.id, |this| {
464+
self.with_parent(ty.id, ty.hir_id, |this| {
454465
intravisit::walk_ty(this, ty);
455466
});
456467
}
457468

458469
fn visit_trait_ref(&mut self, tr: &'hir TraitRef) {
459-
self.insert(tr.path.span, tr.ref_id, Node::TraitRef(tr));
470+
self.insert(tr.path.span, tr.ref_id, tr.hir_ref_id, Node::TraitRef(tr));
460471

461-
self.with_parent(tr.ref_id, |this| {
472+
self.with_parent(tr.ref_id, tr.hir_ref_id, |this| {
462473
intravisit::walk_trait_ref(this, tr);
463474
});
464475
}
465476

466477
fn visit_fn(&mut self, fk: intravisit::FnKind<'hir>, fd: &'hir FnDecl,
467-
b: BodyId, s: Span, id: NodeId) {
468-
assert_eq!(self.parent_node, id);
478+
b: BodyId, s: Span, id: HirId) {
479+
assert_eq!(self.parent_hir, id);
469480
intravisit::walk_fn(self, fk, fd, b, s, id);
470481
}
471482

472483
fn visit_block(&mut self, block: &'hir Block) {
473-
self.insert(block.span, block.id, Node::Block(block));
474-
self.with_parent(block.id, |this| {
484+
self.insert(block.span, block.id, block.hir_id, Node::Block(block));
485+
self.with_parent(block.id, block.hir_id, |this| {
475486
intravisit::walk_block(this, block);
476487
});
477488
}
478489

479490
fn visit_local(&mut self, l: &'hir Local) {
480-
self.insert(l.span, l.id, Node::Local(l));
481-
self.with_parent(l.id, |this| {
491+
self.insert(l.span, l.id, l.hir_id, Node::Local(l));
492+
self.with_parent(l.id, l.hir_id, |this| {
482493
intravisit::walk_local(this, l)
483494
})
484495
}
485496

486497
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
487-
self.insert(lifetime.span, lifetime.id, Node::Lifetime(lifetime));
498+
self.insert(lifetime.span, lifetime.id, lifetime.hir_id, Node::Lifetime(lifetime));
488499
}
489500

490501
fn visit_vis(&mut self, visibility: &'hir Visibility) {
491502
match visibility.node {
492503
VisibilityKind::Public |
493504
VisibilityKind::Crate(_) |
494505
VisibilityKind::Inherited => {}
495-
VisibilityKind::Restricted { id, .. } => {
496-
self.insert(visibility.span, id, Node::Visibility(visibility));
497-
self.with_parent(id, |this| {
506+
VisibilityKind::Restricted { id, hir_id, .. } => {
507+
self.insert(visibility.span, id, hir_id, Node::Visibility(visibility));
508+
self.with_parent(id, hir_id, |this| {
498509
intravisit::walk_vis(this, visibility);
499510
});
500511
}
@@ -505,21 +516,20 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
505516
let def_index = self.definitions.opt_def_index(macro_def.id).unwrap();
506517

507518
self.with_dep_node_owner(def_index, macro_def, |this| {
508-
this.insert(macro_def.span, macro_def.id, Node::MacroDef(macro_def));
519+
this.insert(macro_def.span, macro_def.id, macro_def.hir_id, Node::MacroDef(macro_def));
509520
});
510521
}
511522

512-
fn visit_variant(&mut self, v: &'hir Variant, g: &'hir Generics, item_id: NodeId) {
513-
let id = v.node.data.id();
514-
self.insert(v.span, id, Node::Variant(v));
515-
self.with_parent(id, |this| {
523+
fn visit_variant(&mut self, v: &'hir Variant, g: &'hir Generics, item_id: HirId) {
524+
self.insert(v.span, v.node.data.id(), v.node.data.hir_id(), Node::Variant(v));
525+
self.with_parent(v.node.data.id(), v.node.data.hir_id(), |this| {
516526
intravisit::walk_variant(this, v, g, item_id);
517527
});
518528
}
519529

520530
fn visit_struct_field(&mut self, field: &'hir StructField) {
521-
self.insert(field.span, field.id, Node::Field(field));
522-
self.with_parent(field.id, |this| {
531+
self.insert(field.span, field.id, field.hir_id, Node::Field(field));
532+
self.with_parent(field.id, field.hir_id, |this| {
523533
intravisit::walk_struct_field(this, field);
524534
});
525535
}

src/librustc/hir/map/hir_id_validator.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ impl<'a, 'hir: 'a> OuterVisitor<'a, 'hir> {
5555
impl<'a, 'hir: 'a> ItemLikeVisitor<'hir> for OuterVisitor<'a, 'hir> {
5656
fn visit_item(&mut self, i: &'hir hir::Item) {
5757
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
58-
inner_visitor.check(i.id, |this| intravisit::walk_item(this, i));
58+
inner_visitor.check(i.hir_id, |this| intravisit::walk_item(this, i));
5959
}
6060

6161
fn visit_trait_item(&mut self, i: &'hir hir::TraitItem) {
6262
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
63-
inner_visitor.check(i.id, |this| intravisit::walk_trait_item(this, i));
63+
inner_visitor.check(i.hir_id, |this| intravisit::walk_trait_item(this, i));
6464
}
6565

6666
fn visit_impl_item(&mut self, i: &'hir hir::ImplItem) {
6767
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
68-
inner_visitor.check(i.id, |this| intravisit::walk_impl_item(this, i));
68+
inner_visitor.check(i.hir_id, |this| intravisit::walk_impl_item(this, i));
6969
}
7070
}
7171

@@ -77,10 +77,10 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
7777
}
7878

7979
fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self,
80-
node_id: NodeId,
80+
hir_id: HirId,
8181
walk: F) {
8282
assert!(self.owner_def_index.is_none());
83-
let owner_def_index = self.hir_map.local_def_id(node_id).index;
83+
let owner_def_index = self.hir_map.local_def_id_from_hir_id(hir_id).index;
8484
self.owner_def_index = Some(owner_def_index);
8585
walk(self);
8686

@@ -147,35 +147,26 @@ impl<'a, 'hir: 'a> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
147147
intravisit::NestedVisitorMap::OnlyBodies(self.hir_map)
148148
}
149149

150-
fn visit_id(&mut self, node_id: NodeId) {
150+
fn visit_id(&mut self, hir_id: HirId) {
151151
let owner = self.owner_def_index.expect("no owner_def_index");
152-
let stable_id = self.hir_map.definitions().node_to_hir_id[node_id];
153152

154-
if stable_id == hir::DUMMY_HIR_ID {
155-
self.error(|| format!("HirIdValidator: No HirId assigned for NodeId {}: {:?}",
156-
node_id,
157-
self.hir_map.node_to_string(node_id)));
153+
if hir_id == hir::DUMMY_HIR_ID {
154+
self.error(|| format!("HirIdValidator: HirId {:?} is invalid",
155+
self.hir_map.hir_to_string(hir_id)));
158156
return;
159157
}
160158

161-
if owner != stable_id.owner {
159+
if owner != hir_id.owner {
162160
self.error(|| format!(
163161
"HirIdValidator: The recorded owner of {} is {} instead of {}",
164-
self.hir_map.node_to_string(node_id),
165-
self.hir_map.def_path(DefId::local(stable_id.owner)).to_string_no_crate(),
162+
self.hir_map.hir_to_string(hir_id),
163+
self.hir_map.def_path(DefId::local(hir_id.owner)).to_string_no_crate(),
166164
self.hir_map.def_path(DefId::local(owner)).to_string_no_crate()));
167165
}
168166

169-
if let Some(prev) = self.hir_ids_seen.insert(stable_id.local_id, node_id) {
170-
if prev != node_id {
171-
self.error(|| format!(
172-
"HirIdValidator: Same HirId {}/{} assigned for nodes {} and {}",
173-
self.hir_map.def_path(DefId::local(stable_id.owner)).to_string_no_crate(),
174-
stable_id.local_id.as_usize(),
175-
self.hir_map.node_to_string(prev),
176-
self.hir_map.node_to_string(node_id)));
177-
}
178-
}
167+
let node_id = self.hir_map.hir_to_node_id(hir_id);
168+
169+
self.hir_ids_seen.insert(hir_id.local_id, node_id);
179170
}
180171

181172
fn visit_impl_item_ref(&mut self, _: &'hir hir::ImplItemRef) {

0 commit comments

Comments
 (0)