Rollup merge of #130412 - compiler-errors:rpitit-overcapture, r=jieyouxu

Don't ICE when RPITIT captures more method args than trait definition

Make sure we don't ICE when an RPITIT captures more method args than the trait definition, which is not allowed. This was because we were using the wrong def id for error reporting.

Due to the default lifetime capture rules of RPITITs (capturing everything in scope), this is only doable if we use precise capturing, which isn't currently allowed for RPITITs anyways but we still end up reaching the relevant codepaths.

Fixes #129850
This commit is contained in:
Matthias Krüger 2024-09-16 18:34:01 +02:00 committed by GitHub
commit 4e68d06b52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 73 additions and 17 deletions

View file

@ -726,7 +726,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
num_trait_args,
num_impl_args,
def_id,
impl_def_id: impl_m.container_id(tcx),
impl_m_def_id: impl_m.def_id,
ty,
return_span,
}) {
@ -844,12 +844,18 @@ where
struct RemapHiddenTyRegions<'tcx> {
tcx: TyCtxt<'tcx>,
/// Map from early/late params of the impl to identity regions of the RPITIT (GAT)
/// in the trait.
map: FxIndexMap<ty::Region<'tcx>, ty::Region<'tcx>>,
num_trait_args: usize,
num_impl_args: usize,
/// Def id of the RPITIT (GAT) in the *trait*.
def_id: DefId,
impl_def_id: DefId,
/// Def id of the impl method which owns the opaque hidden type we're remapping.
impl_m_def_id: DefId,
/// The hidden type we're remapping. Useful for diagnostics.
ty: Ty<'tcx>,
/// Span of the return type. Useful for diagnostics.
return_span: Span,
}
@ -885,8 +891,7 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
ty::ReLateParam(_) => {}
// Remap early-bound regions as long as they don't come from the `impl` itself,
// in which case we don't really need to renumber them.
ty::ReEarlyParam(ebr)
if ebr.index >= self.tcx.generics_of(self.impl_def_id).count() as u32 => {}
ty::ReEarlyParam(ebr) if ebr.index as usize >= self.num_impl_args => {}
_ => return Ok(region),
}
@ -899,7 +904,7 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
);
}
} else {
let guar = match region.opt_param_def_id(self.tcx, self.tcx.parent(self.def_id)) {
let guar = match region.opt_param_def_id(self.tcx, self.impl_m_def_id) {
Some(def_id) => {
let return_span = if let ty::Alias(ty::Opaque, opaque_ty) = self.ty.kind() {
self.tcx.def_span(opaque_ty.def_id)

View file

@ -255,7 +255,9 @@ impl<'tcx> Generics {
let param = self.param_at(param.index as usize, tcx);
match param.kind {
GenericParamDefKind::Lifetime => param,
_ => bug!("expected lifetime parameter, but found another generic parameter"),
_ => {
bug!("expected lifetime parameter, but found another generic parameter: {param:#?}")
}
}
}
@ -264,7 +266,7 @@ impl<'tcx> Generics {
let param = self.param_at(param.index as usize, tcx);
match param.kind {
GenericParamDefKind::Type { .. } => param,
_ => bug!("expected type parameter, but found another generic parameter"),
_ => bug!("expected type parameter, but found another generic parameter: {param:#?}"),
}
}
@ -273,7 +275,7 @@ impl<'tcx> Generics {
let param = self.param_at(param.index as usize, tcx);
match param.kind {
GenericParamDefKind::Const { .. } => param,
_ => bug!("expected const parameter, but found another generic parameter"),
_ => bug!("expected const parameter, but found another generic parameter: {param:#?}"),
}
}