Skip to content

Commit f573049

Browse files
committed
Auto merge of #58232 - ljedrz:HirIdification_continued, r=Zoxc
HirId-ify intravisit A big step towards #57578. This affects mostly `hir::{collector, intravisit}` and `rustc::lint`.
2 parents aadbc45 + 404e643 commit f573049

File tree

26 files changed

+254
-254
lines changed

26 files changed

+254
-254
lines changed

src/librustc/hir/intravisit.rs

+66-67
Large diffs are not rendered by default.

src/librustc/hir/lowering.rs

+3-1
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

+59-55
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
2727
/// The node map
2828
map: Vec<Option<Entry<'hir>>>,
2929
/// The parent of this node
30-
parent_node: NodeId,
31-
32-
parent_hir: hir::HirId,
30+
parent_node: hir::HirId,
3331

3432
// These fields keep track of the currently relevant DepNodes during
3533
// the visitor's traversal.
@@ -40,6 +38,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
4038

4139
dep_graph: &'a DepGraph,
4240
definitions: &'a definitions::Definitions,
41+
hir_to_node_id: &'a FxHashMap<HirId, NodeId>,
4342

4443
hcx: StableHashingContext<'a>,
4544

@@ -100,6 +99,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
10099
krate: &'hir Crate,
101100
dep_graph: &'a DepGraph,
102101
definitions: &'a definitions::Definitions,
102+
hir_to_node_id: &'a FxHashMap<HirId, NodeId>,
103103
mut hcx: StableHashingContext<'a>)
104104
-> NodeCollector<'a, 'hir> {
105105
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
@@ -147,14 +147,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
147147
krate,
148148
source_map: sess.source_map(),
149149
map: repeat(None).take(sess.current_node_id_count()).collect(),
150-
parent_node: CRATE_NODE_ID,
151-
parent_hir: hir::CRATE_HIR_ID,
150+
parent_node: hir::CRATE_HIR_ID,
152151
current_signature_dep_index: root_mod_sig_dep_index,
153152
current_full_dep_index: root_mod_full_dep_index,
154153
current_dep_node_owner: CRATE_DEF_INDEX,
155154
currently_in_body: false,
156155
dep_graph,
157156
definitions,
157+
hir_to_node_id,
158158
hcx,
159159
hir_body_nodes,
160160
};
@@ -228,10 +228,10 @@ 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+
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
232232
let entry = Entry {
233-
parent: self.parent_node,
234-
parent_hir: self.parent_hir,
233+
parent: self.hir_to_node_id[&self.parent_node],
234+
parent_hir: self.parent_node,
235235
dep_node: if self.currently_in_body {
236236
self.current_full_dep_index
237237
} else {
@@ -240,21 +240,23 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
240240
node,
241241
};
242242

243+
let node_id = self.hir_to_node_id[&hir_id];
244+
243245
// Make sure that the DepNode of some node coincides with the HirId
244246
// owner of that node.
245247
if cfg!(debug_assertions) {
246-
let hir_id = self.definitions.node_to_hir_id(id);
248+
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);
247249

248250
if hir_id.owner != self.current_dep_node_owner {
249-
let node_str = match self.definitions.opt_def_index(id) {
251+
let node_str = match self.definitions.opt_def_index(node_id) {
250252
Some(def_index) => {
251253
self.definitions.def_path(def_index).to_string_no_crate()
252254
}
253255
None => format!("{:?}", node)
254256
};
255257

256258
let forgot_str = if hir_id == crate::hir::DUMMY_HIR_ID {
257-
format!("\nMaybe you forgot to lower the node id {:?}?", id)
259+
format!("\nMaybe you forgot to lower the node id {:?}?", node_id)
258260
} else {
259261
String::new()
260262
};
@@ -276,12 +278,16 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
276278
}
277279
}
278280

279-
self.insert_entry(id, entry);
281+
self.insert_entry(node_id, entry);
280282
}
281283

