Skip to content

Commit 811a1ca

Browse files
committed
Make associated_item_def_ids for traits use an unstable option to also return associated types for RPITITs
1 parent be72bec commit 811a1ca

File tree

2 files changed

+100
-5
lines changed

2 files changed

+100
-5
lines changed

compiler/rustc_middle/src/query/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ rustc_queries! {
9090
/// Definitions that were generated with no HIR, would be feeded to return `None`.
9191
query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId>{
9292
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key.to_def_id()) }
93+
feedable
9394
}
9495

9596
/// Gives access to the HIR node's parent for the HIR owner `key`.
@@ -166,6 +167,7 @@ rustc_queries! {
166167
}
167168
cache_on_disk_if { key.is_local() }
168169
separate_provide_extern
170+
feedable
169171
}
170172

171173
query collect_return_position_impl_trait_in_trait_tys(key: DefId)
@@ -222,6 +224,7 @@ rustc_queries! {
222224
arena_cache
223225
cache_on_disk_if { key.is_local() }
224226
separate_provide_extern
227+
feedable
225228
}
226229

227230
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to the
@@ -264,6 +267,7 @@ rustc_queries! {
264267
desc { |tcx| "finding item bounds for `{}`", tcx.def_path_str(key) }
265268
cache_on_disk_if { key.is_local() }
266269
separate_provide_extern
270+
feedable
267271
}
268272

269273
/// Elaborated version of the predicates from `explicit_item_bounds`.
@@ -588,6 +592,7 @@ rustc_queries! {
588592
desc { |tcx| "computing explicit predicates of `{}`", tcx.def_path_str(key) }
589593
cache_on_disk_if { key.is_local() }
590594
separate_provide_extern
595+
feedable
591596
}
592597

593598
/// Returns the inferred outlives predicates (e.g., for `struct
@@ -596,6 +601,7 @@ rustc_queries! {
596601
desc { |tcx| "computing inferred outlives predicates of `{}`", tcx.def_path_str(key) }
597602
cache_on_disk_if { key.is_local() }
598603
separate_provide_extern
604+
feedable
599605
}
600606

601607
/// Maps from the `DefId` of a trait to the list of
@@ -728,6 +734,7 @@ rustc_queries! {
728734
desc { |tcx| "computing associated item data for `{}`", tcx.def_path_str(key) }
729735
cache_on_disk_if { key.is_local() }
730736
separate_provide_extern
737+
feedable
731738
}
732739

733740
/// Collects the associated items defined on a trait or impl.
@@ -1142,6 +1149,7 @@ rustc_queries! {
11421149
desc { |tcx| "looking up definition kind of `{}`", tcx.def_path_str(def_id) }
11431150
cache_on_disk_if { def_id.is_local() }
11441151
separate_provide_extern
1152+
feedable
11451153
}
11461154

11471155
/// The `opt_rpitit_info` query returns the pair of the def id of the function where the RPIT
@@ -1165,6 +1173,7 @@ rustc_queries! {
11651173
desc { |tcx| "looking up span for `{}`'s identifier", tcx.def_path_str(def_id) }
11661174
cache_on_disk_if { def_id.is_local() }
11671175
separate_provide_extern
1176+
feedable
11681177
}
11691178

