From: Camille GILLOT Date: Sat, 12 Mar 2022 23:58:21 +0000 (+0100) Subject: Remove trait_of_item query. X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;ds=sidebyside;h=957548183d774c2f19b69ba50b8ed822130d8628;p=rust.git Remove trait_of_item query. --- diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs index 628298df473..0adb88a180f 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs @@ -772,7 +772,7 @@ fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location let mut nonconst_call_permission = false; if let Some(callee_trait) = tcx.trait_of_item(callee) && tcx.has_attr(callee_trait, sym::const_trait) - && Some(callee_trait) == tcx.trait_of_item(caller) + && Some(callee_trait) == tcx.trait_of_item(caller.to_def_id()) // Can only call methods when it's `::f`. && tcx.types.self_param == substs.type_at(0) { diff --git a/compiler/rustc_incremental/src/persist/dirty_clean.rs b/compiler/rustc_incremental/src/persist/dirty_clean.rs index 35a278e6c92..710c4a01b24 100644 --- a/compiler/rustc_incremental/src/persist/dirty_clean.rs +++ b/compiler/rustc_incremental/src/persist/dirty_clean.rs @@ -80,7 +80,7 @@ /// Extra `DepNode`s for functions and methods. const EXTRA_ASSOCIATED: &[&str] = &[label_strs::associated_item]; -const EXTRA_TRAIT: &[&str] = &[label_strs::trait_of_item]; +const EXTRA_TRAIT: &[&str] = &[]; // Fully Built Labels diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index d8d2ac32c2f..40dc4fb052d 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1303,19 +1303,6 @@ fn get_implementations_of_trait( } } - fn get_trait_of_item(self, id: DefIndex) -> Option { - let def_key = self.def_key(id); - match def_key.disambiguated_data.data { - DefPathData::TypeNs(..) | DefPathData::ValueNs(..) => (), - // Not an associated item - _ => return None, - } - def_key.parent.and_then(|parent_index| match self.kind(parent_index) { - EntryKind::Trait | EntryKind::TraitAlias => Some(self.local_def_id(parent_index)), - _ => None, - }) - } - fn get_native_libraries(self, sess: &'a Session) -> impl Iterator + 'a { self.root.native_libraries.decode((self, sess)) } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 6bf237b8ed5..38ce50e8323 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -235,7 +235,6 @@ fn into_args(self) -> (DefId, SimplifiedType) { inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) } is_foreign_item => { cdata.is_foreign_item(def_id.index) } item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) } - trait_of_item => { cdata.get_trait_of_item(def_id.index) } is_mir_available => { cdata.is_item_mir_available(def_id.index) } is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) } diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index ffa66b79dbf..d8483e7e409 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1147,14 +1147,6 @@ separate_provide_extern } - /// Given an `associated_item`, find the trait it belongs to. - /// Return `None` if the `DefId` is not an associated item. - query trait_of_item(associated_item: DefId) -> Option { - desc { |tcx| "finding trait defining `{}`", tcx.def_path_str(associated_item) } - cache_on_disk_if { associated_item.is_local() } - separate_provide_extern - } - query is_ctfe_mir_available(key: DefId) -> bool { desc { |tcx| "checking if item has ctfe mir available: `{}`", tcx.def_path_str(key) } cache_on_disk_if { key.is_local() } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 77c6c532f41..1978f84c137 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -2191,10 +2191,29 @@ pub fn trait_id_of_impl(self, def_id: DefId) -> Option { self.impl_trait_ref(def_id).map(|tr| tr.def_id) } + /// If the given `DefId` describes an item belonging to a trait, + /// returns the `DefId` of the trait that the trait item belongs to; + /// otherwise, returns `None`. + pub fn trait_of_item(self, def_id: DefId) -> Option { + if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = self.def_kind(def_id) { + let parent = self.parent(def_id); + if let DefKind::Trait | DefKind::TraitAlias = self.def_kind(parent) { + return Some(parent); + } + } + None + } + /// If the given `DefId` describes a method belonging to an impl, returns the /// `DefId` of the impl that the method belongs to; otherwise, returns `None`. pub fn impl_of_method(self, def_id: DefId) -> Option { - self.opt_associated_item(def_id).and_then(|trait_item| trait_item.impl_container(self)) + if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = self.def_kind(def_id) { + let parent = self.parent(def_id); + if let DefKind::Impl = self.def_kind(parent) { + return Some(parent); + } + } + None } /// If the given `DefId` belongs to a trait that was automatically derived, returns `true`. diff --git a/compiler/rustc_mir_build/src/lints.rs b/compiler/rustc_mir_build/src/lints.rs index 3a32fa310d4..54d549fd66c 100644 --- a/compiler/rustc_mir_build/src/lints.rs +++ b/compiler/rustc_mir_build/src/lints.rs @@ -14,7 +14,7 @@ pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { if let DefKind::Fn | DefKind::AssocFn = tcx.def_kind(def_id) { // If this is trait/impl method, extract the trait's substs. - let trait_substs = match tcx.trait_of_item(def_id) { + let trait_substs = match tcx.trait_of_item(def_id.to_def_id()) { Some(trait_def_id) => { let trait_substs_count = tcx.generics_of(trait_def_id).count(); &InternalSubsts::identity_for_item(tcx, def_id.to_def_id())[..trait_substs_count] diff --git a/compiler/rustc_passes/src/check_const.rs b/compiler/rustc_passes/src/check_const.rs index f1a81b65329..70518284cf9 100644 --- a/compiler/rustc_passes/src/check_const.rs +++ b/compiler/rustc_passes/src/check_const.rs @@ -97,7 +97,7 @@ fn const_check_violated(&self, expr: NonConstExpr, span: Span) { // If the function belongs to a trait, then it must enable the const_trait_impl // feature to use that trait function (with a const default body). - if tcx.trait_of_item(def_id).is_some() { + if tcx.trait_of_item(def_id.to_def_id()).is_some() { return true; } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 7ee3fe844b5..e442c5c9189 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -2129,7 +2129,7 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) { } ] = path.segments && data.trait_ref.def_id == *trait_id - && self.tcx.trait_of_item(item_id) == Some(*trait_id) + && self.tcx.trait_of_item(*item_id) == Some(*trait_id) && !self.is_tainted_by_errors() { let (verb, noun) = match self.tcx.associated_item(item_id).kind { diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index db4f53aace4..515a73ead77 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -9,7 +9,6 @@ pub fn provide(providers: &mut ty::query::Providers) { associated_item_def_ids, associated_items, impl_item_implementor_ids, - trait_of_item, ..*providers }; } @@ -40,13 +39,6 @@ fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> FxHashMap, def_id: DefId) -> Option { - tcx.opt_associated_item(def_id).and_then(|associated_item| associated_item.trait_container(tcx)) -} - fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem { let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let parent_def_id = tcx.hir().get_parent_item(id);