diff options
Diffstat (limited to 'compiler/rustc_middle/src')
| -rw-r--r-- | compiler/rustc_middle/src/ty/assoc.rs | 52 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/context.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/error.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/instance.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/mod.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/sty.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/util.rs | 2 | 
7 files changed, 34 insertions, 41 deletions
| diff --git a/compiler/rustc_middle/src/ty/assoc.rs b/compiler/rustc_middle/src/ty/assoc.rs index e18737c0d7c..c97156ac17f 100644 --- a/compiler/rustc_middle/src/ty/assoc.rs +++ b/compiler/rustc_middle/src/ty/assoc.rs @@ -1,6 +1,6 @@ pub use self::AssocItemContainer::*; -use crate::ty; +use crate::ty::{self, DefIdTree}; use rustc_data_structures::sorted_map::SortedIndexMultiMap; use rustc_hir as hir; use rustc_hir::def::{DefKind, Namespace}; @@ -11,33 +11,8 @@ use super::{TyCtxt, Visibility}; #[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable, Hash, Encodable, Decodable)] pub enum AssocItemContainer { - TraitContainer(DefId), - ImplContainer(DefId), -} - -impl AssocItemContainer { - pub fn impl_def_id(&self) -> Option<DefId> { - match *self { - ImplContainer(id) => Some(id), - _ => None, - } - } - - /// Asserts that this is the `DefId` of an associated item declared - /// in a trait, and returns the trait `DefId`. - pub fn assert_trait(&self) -> DefId { - match *self { - TraitContainer(id) => id, - _ => bug!("associated item has wrong container type: {:?}", self), - } - } - - pub fn id(&self) -> DefId { - match *self { - TraitContainer(id) => id, - ImplContainer(id) => id, - } - } + TraitContainer, + ImplContainer, } /// Information about an associated item @@ -71,6 +46,27 @@ impl AssocItem { tcx.visibility(self.def_id) } + #[inline] + pub fn container_id(&self, tcx: TyCtxt<'_>) -> DefId { + tcx.parent(self.def_id) + } + + #[inline] + pub fn trait_container(&self, tcx: TyCtxt<'_>) -> Option<DefId> { + match self.container { + AssocItemContainer::ImplContainer => None, + AssocItemContainer::TraitContainer => Some(tcx.parent(self.def_id)), + } + } + + #[inline] + pub fn impl_container(&self, tcx: TyCtxt<'_>) -> Option<DefId> { + match self.container { + AssocItemContainer::ImplContainer => Some(tcx.parent(self.def_id)), + AssocItemContainer::TraitContainer => None, + } + } + pub fn signature(&self, tcx: TyCtxt<'_>) -> String { match self.kind { ty::AssocKind::Fn => { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 541763e294f..0a0f45ce1a0 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1668,8 +1668,7 @@ impl<'tcx> TyCtxt<'tcx> { // Checks if the bound region is in Impl Item. pub fn is_bound_region_in_impl_item(self, suitable_region_binding_scope: LocalDefId) -> bool { - let container_id = - self.associated_item(suitable_region_binding_scope.to_def_id()).container.id(); + let container_id = self.parent(suitable_region_binding_scope.to_def_id()); if self.impl_trait_ref(container_id).is_some() { // For now, we do not try to target impls of traits. This is // because this message is going to suggest that the user diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index 6fbe4ee8f7d..4b0bc3c1114 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -673,7 +673,7 @@ impl<T> Trait<T> for X { // the associated type or calling a method that returns the associated type". let point_at_assoc_fn = self.point_at_methods_that_satisfy_associated_type( diag, - assoc.container.id(), + assoc.container_id(self), current_method_ident, proj_ty.item_def_id, values.expected, diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 33a46f809b0..53218225d53 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -460,7 +460,7 @@ impl<'tcx> Instance<'tcx> { && !matches!( tcx.opt_associated_item(def.did), Some(ty::AssocItem { - container: ty::AssocItemContainer::TraitContainer(_), + container: ty::AssocItemContainer::TraitContainer, .. }) ) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 96ce1fef77e..77c6c532f41 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -2194,10 +2194,7 @@ impl<'tcx> TyCtxt<'tcx> { /// 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<DefId> { - self.opt_associated_item(def_id).and_then(|trait_item| match trait_item.container { - TraitContainer(_) => None, - ImplContainer(def_id) => Some(def_id), - }) + self.opt_associated_item(def_id).and_then(|trait_item| trait_item.impl_container(self)) } /// If the given `DefId` belongs to a trait that was automatically derived, returns `true`. diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 411d5c55829..fb0a4b4e8f4 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1179,13 +1179,14 @@ pub struct ProjectionTy<'tcx> { /// The `DefId` of the `TraitItem` for the associated type `N`. /// /// Note that this is not the `DefId` of the `TraitRef` containing this - /// associated type, which is in `tcx.associated_item(item_def_id).container`. + /// associated type, which is in `tcx.associated_item(item_def_id).container`, + /// aka. `tcx.parent(item_def_id).unwrap()`. pub item_def_id: DefId, } impl<'tcx> ProjectionTy<'tcx> { pub fn trait_def_id(&self, tcx: TyCtxt<'tcx>) -> DefId { - tcx.associated_item(self.item_def_id).container.id() + tcx.parent(self.item_def_id) } /// Extracts the underlying trait reference and own substs from this projection. @@ -1195,7 +1196,7 @@ impl<'tcx> ProjectionTy<'tcx> { &self, tcx: TyCtxt<'tcx>, ) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) { - let def_id = tcx.associated_item(self.item_def_id).container.id(); + let def_id = tcx.parent(self.item_def_id); let trait_generics = tcx.generics_of(def_id); ( ty::TraitRef { def_id, substs: self.substs.truncate_to(tcx, trait_generics) }, @@ -1433,7 +1434,7 @@ impl<'tcx> ExistentialProjection<'tcx> { /// then this function would return an `exists T. T: Iterator` existential trait /// reference. pub fn trait_ref(&self, tcx: TyCtxt<'tcx>) -> ty::ExistentialTraitRef<'tcx> { - let def_id = tcx.associated_item(self.item_def_id).container.id(); + let def_id = tcx.parent(self.item_def_id); let subst_count = tcx.generics_of(def_id).count() - 1; let substs = tcx.intern_substs(&self.substs[..subst_count]); ty::ExistentialTraitRef { def_id, substs } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 4d2f69b23fa..4b5bf80c071 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -402,7 +402,7 @@ impl<'tcx> TyCtxt<'tcx> { Some(dtor) => dtor.did, }; - let impl_def_id = self.associated_item(dtor).container.id(); + let impl_def_id = self.parent(dtor); let impl_generics = self.generics_of(impl_def_id); // We have a destructor - all the parameters that are not | 
