Skip to content

Commit bb744e1

Browse files
committed
Auto merge of rust-lang#87568 - petrochenkov:localevel, r=cjgillot
rustc: Replace `HirId`s with `LocalDefId`s in `AccessLevels` tables and passes using those tables - primarily privacy checking, stability checking and dead code checking. All these passes work with definitions rather than with arbitrary HIR nodes. r? `@cjgillot` cc `@lambinoo` (rust-lang#87487)
2 parents 2b8de6f + f921410 commit bb744e1

27 files changed

+299
-333
lines changed

compiler/rustc_lint/src/builtin.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc_feature::{deprecated_attributes, AttributeGate, AttributeTemplate, Att
3838
use rustc_feature::{GateIssue, Stability};
3939
use rustc_hir as hir;
4040
use rustc_hir::def::{DefKind, Res};
41-
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet};
41+
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
4242
use rustc_hir::{ForeignItemKind, GenericParamKind, PatKind};
4343
use rustc_hir::{HirId, Node};
4444
use rustc_index::vec::Idx;
@@ -511,7 +511,7 @@ impl MissingDoc {
511511
fn check_missing_docs_attrs(
512512
&self,
513513
cx: &LateContext<'_>,
514-
id: hir::HirId,
514+
def_id: LocalDefId,
515515
sp: Span,
516516
article: &'static str,
517517
desc: &'static str,
@@ -530,13 +530,13 @@ impl MissingDoc {
530530
// Only check publicly-visible items, using the result from the privacy pass.
531531
// It's an option so the crate root can also use this function (it doesn't
532532
// have a `NodeId`).
533-
if id != hir::CRATE_HIR_ID {
534-
if !cx.access_levels.is_exported(id) {
533+
if def_id != CRATE_DEF_ID {
534+
if !cx.access_levels.is_exported(def_id) {
535535
return;
536536
}
537537
}
538538

539-
let attrs = cx.tcx.hir().attrs(id);
539+
let attrs = cx.tcx.get_attrs(def_id.to_def_id());
540540
let has_doc = attrs.iter().any(|a| has_doc(cx.sess(), a));
541541
if !has_doc {
542542
cx.struct_span_lint(
@@ -568,12 +568,12 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
568568
}
569569

570570
fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) {
571-
self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.module().inner, "the", "crate");
571+
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, krate.module().inner, "the", "crate");
572572

573573
for macro_def in krate.exported_macros() {
574574
// Non exported macros should be skipped, since `missing_docs` only
575575
// applies to externally visible items.
576-
if !cx.access_levels.is_exported(macro_def.hir_id()) {
576+
if !cx.access_levels.is_exported(macro_def.def_id) {
577577
continue;
578578
}
579579

@@ -632,7 +632,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
632632

633633
let (article, desc) = cx.tcx.article_and_description(it.def_id.to_def_id());
634634

635-
self.check_missing_docs_attrs(cx, it.hir_id(), it.span, article, desc);
635+
self.check_missing_docs_attrs(cx, it.def_id, it.span, article, desc);
636636
}
637637

638638
fn check_trait_item(&mut self, cx: &LateContext<'_>, trait_item: &hir::TraitItem<'_>) {
@@ -642,7 +642,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
642642

643643
let (article, desc) = cx.tcx.article_and_description(trait_item.def_id.to_def_id());
644644

645-
self.check_missing_docs_attrs(cx, trait_item.hir_id(), trait_item.span, article, desc);
645+
self.check_missing_docs_attrs(cx, trait_item.def_id, trait_item.span, article, desc);
646646
}
647647

648648
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
@@ -652,22 +652,23 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
652652
}
653653

654654
let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
655-
self.check_missing_docs_attrs(cx, impl_item.hir_id(), impl_item.span, article, desc);
655+
self.check_missing_docs_attrs(cx, impl_item.def_id, impl_item.span, article, desc);
656656
}
657657

658658
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'_>) {
659659
let (article, desc) = cx.tcx.article_and_description(foreign_item.def_id.to_def_id());
660-
self.check_missing_docs_attrs(cx, foreign_item.hir_id(), foreign_item.span, article, desc);
660+
self.check_missing_docs_attrs(cx, foreign_item.def_id, foreign_item.span, article, desc);
661661
}
662662

663663
fn check_field_def(&mut self, cx: &LateContext<'_>, sf: &hir::FieldDef<'_>) {
664664
if !sf.is_positional() {
665-
self.check_missing_docs_attrs(cx, sf.hir_id, sf.span, "a", "struct field")
665+
let def_id = cx.tcx.hir().local_def_id(sf.hir_id);
666+
self.check_missing_docs_attrs(cx, def_id, sf.span, "a", "struct field")
666667
}
667668
}
668669

669670
fn check_variant(&mut self, cx: &LateContext<'_>, v: &hir::Variant<'_>) {
670-
self.check_missing_docs_attrs(cx, v.id, v.span, "a", "variant");
671+
self.check_missing_docs_attrs(cx, cx.tcx.hir().local_def_id(v.id), v.span, "a", "variant");
671672
}
672673
}
673674

@@ -709,7 +710,7 @@ declare_lint_pass!(MissingCopyImplementations => [MISSING_COPY_IMPLEMENTATIONS])
709710

710711
impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
711712
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
712-
if !cx.access_levels.is_reachable(item.hir_id()) {
713+
if !cx.access_levels.is_reachable(item.def_id) {
713714
return;
714715
}
715716
let (def, ty) = match item.kind {
@@ -796,7 +797,7 @@ impl_lint_pass!(MissingDebugImplementations => [MISSING_DEBUG_IMPLEMENTATIONS]);
796797

797798
impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations {
798799
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
799-
if !cx.access_levels.is_reachable(item.hir_id()) {
800+
if !cx.access_levels.is_reachable(item.def_id) {
800801
return;
801802
}
802803

@@ -1314,14 +1315,14 @@ impl UnreachablePub {
13141315
&self,
13151316
cx: &LateContext<'_>,
13161317
what: &str,
1317-
id: hir::HirId,
1318+
def_id: LocalDefId,
13181319
vis: &hir::Visibility<'_>,
13191320
span: Span,
13201321
exportable: bool,
13211322
) {
13221323
let mut applicability = Applicability::MachineApplicable;
13231324
match vis.node {
1324-
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(id) => {
1325+
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(def_id) => {
13251326
if span.from_expansion() {
13261327
applicability = Applicability::MaybeIncorrect;
13271328
}
@@ -1354,26 +1355,27 @@ impl UnreachablePub {
13541355

13551356
impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
13561357
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
1357-
self.perform_lint(cx, "item", item.hir_id(), &item.vis, item.span, true);
1358+
self.perform_lint(cx, "item", item.def_id, &item.vis, item.span, true);
13581359
}
13591360

13601361
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'tcx>) {
13611362
self.perform_lint(
13621363
cx,
13631364
"item",
1364-
foreign_item.hir_id(),
1365+
foreign_item.def_id,
13651366
&foreign_item.vis,
13661367
foreign_item.span,
13671368
true,
13681369
);
13691370
}
13701371

13711372
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
1372-
self.perform_lint(cx, "field", field.hir_id, &field.vis, field.span, false);
1373+
let def_id = cx.tcx.hir().local_def_id(field.hir_id);
1374+
self.perform_lint(cx, "field", def_id, &field.vis, field.span, false);
13731375
}
13741376

13751377
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
1376-
self.perform_lint(cx, "item", impl_item.hir_id(), &impl_item.vis, impl_item.span, false);
1378+
self.perform_lint(cx, "item", impl_item.def_id, &impl_item.vis, impl_item.span, false);
13771379
}
13781380
}
13791381

compiler/rustc_middle/src/hir/map/mod.rs

-8
Original file line numberDiff line numberDiff line change
@@ -581,14 +581,6 @@ impl<'hir> Map<'hir> {
581581
self.body_const_context(self.local_def_id(self.enclosing_body_owner(hir_id))).is_some()
582582
}
583583

584-
/// Whether `hir_id` corresponds to a `mod` or a crate.
585-
pub fn is_hir_id_module(&self, hir_id: HirId) -> bool {
586-
matches!(
587-
self.get(hir_id),
588-
Node::Item(Item { kind: ItemKind::Mod(_), .. }) | Node::Crate(..)
589-
)
590-
}
591-
592584
/// Retrieves the `HirId` for `id`'s enclosing method, unless there's a
593585
/// `while` or `loop` before reaching it, as block tail returns are not
594586
/// available in them.

compiler/rustc_middle/src/middle/privacy.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
//! which are available for use externally when compiled as a library.
44
55
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_hir::HirId;
76
use rustc_macros::HashStable;
8-
use std::fmt;
7+
use rustc_span::def_id::LocalDefId;
98
use std::hash::Hash;
109

1110
/// Represents the levels of accessibility an item can have.
@@ -27,8 +26,8 @@ pub enum AccessLevel {
2726
}
2827

2928
/// Holds a map of accessibility levels for reachable HIR nodes.
30-
#[derive(Clone)]
31-
pub struct AccessLevels<Id = HirId> {
29+
#[derive(Debug)]
30+
pub struct AccessLevels<Id = LocalDefId> {
3231
pub map: FxHashMap<Id, AccessLevel>,
3332
}
3433

@@ -49,14 +48,8 @@ impl<Id: Hash + Eq> AccessLevels<Id> {
4948
}
5049
}
5150

52-
impl<Id: Hash + Eq> Default for AccessLevels<Id> {
51+
impl<Id> Default for AccessLevels<Id> {
5352
fn default() -> Self {
5453
AccessLevels { map: Default::default() }
5554
}
5655
}
57-
58-
impl<Id: Hash + Eq + fmt::Debug> fmt::Debug for AccessLevels<Id> {
59-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60-
fmt::Debug::fmt(&self.map, f)
61-
}
62-
}

compiler/rustc_middle/src/middle/stability.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
1111
use rustc_feature::GateIssue;
1212
use rustc_hir as hir;
1313
use rustc_hir::def::DefKind;
14-
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
14+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
1515
use rustc_hir::{self, HirId};
1616
use rustc_middle::ty::print::with_no_trimmed_paths;
1717
use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE};
@@ -36,12 +36,12 @@ pub struct DeprecationEntry {
3636
pub attr: Deprecation,
3737
/// The `DefId` where the attr was originally attached. `None` for non-local
3838
/// `DefId`'s.
39-
origin: Option<HirId>,
39+
origin: Option<LocalDefId>,
4040
}
4141

4242
impl DeprecationEntry {
43-
pub fn local(attr: Deprecation, id: HirId) -> DeprecationEntry {
44-
DeprecationEntry { attr, origin: Some(id) }
43+
pub fn local(attr: Deprecation, def_id: LocalDefId) -> DeprecationEntry {
44+
DeprecationEntry { attr, origin: Some(def_id) }
4545
}
4646

4747
pub fn external(attr: Deprecation) -> DeprecationEntry {
@@ -61,9 +61,9 @@ impl DeprecationEntry {
6161
pub struct Index<'tcx> {
6262
/// This is mostly a cache, except the stabilities of local items
6363
/// are filled by the annotator.
64-
pub stab_map: FxHashMap<HirId, &'tcx Stability>,
65-
pub const_stab_map: FxHashMap<HirId, &'tcx ConstStability>,
66-
pub depr_map: FxHashMap<HirId, DeprecationEntry>,
64+
pub stab_map: FxHashMap<LocalDefId, &'tcx Stability>,
65+
pub const_stab_map: FxHashMap<LocalDefId, &'tcx ConstStability>,
66+
pub depr_map: FxHashMap<LocalDefId, DeprecationEntry>,
6767

6868
/// Maps for each crate whether it is part of the staged API.
6969
pub staged_api: FxHashMap<CrateNum, bool>,
@@ -73,16 +73,16 @@ pub struct Index<'tcx> {
7373
}
7474

7575
impl<'tcx> Index<'tcx> {
76-
pub fn local_stability(&self, id: HirId) -> Option<&'tcx Stability> {
77-
self.stab_map.get(&id).cloned()
76+
pub fn local_stability(&self, def_id: LocalDefId) -> Option<&'tcx Stability> {
77+
self.stab_map.get(&def_id).copied()
7878
}
7979

80-
pub fn local_const_stability(&self, id: HirId) -> Option<&'tcx ConstStability> {
81-
self.const_stab_map.get(&id).cloned()
80+
pub fn local_const_stability(&self, def_id: LocalDefId) -> Option<&'tcx ConstStability> {
81+
self.const_stab_map.get(&def_id).copied()
8282
}
8383

84-
pub fn local_deprecation_entry(&self, id: HirId) -> Option<DeprecationEntry> {
85-
self.depr_map.get(&id).cloned()
84+
pub fn local_deprecation_entry(&self, def_id: LocalDefId) -> Option<DeprecationEntry> {
85+
self.depr_map.get(&def_id).cloned()
8686
}
8787
}
8888

compiler/rustc_middle/src/ty/context.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -2854,18 +2854,11 @@ pub fn provide(providers: &mut ty::query::Providers) {
28542854
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default())
28552855
};
28562856

2857-
providers.lookup_stability = |tcx, id| {
2858-
let id = tcx.hir().local_def_id_to_hir_id(id.expect_local());
2859-
tcx.stability().local_stability(id)
2860-
};
2861-
providers.lookup_const_stability = |tcx, id| {
2862-
let id = tcx.hir().local_def_id_to_hir_id(id.expect_local());
2863-
tcx.stability().local_const_stability(id)
2864-
};
2865-
providers.lookup_deprecation_entry = |tcx, id| {
2866-
let id = tcx.hir().local_def_id_to_hir_id(id.expect_local());
2867-
tcx.stability().local_deprecation_entry(id)
2868-
};
2857+
providers.lookup_stability = |tcx, id| tcx.stability().local_stability(id.expect_local());
2858+
providers.lookup_const_stability =
2859+
|tcx, id| tcx.stability().local_const_stability(id.expect_local());
2860+
providers.lookup_deprecation_entry =
2861+
|tcx, id| tcx.stability().local_deprecation_entry(id.expect_local());
28692862
providers.extern_mod_stmt_cnum =
28702863
|tcx, id| tcx.resolutions(()).extern_crate_map.get(&id).cloned();
28712864
providers.output_filenames = |tcx, ()| tcx.output_filenames.clone();

0 commit comments

Comments
 (0)