Skip to content

Commit 43294c6

Browse files
committed
middle: HirIdify dead
1 parent 4543fc2 commit 43294c6

File tree

1 file changed

+59
-58
lines changed

1 file changed

+59
-58
lines changed

src/librustc/middle/dead.rs

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use syntax_pos;
2626
// function, then we should explore its block to check for codes that
2727
// may need to be marked as live.
2828
fn should_explore<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
29-
node_id: ast::NodeId) -> bool {
30-
match tcx.hir().find(node_id) {
29+
hir_id: hir::HirId) -> bool {
30+
match tcx.hir().find_by_hir_id(hir_id) {
3131
Some(Node::Item(..)) |
3232
Some(Node::ImplItem(..)) |
3333
Some(Node::ForeignItem(..)) |
@@ -39,33 +39,33 @@ fn should_explore<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
3939
}
4040

4141
struct MarkSymbolVisitor<'a, 'tcx: 'a> {
42-
worklist: Vec<ast::NodeId>,
42+
worklist: Vec<hir::HirId>,
4343
tcx: TyCtxt<'a, 'tcx, 'tcx>,
4444
tables: &'a ty::TypeckTables<'tcx>,
45-
live_symbols: FxHashSet<ast::NodeId>,
45+
live_symbols: FxHashSet<hir::HirId>,
4646
repr_has_repr_c: bool,
4747
in_pat: bool,
4848
inherited_pub_visibility: bool,
4949
ignore_variant_stack: Vec<DefId>,
5050
// maps from tuple struct constructors to tuple struct items
51-
struct_constructors: FxHashMap<ast::NodeId, ast::NodeId>,
51+
struct_constructors: FxHashMap<hir::HirId, hir::HirId>,
5252
}
5353

5454
impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
5555
fn check_def_id(&mut self, def_id: DefId) {
56-
if let Some(node_id) = self.tcx.hir().as_local_node_id(def_id) {
57-
if should_explore(self.tcx, node_id) ||
58-
self.struct_constructors.contains_key(&node_id) {
59-
self.worklist.push(node_id);
56+
if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) {
57+
if should_explore(self.tcx, hir_id) ||
58+
self.struct_constructors.contains_key(&hir_id) {
59+
self.worklist.push(hir_id);
6060
}
61-
self.live_symbols.insert(node_id);
61+
self.live_symbols.insert(hir_id);
6262
}
6363
}
6464

6565
fn insert_def_id(&mut self, def_id: DefId) {
66-
if let Some(node_id) = self.tcx.hir().as_local_node_id(def_id) {
67-
debug_assert!(!should_explore(self.tcx, node_id));
68-
self.live_symbols.insert(node_id);
66+
if let Some(hir_id) = self.tcx.hir().as_local_hir_id(def_id) {
67+
debug_assert!(!should_explore(self.tcx, hir_id));
68+
self.live_symbols.insert(hir_id);
6969
}
7070
}
7171

@@ -136,7 +136,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
136136
// tuple struct constructor function
137137
let id = self.struct_constructors.get(&id).cloned().unwrap_or(id);
138138

139-
if let Some(node) = self.tcx.hir().find(id) {
139+
if let Some(node) = self.tcx.hir().find_by_hir_id(id) {
140140
self.live_symbols.insert(id);
141141
self.visit_node(node);
142142
}
@@ -217,7 +217,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
217217
let live_fields = def.fields().iter().filter(|f| {
218218
has_repr_c || inherited_pub_visibility || f.vis.node.is_pub()
219219
});
220-
self.live_symbols.extend(live_fields.map(|f| f.id));
220+
self.live_symbols.extend(live_fields.map(|f| f.hir_id));
221221

222222
intravisit::walk_struct_def(self, def);
223223
}
@@ -285,7 +285,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
285285
}
286286

287287
fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_, '_, '_>,
288-
id: ast::NodeId,
288+
id: hir::HirId,
289289
attrs: &[ast::Attribute]) -> bool {
290290
if attr::contains_name(attrs, "lang") {
291291
return true;
@@ -306,7 +306,7 @@ fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_, '_, '_>,
306306
return true;
307307
}
308308

309-
let def_id = tcx.hir().local_def_id(id);
309+
let def_id = tcx.hir().local_def_id_from_hir_id(id);
310310
let cg_attrs = tcx.codegen_fn_attrs(def_id);
311311

312312
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive
@@ -333,25 +333,25 @@ fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_, '_, '_>,
333333
// 2) We are not sure to be live or not
334334
// * Implementation of a trait method
335335
struct LifeSeeder<'k, 'tcx: 'k> {
336-
worklist: Vec<ast::NodeId>,
336+
worklist: Vec<hir::HirId>,
337337
krate: &'k hir::Crate,
338338
tcx: TyCtxt<'k, 'tcx, 'tcx>,
339339
// see `MarkSymbolVisitor::struct_constructors`
340-
struct_constructors: FxHashMap<ast::NodeId, ast::NodeId>,
340+
struct_constructors: FxHashMap<hir::HirId, hir::HirId>,
341341
}
342342

343343
impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
344344
fn visit_item(&mut self, item: &hir::Item) {
345345
let allow_dead_code = has_allow_dead_code_or_lang_attr(self.tcx,
346-
item.id,
346+
item.hir_id,
347347
&item.attrs);
348348
if allow_dead_code {
349-
self.worklist.push(item.id);
349+
self.worklist.push(item.hir_id);
350350
}
351351
match item.node {
352352
hir::ItemKind::Enum(ref enum_def, _) if allow_dead_code => {
353353
self.worklist.extend(enum_def.variants.iter()
354-
.map(|variant| variant.node.data.id()));
354+
.map(|variant| variant.node.data.hir_id()));
355355
}
356356
hir::ItemKind::Trait(.., ref trait_item_refs) => {
357357
for trait_item_ref in trait_item_refs {
@@ -360,9 +360,9 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
360360
hir::TraitItemKind::Const(_, Some(_)) |
361361
hir::TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
362362
if has_allow_dead_code_or_lang_attr(self.tcx,
363-
trait_item.id,
363+
trait_item.hir_id,
364364
&trait_item.attrs) {
365-
self.worklist.push(trait_item.id);
365+
self.worklist.push(trait_item.hir_id);
366366
}
367367
}
368368
_ => {}
@@ -374,14 +374,14 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
374374
let impl_item = self.krate.impl_item(impl_item_ref.id);
375375
if opt_trait.is_some() ||
376376
has_allow_dead_code_or_lang_attr(self.tcx,
377-
impl_item.id,
377+
impl_item.hir_id,
378378
&impl_item.attrs) {
379-
self.worklist.push(impl_item_ref.id.node_id);
379+
self.worklist.push(self.tcx.hir().node_to_hir_id(impl_item_ref.id.node_id));
380380
}
381381
}
382382
}
383383
hir::ItemKind::Struct(ref variant_data, _) => {
384-
self.struct_constructors.insert(variant_data.id(), item.id);
384+
self.struct_constructors.insert(variant_data.hir_id(), item.hir_id);
385385
}
386386
_ => ()
387387
}
@@ -400,16 +400,16 @@ fn create_and_seed_worklist<'a, 'tcx>(
400400
tcx: TyCtxt<'a, 'tcx, 'tcx>,
401401
access_levels: &privacy::AccessLevels,
402402
krate: &hir::Crate,
403-
) -> (Vec<ast::NodeId>, FxHashMap<ast::NodeId, ast::NodeId>) {
403+
) -> (Vec<hir::HirId>, FxHashMap<hir::HirId, hir::HirId>) {
404404
let worklist = access_levels.map.iter().filter_map(|(&id, level)| {
405405
if level >= &privacy::AccessLevel::Reachable {
406-
Some(id)
406+
Some(tcx.hir().node_to_hir_id(id))
407407
} else {
408408
None
409409
}
410410
}).chain(
411411
// Seed entry point
412-
tcx.entry_fn(LOCAL_CRATE).map(|(def_id, _)| tcx.hir().as_local_node_id(def_id).unwrap())
412+
tcx.entry_fn(LOCAL_CRATE).map(|(def_id, _)| tcx.hir().as_local_hir_id(def_id).unwrap())
413413
).collect::<Vec<_>>();
414414

415415
// Seed implemented trait items
@@ -427,7 +427,7 @@ fn create_and_seed_worklist<'a, 'tcx>(
427427
fn find_live<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
428428
access_levels: &privacy::AccessLevels,
429429
krate: &hir::Crate)
430-
-> FxHashSet<ast::NodeId> {
430+
-> FxHashSet<hir::HirId> {
431431
let (worklist, struct_constructors) = create_and_seed_worklist(tcx, access_levels, krate);
432432
let mut symbol_visitor = MarkSymbolVisitor {
433433
worklist,
@@ -446,7 +446,7 @@ fn find_live<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
446446

447447
struct DeadVisitor<'a, 'tcx: 'a> {
448448
tcx: TyCtxt<'a, 'tcx, 'tcx>,
449-
live_symbols: FxHashSet<ast::NodeId>,
449+
live_symbols: FxHashSet<hir::HirId>,
450450
}
451451

452452
impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
@@ -461,33 +461,33 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
461461
| hir::ItemKind::Union(..) => true,
462462
_ => false
463463
};
464-
should_warn && !self.symbol_is_live(item.id)
464+
should_warn && !self.symbol_is_live(item.hir_id)
465465
}
466466

467467
fn should_warn_about_field(&mut self, field: &hir::StructField) -> bool {
468468
let field_type = self.tcx.type_of(self.tcx.hir().local_def_id(field.id));
469469
!field.is_positional()
470-
&& !self.symbol_is_live(field.id)
470+
&& !self.symbol_is_live(field.hir_id)
471471
&& !field_type.is_phantom_data()
472-
&& !has_allow_dead_code_or_lang_attr(self.tcx, field.id, &field.attrs)
472+
&& !has_allow_dead_code_or_lang_attr(self.tcx, field.hir_id, &field.attrs)
473473
}
474474

475475
fn should_warn_about_variant(&mut self, variant: &hir::VariantKind) -> bool {
476-
!self.symbol_is_live(variant.data.id())
476+
!self.symbol_is_live(variant.data.hir_id())
477477
&& !has_allow_dead_code_or_lang_attr(self.tcx,
478-
variant.data.id(),
478+
variant.data.hir_id(),
479479
&variant.attrs)
480480
}
481481

482482
fn should_warn_about_foreign_item(&mut self, fi: &hir::ForeignItem) -> bool {
483-
!self.symbol_is_live(fi.id)
484-
&& !has_allow_dead_code_or_lang_attr(self.tcx, fi.id, &fi.attrs)
483+
!self.symbol_is_live(fi.hir_id)
484+
&& !has_allow_dead_code_or_lang_attr(self.tcx, fi.hir_id, &fi.attrs)
485485
}
486486

487-
// id := node id of an item's definition.
487+
// id := HIR id of an item's definition.
488488
fn symbol_is_live(
489489
&mut self,
490-
id: ast::NodeId,
490+
id: hir::HirId,
491491
) -> bool {
492492
if self.live_symbols.contains(&id) {
493493
return true;
@@ -496,12 +496,12 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
496496
// This is done to handle the case where, for example, the static
497497
// method of a private type is used, but the type itself is never
498498
// called directly.
499-
let def_id = self.tcx.hir().local_def_id(id);
499+
let def_id = self.tcx.hir().local_def_id_from_hir_id(id);
500500
let inherent_impls = self.tcx.inherent_impls(def_id);
501501
for &impl_did in inherent_impls.iter() {
502502
for &item_did in &self.tcx.associated_item_def_ids(impl_did)[..] {
503-
if let Some(item_node_id) = self.tcx.hir().as_local_node_id(item_did) {
504-
if self.live_symbols.contains(&item_node_id) {
503+
if let Some(item_hir_id) = self.tcx.hir().as_local_hir_id(item_did) {
504+
if self.live_symbols.contains(&item_hir_id) {
505505
return true;
506506
}
507507
}
@@ -511,18 +511,18 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
511511
}
512512

513513
fn warn_dead_code(&mut self,
514-
id: ast::NodeId,
514+
id: hir::HirId,
515515
span: syntax_pos::Span,
516516
name: ast::Name,
517517
node_type: &str,
518518
participle: &str) {
519519
if !name.as_str().starts_with("_") {
520520
self.tcx
521-
.lint_node(lint::builtin::DEAD_CODE,
522-
id,
523-
span,
524-
&format!("{} is never {}: `{}`",
525-
node_type, participle, name));
521+
.lint_hir(lint::builtin::DEAD_CODE,
522+
id,
523+
span,
524+
&format!("{} is never {}: `{}`",
525+
node_type, participle, name));
526526
}
527527
}
528528
}
@@ -555,7 +555,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
555555
_ => "used"
556556
};
557557
self.warn_dead_code(
558-
item.id,
558+
item.hir_id,
559559
span,
560560
item.ident.name,
561561
item.node.descriptive_variant(),
@@ -572,7 +572,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
572572
g: &'tcx hir::Generics,
573573
id: hir::HirId) {
574574
if self.should_warn_about_variant(&variant.node) {
575-
self.warn_dead_code(variant.node.data.id(), variant.span, variant.node.ident.name,
575+
self.warn_dead_code(variant.node.data.hir_id(), variant.span, variant.node.ident.name,
576576
"variant", "constructed");
577577
} else {
578578
intravisit::walk_variant(self, variant, g, id);
@@ -581,24 +581,24 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
581581

582582
fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem) {
583583
if self.should_warn_about_foreign_item(fi) {
584-
self.warn_dead_code(fi.id, fi.span, fi.ident.name,
584+
self.warn_dead_code(fi.hir_id, fi.span, fi.ident.name,
585585
fi.node.descriptive_variant(), "used");
586586
}
587587
intravisit::walk_foreign_item(self, fi);
588588
}
589589

590590
fn visit_struct_field(&mut self, field: &'tcx hir::StructField) {
591591
if self.should_warn_about_field(&field) {
592-
self.warn_dead_code(field.id, field.span, field.ident.name, "field", "used");
592+
self.warn_dead_code(field.hir_id, field.span, field.ident.name, "field", "used");
593593
}
594594
intravisit::walk_struct_field(self, field);
595595
}
596596

597597
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
598598
match impl_item.node {
599599
hir::ImplItemKind::Const(_, body_id) => {
600-
if !self.symbol_is_live(impl_item.id) {
601-
self.warn_dead_code(impl_item.id,
600+
if !self.symbol_is_live(impl_item.hir_id) {
601+
self.warn_dead_code(impl_item.hir_id,
602602
impl_item.span,
603603
impl_item.ident.name,
604604
"associated const",
@@ -607,9 +607,10 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
607607
self.visit_nested_body(body_id)
608608
}
609609
hir::ImplItemKind::Method(_, body_id) => {
610-
if !self.symbol_is_live(impl_item.id) {
610+
if !self.symbol_is_live(impl_item.hir_id) {
611611
let span = self.tcx.sess.source_map().def_span(impl_item.span);
612-
self.warn_dead_code(impl_item.id, span, impl_item.ident.name, "method", "used");
612+
self.warn_dead_code(impl_item.hir_id, span, impl_item.ident.name, "method",
613+
"used");
613614
}
614615
self.visit_nested_body(body_id)
615616
}

0 commit comments

Comments
 (0)