Skip to content

Commit

Permalink
closure requirements: don't replace bivariant opaque args
Browse files Browse the repository at this point in the history
It is unnecessary, these get constrained when checking that the
opaque type is well-formed.

It also results in the opaque type no longer being well formed.
If you've got `fn foo<'a>() -> impl Sized + 'a` the opaque is
`type Opaque<'a, 'aDummy> where 'a: 'aDummy, 'aDummy: 'a` where
`'aDummy`  is bivariant. If we call `foo::<'b>()`  inside of a closure
and its return type ends up in a type test, we start out with the WF
`Opaque<'b, 'b>`, and then replace the bivariant `'b` with `'static`.
`Opaque<'b, 'static>`  is no longer well-formed. Given how these type
tests are used, I don't think this caused any practical issues.
  • Loading branch information
lcnr committed Dec 3, 2024
1 parent 67defd7 commit 8770c77
Showing 1 changed file with 0 additions and 29 deletions.
29 changes: 0 additions & 29 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,35 +1068,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
ty: Ty<'tcx>,
) -> Option<ClosureOutlivesSubject<'tcx>> {
let tcx = infcx.tcx;

// Opaque types' args may include useless lifetimes.
// We will replace them with ReStatic.
struct OpaqueFolder<'tcx> {
tcx: TyCtxt<'tcx>,
}
impl<'tcx> ty::TypeFolder<TyCtxt<'tcx>> for OpaqueFolder<'tcx> {
fn cx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
use ty::TypeSuperFoldable as _;
let tcx = self.tcx;
let &ty::Alias(ty::Opaque, ty::AliasTy { args, def_id, .. }) = t.kind() else {
return t.super_fold_with(self);
};
let args = std::iter::zip(args, tcx.variances_of(def_id)).map(|(arg, v)| {
match (arg.unpack(), v) {
(ty::GenericArgKind::Lifetime(_), ty::Bivariant) => {
tcx.lifetimes.re_static.into()
}
_ => arg.fold_with(self),
}
});
Ty::new_opaque(tcx, def_id, tcx.mk_args_from_iter(args))
}
}

let ty = ty.fold_with(&mut OpaqueFolder { tcx });
let mut failed = false;

let ty = fold_regions(tcx, ty, |r, _depth| {
Expand Down

0 comments on commit 8770c77

Please sign in to comment.