11701179
query lookup_stability(def_id: DefId) -> Option<attr::Stability> {
@@ -1506,6 +1515,7 @@ rustc_queries! {
15061515
desc { |tcx| "looking up whether `{}` is a default impl", tcx.def_path_str(def_id) }
15071516
cache_on_disk_if { def_id.is_local() }
15081517
separate_provide_extern
1518+
feedable
15091519
}
15101520

15111521
query check_well_formed(key: hir::OwnerId) -> () {
@@ -1703,6 +1713,7 @@ rustc_queries! {
17031713
query visibility(def_id: DefId) -> ty::Visibility<DefId> {
17041714
desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) }
17051715
separate_provide_extern
1716+
feedable
17061717
}
17071718

17081719
query inhabited_predicate_adt(key: DefId) -> ty::inhabitedness::InhabitedPredicate<'tcx> {

compiler/rustc_ty_utils/src/assoc.rs

+89-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use rustc_hir::def::DefKind;
44
use rustc_hir::def_id::{DefId, LocalDefId};
55
use rustc_hir::definitions::DefPathData;
66
use rustc_hir::intravisit::{self, Visitor};
7-
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
7+
use rustc_middle::ty::{self, DefIdTree, ImplTraitInTraitData, InternalSubsts, TyCtxt};
8+
use rustc_span::symbol::kw;
89

910
pub fn provide(providers: &mut ty::query::Providers) {
1011
*providers = ty::query::Providers {
@@ -21,9 +22,37 @@ pub fn provide(providers: &mut ty::query::Providers) {
2122
fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
2223
let item = tcx.hir().expect_item(def_id.expect_local());
2324
match item.kind {
24-
hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter(
25-
trait_item_refs.iter().map(|trait_item_ref| trait_item_ref.id.owner_id.to_def_id()),
26-
),
25+
hir::ItemKind::Trait(.., ref trait_item_refs) => {
26+
if tcx.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty {
27+
// We collect RPITITs for each trait method's return type and create a
28+
// corresponding associated item using associated_items_for_impl_trait_in_trait
29+
// query.
30+
tcx.arena.alloc_from_iter(
31+
trait_item_refs
32+
.iter()
33+
.map(|trait_item_ref| trait_item_ref.id.owner_id.to_def_id())
34+
.chain(
35+
trait_item_refs
36+
.iter()
37+
.filter(|trait_item_ref| {
38+
matches!(trait_item_ref.kind, hir::AssocItemKind::Fn { .. })
39+
})
40+
.flat_map(|trait_item_ref| {
41+
let trait_fn_def_id =
42+
trait_item_ref.id.owner_id.def_id.to_def_id();
43+
tcx.associated_items_for_impl_trait_in_trait(trait_fn_def_id)
44+
})
45+
.map(|def_id| *def_id),
46+
),
47+
)
48+
} else {
49+
tcx.arena.alloc_from_iter(
50+
trait_item_refs
51+
.iter()
52+
.map(|trait_item_ref| trait_item_ref.id.owner_id.to_def_id()),
53+
)
54+
}
55+
}
2756
hir::ItemKind::Impl(ref impl_) => tcx.arena.alloc_from_iter(
2857
impl_.items.iter().map(|impl_item_ref| impl_item_ref.id.owner_id.to_def_id()),
2958
),
@@ -193,7 +222,62 @@ fn associated_item_for_impl_trait_in_trait(
193222
let span = tcx.def_span(opaque_ty_def_id);
194223
let trait_assoc_ty =
195224
tcx.at(span).create_def(trait_def_id.expect_local(), DefPathData::ImplTraitAssocTy);
196-
trait_assoc_ty.def_id()
225+
226+
let local_def_id = trait_assoc_ty.def_id();
227+
let def_id = local_def_id.to_def_id();
228+
229+
trait_assoc_ty.opt_def_kind(Some(DefKind::AssocTy));
230+
231+
// There's no HIR associated with this new synthesized `def_id`, so feed
232+
// `opt_local_def_id_to_hir_id` with `None`.
233+
trait_assoc_ty.opt_local_def_id_to_hir_id(None);
234+
235+
// Copy span of the opaque.
236+
trait_assoc_ty.def_ident_span(Some(span));
237+
238+
// Add the def_id of the function and opaque that generated this synthesized associated type.
239+
trait_assoc_ty.opt_rpitit_info(Some(ImplTraitInTraitData::Trait {
240+
fn_def_id,
241+
opaque_def_id: opaque_ty_def_id.to_def_id(),
242+
}));
243+
244+
trait_assoc_ty.associated_item(ty::AssocItem {
245+
name: kw::Empty,
246+
kind: ty::AssocKind::Type,
247+
def_id,
248+
trait_item_def_id: None,
249+
container: ty::TraitContainer,
250+
fn_has_self_parameter: false,
251+
});
252+
253+
// Copy visility of the containing function.
254+
trait_assoc_ty.visibility(tcx.visibility(fn_def_id));
255+
256+
// Copy impl_defaultness of the containing function.
257+
trait_assoc_ty.impl_defaultness(tcx.impl_defaultness(fn_def_id));
258+
259+
// Copy type_of of the opaque.
260+
trait_assoc_ty.type_of(ty::EarlyBinder(tcx.mk_opaque(
261+
opaque_ty_def_id.to_def_id(),
262+
InternalSubsts::identity_for_item(tcx, opaque_ty_def_id.to_def_id()),
263+
)));
264+
265+
// Copy generics_of of the opaque.
266+
trait_assoc_ty.generics_of(tcx.generics_of(opaque_ty_def_id).clone());
267+
268+
// There are no predicates for the synthesized associated type.
269+
trait_assoc_ty.explicit_predicates_of(ty::GenericPredicates {
270+
parent: Some(trait_def_id),
271+
predicates: &[],
272+
});
273+
274+
// There are no inferred outlives for the synthesized associated type.
275+
trait_assoc_ty.inferred_outlives_of(&[]);
276+
277+
// FIXME implement this.
278+
trait_assoc_ty.explicit_item_bounds(&[]);
279+
280+
local_def_id
197281
}
198282

199283
/// Given an `trait_assoc_def_id` that corresponds to a previously synthesized impl trait in trait

0 commit comments

Comments
 (0)