Auto merge of #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` (#87487)
This commit is contained in:
bors 2021-08-04 02:04:04 +00:00
commit bb744e1e9f
27 changed files with 299 additions and 333 deletions

View File

@ -38,7 +38,7 @@ use rustc_feature::{deprecated_attributes, AttributeGate, AttributeTemplate, Att
use rustc_feature::{GateIssue, Stability};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet};
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
use rustc_hir::{ForeignItemKind, GenericParamKind, PatKind};
use rustc_hir::{HirId, Node};
use rustc_index::vec::Idx;
@ -511,7 +511,7 @@ impl MissingDoc {
fn check_missing_docs_attrs(
&self,
cx: &LateContext<'_>,
id: hir::HirId,
def_id: LocalDefId,
sp: Span,
article: &'static str,
desc: &'static str,
@ -530,13 +530,13 @@ impl MissingDoc {
// Only check publicly-visible items, using the result from the privacy pass.
// It's an option so the crate root can also use this function (it doesn't
// have a `NodeId`).
if id != hir::CRATE_HIR_ID {
if !cx.access_levels.is_exported(id) {
if def_id != CRATE_DEF_ID {
if !cx.access_levels.is_exported(def_id) {
return;
}
}
let attrs = cx.tcx.hir().attrs(id);
let attrs = cx.tcx.get_attrs(def_id.to_def_id());
let has_doc = attrs.iter().any(|a| has_doc(cx.sess(), a));
if !has_doc {
cx.struct_span_lint(
@ -568,12 +568,12 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
}
fn check_crate(&mut self, cx: &LateContext<'_>, krate: &hir::Crate<'_>) {
self.check_missing_docs_attrs(cx, hir::CRATE_HIR_ID, krate.module().inner, "the", "crate");
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, krate.module().inner, "the", "crate");
for macro_def in krate.exported_macros() {
// Non exported macros should be skipped, since `missing_docs` only
// applies to externally visible items.
if !cx.access_levels.is_exported(macro_def.hir_id()) {
if !cx.access_levels.is_exported(macro_def.def_id) {
continue;
}
@ -632,7 +632,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
let (article, desc) = cx.tcx.article_and_description(it.def_id.to_def_id());
self.check_missing_docs_attrs(cx, it.hir_id(), it.span, article, desc);
self.check_missing_docs_attrs(cx, it.def_id, it.span, article, desc);
}
fn check_trait_item(&mut self, cx: &LateContext<'_>, trait_item: &hir::TraitItem<'_>) {
@ -642,7 +642,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
let (article, desc) = cx.tcx.article_and_description(trait_item.def_id.to_def_id());
self.check_missing_docs_attrs(cx, trait_item.hir_id(), trait_item.span, article, desc);
self.check_missing_docs_attrs(cx, trait_item.def_id, trait_item.span, article, desc);
}
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
@ -652,22 +652,23 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
}
let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
self.check_missing_docs_attrs(cx, impl_item.hir_id(), impl_item.span, article, desc);
self.check_missing_docs_attrs(cx, impl_item.def_id, impl_item.span, article, desc);
}
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'_>) {
let (article, desc) = cx.tcx.article_and_description(foreign_item.def_id.to_def_id());
self.check_missing_docs_attrs(cx, foreign_item.hir_id(), foreign_item.span, article, desc);
self.check_missing_docs_attrs(cx, foreign_item.def_id, foreign_item.span, article, desc);
}
fn check_field_def(&mut self, cx: &LateContext<'_>, sf: &hir::FieldDef<'_>) {
if !sf.is_positional() {
self.check_missing_docs_attrs(cx, sf.hir_id, sf.span, "a", "struct field")
let def_id = cx.tcx.hir().local_def_id(sf.hir_id);
self.check_missing_docs_attrs(cx, def_id, sf.span, "a", "struct field")
}
}
fn check_variant(&mut self, cx: &LateContext<'_>, v: &hir::Variant<'_>) {
self.check_missing_docs_attrs(cx, v.id, v.span, "a", "variant");
self.check_missing_docs_attrs(cx, cx.tcx.hir().local_def_id(v.id), v.span, "a", "variant");
}
}
@ -709,7 +710,7 @@ declare_lint_pass!(MissingCopyImplementations => [MISSING_COPY_IMPLEMENTATIONS])
impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
if !cx.access_levels.is_reachable(item.hir_id()) {
if !cx.access_levels.is_reachable(item.def_id) {
return;
}
let (def, ty) = match item.kind {
@ -796,7 +797,7 @@ impl_lint_pass!(MissingDebugImplementations => [MISSING_DEBUG_IMPLEMENTATIONS]);
impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations {
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
if !cx.access_levels.is_reachable(item.hir_id()) {
if !cx.access_levels.is_reachable(item.def_id) {
return;
}
@ -1314,14 +1315,14 @@ impl UnreachablePub {
&self,
cx: &LateContext<'_>,
what: &str,
id: hir::HirId,
def_id: LocalDefId,
vis: &hir::Visibility<'_>,
span: Span,
exportable: bool,
) {
let mut applicability = Applicability::MachineApplicable;
match vis.node {
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(id) => {
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(def_id) => {
if span.from_expansion() {
applicability = Applicability::MaybeIncorrect;
}
@ -1354,14 +1355,14 @@ impl UnreachablePub {
impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
self.perform_lint(cx, "item", item.hir_id(), &item.vis, item.span, true);
self.perform_lint(cx, "item", item.def_id, &item.vis, item.span, true);
}
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'tcx>) {
self.perform_lint(
cx,
"item",
foreign_item.hir_id(),
foreign_item.def_id,
&foreign_item.vis,
foreign_item.span,
true,
@ -1369,11 +1370,12 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
}
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
self.perform_lint(cx, "field", field.hir_id, &field.vis, field.span, false);
let def_id = cx.tcx.hir().local_def_id(field.hir_id);
self.perform_lint(cx, "field", def_id, &field.vis, field.span, false);
}
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
self.perform_lint(cx, "item", impl_item.hir_id(), &impl_item.vis, impl_item.span, false);
self.perform_lint(cx, "item", impl_item.def_id, &impl_item.vis, impl_item.span, false);
}
}

View File

@ -581,14 +581,6 @@ impl<'hir> Map<'hir> {
self.body_const_context(self.local_def_id(self.enclosing_body_owner(hir_id))).is_some()
}
/// Whether `hir_id` corresponds to a `mod` or a crate.
pub fn is_hir_id_module(&self, hir_id: HirId) -> bool {
matches!(
self.get(hir_id),
Node::Item(Item { kind: ItemKind::Mod(_), .. }) | Node::Crate(..)
)
}
/// Retrieves the `HirId` for `id`'s enclosing method, unless there's a
/// `while` or `loop` before reaching it, as block tail returns are not
/// available in them.

View File

@ -3,9 +3,8 @@
//! which are available for use externally when compiled as a library.
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::HirId;
use rustc_macros::HashStable;
use std::fmt;
use rustc_span::def_id::LocalDefId;
use std::hash::Hash;
/// Represents the levels of accessibility an item can have.
@ -27,8 +26,8 @@ pub enum AccessLevel {
}
/// Holds a map of accessibility levels for reachable HIR nodes.
#[derive(Clone)]
pub struct AccessLevels<Id = HirId> {
#[derive(Debug)]
pub struct AccessLevels<Id = LocalDefId> {
pub map: FxHashMap<Id, AccessLevel>,
}
@ -49,14 +48,8 @@ impl<Id: Hash + Eq> AccessLevels<Id> {
}
}
impl<Id: Hash + Eq> Default for AccessLevels<Id> {
impl<Id> Default for AccessLevels<Id> {
fn default() -> Self {
AccessLevels { map: Default::default() }
}
}
impl<Id: Hash + Eq + fmt::Debug> fmt::Debug for AccessLevels<Id> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.map, f)
}
}

View File

@ -11,7 +11,7 @@ use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_feature::GateIssue;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::{self, HirId};
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_session::lint::builtin::{DEPRECATED, DEPRECATED_IN_FUTURE, SOFT_UNSTABLE};
@ -36,12 +36,12 @@ pub struct DeprecationEntry {
pub attr: Deprecation,
/// The `DefId` where the attr was originally attached. `None` for non-local
/// `DefId`'s.
origin: Option<HirId>,
origin: Option<LocalDefId>,
}
impl DeprecationEntry {
pub fn local(attr: Deprecation, id: HirId) -> DeprecationEntry {
DeprecationEntry { attr, origin: Some(id) }
pub fn local(attr: Deprecation, def_id: LocalDefId) -> DeprecationEntry {
DeprecationEntry { attr, origin: Some(def_id) }
}
pub fn external(attr: Deprecation) -> DeprecationEntry {
@ -61,9 +61,9 @@ impl DeprecationEntry {
pub struct Index<'tcx> {
/// This is mostly a cache, except the stabilities of local items
/// are filled by the annotator.
pub stab_map: FxHashMap<HirId, &'tcx Stability>,
pub const_stab_map: FxHashMap<HirId, &'tcx ConstStability>,
pub depr_map: FxHashMap<HirId, DeprecationEntry>,
pub stab_map: FxHashMap<LocalDefId, &'tcx Stability>,
pub const_stab_map: FxHashMap<LocalDefId, &'tcx ConstStability>,
pub depr_map: FxHashMap<LocalDefId, DeprecationEntry>,
/// Maps for each crate whether it is part of the staged API.
pub staged_api: FxHashMap<CrateNum, bool>,
@ -73,16 +73,16 @@ pub struct Index<'tcx> {
}
impl<'tcx> Index<'tcx> {
pub fn local_stability(&self, id: HirId) -> Option<&'tcx Stability> {
self.stab_map.get(&id).cloned()
pub fn local_stability(&self, def_id: LocalDefId) -> Option<&'tcx Stability> {
self.stab_map.get(&def_id).copied()
}
pub fn local_const_stability(&self, id: HirId) -> Option<&'tcx ConstStability> {
self.const_stab_map.get(&id).cloned()
pub fn local_const_stability(&self, def_id: LocalDefId) -> Option<&'tcx ConstStability> {
self.const_stab_map.get(&def_id).copied()
}
pub fn local_deprecation_entry(&self, id: HirId) -> Option<DeprecationEntry> {
self.depr_map.get(&id).cloned()
pub fn local_deprecation_entry(&self, def_id: LocalDefId) -> Option<DeprecationEntry> {
self.depr_map.get(&def_id).cloned()
}
}

View File

@ -2854,18 +2854,11 @@ pub fn provide(providers: &mut ty::query::Providers) {
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default())
};
providers.lookup_stability = |tcx, id| {
let id = tcx.hir().local_def_id_to_hir_id(id.expect_local());
tcx.stability().local_stability(id)
};
providers.lookup_const_stability = |tcx, id| {
let id = tcx.hir().local_def_id_to_hir_id(id.expect_local());
tcx.stability().local_const_stability(id)
};
providers.lookup_deprecation_entry = |tcx, id| {
let id = tcx.hir().local_def_id_to_hir_id(id.expect_local());
tcx.stability().local_deprecation_entry(id)
};
providers.lookup_stability = |tcx, id| tcx.stability().local_stability(id.expect_local());
providers.lookup_const_stability =
|tcx, id| tcx.stability().local_const_stability(id.expect_local());
providers.lookup_deprecation_entry =
|tcx, id| tcx.stability().local_deprecation_entry(id.expect_local());
providers.extern_mod_stmt_cnum =
|tcx, id| tcx.resolutions(()).extern_crate_map.get(&id).cloned();
providers.output_filenames = |tcx, ()| tcx.output_filenames.clone();

View File

@ -5,7 +5,7 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::{Node, PatKind, TyKind};
@ -14,16 +14,15 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::privacy;
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
use rustc_session::lint;
use rustc_span::symbol::{sym, Symbol};
// Any local node that may call something in its body block should be
// explored. For example, if it's a live Node::Item that is a
// function, then we should explore its block to check for codes that
// may need to be marked as live.
fn should_explore(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
matches!(
tcx.hir().find(hir_id),
tcx.hir().find(tcx.hir().local_def_id_to_hir_id(def_id)),
Some(
Node::Item(..)
| Node::ImplItem(..)
@ -31,23 +30,22 @@ fn should_explore(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
| Node::TraitItem(..)
| Node::Variant(..)
| Node::AnonConst(..)
| Node::Pat(..),
)
)
}
struct MarkSymbolVisitor<'tcx> {
worklist: Vec<hir::HirId>,
worklist: Vec<LocalDefId>,
tcx: TyCtxt<'tcx>,
maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
live_symbols: FxHashSet<hir::HirId>,
live_symbols: FxHashSet<LocalDefId>,
repr_has_repr_c: bool,
in_pat: bool,
inherited_pub_visibility: bool,
pub_visibility: bool,
ignore_variant_stack: Vec<DefId>,
// maps from tuple struct constructors to tuple struct items
struct_constructors: FxHashMap<hir::HirId, hir::HirId>,
struct_constructors: FxHashMap<LocalDefId, LocalDefId>,
}
impl<'tcx> MarkSymbolVisitor<'tcx> {
@ -62,19 +60,17 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
fn check_def_id(&mut self, def_id: DefId) {
if let Some(def_id) = def_id.as_local() {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
if should_explore(self.tcx, hir_id) || self.struct_constructors.contains_key(&hir_id) {
self.worklist.push(hir_id);
if should_explore(self.tcx, def_id) || self.struct_constructors.contains_key(&def_id) {
self.worklist.push(def_id);
}
self.live_symbols.insert(hir_id);
self.live_symbols.insert(def_id);
}
}
fn insert_def_id(&mut self, def_id: DefId) {
if let Some(def_id) = def_id.as_local() {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
debug_assert!(!should_explore(self.tcx, hir_id));
self.live_symbols.insert(hir_id);
debug_assert!(!should_explore(self.tcx, def_id));
self.live_symbols.insert(def_id);
}
}
@ -233,9 +229,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
// in the case of tuple struct constructors we want to check the item, not the generated
// tuple struct constructor function
let id = self.struct_constructors.get(&id).cloned().unwrap_or(id);
let id = self.struct_constructors.get(&id).copied().unwrap_or(id);
if let Some(node) = self.tcx.hir().find(id) {
if let Some(node) = self.tcx.hir().find(self.tcx.hir().local_def_id_to_hir_id(id)) {
self.live_symbols.insert(id);
self.visit_node(node);
}
@ -326,7 +322,8 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
let live_fields = def.fields().iter().filter(|f| {
has_repr_c || (pub_visibility && (inherited_pub_visibility || f.vis.node.is_pub()))
});
self.live_symbols.extend(live_fields.map(|f| f.hir_id));
let hir = self.tcx.hir();
self.live_symbols.extend(live_fields.map(|f| hir.local_def_id(f.hir_id)));
intravisit::walk_struct_def(self, def);
}
@ -398,7 +395,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
}
fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
self.live_symbols.insert(c.hir_id);
self.live_symbols.insert(self.tcx.hir().local_def_id(c.hir_id));
intravisit::walk_anon_const(self, c);
}
}
@ -445,47 +442,52 @@ fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
// 2) We are not sure to be live or not
// * Implementations of traits and trait methods
struct LifeSeeder<'k, 'tcx> {
worklist: Vec<hir::HirId>,
worklist: Vec<LocalDefId>,
krate: &'k hir::Crate<'k>,
tcx: TyCtxt<'tcx>,
// see `MarkSymbolVisitor::struct_constructors`
struct_constructors: FxHashMap<hir::HirId, hir::HirId>,
struct_constructors: FxHashMap<LocalDefId, LocalDefId>,
}
impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
fn visit_item(&mut self, item: &hir::Item<'_>) {
let allow_dead_code = has_allow_dead_code_or_lang_attr(self.tcx, item.hir_id());
if allow_dead_code {
self.worklist.push(item.hir_id());
self.worklist.push(item.def_id);
}
match item.kind {
hir::ItemKind::Enum(ref enum_def, _) => {
let hir = self.tcx.hir();
if allow_dead_code {
self.worklist.extend(enum_def.variants.iter().map(|variant| variant.id));
self.worklist.extend(
enum_def.variants.iter().map(|variant| hir.local_def_id(variant.id)),
);
}
for variant in enum_def.variants {
if let Some(ctor_hir_id) = variant.data.ctor_hir_id() {
self.struct_constructors.insert(ctor_hir_id, variant.id);
self.struct_constructors
.insert(hir.local_def_id(ctor_hir_id), hir.local_def_id(variant.id));
}
}
}
hir::ItemKind::Impl(hir::Impl { ref of_trait, items, .. }) => {
if of_trait.is_some() {
self.worklist.push(item.hir_id());
self.worklist.push(item.def_id);
}
for impl_item_ref in items {
let impl_item = self.krate.impl_item(impl_item_ref.id);
if of_trait.is_some()
|| has_allow_dead_code_or_lang_attr(self.tcx, impl_item.hir_id())
{
self.worklist.push(impl_item_ref.id.hir_id());
self.worklist.push(impl_item_ref.id.def_id);
}
}
}
hir::ItemKind::Struct(ref variant_data, _) => {
if let Some(ctor_hir_id) = variant_data.ctor_hir_id() {
self.struct_constructors.insert(ctor_hir_id, item.hir_id());
self.struct_constructors
.insert(self.tcx.hir().local_def_id(ctor_hir_id), item.def_id);
}
}
_ => (),
@ -497,7 +499,7 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(_, hir::TraitFn::Provided(_)))
&& has_allow_dead_code_or_lang_attr(self.tcx, trait_item.hir_id())
{
self.worklist.push(trait_item.hir_id());
self.worklist.push(trait_item.def_id);
}
}
@ -510,7 +512,7 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
if matches!(foreign_item.kind, Static(..) | Fn(..))
&& has_allow_dead_code_or_lang_attr(self.tcx, foreign_item.hir_id())
{
self.worklist.push(foreign_item.hir_id());
self.worklist.push(foreign_item.def_id);
}
}
}
@ -519,7 +521,7 @@ fn create_and_seed_worklist<'tcx>(
tcx: TyCtxt<'tcx>,
access_levels: &privacy::AccessLevels,
krate: &hir::Crate<'_>,
) -> (Vec<hir::HirId>, FxHashMap<hir::HirId, hir::HirId>) {
) -> (Vec<LocalDefId>, FxHashMap<LocalDefId, LocalDefId>) {
let worklist = access_levels
.map
.iter()
@ -528,12 +530,8 @@ fn create_and_seed_worklist<'tcx>(
if level >= privacy::AccessLevel::Reachable { Some(id) } else { None }
},
)
.chain(
// Seed entry point
tcx.entry_fn(()).and_then(|(def_id, _)| {
def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
}),
)
// Seed entry point
.chain(tcx.entry_fn(()).and_then(|(def_id, _)| def_id.as_local()))
.collect::<Vec<_>>();
// Seed implemented trait items
@ -548,7 +546,7 @@ fn find_live<'tcx>(
tcx: TyCtxt<'tcx>,
access_levels: &privacy::AccessLevels,
krate: &hir::Crate<'_>,
) -> FxHashSet<hir::HirId> {
) -> FxHashSet<LocalDefId> {
let (worklist, struct_constructors) = create_and_seed_worklist(tcx, access_levels, krate);
let mut symbol_visitor = MarkSymbolVisitor {
worklist,
@ -568,7 +566,7 @@ fn find_live<'tcx>(
struct DeadVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
live_symbols: FxHashSet<hir::HirId>,
live_symbols: FxHashSet<LocalDefId>,
}
impl DeadVisitor<'tcx> {
@ -583,42 +581,41 @@ impl DeadVisitor<'tcx> {
| hir::ItemKind::Struct(..)
| hir::ItemKind::Union(..)
);
should_warn && !self.symbol_is_live(item.hir_id())
should_warn && !self.symbol_is_live(item.def_id)
}
fn should_warn_about_field(&mut self, field: &hir::FieldDef<'_>) -> bool {
let field_type = self.tcx.type_of(self.tcx.hir().local_def_id(field.hir_id));
let def_id = self.tcx.hir().local_def_id(field.hir_id);
let field_type = self.tcx.type_of(def_id);
!field.is_positional()
&& !self.symbol_is_live(field.hir_id)
&& !self.symbol_is_live(def_id)
&& !field_type.is_phantom_data()
&& !has_allow_dead_code_or_lang_attr(self.tcx, field.hir_id)
}
fn should_warn_about_variant(&mut self, variant: &hir::Variant<'_>) -> bool {
!self.symbol_is_live(variant.id) && !has_allow_dead_code_or_lang_attr(self.tcx, variant.id)
let def_id = self.tcx.hir().local_def_id(variant.id);
!self.symbol_is_live(def_id) && !has_allow_dead_code_or_lang_attr(self.tcx, variant.id)
}
fn should_warn_about_foreign_item(&mut self, fi: &hir::ForeignItem<'_>) -> bool {
!self.symbol_is_live(fi.hir_id())
&& !has_allow_dead_code_or_lang_attr(self.tcx, fi.hir_id())
!self.symbol_is_live(fi.def_id) && !has_allow_dead_code_or_lang_attr(self.tcx, fi.hir_id())
}
// id := HIR id of an item's definition.
fn symbol_is_live(&mut self, id: hir::HirId) -> bool {
if self.live_symbols.contains(&id) {
fn symbol_is_live(&mut self, def_id: LocalDefId) -> bool {
if self.live_symbols.contains(&def_id) {
return true;
}
// If it's a type whose items are live, then it's live, too.
// This is done to handle the case where, for example, the static
// method of a private type is used, but the type itself is never
// called directly.
let def_id = self.tcx.hir().local_def_id(id);
let inherent_impls = self.tcx.inherent_impls(def_id);
for &impl_did in inherent_impls.iter() {
for item_did in self.tcx.associated_item_def_ids(impl_did) {
if let Some(did) = item_did.as_local() {
let item_hir_id = self.tcx.hir().local_def_id_to_hir_id(did);
if self.live_symbols.contains(&item_hir_id) {
if let Some(def_id) = item_did.as_local() {
if self.live_symbols.contains(&def_id) {
return true;
}
}
@ -721,7 +718,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
match impl_item.kind {
hir::ImplItemKind::Const(_, body_id) => {
if !self.symbol_is_live(impl_item.hir_id()) {
if !self.symbol_is_live(impl_item.def_id) {
self.warn_dead_code(
impl_item.hir_id(),
impl_item.span,
@ -732,7 +729,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> {
self.visit_nested_body(body_id)
}
hir::ImplItemKind::Fn(_, body_id) => {
if !self.symbol_is_live(impl_item.hir_id()) {
if !self.symbol_is_live(impl_item.def_id) {
// FIXME(66095): Because impl_item.span is annotated with things
// like expansion data, and ident.span isn't, we use the
// def_span method if it's part of a macro invocation

View File

@ -351,7 +351,7 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(ref trait_ref), ref items, .. }) =
item.kind
{
if !self.access_levels.is_reachable(item.hir_id()) {
if !self.access_levels.is_reachable(item.def_id) {
// FIXME(#53488) remove `let`
let tcx = self.tcx;
self.worklist.extend(items.iter().map(|ii_ref| ii_ref.id.def_id));
@ -404,9 +404,7 @@ fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashSet<LocalDefId> {
// If other crates link to us, they're going to expect to be able to
// use the lang items, so we need to be sure to mark them as
// exported.
reachable_context
.worklist
.extend(access_levels.map.iter().map(|(id, _)| tcx.hir().local_def_id(*id)));
reachable_context.worklist.extend(access_levels.map.keys());
for item in tcx.lang_items().items().iter() {
if let Some(def_id) = *item {
if let Some(def_id) = def_id.as_local() {

View File

@ -7,7 +7,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::{FieldDef, Generics, HirId, Item, TraitRef, Ty, TyKind, Variant};
use rustc_middle::hir::map::Map;
@ -99,7 +99,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
// If the node is a function, `fn_sig` is its signature
fn annotate<F>(
&mut self,
hir_id: HirId,
def_id: LocalDefId,
item_sp: Span,
fn_sig: Option<&'tcx hir::FnSig<'tcx>>,
kind: AnnotationKind,
@ -110,11 +110,11 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
) where
F: FnOnce(&mut Self),
{
let attrs = self.tcx.hir().attrs(hir_id);
debug!("annotate(id = {:?}, attrs = {:?})", hir_id, attrs);
let attrs = self.tcx.get_attrs(def_id.to_def_id());
debug!("annotate(id = {:?}, attrs = {:?})", def_id, attrs);
let mut did_error = false;
if !self.tcx.features().staged_api {
did_error = self.forbid_staged_api_attrs(hir_id, attrs, inherit_deprecation.clone());
did_error = self.forbid_staged_api_attrs(def_id, attrs, inherit_deprecation.clone());
}
let depr = if did_error { None } else { attr::find_deprecation(&self.tcx.sess, attrs) };
@ -123,6 +123,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
is_deprecated = true;
if kind == AnnotationKind::Prohibited || kind == AnnotationKind::DeprecationProhibited {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
self.tcx.struct_span_lint_hir(USELESS_DEPRECATED, hir_id, *span, |lint| {
lint.build("this `#[deprecated]` annotation has no effect")
.span_suggestion_short(
@ -136,13 +137,13 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
}
// `Deprecation` is just two pointers, no need to intern it
let depr_entry = DeprecationEntry::local(depr.clone(), hir_id);
self.index.depr_map.insert(hir_id, depr_entry);
let depr_entry = DeprecationEntry::local(depr.clone(), def_id);
self.index.depr_map.insert(def_id, depr_entry);
} else if let Some(parent_depr) = self.parent_depr.clone() {
if inherit_deprecation.yes() {
is_deprecated = true;
info!("tagging child {:?} as deprecated from parent", hir_id);
self.index.depr_map.insert(hir_id, parent_depr);
info!("tagging child {:?} as deprecated from parent", def_id);
self.index.depr_map.insert(def_id, parent_depr);
}
}
@ -157,7 +158,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
}
} else {
self.recurse_with_stability_attrs(
depr.map(|(d, _)| DeprecationEntry::local(d, hir_id)),
depr.map(|(d, _)| DeprecationEntry::local(d, def_id)),
None,
None,
visit_children,
@ -170,7 +171,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
let const_stab = const_stab.map(|(const_stab, const_span_node)| {
let const_stab = self.tcx.intern_const_stability(const_stab);
self.index.const_stab_map.insert(hir_id, const_stab);
self.index.const_stab_map.insert(def_id, const_stab);
const_span = Some(const_span_node);
const_stab
});
@ -183,7 +184,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
&& !fn_sig.header.is_const()
{
if !self.in_trait_impl
|| (self.in_trait_impl && !self.tcx.is_const_fn_raw(hir_id.owner.to_def_id()))
|| (self.in_trait_impl && !self.tcx.is_const_fn_raw(def_id.to_def_id()))
{
missing_const_err(&self.tcx.sess, fn_sig.span, const_span);
}
@ -196,7 +197,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
debug!("annotate: const_stab not found, parent = {:?}", self.parent_const_stab);
if let Some(parent) = self.parent_const_stab {
if parent.level.is_unstable() {
self.index.const_stab_map.insert(hir_id, parent);
self.index.const_stab_map.insert(def_id, parent);
}
}
}
@ -271,7 +272,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
}
}
self.index.stab_map.insert(hir_id, stab);
self.index.stab_map.insert(def_id, stab);
stab
});
@ -281,13 +282,13 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
if inherit_deprecation.yes() && stab.level.is_unstable()
|| inherit_from_parent.yes()
{
self.index.stab_map.insert(hir_id, stab);
self.index.stab_map.insert(def_id, stab);
}
}
}
self.recurse_with_stability_attrs(
depr.map(|(d, _)| DeprecationEntry::local(d, hir_id)),
depr.map(|(d, _)| DeprecationEntry::local(d, def_id)),
stab,
if inherit_const_stability.yes() { const_stab } else { None },
visit_children,
@ -333,7 +334,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
// returns true if an error occurred, used to suppress some spurious errors
fn forbid_staged_api_attrs(
&mut self,
hir_id: HirId,
def_id: LocalDefId,
attrs: &[Attribute],
inherit_deprecation: InheritDeprecation,
) -> bool {
@ -365,7 +366,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
// -Zforce-unstable-if-unmarked is set.
if let Some(stab) = self.parent_stab {
if inherit_deprecation.yes() && stab.level.is_unstable() {
self.index.stab_map.insert(hir_id, stab);
self.index.stab_map.insert(def_id, stab);
}
}
@ -407,7 +408,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
hir::ItemKind::Struct(ref sd, _) => {
if let Some(ctor_hir_id) = sd.ctor_hir_id() {
self.annotate(
ctor_hir_id,
self.tcx.hir().local_def_id(ctor_hir_id),
i.span,
None,
AnnotationKind::Required,
@ -425,7 +426,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
}
self.annotate(
i.hir_id(),
i.def_id,
i.span,
fn_sig,
kind,
@ -444,7 +445,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
};
self.annotate(
ti.hir_id(),
ti.def_id,
ti.span,
fn_sig,
AnnotationKind::Required,
@ -467,7 +468,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
};
self.annotate(
ii.hir_id(),
ii.def_id,
ii.span,
fn_sig,
kind,
@ -482,7 +483,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
fn visit_variant(&mut self, var: &'tcx Variant<'tcx>, g: &'tcx Generics<'tcx>, item_id: HirId) {
self.annotate(
var.id,
self.tcx.hir().local_def_id(var.id),
var.span,
None,
AnnotationKind::Required,
@ -492,7 +493,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
|v| {
if let Some(ctor_hir_id) = var.data.ctor_hir_id() {
v.annotate(
ctor_hir_id,
v.tcx.hir().local_def_id(ctor_hir_id),
var.span,
None,
AnnotationKind::Required,
@ -510,7 +511,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
fn visit_field_def(&mut self, s: &'tcx FieldDef<'tcx>) {
self.annotate(
s.hir_id,
self.tcx.hir().local_def_id(s.hir_id),
s.span,
None,
AnnotationKind::Required,
@ -525,7 +526,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
fn visit_foreign_item(&mut self, i: &'tcx hir::ForeignItem<'tcx>) {
self.annotate(
i.hir_id(),
i.def_id,
i.span,
None,
AnnotationKind::Required,
@ -540,7 +541,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
self.annotate(
md.hir_id(),
md.def_id,
md.span,
None,
AnnotationKind::Required,
@ -560,7 +561,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
};
self.annotate(
p.hir_id,
self.tcx.hir().local_def_id(p.hir_id),
p.span,
None,
kind,
@ -580,22 +581,19 @@ struct MissingStabilityAnnotations<'tcx> {
}
impl<'tcx> MissingStabilityAnnotations<'tcx> {
fn check_missing_stability(&self, hir_id: HirId, span: Span) {
let stab = self.tcx.stability().local_stability(hir_id);
let is_error =
!self.tcx.sess.opts.test && stab.is_none() && self.access_levels.is_reachable(hir_id);
if is_error {
let def_id = self.tcx.hir().local_def_id(hir_id);
fn check_missing_stability(&self, def_id: LocalDefId, span: Span) {
let stab = self.tcx.stability().local_stability(def_id);
if !self.tcx.sess.opts.test && stab.is_none() && self.access_levels.is_reachable(def_id) {
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
self.tcx.sess.span_err(span, &format!("{} has missing stability attribute", descr));
}
}
fn check_missing_const_stability(&self, hir_id: HirId, span: Span) {
fn check_missing_const_stability(&self, def_id: LocalDefId, span: Span) {
let stab_map = self.tcx.stability();
let stab = stab_map.local_stability(hir_id);
let stab = stab_map.local_stability(def_id);
if stab.map_or(false, |stab| stab.level.is_stable()) {
let const_stab = stab_map.local_const_stability(hir_id);
let const_stab = stab_map.local_const_stability(def_id);
if const_stab.is_none() {
self.tcx.sess.span_err(
span,
@ -624,7 +622,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
hir::ItemKind::Impl(hir::Impl { of_trait: None, .. })
| hir::ItemKind::ForeignMod { .. }
) {
self.check_missing_stability(i.hir_id(), i.span);
self.check_missing_stability(i.def_id, i.span);
}
// Ensure `const fn` that are `stable` have one of `rustc_const_unstable` or
@ -632,42 +630,42 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
if self.tcx.features().staged_api
&& matches!(&i.kind, hir::ItemKind::Fn(sig, ..) if sig.header.is_const())
{
self.check_missing_const_stability(i.hir_id(), i.span);
self.check_missing_const_stability(i.def_id, i.span);
}
intravisit::walk_item(self, i)
}
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem<'tcx>) {
self.check_missing_stability(ti.hir_id(), ti.span);
self.check_missing_stability(ti.def_id, ti.span);
intravisit::walk_trait_item(self, ti);
}
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
let impl_def_id = self.tcx.hir().local_def_id(self.tcx.hir().get_parent_item(ii.hir_id()));
if self.tcx.impl_trait_ref(impl_def_id).is_none() {
self.check_missing_stability(ii.hir_id(), ii.span);
self.check_missing_stability(ii.def_id, ii.span);
}
intravisit::walk_impl_item(self, ii);
}
fn visit_variant(&mut self, var: &'tcx Variant<'tcx>, g: &'tcx Generics<'tcx>, item_id: HirId) {
self.check_missing_stability(var.id, var.span);
self.check_missing_stability(self.tcx.hir().local_def_id(var.id), var.span);
intravisit::walk_variant(self, var, g, item_id);
}
fn visit_field_def(&mut self, s: &'tcx FieldDef<'tcx>) {
self.check_missing_stability(s.hir_id, s.span);
self.check_missing_stability(self.tcx.hir().local_def_id(s.hir_id), s.span);
intravisit::walk_field_def(self, s);
}
fn visit_foreign_item(&mut self, i: &'tcx hir::ForeignItem<'tcx>) {
self.check_missing_stability(i.hir_id(), i.span);
self.check_missing_stability(i.def_id, i.span);
intravisit::walk_foreign_item(self, i);
}
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
self.check_missing_stability(md.hir_id(), md.span);
self.check_missing_stability(md.def_id, md.span);
}
// Note that we don't need to `check_missing_stability` for default generic parameters,
@ -731,7 +729,7 @@ fn stability_index(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> {
}
annotator.annotate(
hir::CRATE_HIR_ID,
CRATE_DEF_ID,
krate.module().inner,
None,
AnnotationKind::Required,
@ -929,7 +927,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
if tcx.stability().staged_api[&LOCAL_CRATE] {
let krate = tcx.hir().krate();
let mut missing = MissingStabilityAnnotations { tcx, access_levels };
missing.check_missing_stability(hir::CRATE_HIR_ID, krate.module().inner);
missing.check_missing_stability(CRATE_DEF_ID, krate.module().inner);
intravisit::walk_crate(&mut missing, krate);
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
}

View File

@ -11,7 +11,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::intravisit::{self, DeepVisitor, NestedVisitorMap, Visitor};
use rustc_hir::{AssocItemKind, HirIdSet, Node, PatKind};
use rustc_middle::bug;
@ -385,8 +385,7 @@ impl VisibilityLike for Option<AccessLevel> {
fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self {
cmp::min(
if let Some(def_id) = def_id.as_local() {
let hir_id = find.tcx.hir().local_def_id_to_hir_id(def_id);
find.access_levels.map.get(&hir_id).cloned()
find.access_levels.map.get(&def_id).copied()
} else {
Self::MAX
},
@ -416,7 +415,7 @@ struct EmbargoVisitor<'tcx> {
/// pub macro m() {
/// n::p::f()
/// }
macro_reachable: FxHashSet<(hir::HirId, DefId)>,
macro_reachable: FxHashSet<(LocalDefId, LocalDefId)>,
/// Previous accessibility level; `None` means unreachable.
prev_level: Option<AccessLevel>,
/// Has something changed in the level map?
@ -430,16 +429,16 @@ struct ReachEverythingInTheInterfaceVisitor<'a, 'tcx> {
}
impl EmbargoVisitor<'tcx> {
fn get(&self, id: hir::HirId) -> Option<AccessLevel> {
self.access_levels.map.get(&id).cloned()
fn get(&self, def_id: LocalDefId) -> Option<AccessLevel> {
self.access_levels.map.get(&def_id).copied()
}
/// Updates node level and returns the updated level.
fn update(&mut self, id: hir::HirId, level: Option<AccessLevel>) -> Option<AccessLevel> {
let old_level = self.get(id);
fn update(&mut self, def_id: LocalDefId, level: Option<AccessLevel>) -> Option<AccessLevel> {
let old_level = self.get(def_id);
// Accessibility levels can only grow.
if level > old_level {
self.access_levels.map.insert(id, level.unwrap());
self.access_levels.map.insert(def_id, level.unwrap());
self.changed = true;
level
} else {
@ -461,31 +460,33 @@ impl EmbargoVisitor<'tcx> {
/// Updates the item as being reachable through a macro defined in the given
/// module. Returns `true` if the level has changed.
fn update_macro_reachable(&mut self, reachable_mod: hir::HirId, defining_mod: DefId) -> bool {
if self.macro_reachable.insert((reachable_mod, defining_mod)) {
self.update_macro_reachable_mod(reachable_mod, defining_mod);
fn update_macro_reachable(
&mut self,
module_def_id: LocalDefId,
defining_mod: LocalDefId,
) -> bool {
if self.macro_reachable.insert((module_def_id, defining_mod)) {
self.update_macro_reachable_mod(module_def_id, defining_mod);
true
} else {
false
}
}
fn update_macro_reachable_mod(&mut self, reachable_mod: hir::HirId, defining_mod: DefId) {
let module_def_id = self.tcx.hir().local_def_id(reachable_mod);
fn update_macro_reachable_mod(&mut self, module_def_id: LocalDefId, defining_mod: LocalDefId) {
let module = self.tcx.hir().get_module(module_def_id).0;
for item_id in module.item_ids {
let def_kind = self.tcx.def_kind(item_id.def_id);
let vis = self.tcx.visibility(item_id.def_id);
self.update_macro_reachable_def(item_id.hir_id(), def_kind, vis, defining_mod);
self.update_macro_reachable_def(item_id.def_id, def_kind, vis, defining_mod);
}
if let Some(exports) = self.tcx.module_exports(module_def_id) {
for export in exports {
if export.vis.is_accessible_from(defining_mod, self.tcx) {
if export.vis.is_accessible_from(defining_mod.to_def_id(), self.tcx) {
if let Res::Def(def_kind, def_id) = export.res {
if let Some(def_id) = def_id.as_local() {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
let vis = self.tcx.visibility(def_id.to_def_id());
self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod);
self.update_macro_reachable_def(def_id, def_kind, vis, defining_mod);
}
}
}
@ -495,14 +496,14 @@ impl EmbargoVisitor<'tcx> {
fn update_macro_reachable_def(
&mut self,
hir_id: hir::HirId,
def_id: LocalDefId,
def_kind: DefKind,
vis: ty::Visibility,
module: DefId,
module: LocalDefId,
) {
let level = Some(AccessLevel::Reachable);
if let ty::Visibility::Public = vis {
self.update(hir_id, level);
self.update(def_id, level);
}
match def_kind {
// No type privacy, so can be directly marked as reachable.
@ -511,8 +512,8 @@ impl EmbargoVisitor<'tcx> {
| DefKind::Static
| DefKind::TraitAlias
| DefKind::TyAlias => {
if vis.is_accessible_from(module, self.tcx) {
self.update(hir_id, level);
if vis.is_accessible_from(module.to_def_id(), self.tcx) {
self.update(def_id, level);
}
}
@ -521,23 +522,23 @@ impl EmbargoVisitor<'tcx> {
// hygiene these don't need to be marked reachable. The contents of
// the module, however may be reachable.
DefKind::Mod => {
if vis.is_accessible_from(module, self.tcx) {
self.update_macro_reachable(hir_id, module);
if vis.is_accessible_from(module.to_def_id(), self.tcx) {
self.update_macro_reachable(def_id, module);
}
}
DefKind::Struct | DefKind::Union => {
// While structs and unions have type privacy, their fields do
// not.
// While structs and unions have type privacy, their fields do not.
if let ty::Visibility::Public = vis {
let item = self.tcx.hir().expect_item(hir_id);
let item =
self.tcx.hir().expect_item(self.tcx.hir().local_def_id_to_hir_id(def_id));
if let hir::ItemKind::Struct(ref struct_def, _)
| hir::ItemKind::Union(ref struct_def, _) = item.kind
{
for field in struct_def.fields() {
let field_vis =
self.tcx.visibility(self.tcx.hir().local_def_id(field.hir_id));
if field_vis.is_accessible_from(module, self.tcx) {
if field_vis.is_accessible_from(module.to_def_id(), self.tcx) {
self.reach(field.hir_id, level).ty();
}
}
@ -616,7 +617,7 @@ impl EmbargoVisitor<'tcx> {
continue;
}
if let hir::ItemKind::Use(..) = item.kind {
self.update(item.hir_id(), Some(AccessLevel::Exported));
self.update(item.def_id, Some(AccessLevel::Exported));
}
}
}
@ -665,47 +666,48 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
};
// Update level of the item itself.
let item_level = self.update(item.hir_id(), inherited_item_level);
let item_level = self.update(item.def_id, inherited_item_level);
// Update levels of nested things.
match item.kind {
hir::ItemKind::Enum(ref def, _) => {
for variant in def.variants {
let variant_level = self.update(variant.id, item_level);
let variant_level =
self.update(self.tcx.hir().local_def_id(variant.id), item_level);
if let Some(ctor_hir_id) = variant.data.ctor_hir_id() {
self.update(ctor_hir_id, item_level);
self.update(self.tcx.hir().local_def_id(ctor_hir_id), item_level);
}
for field in variant.data.fields() {
self.update(field.hir_id, variant_level);
self.update(self.tcx.hir().local_def_id(field.hir_id), variant_level);
}
}
}
hir::ItemKind::Impl(ref impl_) => {
for impl_item_ref in impl_.items {
if impl_.of_trait.is_some() || impl_item_ref.vis.node.is_pub() {
self.update(impl_item_ref.id.hir_id(), item_level);
self.update(impl_item_ref.id.def_id, item_level);
}
}
}
hir::ItemKind::Trait(.., trait_item_refs) => {
for trait_item_ref in trait_item_refs {
self.update(trait_item_ref.id.hir_id(), item_level);
self.update(trait_item_ref.id.def_id, item_level);
}
}
hir::ItemKind::Struct(ref def, _) | hir::ItemKind::Union(ref def, _) => {
if let Some(ctor_hir_id) = def.ctor_hir_id() {
self.update(ctor_hir_id, item_level);
self.update(self.tcx.hir().local_def_id(ctor_hir_id), item_level);
}
for field in def.fields() {
if field.vis.node.is_pub() {
self.update(field.hir_id, item_level);
self.update(self.tcx.hir().local_def_id(field.hir_id), item_level);
}
}
}
hir::ItemKind::ForeignMod { items, .. } => {
for foreign_item in items {
if foreign_item.vis.node.is_pub() {
self.update(foreign_item.id.hir_id(), item_level);
self.update(foreign_item.id.def_id, item_level);
}
}
}
@ -789,7 +791,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
self.reach(item.hir_id(), item_level).generics().predicates().ty().trait_ref();
for impl_item_ref in impl_.items {
let impl_item_level = self.get(impl_item_ref.id.hir_id());
let impl_item_level = self.get(impl_item_ref.id.def_id);
if impl_item_level.is_some() {
self.reach(impl_item_ref.id.hir_id(), impl_item_level)
.generics()
@ -806,21 +808,21 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
self.reach(item.hir_id(), item_level).generics().predicates();
}
for variant in def.variants {
let variant_level = self.get(variant.id);
let variant_level = self.get(self.tcx.hir().local_def_id(variant.id));
if variant_level.is_some() {
for field in variant.data.fields() {
self.reach(field.hir_id, variant_level).ty();
}
// Corner case: if the variant is reachable, but its
// enum is not, make the enum reachable as well.
self.update(item.hir_id(), variant_level);
self.update(item.def_id, variant_level);
}
}
}
// Visit everything, but foreign items have their own levels.
hir::ItemKind::ForeignMod { items, .. } => {
for foreign_item in items {
let foreign_item_level = self.get(foreign_item.id.hir_id());
let foreign_item_level = self.get(foreign_item.id.def_id);
if foreign_item_level.is_some() {
self.reach(foreign_item.id.hir_id(), foreign_item_level)
.generics()
@ -834,7 +836,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
if item_level.is_some() {
self.reach(item.hir_id(), item_level).generics().predicates();
for field in struct_def.fields() {
let field_level = self.get(field.hir_id);
let field_level = self.get(self.tcx.hir().local_def_id(field.hir_id));
if field_level.is_some() {
self.reach(field.hir_id, field_level).ty();
}
@ -867,8 +869,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
if export.vis == ty::Visibility::Public {
if let Some(def_id) = export.res.opt_def_id() {
if let Some(def_id) = def_id.as_local() {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
self.update(hir_id, Some(AccessLevel::Exported));
self.update(def_id, Some(AccessLevel::Exported));
}
}
}
@ -888,32 +889,35 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
// `#[macro_export]`-ed `macro_rules!` are `Public` since they
// ignore their containing path to always appear at the crate root.
if md.ast.macro_rules {
self.update(md.hir_id(), Some(AccessLevel::Public));
self.update(md.def_id, Some(AccessLevel::Public));
}
return;
}
let macro_module_def_id = ty::DefIdTree::parent(self.tcx, md.def_id.to_def_id()).unwrap();
let hir_id = macro_module_def_id
.as_local()
.map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id));
let mut module_id = match hir_id {
Some(module_id) if self.tcx.hir().is_hir_id_module(module_id) => module_id,
// `module_id` doesn't correspond to a `mod`, return early (#63164, #65252).
_ => return,
};
let level = if md.vis.node.is_pub() { self.get(module_id) } else { None };
let new_level = self.update(md.hir_id(), level);
let macro_module_def_id =
ty::DefIdTree::parent(self.tcx, md.def_id.to_def_id()).unwrap().expect_local();
if self.tcx.hir().opt_def_kind(macro_module_def_id) != Some(DefKind::Mod) {
// The macro's parent doesn't correspond to a `mod`, return early (#63164, #65252).
return;
}
let level = if md.vis.node.is_pub() { self.get(macro_module_def_id) } else { None };
let new_level = self.update(md.def_id, level);
if new_level.is_none() {
return;
}
// Since we are starting from an externally visible module,
// all the parents in the loop below are also guaranteed to be modules.
let mut module_def_id = macro_module_def_id;
loop {
let changed_reachability = self.update_macro_reachable(module_id, macro_module_def_id);
if changed_reachability || module_id == hir::CRATE_HIR_ID {
let changed_reachability =
self.update_macro_reachable(module_def_id, macro_module_def_id);
if changed_reachability || module_def_id == CRATE_DEF_ID {
break;
}
module_id = self.tcx.hir().get_parent_node(module_id);
module_def_id =
ty::DefIdTree::parent(self.tcx, module_def_id.to_def_id()).unwrap().expect_local();
}
}
}
@ -971,8 +975,7 @@ impl DefIdVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'tcx> {
if let (ty::Visibility::Public, _) | (_, Some(AccessLevel::ReachableFromImplTrait)) =
(self.tcx().visibility(def_id.to_def_id()), self.access_level)
{
let hir_id = self.ev.tcx.hir().local_def_id_to_hir_id(def_id);
self.ev.update(hir_id, self.access_level);
self.ev.update(def_id, self.access_level);
}
}
ControlFlow::CONTINUE
@ -1449,7 +1452,7 @@ impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
}
}
fn trait_is_public(&self, trait_id: hir::HirId) -> bool {
fn trait_is_public(&self, trait_id: LocalDefId) -> bool {
// FIXME: this would preferably be using `exported_items`, but all
// traits are exported currently (see `EmbargoVisitor.exported_trait`).
self.access_levels.is_public(trait_id)
@ -1463,8 +1466,8 @@ impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
}
}
fn item_is_public(&self, id: &hir::HirId, vis: &hir::Visibility<'_>) -> bool {
self.access_levels.is_reachable(*id) || vis.node.is_pub()
fn item_is_public(&self, def_id: LocalDefId, vis: &hir::Visibility<'_>) -> bool {
self.access_levels.is_reachable(def_id) || vis.node.is_pub()
}
}
@ -1524,7 +1527,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
hir::ItemKind::ForeignMod { .. } => {}
hir::ItemKind::Trait(.., ref bounds, _) => {
if !self.trait_is_public(item.hir_id()) {
if !self.trait_is_public(item.def_id) {
return;
}
@ -1564,10 +1567,8 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
let not_private_trait = impl_.of_trait.as_ref().map_or(
true, // no trait counts as public trait
|tr| {
let did = tr.path.res.def_id();
if let Some(did) = did.as_local() {
self.trait_is_public(self.tcx.hir().local_def_id_to_hir_id(did))
if let Some(def_id) = tr.path.res.def_id().as_local() {
self.trait_is_public(def_id)
} else {
true // external traits must be public
}
@ -1587,7 +1588,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
match impl_item.kind {
hir::ImplItemKind::Const(..) | hir::ImplItemKind::Fn(..) => {
self.access_levels.is_reachable(impl_item_ref.id.hir_id())
self.access_levels.is_reachable(impl_item_ref.id.def_id)
}
hir::ImplItemKind::TyAlias(_) => false,
}
@ -1607,10 +1608,8 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
match impl_item.kind {
hir::ImplItemKind::Const(..) | hir::ImplItemKind::Fn(..)
if self.item_is_public(
&impl_item.hir_id(),
&impl_item.vis,
) =>
if self
.item_is_public(impl_item.def_id, &impl_item.vis) =>
{
intravisit::walk_impl_item(self, impl_item)
}
@ -1651,7 +1650,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
// methods will be visible as `Public::foo`.
let mut found_pub_static = false;
for impl_item_ref in impl_.items {
if self.item_is_public(&impl_item_ref.id.hir_id(), &impl_item_ref.vis) {
if self.item_is_public(impl_item_ref.id.def_id, &impl_item_ref.vis) {
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
match impl_item_ref.kind {
AssocItemKind::Const => {
@ -1678,7 +1677,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
hir::ItemKind::TyAlias(..) => return,
// Not at all public, so we don't care.
_ if !self.item_is_public(&item.hir_id(), &item.vis) => {
_ if !self.item_is_public(item.def_id, &item.vis) => {
return;
}
@ -1714,7 +1713,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
}
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
if self.access_levels.is_reachable(item.hir_id()) {
if self.access_levels.is_reachable(item.def_id) {
intravisit::walk_foreign_item(self, item)
}
}
@ -1734,7 +1733,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
g: &'tcx hir::Generics<'tcx>,
item_id: hir::HirId,
) {
if self.access_levels.is_reachable(v.id) {
if self.access_levels.is_reachable(self.tcx.hir().local_def_id(v.id)) {
self.in_variant = true;
intravisit::walk_variant(self, v, g, item_id);
self.in_variant = false;
@ -2150,7 +2149,7 @@ fn privacy_access_levels(tcx: TyCtxt<'_>, (): ()) -> &AccessLevels {
break;
}
}
visitor.update(hir::CRATE_HIR_ID, Some(AccessLevel::Public));
visitor.update(CRATE_DEF_ID, Some(AccessLevel::Public));
tcx.arena.alloc(visitor.access_levels)
}

View File

@ -255,16 +255,17 @@ impl<'tcx> DumpVisitor<'tcx> {
&mut self,
sig: &'tcx hir::FnSig<'tcx>,
body: Option<hir::BodyId>,
hir_id: hir::HirId,
def_id: LocalDefId,
ident: Ident,
generics: &'tcx hir::Generics<'tcx>,
vis: &hir::Visibility<'tcx>,
span: Span,
) {
debug!("process_method: {}:{}", hir_id, ident);
debug!("process_method: {:?}:{}", def_id, ident);
let map = &self.tcx.hir();
self.nest_typeck_results(map.local_def_id(hir_id), |v| {
let hir_id = map.local_def_id_to_hir_id(def_id);
self.nest_typeck_results(def_id, |v| {
if let Some(mut method_data) = v.save_ctxt.get_method_data(hir_id, ident, span) {
if let Some(body) = body {
v.process_formals(map.body(body).params, &method_data.qualname);
@ -275,7 +276,7 @@ impl<'tcx> DumpVisitor<'tcx> {
fn_to_string(sig.decl, sig.header, Some(ident.name), generics, vis, &[], None);
method_data.sig = sig::method_signature(hir_id, ident, generics, sig, &v.save_ctxt);
v.dumper.dump_def(&access_from_vis!(v.save_ctxt, vis, hir_id), method_data);
v.dumper.dump_def(&access_from_vis!(v.save_ctxt, vis, def_id), method_data);
}
// walk arg and return types
@ -301,7 +302,10 @@ impl<'tcx> DumpVisitor<'tcx> {
) {
let field_data = self.save_ctxt.get_field_data(field, parent_id);
if let Some(field_data) = field_data {
self.dumper.dump_def(&access_from!(self.save_ctxt, field, field.hir_id), field_data);
self.dumper.dump_def(
&access_from!(self.save_ctxt, field, self.tcx.hir().local_def_id(field.hir_id)),
field_data,
);
}
}
@ -366,7 +370,7 @@ impl<'tcx> DumpVisitor<'tcx> {
v.process_formals(body.params, &fn_data.qualname);
v.process_generic_params(ty_params, &fn_data.qualname, item.hir_id());
v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.hir_id()), fn_data);
v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.def_id), fn_data);
}
for arg in decl.inputs {
@ -390,7 +394,7 @@ impl<'tcx> DumpVisitor<'tcx> {
self.nest_typeck_results(item.def_id, |v| {
if let Some(var_data) = v.save_ctxt.get_item_data(item) {
down_cast_data!(var_data, DefData, item.span);
v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.hir_id()), var_data);
v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.def_id), var_data);
}
v.visit_ty(&typ);
v.visit_expr(expr);
@ -399,7 +403,7 @@ impl<'tcx> DumpVisitor<'tcx> {
fn process_assoc_const(
&mut self,
hir_id: hir::HirId,
def_id: LocalDefId,
ident: Ident,
typ: &'tcx hir::Ty<'tcx>,
expr: Option<&'tcx hir::Expr<'tcx>>,
@ -407,15 +411,15 @@ impl<'tcx> DumpVisitor<'tcx> {
vis: &hir::Visibility<'tcx>,
attrs: &'tcx [ast::Attribute],
) {
let qualname =
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(hir_id).to_def_id()));
let qualname = format!("::{}", self.tcx.def_path_str(def_id.to_def_id()));
if !self.span.filter_generated(ident.span) {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
let sig = sig::assoc_const_signature(hir_id, ident.name, typ, expr, &self.save_ctxt);
let span = self.span_from_span(ident.span);
self.dumper.dump_def(
&access_from_vis!(self.save_ctxt, vis, hir_id),
&access_from_vis!(self.save_ctxt, vis, def_id),
Def {
kind: DefKind::Const,
id: id_from_hir_id(hir_id, &self.save_ctxt),
@ -434,7 +438,7 @@ impl<'tcx> DumpVisitor<'tcx> {
}
// walk type and init value
self.nest_typeck_results(self.tcx.hir().local_def_id(hir_id), |v| {
self.nest_typeck_results(def_id, |v| {
v.visit_ty(typ);
if let Some(expr) = expr {
v.visit_expr(expr);
@ -484,7 +488,7 @@ impl<'tcx> DumpVisitor<'tcx> {
let span = self.span_from_span(item.ident.span);
let attrs = self.tcx.hir().attrs(item.hir_id());
self.dumper.dump_def(
&access_from!(self.save_ctxt, item, item.hir_id()),
&access_from!(self.save_ctxt, item, item.def_id),
Def {
kind,
id: id_from_def_id(item.def_id.to_def_id()),
@ -525,7 +529,7 @@ impl<'tcx> DumpVisitor<'tcx> {
};
down_cast_data!(enum_data, DefData, item.span);
let access = access_from!(self.save_ctxt, item, item.hir_id());
let access = access_from!(self.save_ctxt, item, item.def_id);
for variant in enum_definition.variants {
let name = variant.ident.name.to_string();
@ -660,7 +664,7 @@ impl<'tcx> DumpVisitor<'tcx> {
methods.iter().map(|i| id_from_def_id(i.id.def_id.to_def_id())).collect();
let attrs = self.tcx.hir().attrs(item.hir_id());
self.dumper.dump_def(
&access_from!(self.save_ctxt, item, item.hir_id()),
&access_from!(self.save_ctxt, item, item.def_id),
Def {
kind: DefKind::Trait,
id,
@ -723,7 +727,7 @@ impl<'tcx> DumpVisitor<'tcx> {
fn process_mod(&mut self, item: &'tcx hir::Item<'tcx>) {
if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
down_cast_data!(mod_data, DefData, item.span);
self.dumper.dump_def(&access_from!(self.save_ctxt, item, item.hir_id()), mod_data);
self.dumper.dump_def(&access_from!(self.save_ctxt, item, item.def_id), mod_data);
}
}
@ -985,7 +989,7 @@ impl<'tcx> DumpVisitor<'tcx> {
let respan = respan(vis_span, hir::VisibilityKind::Public);
let attrs = self.tcx.hir().attrs(trait_item.hir_id());
self.process_assoc_const(
trait_item.hir_id(),
trait_item.def_id,
trait_item.ident,
&ty,
body,
@ -1001,7 +1005,7 @@ impl<'tcx> DumpVisitor<'tcx> {
self.process_method(
sig,
body,
trait_item.hir_id(),
trait_item.def_id,
trait_item.ident,
&trait_item.generics,
&respan,
@ -1058,7 +1062,7 @@ impl<'tcx> DumpVisitor<'tcx> {
let body = self.tcx.hir().body(body);
let attrs = self.tcx.hir().attrs(impl_item.hir_id());
self.process_assoc_const(
impl_item.hir_id(),
impl_item.def_id,
impl_item.ident,
&ty,
Some(&body.value),
@ -1071,7 +1075,7 @@ impl<'tcx> DumpVisitor<'tcx> {
self.process_method(
sig,
Some(body),
impl_item.hir_id(),
impl_item.def_id,
impl_item.ident,
&impl_item.generics,
&impl_item.vis,
@ -1146,7 +1150,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
hir::ItemKind::Use(path, hir::UseKind::Single) => {
let sub_span = path.segments.last().unwrap().ident.span;
if !self.span.filter_generated(sub_span) {
let access = access_from!(self.save_ctxt, item, item.hir_id());
let access = access_from!(self.save_ctxt, item, item.def_id);
let ref_id = self.lookup_def_id(item.hir_id()).map(id_from_def_id);
let span = self.span_from_span(sub_span);
let parent =
@ -1175,7 +1179,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
// we don't want to track anyway, since it's probably macro-internal `use`
if let Some(sub_span) = self.span.sub_span_of_star(item.span) {
if !self.span.filter_generated(item.span) {
let access = access_from!(self.save_ctxt, item, item.hir_id());
let access = access_from!(self.save_ctxt, item, item.def_id);
let span = self.span_from_span(sub_span);
let parent =
self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id);
@ -1248,7 +1252,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
let attrs = self.tcx.hir().attrs(item.hir_id());
self.dumper.dump_def(
&access_from!(self.save_ctxt, item, item.hir_id()),
&access_from!(self.save_ctxt, item, item.def_id),
Def {
kind: DefKind::Type,
id,
@ -1432,7 +1436,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
}
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
let access = access_from!(self.save_ctxt, item, item.hir_id());
let access = access_from!(self.save_ctxt, item, item.def_id);
match item.kind {
hir::ForeignItemKind::Fn(decl, _, ref generics) => {

View File

@ -374,15 +374,8 @@ crate fn run_global_ctxt(
});
rustc_passes::stability::check_unused_or_stable_features(tcx);
let access_levels = tcx.privacy_access_levels(());
// Convert from a HirId set to a DefId set since we don't always have easy access
// to the map from defid -> hirid
let access_levels = AccessLevels {
map: access_levels
.map
.iter()
.map(|(&k, &v)| (tcx.hir().local_def_id(k).to_def_id(), v))
.collect(),
map: tcx.privacy_access_levels(()).map.iter().map(|(k, v)| (k.to_def_id(), *v)).collect(),
};
let mut ctxt = DocContext {

View File

@ -1244,11 +1244,8 @@ impl LinkCollector<'_, '_> {
item.def_id.expect_def_id().as_local().map(|src_id| (src_id, dst_id))
})
{
let hir_src = self.cx.tcx.hir().local_def_id_to_hir_id(src_id);
let hir_dst = self.cx.tcx.hir().local_def_id_to_hir_id(dst_id);
if self.cx.tcx.privacy_access_levels(()).is_exported(hir_src)
&& !self.cx.tcx.privacy_access_levels(()).is_exported(hir_dst)
if self.cx.tcx.privacy_access_levels(()).is_exported(src_id)
&& !self.cx.tcx.privacy_access_levels(()).is_exported(dst_id)
{
privacy_error(self.cx, &diag_info, &path_str);
}

View File

@ -21,6 +21,7 @@ use rustc_parse::maybe_new_parser_from_source_str;
use rustc_parse::parser::ForceCollect;
use rustc_session::parse::ParseSess;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::edition::Edition;
use rustc_span::source_map::{BytePos, FilePathMapping, MultiSpan, SourceMap, Span};
use rustc_span::{sym, FileName, Pos};
@ -231,7 +232,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
fpu.visit_expr(&body.value);
lint_for_missing_headers(
cx,
item.hir_id(),
item.def_id,
item.span,
sig,
headers,
@ -258,7 +259,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
let headers = check_attrs(cx, &self.valid_idents, attrs);
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
if !in_external_macro(cx.tcx.sess, item.span) {
lint_for_missing_headers(cx, item.hir_id(), item.span, sig, headers, None, None);
lint_for_missing_headers(cx, item.def_id, item.span, sig, headers, None, None);
}
}
}
@ -279,7 +280,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
fpu.visit_expr(&body.value);
lint_for_missing_headers(
cx,
item.hir_id(),
item.def_id,
item.span,
sig,
headers,
@ -292,14 +293,14 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
fn lint_for_missing_headers<'tcx>(
cx: &LateContext<'tcx>,
hir_id: hir::HirId,
def_id: LocalDefId,
span: impl Into<MultiSpan> + Copy,
sig: &hir::FnSig<'_>,
headers: DocHeaders,
body_id: Option<hir::BodyId>,
panic_span: Option<Span>,
) {
if !cx.access_levels.is_exported(hir_id) {
if !cx.access_levels.is_exported(def_id) {
return; // Private functions do not require doc comments
}
if !headers.safety && sig.header.unsafety == hir::Unsafety::Unsafe {
@ -321,6 +322,7 @@ fn lint_for_missing_headers<'tcx>(
);
}
if !headers.errors {
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type) {
span_lint(
cx,

View File

@ -297,7 +297,7 @@ impl LateLintPass<'_> for EnumVariantNames {
}
}
if let ItemKind::Enum(ref def, _) = item.kind {
if !(self.avoid_breaking_exported_api && cx.access_levels.is_exported(item.hir_id())) {
if !(self.avoid_breaking_exported_api && cx.access_levels.is_exported(item.def_id)) {
check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span);
}
}

View File

@ -71,7 +71,7 @@ impl LateLintPass<'_> for ExhaustiveItems {
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
if_chain! {
if let ItemKind::Enum(..) | ItemKind::Struct(..) = item.kind;
if cx.access_levels.is_exported(item.hir_id());
if cx.access_levels.is_exported(item.def_id);
let attrs = cx.tcx.hir().attrs(item.hir_id());
if !attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
then {

View File

@ -1,6 +1,6 @@
use rustc_ast::ast::Attribute;
use rustc_errors::Applicability;
use rustc_hir::def_id::DefIdSet;
use rustc_hir::def_id::{DefIdSet, LocalDefId};
use rustc_hir::{self as hir, def::Res, intravisit, QPath};
use rustc_lint::{LateContext, LintContext};
use rustc_middle::{
@ -22,7 +22,7 @@ pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
let attrs = cx.tcx.hir().attrs(item.hir_id());
let attr = must_use_attr(attrs);
if let hir::ItemKind::Fn(ref sig, ref _generics, ref body_id) = item.kind {
let is_public = cx.access_levels.is_exported(item.hir_id());
let is_public = cx.access_levels.is_exported(item.def_id);
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
if let Some(attr) = attr {
check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr);
@ -33,7 +33,7 @@ pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
sig.decl,
cx.tcx.hir().body(*body_id),
item.span,
item.hir_id(),
item.def_id,
item.span.with_hi(sig.decl.output.span().hi()),
"this function could have a `#[must_use]` attribute",
);
@ -43,7 +43,7 @@ pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
pub(super) fn check_impl_item(cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
if let hir::ImplItemKind::Fn(ref sig, ref body_id) = item.kind {
let is_public = cx.access_levels.is_exported(item.hir_id());
let is_public = cx.access_levels.is_exported(item.def_id);
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
let attrs = cx.tcx.hir().attrs(item.hir_id());
let attr = must_use_attr(attrs);
@ -55,7 +55,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<
sig.decl,
cx.tcx.hir().body(*body_id),
item.span,
item.hir_id(),
item.def_id,
item.span.with_hi(sig.decl.output.span().hi()),
"this method could have a `#[must_use]` attribute",
);
@ -65,7 +65,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<
pub(super) fn check_trait_item(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
if let hir::TraitItemKind::Fn(ref sig, ref eid) = item.kind {
let is_public = cx.access_levels.is_exported(item.hir_id());
let is_public = cx.access_levels.is_exported(item.def_id);
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
let attrs = cx.tcx.hir().attrs(item.hir_id());
@ -80,7 +80,7 @@ pub(super) fn check_trait_item(cx: &LateContext<'tcx>, item: &'tcx hir::TraitIte
sig.decl,
body,
item.span,
item.hir_id(),
item.def_id,
item.span.with_hi(sig.decl.output.span().hi()),
"this method could have a `#[must_use]` attribute",
);
@ -132,7 +132,7 @@ fn check_must_use_candidate<'tcx>(
decl: &'tcx hir::FnDecl<'_>,
body: &'tcx hir::Body<'_>,
item_span: Span,
item_id: hir::HirId,
item_id: LocalDefId,
fn_span: Span,
msg: &str,
) {
@ -141,7 +141,7 @@ fn check_must_use_candidate<'tcx>(
|| in_external_macro(cx.sess(), item_span)
|| returns_unit(decl)
|| !cx.access_levels.is_exported(item_id)
|| is_must_use_ty(cx, return_ty(cx, item_id))
|| is_must_use_ty(cx, return_ty(cx, cx.tcx.hir().local_def_id_to_hir_id(item_id)))
{
return;
}

View File

@ -1,6 +1,7 @@
use rustc_hir::{self as hir, intravisit, HirIdSet};
use rustc_lint::LateContext;
use rustc_middle::{hir::map::Map, ty};
use rustc_span::def_id::LocalDefId;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::ty::type_is_unsafe_function;
@ -21,13 +22,13 @@ pub(super) fn check_fn(
intravisit::FnKind::Closure => return,
};
check_raw_ptr(cx, unsafety, decl, body, hir_id);
check_raw_ptr(cx, unsafety, decl, body, cx.tcx.hir().local_def_id(hir_id));
}
pub(super) fn check_trait_item(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
if let hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(eid)) = item.kind {
let body = cx.tcx.hir().body(eid);
check_raw_ptr(cx, sig.header.unsafety, sig.decl, body, item.hir_id());
check_raw_ptr(cx, sig.header.unsafety, sig.decl, body, item.def_id);
}
}
@ -36,10 +37,10 @@ fn check_raw_ptr(
unsafety: hir::Unsafety,
decl: &'tcx hir::FnDecl<'tcx>,
body: &'tcx hir::Body<'tcx>,
hir_id: hir::HirId,
def_id: LocalDefId,
) {
let expr = &body.value;
if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(hir_id) {
if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(def_id) {
let raw_ptrs = iter_input_pats(decl, body)
.zip(decl.inputs.iter())
.filter_map(|(arg, ty)| raw_ptr_arg(arg, ty))

View File

@ -15,7 +15,7 @@ use super::RESULT_UNIT_ERR;
pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
if let hir::ItemKind::Fn(ref sig, ref _generics, _) = item.kind {
let is_public = cx.access_levels.is_exported(item.hir_id());
let is_public = cx.access_levels.is_exported(item.def_id);
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
if is_public {
check_result_unit_err(cx, sig.decl, item.span, fn_header_span);
@ -25,7 +25,7 @@ pub(super) fn check_item(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
pub(super) fn check_impl_item(cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
if let hir::ImplItemKind::Fn(ref sig, _) = item.kind {
let is_public = cx.access_levels.is_exported(item.hir_id());
let is_public = cx.access_levels.is_exported(item.def_id);
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
if is_public && trait_ref_of_method(cx, item.hir_id()).is_none() {
check_result_unit_err(cx, sig.decl, item.span, fn_header_span);
@ -35,7 +35,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<
pub(super) fn check_trait_item(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
if let hir::TraitItemKind::Fn(ref sig, _) = item.kind {
let is_public = cx.access_levels.is_exported(item.hir_id());
let is_public = cx.access_levels.is_exported(item.def_id);
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
if is_public {
check_result_unit_err(cx, sig.decl, item.span, fn_header_span);

View File

@ -112,7 +112,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
}
}
if !cx.access_levels.is_exported(item.hir_id()) {
if !cx.access_levels.is_exported(item.def_id) {
return;
}

View File

@ -131,7 +131,7 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
if item.ident.name == sym::len;
if let ImplItemKind::Fn(sig, _) = &item.kind;
if sig.decl.implicit_self.has_implicit_self();
if cx.access_levels.is_exported(item.hir_id());
if cx.access_levels.is_exported(item.def_id);
if matches!(sig.decl.output, FnRetTy::Return(_));
if let Some(imp) = get_parent_as_impl(cx.tcx, item.hir_id());
if imp.of_trait.is_none();
@ -207,7 +207,7 @@ fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, trait_items
}
}
if cx.access_levels.is_exported(visited_trait.hir_id())
if cx.access_levels.is_exported(visited_trait.def_id)
&& trait_items.iter().any(|i| is_named_self(cx, i, sym::len))
{
let mut current_and_super_traits = DefIdSet::default();
@ -331,11 +331,7 @@ fn check_for_is_empty(
None,
None,
),
Some(is_empty)
if !cx
.access_levels
.is_exported(cx.tcx.hir().local_def_id_to_hir_id(is_empty.def_id.expect_local())) =>
{
Some(is_empty) if !cx.access_levels.is_exported(is_empty.def_id.expect_local()) => {
(
format!(
"{} `{}` has a public `len` method, but a private `is_empty` method",

View File

@ -1903,7 +1903,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
then {
// if this impl block implements a trait, lint in trait definition instead
if !implements_trait && cx.access_levels.is_exported(impl_item.hir_id()) {
if !implements_trait && cx.access_levels.is_exported(impl_item.def_id) {
// check missing trait implementations
for method_config in &TRAIT_METHODS {
if name == method_config.method_name &&
@ -1935,7 +1935,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
if sig.decl.implicit_self.has_implicit_self()
&& !(self.avoid_breaking_exported_api
&& cx.access_levels.is_exported(impl_item.hir_id()))
&& cx.access_levels.is_exported(impl_item.def_id))
{
wrong_self_convention::check(
cx,

View File

@ -87,7 +87,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
return;
}
if !cx.access_levels.is_exported(it.hir_id()) {
if !cx.access_levels.is_exported(it.def_id) {
return;
}
match it.kind {
@ -140,7 +140,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
}
// If the item being implemented is not exported, then we don't need #[inline]
if !cx.access_levels.is_exported(impl_item.hir_id()) {
if !cx.access_levels.is_exported(impl_item.def_id) {
return;
}
@ -155,7 +155,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
};
if let Some(trait_def_id) = trait_def_id {
if trait_def_id.is_local() && !cx.access_levels.is_exported(impl_item.hir_id()) {
if trait_def_id.is_local() && !cx.access_levels.is_exported(impl_item.def_id) {
// If a trait is being implemented for an item, and the
// trait is not exported, we don't need #[inline]
return;

View File

@ -99,7 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
if_chain! {
if sig.decl.inputs.is_empty();
if name == sym::new;
if cx.access_levels.is_reachable(id);
if cx.access_levels.is_reachable(impl_item.def_id);
let self_def_id = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent_item(id));
let self_ty = cx.tcx.type_of(self_def_id);
if TyS::same_type(self_ty, return_ty(cx, id));

View File

@ -14,6 +14,7 @@ use rustc_hir::{BindingAnnotation, Body, FnDecl, HirId, Impl, ItemKind, MutTy, M
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::{sym, Span};
use rustc_target::abi::LayoutOf;
use rustc_target::spec::abi::Abi;
@ -134,13 +135,12 @@ impl<'tcx> PassByRefOrValue {
}
}
fn check_poly_fn(&mut self, cx: &LateContext<'tcx>, hir_id: HirId, decl: &FnDecl<'_>, span: Option<Span>) {
if self.avoid_breaking_exported_api && cx.access_levels.is_exported(hir_id) {
fn check_poly_fn(&mut self, cx: &LateContext<'tcx>, def_id: LocalDefId, decl: &FnDecl<'_>, span: Option<Span>) {
if self.avoid_breaking_exported_api && cx.access_levels.is_exported(def_id) {
return;
}
let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
let fn_sig = cx.tcx.fn_sig(fn_def_id);
let fn_sig = cx.tcx.fn_sig(def_id);
let fn_sig = cx.tcx.erase_late_bound_regions(fn_sig);
let fn_body = cx.enclosing_body.map(|id| cx.tcx.hir().body(id));
@ -231,7 +231,7 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
}
if let hir::TraitItemKind::Fn(method_sig, _) = &item.kind {
self.check_poly_fn(cx, item.hir_id(), &*method_sig.decl, None);
self.check_poly_fn(cx, item.def_id, &*method_sig.decl, None);
}
}
@ -278,6 +278,6 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
}
}
self.check_poly_fn(cx, hir_id, decl, Some(span));
self.check_poly_fn(cx, cx.tcx.hir().local_def_id(hir_id), decl, Some(span));
}
}

View File

@ -41,7 +41,7 @@ impl_lint_pass!(RedundantPubCrate => [REDUNDANT_PUB_CRATE]);
impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
if let VisibilityKind::Crate { .. } = item.vis.node {
if !cx.access_levels.is_exported(item.hir_id()) {
if !cx.access_levels.is_exported(item.def_id) {
if let Some(false) = self.is_exported.last() {
let span = item.span.with_hi(item.ident.span.hi());
let descr = cx.tcx.def_kind(item.def_id).descr(item.def_id.to_def_id());
@ -64,7 +64,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
}
if let ItemKind::Mod { .. } = item.kind {
self.is_exported.push(cx.access_levels.is_exported(item.hir_id()));
self.is_exported.push(cx.access_levels.is_exported(item.def_id));
}
}

View File

@ -81,7 +81,8 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
// Abort if public function/method or closure.
match fn_kind {
FnKind::ItemFn(..) | FnKind::Method(..) => {
if self.avoid_breaking_exported_api && cx.access_levels.is_exported(hir_id) {
let def_id = cx.tcx.hir().local_def_id(hir_id);
if self.avoid_breaking_exported_api && cx.access_levels.is_exported(def_id) {
return;
}
},

View File

@ -104,7 +104,7 @@ impl LateLintPass<'_> for UpperCaseAcronyms {
fn check_item(&mut self, cx: &LateContext<'_>, it: &Item<'_>) {
// do not lint public items or in macros
if in_external_macro(cx.sess(), it.span)
|| (self.avoid_breaking_exported_api && cx.access_levels.is_exported(it.hir_id()))
|| (self.avoid_breaking_exported_api && cx.access_levels.is_exported(it.def_id))
{
return;
}