Use () for inherent_impls.

This commit is contained in:
Camille GILLOT 2021-05-11 13:39:19 +02:00
parent 3aa6f3e8f5
commit 837f3e3e67
7 changed files with 19 additions and 26 deletions

View file

@ -944,13 +944,12 @@ fn encode_def_ids(&mut self) {
record!(self.tables.super_predicates[def_id] <- self.tcx.super_predicates_of(def_id));
}
}
let inherent_impls = tcx.crate_inherent_impls(LOCAL_CRATE);
let inherent_impls = tcx.crate_inherent_impls(());
for (def_id, implementations) in inherent_impls.inherent_impls.iter() {
assert!(def_id.is_local());
if implementations.is_empty() {
continue;
}
record!(self.tables.inherent_impls[def_id] <- implementations.iter().map(|&def_id| {
record!(self.tables.inherent_impls[def_id.to_def_id()] <- implementations.iter().map(|&def_id| {
assert!(def_id.is_local());
def_id.index
}));

View file

@ -740,18 +740,15 @@
/// Gets a complete map from all types to their inherent impls.
/// Not meant to be used directly outside of coherence.
/// (Defined only for `LOCAL_CRATE`.)
query crate_inherent_impls(k: CrateNum)
-> CrateInherentImpls {
query crate_inherent_impls(k: ()) -> CrateInherentImpls {
storage(ArenaCacheSelector<'tcx>)
eval_always
desc { "all inherent impls defined in crate `{:?}`", k }
desc { "all inherent impls defined in crate" }
}
/// Checks all types in the crate for overlap in their inherent impls. Reports errors.
/// Not meant to be used directly outside of coherence.
/// (Defined only for `LOCAL_CRATE`.)
query crate_inherent_impls_overlap_check(_: CrateNum)
query crate_inherent_impls_overlap_check(_: ())
-> () {
eval_always
desc { "check for overlap between inherent impls defined in this crate" }

View file

@ -36,7 +36,7 @@
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, CRATE_DEF_INDEX};
use rustc_hir::{Constness, Node};
use rustc_macros::HashStable;
use rustc_span::hygiene::ExpnId;
@ -1983,7 +1983,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
/// (constructing this map requires touching the entire crate).
#[derive(Clone, Debug, Default, HashStable)]
pub struct CrateInherentImpls {
pub inherent_impls: DefIdMap<Vec<DefId>>,
pub inherent_impls: LocalDefIdMap<Vec<DefId>>,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, HashStable)]

View file

@ -9,16 +9,14 @@
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_middle::ty::{self, CrateInherentImpls, TyCtxt};
use rustc_span::Span;
/// On-demand query: yields a map containing all types mapped to their inherent impls.
pub fn crate_inherent_impls(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CrateInherentImpls {
assert_eq!(crate_num, LOCAL_CRATE);
pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls {
let krate = tcx.hir().krate();
let mut collect = InherentCollect { tcx, impls_map: Default::default() };
krate.visit_all_item_likes(&mut collect);
@ -27,9 +25,9 @@ pub fn crate_inherent_impls(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CrateInhere
/// On-demand query: yields a vector of the inherent impls for a specific type.
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: DefId) -> &[DefId] {
assert!(ty_def_id.is_local());
let ty_def_id = ty_def_id.expect_local();
let crate_map = tcx.crate_inherent_impls(ty_def_id.krate);
let crate_map = tcx.crate_inherent_impls(());
match crate_map.inherent_impls.get(&ty_def_id) {
Some(v) => &v[..],
None => &[],
@ -364,7 +362,7 @@ fn visit_foreign_item(&mut self, _foreign_item: &hir::ForeignItem<'_>) {}
impl InherentCollect<'tcx> {
fn check_def_id(&mut self, item: &hir::Item<'_>, def_id: DefId) {
if def_id.is_local() {
if let Some(def_id) = def_id.as_local() {
// Add the implementation to the mapping from implementation to base
// type def ID, if there is a base type for this implementation and
// the implementation does not have any associated traits.

View file

@ -1,7 +1,7 @@
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc_hir::def_id::DefId;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::Symbol;
@ -9,8 +9,7 @@
use smallvec::SmallVec;
use std::collections::hash_map::Entry;
pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, crate_num: CrateNum) {
assert_eq!(crate_num, LOCAL_CRATE);
pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, (): ()) {
let krate = tcx.hir().krate();
krate.visit_all_item_likes(&mut InherentOverlapChecker { tcx });
}

View file

@ -6,7 +6,7 @@
// mappings. That mapping code resides here.
use rustc_errors::struct_span_err;
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt, TypeFoldable};
use rustc_span::Span;
@ -203,8 +203,8 @@ pub fn check_coherence(tcx: TyCtxt<'_>) {
tcx.sess.time("orphan_checking", || orphan::check(tcx));
// these queries are executed for side-effects (error reporting):
tcx.ensure().crate_inherent_impls(LOCAL_CRATE);
tcx.ensure().crate_inherent_impls_overlap_check(LOCAL_CRATE);
tcx.ensure().crate_inherent_impls(());
tcx.ensure().crate_inherent_impls_overlap_check(());
}
/// Checks whether an impl overlaps with the automatic `impl Trait for dyn Trait`.

View file

@ -3,7 +3,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::in_macro;
use rustc_hir::def_id::DefIdMap;
use rustc_hir::{def_id, Crate, Impl, Item, ItemKind};
use rustc_hir::{Crate, Impl, Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::Span;
@ -68,7 +68,7 @@ fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) {
fn check_crate_post(&mut self, cx: &LateContext<'tcx>, krate: &'tcx Crate<'_>) {
if !krate.items.is_empty() {
// Retrieve all inherent implementations from the crate, grouped by type
for impls in cx.tcx.crate_inherent_impls(def_id::LOCAL_CRATE).inherent_impls.values() {
for impls in cx.tcx.crate_inherent_impls(()).inherent_impls.values() {
// Filter out implementations that have generic params (type or lifetime)
let mut impl_spans = impls.iter().filter_map(|impl_def| self.impls.get(impl_def));
if let Some(initial_span) = impl_spans.next() {