Skip to content

Commit c1aa9bf

Browse files
Fix subst issues with RPITIT
1 parent b8c35ca commit c1aa9bf

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

compiler/rustc_hir_analysis/src/check/compare_method.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -551,15 +551,40 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
551551
let id_substs = InternalSubsts::identity_for_item(tcx, def_id);
552552
debug!(?id_substs, ?substs);
553553
let map: FxHashMap<ty::GenericArg<'tcx>, ty::GenericArg<'tcx>> =
554-
substs.iter().enumerate().map(|(index, arg)| (arg, id_substs[index])).collect();
554+
std::iter::zip(substs, id_substs).collect();
555555
debug!(?map);
556556

557+
// NOTE(compiler-errors): RPITITs, like all other RPITs, have early-bound
558+
// region substs that are synthesized during AST lowering. These are substs
559+
// that are appended to the parent substs (trait and trait method). However,
560+
// we're trying to infer the unsubstituted type value of the RPITIT inside
561+
// the *impl*, so we can later use the impl's method substitutions to normalize
562+
// an RPITIT to a concrete type.
563+
//
564+
// Due to the design of RPITITs, during AST lowering, we have no idea that
565+
// an impl method is satisfying a trait method with RPITITs in it. Therefore,
566+
// we don't have a list ofearly-bound region substs for the RPITIT in the impl.
567+
// Since early region parameters are index-based, we can't just rebase these
568+
// (trait method) early-bound region substs onto the impl, since region
569+
// parameters are index-based, and there's no guarantee that the indices from
570+
// the trait substs and impl substs line up -- so we subtract the number of
571+
// trait substs and add the number of impl substs to *renumber* these early-
572+
// bound regions to their corresponding indices in the impl's substitutions list.
573+
//
574+
// Also, we only need to account for a difference in trait and impl substs,
575+
// since we previously enforce that the trait method and impl method have the
576+
// same generics.
577+
let num_trait_substs = trait_to_impl_substs.len();
578+
let num_impl_substs = tcx.generics_of(impl_m.container_id(tcx)).params.len();
557579
let ty = tcx.fold_regions(ty, |region, _| {
558-
if let ty::ReFree(_) = region.kind() {
559-
map[&region.into()].expect_region()
560-
} else {
561-
region
562-
}
580+
let ty::ReFree(_) = region.kind() else { return region; };
581+
let ty::ReEarlyBound(e) = map[&region.into()].expect_region().kind()
582+
else { bug!("expected ReFree to map to ReEarlyBound"); };
583+
tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
584+
def_id: e.def_id,
585+
name: e.name,
586+
index: (e.index as usize - num_trait_substs + num_impl_substs) as u32,
587+
}))
563588
});
564589
debug!(%ty);
565590
collected_tys.insert(def_id, ty);

compiler/rustc_middle/src/ty/subst.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,21 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
606606
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
607607
#[cold]
608608
#[inline(never)]
609-
fn region_param_out_of_range(data: ty::EarlyBoundRegion) -> ! {
609+
fn region_param_out_of_range(data: ty::EarlyBoundRegion, substs: &[GenericArg<'_>]) -> ! {
610610
bug!(
611-
"Region parameter out of range when substituting in region {} (index={})",
611+
"Region parameter out of range when substituting in region {} (index={}, substs = {:?})",
612+
data.name,
613+
data.index,
614+
substs,
615+
)
616+
}
617+
618+
#[cold]
619+
#[inline(never)]
620+
fn region_param_invalid(data: ty::EarlyBoundRegion, other: GenericArgKind<'_>) -> ! {
621+
bug!(
622+
"Unexpected parameter {:?} when substituting in region {} (index={})",
623+
other,
612624
data.name,
613625
data.index
614626
)
@@ -624,7 +636,8 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
624636
let rk = self.substs.get(data.index as usize).map(|k| k.unpack());
625637
match rk {
626638
Some(GenericArgKind::Lifetime(lt)) => self.shift_region_through_binders(lt),
627-
_ => region_param_out_of_range(data),
639+
Some(other) => region_param_invalid(data, other),
640+
None => region_param_out_of_range(data, self.substs),
628641
}
629642
}
630643
_ => r,

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,10 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>(
22542254
}
22552255

22562256
let impl_fn_def_id = leaf_def.item.def_id;
2257-
let impl_fn_substs = obligation.predicate.substs.rebase_onto(tcx, trait_fn_def_id, data.substs);
2257+
// Rebase from {trait}::{fn}::{opaque} to {impl}::{fn}::{opaque},
2258+
// since `data.substs` are the impl substs.
2259+
let impl_fn_substs =
2260+
obligation.predicate.substs.rebase_onto(tcx, tcx.parent(trait_fn_def_id), data.substs);
22582261

22592262
let cause = ObligationCause::new(
22602263
obligation.cause.span,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// check-pass
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
trait Foo<T> {
7+
fn foo<F2: Foo<T>>(self) -> impl Foo<T>;
8+
}
9+
10+
struct Bar;
11+
12+
impl Foo<u8> for Bar {
13+
fn foo<F2: Foo<u8>>(self) -> impl Foo<u8> {
14+
self
15+
}
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)