Skip to content

Commit d7ea161

Browse files
committed
Remove DefId from AssocItemContainer.
1 parent 8ee4446 commit d7ea161

File tree

40 files changed

+190
-220
lines changed

40 files changed

+190
-220
lines changed

compiler/rustc_const_eval/src/util/call_kind.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ pub fn call_kind<'tcx>(
6666
from_hir_call: bool,
6767
self_arg: Option<Ident>,
6868
) -> CallKind<'tcx> {
69-
let parent = tcx.opt_associated_item(method_did).and_then(|assoc| match assoc.container {
70-
AssocItemContainer::ImplContainer(impl_did) => tcx.trait_id_of_impl(impl_did),
71-
AssocItemContainer::TraitContainer(trait_did) => Some(trait_did),
69+
let parent = tcx.opt_associated_item(method_did).and_then(|assoc| {
70+
let container_id = assoc.container_id(tcx);
71+
match assoc.container {
72+
AssocItemContainer::ImplContainer => tcx.trait_id_of_impl(container_id),
73+
AssocItemContainer::TraitContainer => Some(container_id),
74+
}
7275
});
7376

7477
let fn_call = parent

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
7676
"...is used and required to live as long as `'static` here \
7777
because of an implicit lifetime bound on the {}",
7878
match ctxt.assoc_item.container {
79-
AssocItemContainer::TraitContainer(id) =>
80-
format!("`impl` of `{}`", tcx.def_path_str(id)),
81-
AssocItemContainer::ImplContainer(_) =>
82-
"inherent `impl`".to_string(),
79+
AssocItemContainer::TraitContainer => {
80+
let id = ctxt.assoc_item.container_id(tcx);
81+
format!("`impl` of `{}`", tcx.def_path_str(id))
82+
}
83+
AssocItemContainer::ImplContainer => "inherent `impl`".to_string(),
8384
},
8485
),
8586
);

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub fn method_context(cx: &LateContext<'_>, id: hir::HirId) -> MethodLateContext
2222
let def_id = cx.tcx.hir().local_def_id(id);
2323
let item = cx.tcx.associated_item(def_id);
2424
match item.container {
25-
ty::TraitContainer(..) => MethodLateContext::TraitAutoImpl,
26-
ty::ImplContainer(cid) => match cx.tcx.impl_trait_ref(cid) {
25+
ty::TraitContainer => MethodLateContext::TraitAutoImpl,
26+
ty::ImplContainer => match cx.tcx.impl_trait_ref(item.container_id(cx.tcx)) {
2727
Some(_) => MethodLateContext::TraitImpl,
2828
None => MethodLateContext::PlainImpl,
2929
},

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11141114

11151115
fn get_fn_has_self_parameter(self, id: DefIndex) -> bool {
11161116
match self.kind(id) {
1117-
EntryKind::AssocFn(data) => data.decode(self).has_self,
1117+
EntryKind::AssocFn { has_self, .. } => has_self,
11181118
_ => false,
11191119
}
11201120
}
@@ -1134,26 +1134,21 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11341134
}
11351135

11361136
fn get_associated_item(self, id: DefIndex) -> ty::AssocItem {
1137-
let def_key = self.def_key(id);
1138-
let parent = self.local_def_id(def_key.parent.unwrap());
11391137
let name = self.item_name(id);
11401138

11411139
let (kind, container, has_self) = match self.kind(id) {
11421140
EntryKind::AssocConst(container) => (ty::AssocKind::Const, container, false),
1143-
EntryKind::AssocFn(data) => {
1144-
let data = data.decode(self);
1145-
(ty::AssocKind::Fn, data.container, data.has_self)
1146-
}
1141+
EntryKind::AssocFn { container, has_self } => (ty::AssocKind::Fn, container, has_self),
11471142
EntryKind::AssocType(container) => (ty::AssocKind::Type, container, false),
1148-
_ => bug!("cannot get associated-item of `{:?}`", def_key),
1143+
_ => bug!("cannot get associated-item of `{:?}`", id),
11491144
};
11501145

11511146
ty::AssocItem {
11521147
name,
11531148
kind,
11541149
def_id: self.local_def_id(id),
11551150
trait_item_def_id: self.get_trait_item_def_id(id),
1156-
container: container.with_def_id(parent),
1151+
container,
11571152
fn_has_self_parameter: has_self,
11581153
}
11591154
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12221222
|s| s.print_trait_item(ast_item),
12231223
);
12241224

1225-
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(AssocContainer::Trait));
1225+
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(ty::AssocItemContainer::TraitContainer));
12261226
record!(self.tables.mir_const_qualif[def_id] <- mir::ConstQualifs::default());
12271227
record!(self.tables.rendered_const[def_id] <- rendered);
12281228
}
@@ -1238,14 +1238,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12381238
};
12391239
self.tables.asyncness.set(def_id.index, m_sig.header.asyncness);
12401240
self.tables.constness.set(def_id.index, hir::Constness::NotConst);
1241-
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
1242-
container:AssocContainer::Trait,
1241+
record!(self.tables.kind[def_id] <- EntryKind::AssocFn {
1242+
container: ty::AssocItemContainer::TraitContainer,
12431243
has_self: trait_item.fn_has_self_parameter,
1244-
})));
1244+
});
12451245
}
12461246
ty::AssocKind::Type => {
12471247
self.encode_explicit_item_bounds(def_id);
1248-
record!(self.tables.kind[def_id] <- EntryKind::AssocType(AssocContainer::Trait));
1248+
record!(self.tables.kind[def_id] <- EntryKind::AssocType(ty::AssocItemContainer::TraitContainer));
12491249
}
12501250
}
12511251
match trait_item.kind {
@@ -1277,7 +1277,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12771277
let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id);
12781278
let const_data = self.encode_rendered_const_for_body(body_id);
12791279

