Skip to content

Commit 10d0ff9

Browse files
committed
Explain what the heck is going on with this lifetime remapping business
1 parent 44e2150 commit 10d0ff9

File tree

1 file changed

+12
-5
lines changed
  • compiler/rustc_hir_analysis/src/check

1 file changed

+12
-5
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -504,19 +504,26 @@ fn sanity_check_found_hidden_type<'tcx>(
504504
_ => l,
505505
},
506506
});
507-
// Get the hidden type, and in case it is in a nested opaque type, find that opaque type's
508-
// usage in the function signature and use the generic arguments from the usage site.
507+
// Get the hidden type.
509508
let mut hidden_ty = tcx.type_of(key.def_id).instantiate(tcx, key.args);
509+
// In case it is in a nested opaque type, find that opaque type's
510+
// usage in the function signature and use the generic arguments from the usage site.
511+
// We need to do because RPITs ignore the lifetimes of the function,
512+
// as they have their own copies of all the lifetimes they capture.
513+
// So the only way to get the lifetimes represented in terms of the function,
514+
// is to look how they are used in the function signature (or do some other fancy
515+
// recording of this mapping at ast -> hir lowering time).
510516
if let hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) = origin {
511517
if hidden_ty != ty.ty {
512-
hidden_ty = find_and_apply_rpit_substs(
518+
hidden_ty = find_and_apply_rpit_args(
513519
tcx,
514520
hidden_ty,
515521
defining_use_anchor.to_def_id(),
516522
key.def_id.to_def_id(),
517523
)?;
518524
}
519525
}
526+
520527
// If the hidden types differ, emit a type mismatch diagnostic.
521528
if hidden_ty == ty.ty {
522529
Ok(())
@@ -527,13 +534,13 @@ fn sanity_check_found_hidden_type<'tcx>(
527534
}
528535
}
529536

530-
fn find_and_apply_rpit_substs<'tcx>(
537+
fn find_and_apply_rpit_args<'tcx>(
531538
tcx: TyCtxt<'tcx>,
532539
mut hidden_ty: Ty<'tcx>,
533540
function: DefId,
534541
opaque: DefId,
535542
) -> Result<Ty<'tcx>, ErrorGuaranteed> {
536-
// Find use of the RPIT in the function signature and thus find the right substs to
543+
// Find use of the RPIT in the function signature and thus find the right args to
537544
// convert it into the parameter space of the function signature. This is needed,
538545
// because that's what `type_of` returns, against which we compare later.
539546
let ret = tcx.fn_sig(function).instantiate_identity().output();

0 commit comments

Comments
 (0)