282-
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_id: NodeId, f: F) {
284+
fn with_parent<F: FnOnce(&mut Self)>(
285+
&mut self,
286+
parent_node_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;
285291
f(self);
286292
self.parent_node = parent_node;
287293
}
@@ -352,12 +358,12 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
352358
debug_assert_eq!(i.hir_id.owner,
353359
self.definitions.opt_def_index(i.id).unwrap());
354360
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| {
361+
this.insert(i.span, i.hir_id, Node::Item(i));
362+
this.with_parent(i.hir_id, |this| {
357363
if let ItemKind::Struct(ref struct_def, _) = i.node {
358364
// If this is a tuple-like struct, register the constructor.
359365
if !struct_def.is_struct() {
360-
this.insert(i.span, struct_def.id(), Node::StructCtor(struct_def));
366+
this.insert(i.span, struct_def.hir_id(), Node::StructCtor(struct_def));
361367
}
362368
}
363369
intravisit::walk_item(this, i);
@@ -366,25 +372,25 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
366372
}
367373

368374
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem) {
369-
self.insert(foreign_item.span, foreign_item.id, Node::ForeignItem(foreign_item));
375+
self.insert(foreign_item.span, foreign_item.hir_id, Node::ForeignItem(foreign_item));
370376

371-
self.with_parent(foreign_item.id, |this| {
377+
self.with_parent(foreign_item.hir_id, |this| {
372378
intravisit::walk_foreign_item(this, foreign_item);
373379
});
374380
}
375381

376382
fn visit_generic_param(&mut self, param: &'hir GenericParam) {
377-
self.insert(param.span, param.id, Node::GenericParam(param));
383+
self.insert(param.span, param.hir_id, Node::GenericParam(param));
378384
intravisit::walk_generic_param(self, param);
379385
}
380386

381387
fn visit_trait_item(&mut self, ti: &'hir TraitItem) {
382388
debug_assert_eq!(ti.hir_id.owner,
383389
self.definitions.opt_def_index(ti.id).unwrap());
384390
self.with_dep_node_owner(ti.hir_id.owner, ti, |this| {
385-
this.insert(ti.span, ti.id, Node::TraitItem(ti));
391+
this.insert(ti.span, ti.hir_id, Node::TraitItem(ti));
386392

387-
this.with_parent(ti.id, |this| {
393+
this.with_parent(ti.hir_id, |this| {
388394
intravisit::walk_trait_item(this, ti);
389395
});
390396
});
@@ -394,9 +400,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
394400
debug_assert_eq!(ii.hir_id.owner,
395401
self.definitions.opt_def_index(ii.id).unwrap());
396402
self.with_dep_node_owner(ii.hir_id.owner, ii, |this| {
397-
this.insert(ii.span, ii.id, Node::ImplItem(ii));
403+
this.insert(ii.span, ii.hir_id, Node::ImplItem(ii));
398404

399-
this.with_parent(ii.id, |this| {
405+
this.with_parent(ii.hir_id, |this| {
400406
intravisit::walk_impl_item(this, ii);
401407
});
402408
});
@@ -408,93 +414,92 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
408414
} else {
409415
Node::Pat(pat)
410416
};
411-
self.insert(pat.span, pat.id, node);
417+
self.insert(pat.span, pat.hir_id, node);
412418

413-
self.with_parent(pat.id, |this| {
419+
self.with_parent(pat.hir_id, |this| {
414420
intravisit::walk_pat(this, pat);
415421
});
416422
}
417423

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

421-
self.with_parent(constant.id, |this| {
427+
self.with_parent(constant.hir_id, |this| {
422428
intravisit::walk_anon_const(this, constant);
423429
});
424430
}
425431

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

429-
self.with_parent(expr.id, |this| {
435+
self.with_parent(expr.hir_id, |this| {
430436
intravisit::walk_expr(this, expr);
431437
});
432438
}
433439

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

438-
self.with_parent(id, |this| {
443+
self.with_parent(stmt.hir_id, |this| {
439444
intravisit::walk_stmt(this, stmt);
440445
});
441446
}
442447

443448
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));
449+
if let Some(hir_id) = path_segment.hir_id {
450+
self.insert(path_span, hir_id, Node::PathSegment(path_segment));
446451
}
447452
intravisit::walk_path_segment(self, path_span, path_segment);
448453
}
449454

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

453-
self.with_parent(ty.id, |this| {
458+
self.with_parent(ty.hir_id, |this| {
454459
intravisit::walk_ty(this, ty);
455460
});
456461
}
457462

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

461-
self.with_parent(tr.ref_id, |this| {
466+
self.with_parent(tr.hir_ref_id, |this| {
462467
intravisit::walk_trait_ref(this, tr);
463468
});
464469
}
465470

466471
fn visit_fn(&mut self, fk: intravisit::FnKind<'hir>, fd: &'hir FnDecl,
467-
b: BodyId, s: Span, id: NodeId) {
472+
b: BodyId, s: Span, id: HirId) {
468473
assert_eq!(self.parent_node, id);
469474
intravisit::walk_fn(self, fk, fd, b, s, id);
470475
}
471476

472477
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| {
478+
self.insert(block.span, block.hir_id, Node::Block(block));
479+
self.with_parent(block.hir_id, |this| {
475480
intravisit::walk_block(this, block);
476481
});
477482
}
478483

479484
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| {
485+
self.insert(l.span, l.hir_id, Node::Local(l));
486+
self.with_parent(l.hir_id, |this| {
482487
intravisit::walk_local(this, l)
483488
})
484489
}
485490

486491
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
487-
self.insert(lifetime.span, lifetime.id, Node::Lifetime(lifetime));
492+
self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
488493
}
489494

490495
fn visit_vis(&mut self, visibility: &'hir Visibility) {
491496
match visibility.node {
492497
VisibilityKind::Public |
493498
VisibilityKind::Crate(_) |
494499
VisibilityKind::Inherited => {}
495-
VisibilityKind::Restricted { id, .. } => {
496-
self.insert(visibility.span, id, Node::Visibility(visibility));
497-
self.with_parent(id, |this| {
500+
VisibilityKind::Restricted { hir_id, .. } => {
501+
self.insert(visibility.span, hir_id, Node::Visibility(visibility));
502+
self.with_parent(hir_id, |this| {
498503
intravisit::walk_vis(this, visibility);
499504
});
500505
}
@@ -505,21 +510,20 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
505510
let def_index = self.definitions.opt_def_index(macro_def.id).unwrap();
506511

507512
self.with_dep_node_owner(def_index, macro_def, |this| {
508-
this.insert(macro_def.span, macro_def.id, Node::MacroDef(macro_def));
513+
this.insert(macro_def.span, macro_def.hir_id, Node::MacroDef(macro_def));
509514
});
510515
}
511516

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| {
517+
fn visit_variant(&mut self, v: &'hir Variant, g: &'hir Generics, item_id: HirId) {
518+
self.insert(v.span, v.node.data.hir_id(), Node::Variant(v));
519+
self.with_parent(v.node.data.hir_id(), |this| {
516520
intravisit::walk_variant(this, v, g, item_id);
517521
});
518522
}
519523

520524
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| {
525+
self.insert(field.span, field.hir_id, Node::Field(field));
526+
self.with_parent(field.hir_id, |this| {
523527
intravisit::walk_struct_field(this, field);
524528
});
525529
}

0 commit comments

Comments
 (0)