Skip to content

Commit e132d29

Browse files
committed
Implement generics_of for impl side RPITITs assoc type
1 parent bc9ffbe commit e132d29

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

compiler/rustc_ty_utils/src/assoc.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,12 @@ fn impl_associated_item_for_impl_trait_in_trait(
313313
trait_assoc_def_id: LocalDefId,
314314
impl_fn_def_id: LocalDefId,
315315
) -> LocalDefId {
316-
let impl_def_id = tcx.local_parent(impl_fn_def_id);
316+
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
317+
let impl_def_id = impl_local_def_id.to_def_id();
317318

318319
// FIXME fix the span, we probably want the def_id of the return type of the function
319320
let span = tcx.def_span(impl_fn_def_id);
320-
let impl_assoc_ty = tcx.at(span).create_def(impl_def_id, DefPathData::ImplTraitAssocTy);
321+
let impl_assoc_ty = tcx.at(span).create_def(impl_local_def_id, DefPathData::ImplTraitAssocTy);
321322

322323
let local_def_id = impl_assoc_ty.def_id();
323324
let def_id = local_def_id.to_def_id();
@@ -349,10 +350,37 @@ fn impl_associated_item_for_impl_trait_in_trait(
349350
// Copy impl_defaultness of the containing function.
350351
impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id));
351352

352-
// Copy generics_of the trait's associated item.
353-
// FIXME: This is not correct, in particular the parent is going to be wrong. So we would need
354-
// to copy from trait_assoc_def_id and adjust things.
355-
impl_assoc_ty.generics_of(tcx.generics_of(trait_assoc_def_id).clone());
353+
// Copy generics_of the trait's associated item but the impl as the parent.
354+
impl_assoc_ty.generics_of({
355+
let trait_assoc_generics = tcx.generics_of(trait_assoc_def_id);
356+
let trait_assoc_parent_count = trait_assoc_generics.parent_count;
357+
let mut params = trait_assoc_generics.params.clone();
358+
359+
let parent_generics = tcx.generics_of(impl_def_id);
360+
let parent_count = parent_generics.parent_count + parent_generics.params.len();
361+
362+
let mut impl_fn_params = tcx.generics_of(impl_fn_def_id).params.clone();
363+
364+
for param in &mut params {
365+
param.index = param.index + parent_count as u32 + impl_fn_params.len() as u32
366+
- trait_assoc_parent_count as u32;
367+
}
368+
369+
impl_fn_params.extend(params);
370+
params = impl_fn_params;
371+
372+
let param_def_id_to_index =
373+
params.iter().map(|param| (param.def_id, param.index)).collect();
374+
375+
ty::Generics {
376+
parent: Some(impl_def_id),
377+
parent_count,
378+
params,
379+
param_def_id_to_index,
380+
has_self: false,
381+
has_late_bound_regions: trait_assoc_generics.has_late_bound_regions,
382+
}
383+
});
356384

357385
local_def_id
358386
}

0 commit comments

Comments
 (0)