1280-
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(AssocContainer::Impl));
1280+
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(ty::AssocItemContainer::ImplContainer));
12811281
record!(self.tables.mir_const_qualif[def_id] <- qualifs);
12821282
record!(self.tables.rendered_const[def_id] <- const_data);
12831283
} else {
@@ -1295,13 +1295,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12951295
hir::Constness::NotConst
12961296
};
12971297
self.tables.constness.set(def_id.index, constness);
1298-
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
1299-
container:AssocContainer::Impl,
1298+
record!(self.tables.kind[def_id] <- EntryKind::AssocFn {
1299+
container: ty::AssocItemContainer::ImplContainer,
13001300
has_self: impl_item.fn_has_self_parameter,
1301-
})));
1301+
});
13021302
}
13031303
ty::AssocKind::Type => {
1304-
record!(self.tables.kind[def_id] <- EntryKind::AssocType(AssocContainer::Impl));
1304+
record!(self.tables.kind[def_id] <- EntryKind::AssocType(ty::AssocItemContainer::ImplContainer));
13051305
}
13061306
}
13071307
self.encode_item_type(def_id);

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,9 @@ enum EntryKind {
419419
Generator,
420420
Trait,
421421
Impl,
422-
AssocFn(LazyValue<AssocFnData>),
423-
AssocType(AssocContainer),
424-
AssocConst(AssocContainer),
422+
AssocFn { container: ty::AssocItemContainer, has_self: bool },
423+
AssocType(ty::AssocItemContainer),
424+
AssocConst(ty::AssocItemContainer),
425425
TraitAlias,
426426
}
427427

@@ -434,30 +434,6 @@ struct VariantData {
434434
is_non_exhaustive: bool,
435435
}
436436

