Skip to content

Commit 90bae57

Browse files
committed
Implement generics_of for impl side RPITITs assoc type
1 parent 75954a1 commit 90bae57

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 34 additions & 6 deletions
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_def_id = tcx.parent(impl_fn_def_id.to_def_id());
317317

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

322323
let local_def_id = impl_assoc_ty.def_id();
323324
let def_id = local_def_id.to_def_id();
@@ -347,10 +348,37 @@ fn impl_associated_item_for_impl_trait_in_trait(
347348
// Copy impl_defaultness of the containing function.
348349
impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id));
349350

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

355383
local_def_id
356384
}

0 commit comments

Comments
 (0)