Remove DefId from AssocItemContainer.

This commit is contained in:
Camille GILLOT
2022-03-13 00:52:25 +01:00
parent 8ee4446ee5
commit d7ea161b7e
40 changed files with 190 additions and 220 deletions

View File

@@ -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 => {

View File

@@ -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

View File

@@ -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,

View File

@@ -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,
..
})
)

View File

@@ -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`.

View File

@@ -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 }

View File

@@ -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