Skip to content

Commit 47e15d9

Browse files
committed
query RPITIT in a trait or impl
1 parent dc6c330 commit 47e15d9

File tree

11 files changed

+143
-151
lines changed

11 files changed

+143
-151
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_middle::ty::error::{ExpectedFound, TypeError};
1515
use rustc_middle::ty::{
1616
self, BottomUpFolder, GenericArgs, GenericParamDefKind, Ty, TyCtxt, TypeFoldable, TypeFolder,
1717
TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, Upcast,
18+
associated_types_for_impl_traits_in_associated_fn,
1819
};
1920
use rustc_middle::{bug, span_bug};
2021
use rustc_span::{DUMMY_SP, Span};
@@ -757,7 +758,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
757758
// returning `-> Missing<impl Sized>`, that gets converted to `-> {type error}`,
758759
// and when walking through the signature we end up never collecting the def id
759760
// of the `impl Sized`. Insert that here, so we don't ICE later.
760-
for assoc_item in tcx.associated_types_for_impl_traits_in_associated_fn(trait_m.def_id) {
761+
for assoc_item in associated_types_for_impl_traits_in_associated_fn(tcx, trait_m.def_id) {
761762
if !remapped_types.contains_key(assoc_item) {
762763
remapped_types.insert(
763764
*assoc_item,
@@ -2448,8 +2449,7 @@ fn param_env_with_gat_bounds<'tcx>(
24482449
ty::ImplTraitInTraitData::Impl { fn_def_id }
24492450
| ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
24502451
),
2451-
} => tcx
2452-
.associated_types_for_impl_traits_in_associated_fn(fn_def_id)
2452+
} => associated_types_for_impl_traits_in_associated_fn(tcx, fn_def_id)
24532453
.iter()
24542454
.map(|def_id| tcx.associated_item(*def_id))
24552455
.collect(),

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_middle::ty::trait_def::TraitSpecializationKind;
2020
use rustc_middle::ty::{
2121
self, AdtKind, GenericArgKind, GenericArgs, GenericParamDefKind, Ty, TyCtxt, TypeFlags,
2222
TypeFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode,
23-
Upcast,
23+
Upcast, associated_types_for_impl_traits_in_associated_fn,
2424
};
2525
use rustc_middle::{bug, span_bug};
2626
use rustc_session::parse::feature_err;
@@ -326,7 +326,9 @@ pub(crate) fn check_trait_item<'tcx>(
326326
let mut res = Ok(());
327327

328328
if matches!(tcx.def_kind(def_id), DefKind::AssocFn) {
329-
for &assoc_ty_def_id in tcx.associated_types_for_impl_traits_in_associated_fn(def_id) {
329+
for &assoc_ty_def_id in
330+
associated_types_for_impl_traits_in_associated_fn(tcx, def_id.to_def_id())
331+
{
330332
res = res.and(check_associated_item(tcx, assoc_ty_def_id.expect_local()));
331333
}
332334
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_middle::mir::interpret::LitToConstInput;
4040
use rustc_middle::ty::print::PrintPolyTraitRefExt as _;
4141
use rustc_middle::ty::{
4242
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, Ty, TyCtxt, TypeVisitableExt,
43-
TypingMode, Upcast, fold_regions,
43+
TypingMode, Upcast, associated_types_for_impl_traits_in_associated_fn, fold_regions,
4444
};
4545
use rustc_middle::{bug, span_bug};
4646
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
@@ -2602,7 +2602,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26022602
// do a linear search to map this to the synthetic associated type that
26032603
// it will be lowered to.
26042604
let def_id = if let Some(parent_def_id) = in_trait {
2605-
*tcx.associated_types_for_impl_traits_in_associated_fn(parent_def_id)
2605+
*associated_types_for_impl_traits_in_associated_fn(tcx, parent_def_id.to_def_id())
26062606
.iter()
26072607
.find(|rpitit| match tcx.opt_rpitit_info(**rpitit) {
26082608
Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ provide! { tcx, def_id, other, cdata,
326326
.process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys")))
327327
}
328328

329-
associated_types_for_impl_traits_in_associated_fn => { table_defaulted_array }
329+
associated_types_for_impl_traits_in_trait_or_impl => { table }
330330

331331
visibility => { cdata.get_visibility(def_id.index) }
332332
adt_def => { cdata.get_adt_def(def_id.index, tcx) }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,17 +1382,6 @@ fn should_encode_const(def_kind: DefKind) -> bool {
13821382
}
13831383
}
13841384

1385-
fn should_encode_fn_impl_trait_in_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
1386-
if let Some(assoc_item) = tcx.opt_associated_item(def_id)
1387-
&& assoc_item.container == ty::AssocItemContainer::Trait
1388-
&& assoc_item.is_fn()
1389-
{
1390-
true
1391-
} else {
1392-
false
1393-
}
1394-
}
1395-
13961385
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13971386
fn encode_attrs(&mut self, def_id: LocalDefId) {
13981387
let tcx = self.tcx;
@@ -1617,9 +1606,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16171606
{
16181607
record!(self.tables.trait_impl_trait_tys[def_id] <- table);
16191608
}
1620-
if should_encode_fn_impl_trait_in_trait(tcx, def_id) {
1621-
let table = tcx.associated_types_for_impl_traits_in_associated_fn(def_id);
1622-
record_defaulted_array!(self.tables.associated_types_for_impl_traits_in_associated_fn[def_id] <- table);
1609+
if let DefKind::Impl { .. } | DefKind::Trait = def_kind {
1610+
let table = tcx.associated_types_for_impl_traits_in_trait_or_impl(def_id);
1611+
record!(self.tables.associated_types_for_impl_traits_in_trait_or_impl[def_id] <- table);
16231612
}
16241613
}
16251614

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ define_tables! {
403403
explicit_implied_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
404404
explicit_implied_const_bounds: Table<DefIndex, LazyArray<(ty::PolyTraitRef<'static>, Span)>>,
405405
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
406-
associated_types_for_impl_traits_in_associated_fn: Table<DefIndex, LazyArray<DefId>>,
407406
opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>,
408407
// Reexported names are not associated with individual `DefId`s,
409408
// e.g. a glob import can introduce a lot of names, all with the same `DefId`.
@@ -482,6 +481,7 @@ define_tables! {
482481
assumed_wf_types_for_rpitit: Table<DefIndex, LazyArray<(Ty<'static>, Span)>>,
483482
opaque_ty_origin: Table<DefIndex, LazyValue<hir::OpaqueTyOrigin<DefId>>>,
484483
anon_const_kind: Table<DefIndex, LazyValue<ty::AnonConstKind>>,
484+
associated_types_for_impl_traits_in_trait_or_impl: Table<DefIndex, LazyValue<ty::AssocTyForImplTraitInTraitOrImpl>>,
485485
}
486486

487487
#[derive(TyEncodable, TyDecodable)]

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,21 +1079,9 @@ rustc_queries! {
10791079
desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) }
10801080
}
10811081

1082-
/// Given `fn_def_id` of a trait or of an impl that implements a given trait:
1083-
/// if `fn_def_id` is the def id of a function defined inside a trait, then it creates and returns
1084-
/// the associated items that correspond to each impl trait in return position for that trait.
1085-
/// if `fn_def_id` is the def id of a function defined inside an impl that implements a trait, then it
1086-
/// creates and returns the associated items that correspond to each impl trait in return position
1087-
/// of the implemented trait.
1088-
query associated_types_for_impl_traits_in_associated_fn(fn_def_id: DefId) -> &'tcx [DefId] {
1089-
desc { |tcx| "creating associated items for opaque types returned by `{}`", tcx.def_path_str(fn_def_id) }
1090-
cache_on_disk_if { fn_def_id.is_local() }
1091-
separate_provide_extern
1092-
}
1093-
1094-
query associated_types_for_impl_traits_in_trait(trait_id: DefId) -> &'tcx DefIdMap<&'tcx [DefId]> {
1082+
query associated_types_for_impl_traits_in_trait_or_impl(did: DefId) -> &'tcx ty::AssocTyForImplTraitInTraitOrImpl {
10951083
arena_cache
1096-
desc { |tcx| "creating associated items for trait `{}`", tcx.def_path_str(trait_id) }
1084+
desc { |tcx| "creating rpitit for `{}`", tcx.def_path_str(did) }
10971085
separate_provide_extern
10981086
}
10991087

compiler/rustc_middle/src/ty/assoc.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_attr_data_structures::{AttributeKind, find_attr};
2+
use rustc_data_structures::fx::FxIndexMap;
23
use rustc_data_structures::sorted_map::SortedIndexMultiMap;
34
use rustc_hir as hir;
45
use rustc_hir::def::{DefKind, Namespace};
@@ -285,3 +286,23 @@ impl AssocItems {
285286
.find(|item| tcx.hygienic_eq(ident, item.ident(tcx), parent_def_id))
286287
}
287288
}
289+
290+
#[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable)]
291+
pub struct AssocTyForImplTraitInTraitOrImpl(pub FxIndexMap<DefId, Vec<DefId>>);
292+
293+
/// Given an `fn_def_id` of a trait or a trait implementation:
294+
///
295+
/// if `fn_def_id` is a function defined inside a trait, then it synthesizes
296+
/// a new def id corresponding to a new associated type for each return-
297+
/// position `impl Trait` in the signature.
298+
///
299+
/// if `fn_def_id` is a function inside of an impl, then for each synthetic
300+
/// associated type generated for the corresponding trait function described
301+
/// above, synthesize a corresponding associated type in the impl.
302+
pub fn associated_types_for_impl_traits_in_associated_fn(
303+
tcx: TyCtxt<'_>,
304+
fn_def_id: DefId,
305+
) -> &'_ [DefId] {
306+
let parent_def_id = tcx.parent(fn_def_id);
307+
&tcx.associated_types_for_impl_traits_in_trait_or_impl(parent_def_id).0[&fn_def_id]
308+
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,8 +2194,7 @@ impl<'tcx> TyCtxt<'tcx> {
21942194
return false;
21952195
};
21962196

2197-
return !self
2198-
.associated_types_for_impl_traits_in_associated_fn(trait_item_def_id)
2197+
return !associated_types_for_impl_traits_in_associated_fn(self, trait_item_def_id)
21992198
.is_empty();
22002199
}
22012200
}

compiler/rustc_middle/src/ty/parameterized.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ trivially_parameterized_over_tcx! {
6767
crate::mir::ConstQualifs,
6868
ty::AsyncDestructor,
6969
ty::AssocItemContainer,
70+
ty::AssocTyForImplTraitInTraitOrImpl,
7071
ty::Asyncness,
7172
ty::AnonConstKind,
7273
ty::DeducedParamAttrs,

0 commit comments

Comments
 (0)