Skip to content

Commit f0cdccd

Browse files
authored
Rollup merge of rust-lang#59096 - ljedrz:HirIdify_AccessLevel, r=Zoxc
middle: replace NodeId with HirId in AccessLevels Pushing the limits of HirIdification (rust-lang#57578). Replaces `NodeId` with `HirId` in `middle::privacy::AccessLevels`. Actually this time I was more successful and cracked it; I probably tried to HirIdify too much at once when I attempted it last time ^^. r? @Zoxc
2 parents cadb47c + 856b081 commit f0cdccd

File tree

8 files changed

+55
-52
lines changed

8 files changed

+55
-52
lines changed

src/librustc/middle/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ fn create_and_seed_worklist<'a, 'tcx>(
414414
) -> (Vec<hir::HirId>, FxHashMap<hir::HirId, hir::HirId>) {
415415
let worklist = access_levels.map.iter().filter_map(|(&id, level)| {
416416
if level >= &privacy::AccessLevel::Reachable {
417-
Some(tcx.hir().node_to_hir_id(id))
417+
Some(id)
418418
} else {
419419
None
420420
}

src/librustc/middle/privacy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
//! outside their scopes. This pass will also generate a set of exported items
33
//! which are available for use externally when compiled as a library.
44
5+
use crate::hir::HirId;
56
use crate::util::nodemap::{DefIdSet, FxHashMap};
67

78
use std::hash::Hash;
89
use std::fmt;
9-
use syntax::ast::NodeId;
1010
use rustc_macros::HashStable;
1111

1212
// Accessibility levels, sorted in ascending order
@@ -27,7 +27,7 @@ pub enum AccessLevel {
2727

2828
// Accessibility levels for reachable HIR nodes
2929
#[derive(Clone)]
30-
pub struct AccessLevels<Id = NodeId> {
30+
pub struct AccessLevels<Id = HirId> {
3131
pub map: FxHashMap<Id, AccessLevel>
3232
}
3333

src/librustc/middle/reachable.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
354354

355355
// We need only trait impls here, not inherent impls, and only non-exported ones
356356
if let hir::ItemKind::Impl(.., Some(ref trait_ref), _, ref impl_item_refs) = item.node {
357-
let node_id = self.tcx.hir().hir_to_node_id(item.hir_id);
358-
if !self.access_levels.is_reachable(node_id) {
357+
if !self.access_levels.is_reachable(item.hir_id) {
359358
self.worklist.extend(impl_item_refs.iter().map(|ii_ref| ii_ref.id.hir_id));
360359

361360
let trait_def_id = match trait_ref.path.def {
@@ -415,7 +414,7 @@ fn reachable_set<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) ->
415414
// use the lang items, so we need to be sure to mark them as
416415
// exported.
417416
reachable_context.worklist.extend(
418-
access_levels.map.iter().map(|(id, _)| tcx.hir().node_to_hir_id(*id)));
417+
access_levels.map.iter().map(|(id, _)| *id));
419418
for item in tcx.lang_items().items().iter() {
420419
if let Some(did) = *item {
421420
if let Some(hir_id) = tcx.hir().as_local_hir_id(did) {

src/librustc/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl<'a, 'tcx: 'a> MissingStabilityAnnotations<'a, 'tcx> {
319319
let stab = self.tcx.stability().local_stability(hir_id);
320320
let is_error = !self.tcx.sess.opts.test &&
321321
stab.is_none() &&
322-
self.access_levels.is_reachable(self.tcx.hir().hir_to_node_id(hir_id));
322+
self.access_levels.is_reachable(hir_id);
323323
if is_error {
324324
self.tcx.sess.span_err(
325325
span,

src/librustc_lint/builtin.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ impl MissingDoc {
380380
// It's an option so the crate root can also use this function (it doesn't
381381
// have a NodeId).
382382
if let Some(id) = id {
383-
let node_id = cx.tcx.hir().hir_to_node_id(id);
384-
if !cx.access_levels.is_exported(node_id) {
383+
if !cx.access_levels.is_exported(id) {
385384
return;
386385
}
387386
}
@@ -557,8 +556,7 @@ impl LintPass for MissingCopyImplementations {
557556

558557
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingCopyImplementations {
559558
fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &hir::Item) {
560-
let node_id = cx.tcx.hir().hir_to_node_id(item.hir_id);
561-
if !cx.access_levels.is_reachable(node_id) {
559+
if !cx.access_levels.is_reachable(item.hir_id) {
562560
return;
563561
}
564562
let (def, ty) = match item.node {
@@ -629,8 +627,7 @@ impl LintPass for MissingDebugImplementations {
629627

630628
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations {
631629
fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &hir::Item) {
632-
let node_id = cx.tcx.hir().hir_to_node_id(item.hir_id);
633-
if !cx.access_levels.is_reachable(node_id) {
630+
if !cx.access_levels.is_reachable(item.hir_id) {
634631
return;
635632
}
636633

@@ -1169,9 +1166,8 @@ impl UnreachablePub {
11691166
fn perform_lint(&self, cx: &LateContext<'_, '_>, what: &str, id: hir::HirId,
11701167
vis: &hir::Visibility, span: Span, exportable: bool) {
11711168
let mut applicability = Applicability::MachineApplicable;
1172-
let node_id = cx.tcx.hir().hir_to_node_id(id);
11731169
match vis.node {
1174-
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(node_id) => {
1170+
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(id) => {
11751171
if span.ctxt().outer().expn_info().is_some() {
11761172
applicability = Applicability::MaybeIncorrect;
11771173
}

src/librustc_privacy/lib.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ impl VisibilityLike for Option<AccessLevel> {
379379
// (which require reaching the `DefId`s in them).
380380
const SHALLOW: bool = true;
381381
fn new_min<'a, 'tcx>(find: &FindMin<'a, 'tcx, Self>, def_id: DefId) -> Self {
382-
cmp::min(if let Some(node_id) = find.tcx.hir().as_local_node_id(def_id) {
383-
find.access_levels.map.get(&node_id).cloned()
382+
cmp::min(if let Some(hir_id) = find.tcx.hir().as_local_hir_id(def_id) {
383+
find.access_levels.map.get(&hir_id).cloned()
384384
} else {
385385
Self::MAX
386386
}, find.min)
@@ -410,17 +410,15 @@ struct ReachEverythingInTheInterfaceVisitor<'b, 'a: 'b, 'tcx: 'a> {
410410

411411
impl<'a, 'tcx> EmbargoVisitor<'a, 'tcx> {
412412
fn get(&self, id: hir::HirId) -> Option<AccessLevel> {
413-
let node_id = self.tcx.hir().hir_to_node_id(id);
414-
self.access_levels.map.get(&node_id).cloned()
413+
self.access_levels.map.get(&id).cloned()
415414
}
416415

417416
// Updates node level and returns the updated level.
418417
fn update(&mut self, id: hir::HirId, level: Option<AccessLevel>) -> Option<AccessLevel> {
419418
let old_level = self.get(id);
420419
// Accessibility levels can only grow.
421420
if level > old_level {
422-
let node_id = self.tcx.hir().hir_to_node_id(id);
423-
self.access_levels.map.insert(node_id, level.unwrap());
421+
self.access_levels.map.insert(id, level.unwrap());
424422
self.changed = true;
425423
level
426424
} else {
@@ -1197,8 +1195,7 @@ impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
11971195
fn trait_is_public(&self, trait_id: hir::HirId) -> bool {
11981196
// FIXME: this would preferably be using `exported_items`, but all
11991197
// traits are exported currently (see `EmbargoVisitor.exported_trait`).
1200-
let node_id = self.tcx.hir().hir_to_node_id(trait_id);
1201-
self.access_levels.is_public(node_id)
1198+
self.access_levels.is_public(trait_id)
12021199
}
12031200

12041201
fn check_generic_bound(&mut self, bound: &hir::GenericBound) {
@@ -1210,8 +1207,7 @@ impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
12101207
}
12111208

12121209
fn item_is_public(&self, id: &hir::HirId, vis: &hir::Visibility) -> bool {
1213-
let node_id = self.tcx.hir().hir_to_node_id(*id);
1214-
self.access_levels.is_reachable(node_id) || vis.node.is_pub()
1210+
self.access_levels.is_reachable(*id) || vis.node.is_pub()
12151211
}
12161212
}
12171213

@@ -1325,8 +1321,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
13251321
hir::ImplItemKind::Const(..) |
13261322
hir::ImplItemKind::Method(..) => {
13271323
self.access_levels.is_reachable(
1328-
self.tcx.hir().hir_to_node_id(
1329-
impl_item_ref.id.hir_id))
1324+
impl_item_ref.id.hir_id)
13301325
}
13311326
hir::ImplItemKind::Existential(..) |
13321327
hir::ImplItemKind::Type(_) => false,
@@ -1455,8 +1450,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
14551450
}
14561451

14571452
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem) {
1458-
let node_id = self.tcx.hir().hir_to_node_id(item.hir_id);
1459-
if self.access_levels.is_reachable(node_id) {
1453+
if self.access_levels.is_reachable(item.hir_id) {
14601454
intravisit::walk_foreign_item(self, item)
14611455
}
14621456
}
@@ -1474,8 +1468,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
14741468
v: &'tcx hir::Variant,
14751469
g: &'tcx hir::Generics,
14761470
item_id: hir::HirId) {
1477-
let node_id = self.tcx.hir().hir_to_node_id(v.node.data.hir_id());
1478-
if self.access_levels.is_reachable(node_id) {
1471+
if self.access_levels.is_reachable(v.node.data.hir_id()) {
14791472
self.in_variant = true;
14801473
intravisit::walk_variant(self, v, g, item_id);
14811474
self.in_variant = false;

src/librustc_save_analysis/dump_visitor.rs

+33-18
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,19 @@ macro_rules! down_cast_data {
5858
}
5959

6060
macro_rules! access_from {
61-
($save_ctxt:expr, $vis:expr, $id:expr) => {
61+
($save_ctxt:expr, $item:expr, $id:expr) => {
6262
Access {
63-
public: $vis.node.is_pub(),
63+
public: $item.vis.node.is_pub(),
6464
reachable: $save_ctxt.access_levels.is_reachable($id),
6565
}
6666
};
67+
}
6768

68-
($save_ctxt:expr, $item:expr) => {
69+
macro_rules! access_from_vis {
70+
($save_ctxt:expr, $vis:expr, $id:expr) => {
6971
Access {
70-
public: $item.vis.node.is_pub(),
71-
reachable: $save_ctxt.access_levels.is_reachable($item.id),
72+
public: $vis.node.is_pub(),
73+
reachable: $save_ctxt.access_levels.is_reachable($id),
7274
}
7375
};
7476
}
@@ -303,7 +305,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
303305

304306
method_data.value = sig_str;
305307
method_data.sig = sig::method_signature(id, ident, generics, sig, &self.save_ctxt);
306-
self.dumper.dump_def(&access_from!(self.save_ctxt, vis, id), method_data);
308+
let hir_id = self.tcx.hir().node_to_hir_id(id);
309+
self.dumper.dump_def(&access_from_vis!(self.save_ctxt, vis, hir_id), method_data);
307310
}
308311

309312
// walk arg and return types
@@ -324,7 +327,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
324327
fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) {
325328
let field_data = self.save_ctxt.get_field_data(field, parent_id);
326329
if let Some(field_data) = field_data {
327-
self.dumper.dump_def(&access_from!(self.save_ctxt, field), field_data);
330+
let hir_id = self.tcx.hir().node_to_hir_id(field.id);
331+
self.dumper.dump_def(&access_from!(self.save_ctxt, field, hir_id), field_data);
328332
}
329333
}
330334

@@ -389,7 +393,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
389393
|v| v.process_formals(&decl.inputs, &fn_data.qualname),
390394
);
391395
self.process_generic_params(ty_params, &fn_data.qualname, item.id);
392-
self.dumper.dump_def(&access_from!(self.save_ctxt, item), fn_data);
396+
let hir_id = self.tcx.hir().node_to_hir_id(item.id);
397+
self.dumper.dump_def(&access_from!(self.save_ctxt, item, hir_id), fn_data);
393398
}
394399

395400
for arg in &decl.inputs {
@@ -409,10 +414,11 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
409414
typ: &'l ast::Ty,
410415
expr: &'l ast::Expr,
411416
) {
417+
let hir_id = self.tcx.hir().node_to_hir_id(item.id);
412418
self.nest_tables(item.id, |v| {
413419
if let Some(var_data) = v.save_ctxt.get_item_data(item) {
414420
down_cast_data!(var_data, DefData, item.span);
415-
v.dumper.dump_def(&access_from!(v.save_ctxt, item), var_data);
421+
v.dumper.dump_def(&access_from!(v.save_ctxt, item, hir_id), var_data);
416422
}
417423
v.visit_ty(&typ);
418424
v.visit_expr(expr);
@@ -435,9 +441,10 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
435441
if !self.span.filter_generated(ident.span) {
436442
let sig = sig::assoc_const_signature(id, ident.name, typ, expr, &self.save_ctxt);
437443
let span = self.span_from_span(ident.span);
444+
let hir_id = self.tcx.hir().node_to_hir_id(id);
438445

439446
self.dumper.dump_def(
440-
&access_from!(self.save_ctxt, vis, id),
447+
&access_from_vis!(self.save_ctxt, vis, hir_id),
441448
Def {
442449
kind: DefKind::Const,
443450
id: id_from_node_id(id, &self.save_ctxt),
@@ -512,8 +519,9 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
512519

513520
if !self.span.filter_generated(item.ident.span) {
514521
let span = self.span_from_span(item.ident.span);
522+
let hir_id = self.tcx.hir().node_to_hir_id(item.id);
515523
self.dumper.dump_def(
516-
&access_from!(self.save_ctxt, item),
524+
&access_from!(self.save_ctxt, item, hir_id),
517525
Def {
518526
kind,
519527
id: id_from_node_id(item.id, &self.save_ctxt),
@@ -552,7 +560,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
552560
};
553561
down_cast_data!(enum_data, DefData, item.span);
554562

555-
let access = access_from!(self.save_ctxt, item);
563+
let hir_id = self.tcx.hir().node_to_hir_id(item.id);
564+
let access = access_from!(self.save_ctxt, item, hir_id);
556565

557566
for variant in &enum_definition.variants {
558567
let name = variant.node.ident.name.to_string();
@@ -701,8 +710,9 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
701710
.iter()
702711
.map(|i| id_from_node_id(i.id, &self.save_ctxt))
703712
.collect();
713+
let hir_id = self.tcx.hir().node_to_hir_id(item.id);
704714
self.dumper.dump_def(
705-
&access_from!(self.save_ctxt, item),
715+
&access_from!(self.save_ctxt, item, hir_id),
706716
Def {
707717
kind: DefKind::Trait,
708718
id,
@@ -760,7 +770,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
760770
fn process_mod(&mut self, item: &ast::Item) {
761771
if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
762772
down_cast_data!(mod_data, DefData, item.span);
763-
self.dumper.dump_def(&access_from!(self.save_ctxt, item), mod_data);
773+
let hir_id = self.tcx.hir().node_to_hir_id(item.id);
774+
self.dumper.dump_def(&access_from!(self.save_ctxt, item, hir_id), mod_data);
764775
}
765776
}
766777

@@ -1201,7 +1212,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
12011212

12021213
// The access is calculated using the current tree ID, but with the root tree's visibility
12031214
// (since nested trees don't have their own visibility).
1204-
let access = access_from!(self.save_ctxt, root_item.vis, id);
1215+
let hir_id = self.tcx.hir().node_to_hir_id(id);
1216+
let access = access_from!(self.save_ctxt, root_item, hir_id);
12051217

12061218
// The parent def id of a given use tree is always the enclosing item.
12071219
let parent = self.save_ctxt.tcx.hir().opt_local_def_id(id)
@@ -1400,9 +1412,10 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
14001412
if !self.span.filter_generated(item.ident.span) {
14011413
let span = self.span_from_span(item.ident.span);
14021414
let id = id_from_node_id(item.id, &self.save_ctxt);
1415+
let hir_id = self.tcx.hir().node_to_hir_id(item.id);
14031416

14041417
self.dumper.dump_def(
1405-
&access_from!(self.save_ctxt, item),
1418+
&access_from!(self.save_ctxt, item, hir_id),
14061419
Def {
14071420
kind: DefKind::Type,
14081421
id,
@@ -1431,9 +1444,10 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
14311444
if !self.span.filter_generated(item.ident.span) {
14321445
let span = self.span_from_span(item.ident.span);
14331446
let id = id_from_node_id(item.id, &self.save_ctxt);
1447+
let hir_id = self.tcx.hir().node_to_hir_id(item.id);
14341448

14351449
self.dumper.dump_def(
1436-
&access_from!(self.save_ctxt, item),
1450+
&access_from!(self.save_ctxt, item, hir_id),
14371451
Def {
14381452
kind: DefKind::Type,
14391453
id,
@@ -1631,7 +1645,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
16311645
}
16321646

16331647
fn visit_foreign_item(&mut self, item: &'l ast::ForeignItem) {
1634-
let access = access_from!(self.save_ctxt, item);
1648+
let hir_id = self.tcx.hir().node_to_hir_id(item.id);
1649+
let access = access_from!(self.save_ctxt, item, hir_id);
16351650

16361651
match item.node {
16371652
ast::ForeignItemKind::Fn(ref decl, ref generics) => {

src/librustdoc/core.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,11 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
461461
sess.abort_if_errors();
462462

463463
let access_levels = tcx.privacy_access_levels(LOCAL_CRATE);
464-
// Convert from a NodeId set to a DefId set since we don't always have easy access
465-
// to the map from defid -> nodeid
464+
// Convert from a HirId set to a DefId set since we don't always have easy access
465+
// to the map from defid -> hirid
466466
let access_levels = AccessLevels {
467467
map: access_levels.map.iter()
468-
.map(|(&k, &v)| (tcx.hir().local_def_id(k), v))
468+
.map(|(&k, &v)| (tcx.hir().local_def_id_from_hir_id(k), v))
469469
.collect()
470470
};
471471

0 commit comments

Comments
 (0)