437-
/// Describes whether the container of an associated item
438-
/// is a trait or an impl and whether, in a trait, it has
439-
/// a default, or an in impl, whether it's marked "default".
440-
#[derive(Copy, Clone, TyEncodable, TyDecodable)]
441-
enum AssocContainer {
442-
Trait,
443-
Impl,
444-
}
445-
446-
impl AssocContainer {
447-
fn with_def_id(&self, def_id: DefId) -> ty::AssocItemContainer {
448-
match *self {
449-
AssocContainer::Trait => ty::TraitContainer(def_id),
450-
AssocContainer::Impl => ty::ImplContainer(def_id),
451-
}
452-
}
453-
}
454-
455-
#[derive(MetadataEncodable, MetadataDecodable)]
456-
struct AssocFnData {
457-
container: AssocContainer,
458-
has_self: bool,
459-
}
460-
461437
#[derive(TyEncodable, TyDecodable)]
462438
struct GeneratorData<'tcx> {
463439
layout: mir::GeneratorLayout<'tcx>,
@@ -475,7 +451,6 @@ pub fn provide(providers: &mut Providers) {
475451

476452
trivially_parameterized_over_tcx! {
477453
VariantData,
478-
AssocFnData,
479454
EntryKind,
480455
RawDefId,
481456
TraitImpls,

compiler/rustc_middle/src/ty/assoc.rs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub use self::AssocItemContainer::*;
22

3-
use crate::ty;
3+
use crate::ty::{self, DefIdTree};
44
use rustc_data_structures::sorted_map::SortedIndexMultiMap;
55
use rustc_hir as hir;
66
use rustc_hir::def::{DefKind, Namespace};
@@ -11,33 +11,8 @@ use super::{TyCtxt, Visibility};
1111

1212
#[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable, Hash, Encodable, Decodable)]
1313
pub enum AssocItemContainer {
14-
TraitContainer(DefId),
15-
ImplContainer(DefId),
16-
}
17-
18-
impl AssocItemContainer {
19-
pub fn impl_def_id(&self) -> Option<DefId> {
20-
match *self {
21-
ImplContainer(id) => Some(id),
22-
_ => None,
23-
}
24-
}
25-
26-
/// Asserts that this is the `DefId` of an associated item declared
27-
/// in a trait, and returns the trait `DefId`.
28-
pub fn assert_trait(&self) -> DefId {
29-
match *self {
30-
TraitContainer(id) => id,
31-
_ => bug!("associated item has wrong container type: {:?}", self),
32-
}
33-
}
34-
35-
pub fn id(&self) -> DefId {
36-
match *self {
37-
TraitContainer(id) => id,
38-
ImplContainer(id) => id,
39-
}
40-
}
14+
TraitContainer,
15+
ImplContainer,
4116
}
4217

4318
/// Information about an associated item
@@ -71,6 +46,27 @@ impl AssocItem {
7146
tcx.visibility(self.def_id)
7247
}
7348

49+
#[inline]
50+
pub fn container_id(&self, tcx: TyCtxt<'_>) -> DefId {
51+
tcx.parent(self.def_id)
52+
}
53+
54+
#[inline]
55+
pub fn trait_container(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
56+
match self.container {
57+
AssocItemContainer::ImplContainer => None,
58+
AssocItemContainer::TraitContainer => Some(tcx.parent(self.def_id)),
59+
}
60+
}
61+
62+
#[inline]
63+
pub fn impl_container(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
64+
match self.container {
65+
AssocItemContainer::ImplContainer => Some(tcx.parent(self.def_id)),
66+
AssocItemContainer::TraitContainer => None,
67+
}
68+
}
69+
7470
pub fn signature(&self, tcx: TyCtxt<'_>) -> String {
7571
match self.kind {
7672
ty::AssocKind::Fn => {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,8 +1668,7 @@ impl<'tcx> TyCtxt<'tcx> {
16681668

16691669
// Checks if the bound region is in Impl Item.
16701670
pub fn is_bound_region_in_impl_item(self, suitable_region_binding_scope: LocalDefId) -> bool {
1671-
let container_id =
1672-
self.associated_item(suitable_region_binding_scope.to_def_id()).container.id();
1671+
let container_id = self.parent(suitable_region_binding_scope.to_def_id());
16731672
if self.impl_trait_ref(container_id).is_some() {
16741673
// For now, we do not try to target impls of traits. This is
16751674
// because this message is going to suggest that the user

compiler/rustc_middle/src/ty/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ impl<T> Trait<T> for X {
673673
// the associated type or calling a method that returns the associated type".
674674
let point_at_assoc_fn = self.point_at_methods_that_satisfy_associated_type(
675675
diag,
676-
assoc.container.id(),
676+
assoc.container_id(self),
677677
current_method_ident,
678678
proj_ty.item_def_id,
679679
values.expected,

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl<'tcx> Instance<'tcx> {
460460
&& !matches!(
461461
tcx.opt_associated_item(def.did),
462462
Some(ty::AssocItem {
463-
container: ty::AssocItemContainer::TraitContainer(_),
463+
container: ty::AssocItemContainer::TraitContainer,
464464
..
465465
})
466466
)

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,10 +2194,7 @@ impl<'tcx> TyCtxt<'tcx> {
21942194
/// If the given `DefId` describes a method belonging to an impl, returns the
21952195
/// `DefId` of the impl that the method belongs to; otherwise, returns `None`.
21962196
pub fn impl_of_method(self, def_id: DefId) -> Option<DefId> {
2197-
self.opt_associated_item(def_id).and_then(|trait_item| match trait_item.container {
2198-
TraitContainer(_) => None,
2199-
ImplContainer(def_id) => Some(def_id),
2200-
})
2197+
self.opt_associated_item(def_id).and_then(|trait_item| trait_item.impl_container(self))
22012198
}
22022199

22032200
/// If the given `DefId` belongs to a trait that was automatically derived, returns `true`.

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,13 +1179,14 @@ pub struct ProjectionTy<'tcx> {
11791179
/// The `DefId` of the `TraitItem` for the associated type `N`.
11801180
///
11811181
/// Note that this is not the `DefId` of the `TraitRef` containing this
1182-
/// associated type, which is in `tcx.associated_item(item_def_id).container`.
1182+
/// associated type, which is in `tcx.associated_item(item_def_id).container`,
1183+
/// aka. `tcx.parent(item_def_id).unwrap()`.
11831184
pub item_def_id: DefId,
11841185
}
11851186

11861187
impl<'tcx> ProjectionTy<'tcx> {
11871188
pub fn trait_def_id(&self, tcx: TyCtxt<'tcx>) -> DefId {
1188-
tcx.associated_item(self.item_def_id).container.id()
1189+
tcx.parent(self.item_def_id)
11891190
}
11901191

11911192
/// Extracts the underlying trait reference and own substs from this projection.
@@ -1195,7 +1196,7 @@ impl<'tcx> ProjectionTy<'tcx> {
11951196
&self,
11961197
tcx: TyCtxt<'tcx>,
11971198
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
1198-
let def_id = tcx.associated_item(self.item_def_id).container.id();
1199+
let def_id = tcx.parent(self.item_def_id);
11991200
let trait_generics = tcx.generics_of(def_id);
12001201
(
12011202
ty::TraitRef { def_id, substs: self.substs.truncate_to(tcx, trait_generics) },
@@ -1433,7 +1434,7 @@ impl<'tcx> ExistentialProjection<'tcx> {
14331434
/// then this function would return an `exists T. T: Iterator` existential trait
14341435
/// reference.
14351436
pub fn trait_ref(&self, tcx: TyCtxt<'tcx>) -> ty::ExistentialTraitRef<'tcx> {
1436-
let def_id = tcx.associated_item(self.item_def_id).container.id();
1437+
let def_id = tcx.parent(self.item_def_id);
14371438
let subst_count = tcx.generics_of(def_id).count() - 1;
14381439
let substs = tcx.intern_substs(&self.substs[..subst_count]);
14391440
ty::ExistentialTraitRef { def_id, substs }

compiler/rustc_middle/src/ty/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl<'tcx> TyCtxt<'tcx> {
402402
Some(dtor) => dtor.did,
403403
};
404404

405-
let impl_def_id = self.associated_item(dtor).container.id();
405+
let impl_def_id = self.parent(dtor);
406406
let impl_generics = self.generics_of(impl_def_id);
407407

408408
// We have a destructor - all the parameters that are not

compiler/rustc_mir_build/src/lints.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::graph::iterate::{
44
use rustc_hir::def::DefKind;
55
use rustc_middle::mir::{BasicBlock, BasicBlocks, Body, Operand, TerminatorKind};
66
use rustc_middle::ty::subst::{GenericArg, InternalSubsts};
7-
use rustc_middle::ty::{self, AssocItem, AssocItemContainer, Instance, TyCtxt};
7+
use rustc_middle::ty::{self, Instance, TyCtxt};
88
use rustc_session::lint::builtin::UNCONDITIONAL_RECURSION;
99
use rustc_span::Span;
1010
use std::ops::ControlFlow;
@@ -14,11 +14,9 @@ pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
1414

1515
if let DefKind::Fn | DefKind::AssocFn = tcx.def_kind(def_id) {
1616
// If this is trait/impl method, extract the trait's substs.
17-
let trait_substs = match tcx.opt_associated_item(def_id.to_def_id()) {
18-
Some(AssocItem {
19-
container: AssocItemContainer::TraitContainer(trait_def_id), ..
20-
}) => {
21-
let trait_substs_count = tcx.generics_of(*trait_def_id).count();
17+
let trait_substs = match tcx.trait_of_item(def_id) {
18+
Some(trait_def_id) => {
19+
let trait_substs_count = tcx.generics_of(trait_def_id).count();
2220
&InternalSubsts::identity_for_item(tcx, def_id.to_def_id())[..trait_substs_count]
2321
}
2422
_ => &[],

compiler/rustc_privacy/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ where
212212
// `impl Pub<Priv> { pub fn my_method() {} }` is considered a private type,
213213
// so we need to visit the self type additionally.
214214
if let Some(assoc_item) = tcx.opt_associated_item(def_id) {
215-
if let ty::ImplContainer(impl_def_id) = assoc_item.container {
215+
if let Some(impl_def_id) = assoc_item.impl_container(tcx) {
216216
tcx.type_of(impl_def_id).visit_with(self)?;
217217
}
218218
}

compiler/rustc_save_analysis/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ impl<'tcx> SaveContext<'tcx> {
564564
return None;
565565
};
566566
let (def_id, decl_id) = match self.tcx.associated_item(method_id).container {
567-
ty::ImplContainer(_) => (Some(method_id), None),
568-
ty::TraitContainer(_) => (None, Some(method_id)),
567+
ty::ImplContainer => (Some(method_id), None),
568+
ty::TraitContainer => (None, Some(method_id)),
569569
};
570570
let sub_span = seg.ident.span;
571571
filter!(self.span_utils, sub_span);

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
27142714
if let Some(ident) = self
27152715
.tcx
27162716
.opt_associated_item(trait_item_def_id)
2717-
.and_then(|i| self.tcx.opt_item_ident(i.container.id()))
2717+
.and_then(|i| self.tcx.opt_item_ident(i.container_id(self.tcx)))
27182718
{
27192719
assoc_span.push_span_label(ident.span, "in this trait");
27202720
}

0 commit comments

Comments
 (0)