Skip to content

Commit 3c780cd

Browse files
committed
Move opt_rpitit_info field to hir::AssocKind::Fn.
From `hir::AssocItem`.
1 parent 1b68ad1 commit 3c780cd

File tree

28 files changed

+103
-91
lines changed

28 files changed

+103
-91
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ fn best_definition_site_of_opaque<'tcx>(
449449
return Some(span);
450450
}
451451
}
452-
ty::AssocKind::Type => {}
452+
ty::AssocKind::Type { .. } => {}
453453
}
454454
}
455455

@@ -740,7 +740,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
740740

741741
for &assoc_item in assoc_items.in_definition_order() {
742742
match assoc_item.kind {
743-
ty::AssocKind::Type if assoc_item.defaultness(tcx).has_value() => {
743+
ty::AssocKind::Type { .. } if assoc_item.defaultness(tcx).has_value() => {
744744
let trait_args = GenericArgs::identity_for_item(tcx, def_id);
745745
let _: Result<_, rustc_errors::ErrorGuaranteed> = check_type_bounds(
746746
tcx,
@@ -953,7 +953,7 @@ fn check_impl_items_against_trait<'tcx>(
953953
);
954954
}
955955
ty::AssocKind::Const => {}
956-
ty::AssocKind::Type => {}
956+
ty::AssocKind::Type { .. } => {}
957957
}
958958
}
959959

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub(super) fn compare_impl_item(
4444

4545
match impl_item.kind {
4646
ty::AssocKind::Fn { .. } => compare_impl_method(tcx, impl_item, trait_item, impl_trait_ref),
47-
ty::AssocKind::Type => compare_impl_ty(tcx, impl_item, trait_item, impl_trait_ref),
47+
ty::AssocKind::Type { .. } => compare_impl_ty(tcx, impl_item, trait_item, impl_trait_ref),
4848
ty::AssocKind::Const => compare_impl_const(tcx, impl_item, trait_item, impl_trait_ref),
4949
}
5050
}
@@ -1703,7 +1703,7 @@ fn compare_generic_param_kinds<'tcx>(
17031703
trait_item: ty::AssocItem,
17041704
delay: bool,
17051705
) -> Result<(), ErrorGuaranteed> {
1706-
assert_eq!(impl_item.kind, trait_item.kind);
1706+
assert_eq!(impl_item.as_tag(), trait_item.as_tag());
17071707

17081708
let ty_const_params_of = |def_id| {
17091709
tcx.generics_of(def_id).own_params.iter().filter(|param| {
@@ -2235,16 +2235,19 @@ fn param_env_with_gat_bounds<'tcx>(
22352235
// of the RPITITs associated with the same body. This is because checking
22362236
// the item bounds of RPITITs often involves nested RPITITs having to prove
22372237
// bounds about themselves.
2238-
let impl_tys_to_install = match impl_ty.opt_rpitit_info {
2239-
None => vec![impl_ty],
2240-
Some(
2241-
ty::ImplTraitInTraitData::Impl { fn_def_id }
2242-
| ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
2243-
) => tcx
2238+
let impl_tys_to_install = match impl_ty.kind {
2239+
ty::AssocKind::Type {
2240+
opt_rpitit_info:
2241+
Some(
2242+
ty::ImplTraitInTraitData::Impl { fn_def_id }
2243+
| ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
2244+
),
2245+
} => tcx
22442246
.associated_types_for_impl_traits_in_associated_fn(fn_def_id)
22452247
.iter()
22462248
.map(|def_id| tcx.associated_item(*def_id))
22472249
.collect(),
2250+
_ => vec![impl_ty],
22482251
};
22492252

22502253
for impl_ty in impl_tys_to_install {

compiler/rustc_hir_analysis/src/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ fn suggestion_signature<'tcx>(
499499
tcx.predicates_of(assoc.def_id).instantiate_own(tcx, args),
500500
assoc,
501501
),
502-
ty::AssocKind::Type => {
502+
ty::AssocKind::Type { .. } => {
503503
let (generics, where_clauses) = bounds_from_generic_predicates(
504504
tcx,
505505
tcx.predicates_of(assoc.def_id).instantiate_own(tcx, args),

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
408408
let gat_def_id = gat_item.def_id.expect_local();
409409
let gat_item = tcx.associated_item(gat_def_id);
410410
// If this item is not an assoc ty, or has no args, then it's not a GAT
411-
if gat_item.kind != ty::AssocKind::Type {
411+
if !gat_item.is_type() {
412412
continue;
413413
}
414414
let gat_generics = tcx.generics_of(gat_def_id);
@@ -453,7 +453,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
453453
)
454454
}
455455
// In our example, this corresponds to the `Iter` and `Item` associated types
456-
ty::AssocKind::Type => {
456+
ty::AssocKind::Type { .. } => {
457457
// If our associated item is a GAT with missing bounds, add them to
458458
// the param-env here. This allows this GAT to propagate missing bounds
459459
// to other GATs.
@@ -1101,7 +1101,7 @@ fn check_associated_item(
11011101
);
11021102
check_method_receiver(wfcx, hir_sig, item, self_ty)
11031103
}
1104-
ty::AssocKind::Type => {
1104+
ty::AssocKind::Type { .. } => {
11051105
if let ty::AssocItemContainer::Trait = item.container {
11061106
check_associated_type_bounds(wfcx, item, span)
11071107
}

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub(super) fn find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
3636
locator.check(assoc_id.expect_local())
3737
}
3838
// Associated types don't have bodies, so they can't constrain hidden types
39-
ty::AssocKind::Type => {}
39+
ty::AssocKind::Type { .. } => {}
4040
}
4141
}
4242

compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use GenericArgsInfo::*;
44
use rustc_errors::codes::*;
55
use rustc_errors::{Applicability, Diag, Diagnostic, EmissionGuarantee, MultiSpan, pluralize};
66
use rustc_hir as hir;
7-
use rustc_middle::ty::{self as ty, AssocItems, AssocKind, TyCtxt};
7+
use rustc_middle::ty::{self as ty, AssocItems, TyCtxt};
88
use rustc_span::def_id::DefId;
99
use tracing::debug;
1010

@@ -486,7 +486,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
486486
let items: &AssocItems = self.tcx.associated_items(self.def_id);
487487
items
488488
.in_definition_order()
489-
.filter(|item| item.kind == AssocKind::Type)
489+
.filter(|item| item.is_type())
490490
.filter(|item| {
491491
!self
492492
.gen_args

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
201201
tcx.associated_items(pred.trait_ref.def_id)
202202
.in_definition_order()
203203
// We only care about associated types.
204-
.filter(|item| item.kind == ty::AssocKind::Type)
204+
.filter(|item| item.is_type())
205205
// No RPITITs -- they're not dyn-compatible for now.
206206
.filter(|item| !item.is_impl_trait_in_trait())
207207
// If the associated type has a `where Self: Sized` bound,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
17331733
.any(|i| {
17341734
i.kind.namespace() == Namespace::TypeNS
17351735
&& i.ident(tcx).normalize_to_macros_2_0() == assoc_ident
1736-
&& matches!(i.kind, ty::AssocKind::Type)
1736+
&& i.is_type()
17371737
})
17381738
// Consider only accessible traits
17391739
&& tcx.visibility(*trait_def_id)

compiler/rustc_hir_analysis/src/impl_wf_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub(crate) fn enforce_impl_lifetime_params_are_constrained(
112112
.flat_map(|def_id| {
113113
let item = tcx.associated_item(def_id);
114114
match item.kind {
115-
ty::AssocKind::Type => {
115+
ty::AssocKind::Type { .. } => {
116116
if item.defaultness(tcx).has_value() {
117117
cgp::parameters_for(tcx, tcx.type_of(def_id).instantiate_identity(), true)
118118
} else {

compiler/rustc_hir_typeck/src/method/probe.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1671,15 +1671,7 @@ impl<'tcx> Pick<'tcx> {
16711671
/// Do not use for type checking.
16721672
pub(crate) fn differs_from(&self, other: &Self) -> bool {
16731673
let Self {
1674-
item:
1675-
AssocItem {
1676-
def_id,
1677-
name: _,
1678-
kind: _,
1679-
container: _,
1680-
trait_item_def_id: _,
1681-
opt_rpitit_info: _,
1682-
},
1674+
item: AssocItem { def_id, name: _, kind: _, container: _, trait_item_def_id: _ },
16831675
kind: _,
16841676
import_ids: _,
16851677
autoderefs: _,
@@ -2253,7 +2245,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
22532245
match self.mode {
22542246
Mode::MethodCall => item.is_method(),
22552247
Mode::Path => match item.kind {
2256-
ty::AssocKind::Type => false,
2248+
ty::AssocKind::Type { .. } => false,
22572249
ty::AssocKind::Fn { .. } | ty::AssocKind::Const => true,
22582250
},
22592251
}

compiler/rustc_metadata/src/rmeta/decoder.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1342,20 +1342,24 @@ impl<'a> CrateMetadataRef<'a> {
13421342
DefKind::AssocFn => {
13431343
ty::AssocKind::Fn { has_self: self.get_fn_has_self_parameter(id, sess) }
13441344
}
1345-
DefKind::AssocTy => ty::AssocKind::Type,
1345+
DefKind::AssocTy => ty::AssocKind::Type {
1346+
opt_rpitit_info: self
1347+
.root
1348+
.tables
1349+
.opt_rpitit_info
1350+
.get(self, id)
1351+
.map(|d| d.decode(self)),
1352+
},
13461353
_ => bug!("cannot get associated-item of `{:?}`", self.def_key(id)),
13471354
};
13481355
let container = self.root.tables.assoc_container.get(self, id).unwrap();
1349-
let opt_rpitit_info =
1350-
self.root.tables.opt_rpitit_info.get(self, id).map(|d| d.decode(self));
13511356

13521357
ty::AssocItem {
13531358
name,
13541359
kind,
13551360
def_id: self.local_def_id(id),
13561361
trait_item_def_id: self.get_trait_item_def_id(id),
13571362
container,
1358-
opt_rpitit_info,
13591363
}
13601364
}
13611365

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16911691

16921692
match item.container {
16931693
AssocItemContainer::Trait => {
1694-
if let ty::AssocKind::Type = item.kind {
1694+
if item.is_type() {
16951695
self.encode_explicit_item_bounds(def_id);
16961696
self.encode_explicit_item_self_bounds(def_id);
16971697
if tcx.is_conditionally_const(def_id) {
@@ -1706,7 +1706,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17061706
}
17071707
}
17081708
}
1709-
if let Some(rpitit_info) = item.opt_rpitit_info {
1709+
if let ty::AssocKind::Type { opt_rpitit_info: Some(rpitit_info) } = item.kind {
17101710
record!(self.tables.opt_rpitit_info[def_id] <- rpitit_info);
17111711
if matches!(rpitit_info, ty::ImplTraitInTraitData::Trait { .. }) {
17121712
record_array!(

compiler/rustc_middle/src/ty/assoc.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ pub struct AssocItem {
2525
/// If this is an item in an impl of a trait then this is the `DefId` of
2626
/// the associated item on the trait that this implements.
2727
pub trait_item_def_id: Option<DefId>,
28-
29-
/// `Some` if the associated item (an associated type) comes from the
30-
/// return-position `impl Trait` in trait desugaring. The `ImplTraitInTraitData`
31-
/// provides additional information about its source.
32-
pub opt_rpitit_info: Option<ty::ImplTraitInTraitData>,
3328
}
3429

3530
impl AssocItem {
@@ -81,7 +76,7 @@ impl AssocItem {
8176
// regions just fine, showing `fn(&MyType)`.
8277
tcx.fn_sig(self.def_id).instantiate_identity().skip_binder().to_string()
8378
}
84-
ty::AssocKind::Type => format!("type {};", self.name),
79+
ty::AssocKind::Type { .. } => format!("type {};", self.name),
8580
ty::AssocKind::Const => {
8681
format!(
8782
"const {}: {:?};",
@@ -97,10 +92,14 @@ impl AssocItem {
9792
ty::AssocKind::Const => "associated const",
9893
ty::AssocKind::Fn { has_self: true } => "method",
9994
ty::AssocKind::Fn { has_self: false } => "associated function",
100-
ty::AssocKind::Type => "associated type",
95+
ty::AssocKind::Type { .. } => "associated type",
10196
}
10297
}
10398

99+
pub fn is_type(&self) -> bool {
100+
matches!(self.kind, ty::AssocKind::Type { .. })
101+
}
102+
104103
pub fn is_fn(&self) -> bool {
105104
matches!(self.kind, ty::AssocKind::Fn { .. })
106105
}
@@ -113,12 +112,12 @@ impl AssocItem {
113112
match self.kind {
114113
AssocKind::Const => AssocTag::Const,
115114
AssocKind::Fn { .. } => AssocTag::Fn,
116-
AssocKind::Type => AssocTag::Type,
115+
AssocKind::Type { .. } => AssocTag::Type,
117116
}
118117
}
119118

120119
pub fn is_impl_trait_in_trait(&self) -> bool {
121-
self.opt_rpitit_info.is_some()
120+
matches!(self.kind, AssocKind::Type { opt_rpitit_info: Some(_) })
122121
}
123122

124123
/// Returns true if:
@@ -143,14 +142,21 @@ impl AssocItem {
143142
#[derive(Copy, Clone, PartialEq, Debug, HashStable, Eq, Hash, Encodable, Decodable)]
144143
pub enum AssocKind {
145144
Const,
146-
Fn { has_self: bool },
147-
Type,
145+
Fn {
146+
has_self: bool,
147+
},
148+
Type {
149+
/// `Some` if the associated type comes from an RPITIT. The
150+
/// `ImplTraitInTraitData` provides additional information about its
151+
/// source.
152+
opt_rpitit_info: Option<ty::ImplTraitInTraitData>,
153+
},
148154
}
149155

150156
impl AssocKind {
151157
pub fn namespace(&self) -> Namespace {
152158
match *self {
153-
ty::AssocKind::Type => Namespace::TypeNS,
159+
ty::AssocKind::Type { .. } => Namespace::TypeNS,
154160
ty::AssocKind::Const | ty::AssocKind::Fn { .. } => Namespace::ValueNS,
155161
}
156162
}
@@ -159,7 +165,7 @@ impl AssocKind {
159165
match self {
160166
AssocKind::Const => DefKind::AssocConst,
161167
AssocKind::Fn { .. } => DefKind::AssocFn,
162-
AssocKind::Type => DefKind::AssocTy,
168+
AssocKind::Type { .. } => DefKind::AssocTy,
163169
}
164170
}
165171
}
@@ -170,7 +176,7 @@ impl std::fmt::Display for AssocKind {
170176
AssocKind::Fn { has_self: true } => write!(f, "method"),
171177
AssocKind::Fn { has_self: false } => write!(f, "associated function"),
172178
AssocKind::Const => write!(f, "associated const"),
173-
AssocKind::Type => write!(f, "associated type"),
179+
AssocKind::Type { .. } => write!(f, "associated type"),
174180
}
175181
}
176182
}

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
464464
fn associated_type_def_ids(self, def_id: DefId) -> impl IntoIterator<Item = DefId> {
465465
self.associated_items(def_id)
466466
.in_definition_order()
467-
.filter(|assoc_item| matches!(assoc_item.kind, ty::AssocKind::Type))
467+
.filter(|assoc_item| assoc_item.is_type())
468468
.map(|assoc_item| assoc_item.def_id)
469469
}
470470

compiler/rustc_middle/src/ty/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1610,8 +1610,10 @@ impl<'tcx> TyCtxt<'tcx> {
16101610
/// return-position `impl Trait` from a trait, then provide the source info
16111611
/// about where that RPITIT came from.
16121612
pub fn opt_rpitit_info(self, def_id: DefId) -> Option<ImplTraitInTraitData> {
1613-
if let DefKind::AssocTy = self.def_kind(def_id) {
1614-
self.associated_item(def_id).opt_rpitit_info
1613+
if let DefKind::AssocTy = self.def_kind(def_id)
1614+
&& let AssocKind::Type { opt_rpitit_info } = self.associated_item(def_id).kind
1615+
{
1616+
opt_rpitit_info
16151617
} else {
16161618
None
16171619
}

compiler/rustc_middle/src/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ impl<'tcx> Ty<'tcx> {
734734
.map(|principal| {
735735
tcx.associated_items(principal.def_id())
736736
.in_definition_order()
737-
.filter(|item| item.kind == ty::AssocKind::Type)
737+
.filter(|item| item.is_type())
738738
.filter(|item| !item.is_impl_trait_in_trait())
739739
.filter(|item| !tcx.generics_require_sized_self(item.def_id))
740740
.count()

compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ fn trait_object_ty<'tcx>(tcx: TyCtxt<'tcx>, poly_trait_ref: ty::PolyTraitRef<'tc
240240
.flat_map(|super_poly_trait_ref| {
241241
tcx.associated_items(super_poly_trait_ref.def_id())
242242
.in_definition_order()
243-
.filter(|item| item.kind == ty::AssocKind::Type)
243+
.filter(|item| item.is_type())
244244
.filter(|item| !tcx.generics_require_sized_self(item.def_id))
245245
.map(move |assoc_ty| {
246246
super_poly_trait_ref.map_bound(|super_trait_ref| {

compiler/rustc_smir/src/rustc_smir/convert/ty.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -894,12 +894,14 @@ impl<'tcx> Stable<'tcx> for rustc_session::cstore::ForeignModule {
894894
impl<'tcx> Stable<'tcx> for ty::AssocKind {
895895
type T = stable_mir::ty::AssocKind;
896896

897-
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
897+
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
898898
use stable_mir::ty::AssocKind;
899899
match *self {
900900
ty::AssocKind::Const => AssocKind::Const,
901901
ty::AssocKind::Fn { has_self } => AssocKind::Fn { has_self },
902-
ty::AssocKind::Type => AssocKind::Type,
902+
ty::AssocKind::Type { opt_rpitit_info } => AssocKind::Type {
903+
opt_rpitit_info: opt_rpitit_info.map(|rpitit| rpitit.stable(tables)),
904+
},
903905
}
904906
}
905907
}
@@ -926,7 +928,6 @@ impl<'tcx> Stable<'tcx> for ty::AssocItem {
926928
kind: self.kind.stable(tables),
927929
container: self.container.stable(tables),
928930
trait_item_def_id: self.trait_item_def_id.map(|did| tables.assoc_def(did)),
929-
opt_rpitit_info: self.opt_rpitit_info.map(|rpitit| rpitit.stable(tables)),
930931
}
931932
}
932933
}

0 commit comments

Comments
 (0)