@@ -53,9 +53,37 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
5353 )
5454 }
5555 }
56- hir:: ItemKind :: Impl ( ref impl_) => tcx. arena . alloc_from_iter (
57- impl_. items . iter ( ) . map ( |impl_item_ref| impl_item_ref. id . owner_id . to_def_id ( ) ) ,
58- ) ,
56+ hir:: ItemKind :: Impl ( ref impl_) => {
57+ if tcx. sess . opts . unstable_opts . lower_impl_trait_in_trait_to_assoc_ty {
58+ // We collect RPITITs for each trait method's return type, on the impl side too and
59+ // create a corresponding associated item using
60+ // associated_items_for_impl_trait_in_trait query.
61+ tcx. arena . alloc_from_iter (
62+ impl_
63+ . items
64+ . iter ( )
65+ . map ( |impl_item_ref| impl_item_ref. id . owner_id . to_def_id ( ) )
66+ . chain ( impl_. of_trait . iter ( ) . flat_map ( |_| {
67+ impl_
68+ . items
69+ . iter ( )
70+ . filter ( |impl_item_ref| {
71+ matches ! ( impl_item_ref. kind, hir:: AssocItemKind :: Fn { .. } )
72+ } )
73+ . flat_map ( |impl_item_ref| {
74+ let impl_fn_def_id =
75+ impl_item_ref. id . owner_id . def_id . to_def_id ( ) ;
76+ tcx. associated_items_for_impl_trait_in_trait ( impl_fn_def_id)
77+ } )
78+ . map ( |def_id| * def_id)
79+ } ) ) ,
80+ )
81+ } else {
82+ tcx. arena . alloc_from_iter (
83+ impl_. items . iter ( ) . map ( |impl_item_ref| impl_item_ref. id . owner_id . to_def_id ( ) ) ,
84+ )
85+ }
86+ }
5987 _ => span_bug ! ( item. span, "associated_item_def_ids: not impl or trait" ) ,
6088 }
6189}
@@ -290,8 +318,40 @@ fn impl_associated_item_for_impl_trait_in_trait(
290318) -> LocalDefId {
291319 let impl_def_id = tcx. local_parent ( impl_fn_def_id) ;
292320
293- let span = tcx. def_span ( trait_assoc_def_id) ;
321+ // FIXME fix the span, we probably want the def_id of the return type of the function
322+ let span = tcx. def_span ( impl_fn_def_id) ;
294323 let impl_assoc_ty = tcx. at ( span) . create_def ( impl_def_id, DefPathData :: ImplTraitAssocTy ) ;
295324
296- impl_assoc_ty. def_id ( )
325+ let local_def_id = impl_assoc_ty. def_id ( ) ;
326+ let def_id = local_def_id. to_def_id ( ) ;
327+
328+ impl_assoc_ty. opt_def_kind ( Some ( DefKind :: AssocTy ) ) ;
329+
330+ // There's no HIR associated with this new synthesized `def_id`, so feed
331+ // `opt_local_def_id_to_hir_id` with `None`.
332+ impl_assoc_ty. opt_local_def_id_to_hir_id ( None ) ;
333+
334+ // Add the def_id of the function that generated this synthesized associated type.
335+ impl_assoc_ty. opt_rpitit_info ( Some ( ImplTraitInTraitData :: Impl {
336+ fn_def_id : impl_fn_def_id. to_def_id ( ) ,
337+ } ) ) ;
338+
339+ impl_assoc_ty. associated_item ( ty:: AssocItem {
340+ name : kw:: Empty ,
341+ kind : ty:: AssocKind :: Type ,
342+ def_id,
343+ trait_item_def_id : Some ( trait_assoc_def_id. to_def_id ( ) ) ,
344+ container : ty:: ImplContainer ,
345+ fn_has_self_parameter : false ,
346+ } ) ;
347+
348+ // Copy impl_defaultness of the containing function.
349+ impl_assoc_ty. impl_defaultness ( tcx. impl_defaultness ( impl_fn_def_id) ) ;
350+
351+ // Copy generics_of the trait's associated item.
352+ // FIXME: This is not correct, in particular the parent is going to be wrong. So we would need
353+ // to copy from trait_assoc_def_id and adjust things.
354+ impl_assoc_ty. generics_of ( tcx. generics_of ( trait_assoc_def_id) . clone ( ) ) ;
355+
356+ local_def_id
297357}
0 commit comments