@@ -732,8 +732,6 @@ fn check_opaque_meets_bounds<'tcx>(
732732 span : Span ,
733733 origin : & hir:: OpaqueTyOrigin ,
734734) {
735- let hidden_type = tcx. bound_type_of ( def_id. to_def_id ( ) ) . subst ( tcx, substs) ;
736-
737735 let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
738736 let defining_use_anchor = match * origin {
739737 hir:: OpaqueTyOrigin :: FnReturn ( did) | hir:: OpaqueTyOrigin :: AsyncFn ( did) => did,
@@ -748,14 +746,26 @@ fn check_opaque_meets_bounds<'tcx>(
748746 let ocx = ObligationCtxt :: new ( & infcx) ;
749747 let opaque_ty = tcx. mk_opaque ( def_id. to_def_id ( ) , substs) ;
750748
749+ // `ReErased` regions appear in the "parent_substs" of closures/generators.
750+ // We're ignoring them here and replacing them with fresh region variables.
751+ // See tests in ui/type-alias-impl-trait/closure_{parent_substs,wf_outlives}.rs.
752+ //
753+ // FIXME: Consider wrapping the hidden type in an existential `Binder` and instantiating it
754+ // here rather than using ReErased.
755+ let hidden_ty = tcx. bound_type_of ( def_id. to_def_id ( ) ) . subst ( tcx, substs) ;
756+ let hidden_ty = tcx. fold_regions ( hidden_ty, |re, _dbi| match re. kind ( ) {
757+ ty:: ReErased => infcx. next_region_var ( RegionVariableOrigin :: MiscVariable ( span) ) ,
758+ _ => re,
759+ } ) ;
760+
751761 let misc_cause = traits:: ObligationCause :: misc ( span, hir_id) ;
752762
753- match infcx. at ( & misc_cause, param_env) . eq ( opaque_ty, hidden_type ) {
763+ match infcx. at ( & misc_cause, param_env) . eq ( opaque_ty, hidden_ty ) {
754764 Ok ( infer_ok) => ocx. register_infer_ok_obligations ( infer_ok) ,
755765 Err ( ty_err) => {
756766 tcx. sess . delay_span_bug (
757767 span,
758- & format ! ( "could not unify `{hidden_type }` with revealed type:\n {ty_err}" ) ,
768+ & format ! ( "could not unify `{hidden_ty }` with revealed type:\n {ty_err}" ) ,
759769 ) ;
760770 }
761771 }
@@ -764,7 +774,7 @@ fn check_opaque_meets_bounds<'tcx>(
764774 // Defining use functions may have more bounds than the opaque type, which is ok, as long as the
765775 // hidden type is well formed even without those bounds.
766776 let predicate =
767- ty:: Binder :: dummy ( ty:: PredicateKind :: WellFormed ( hidden_type . into ( ) ) ) . to_predicate ( tcx) ;
777+ ty:: Binder :: dummy ( ty:: PredicateKind :: WellFormed ( hidden_ty . into ( ) ) ) . to_predicate ( tcx) ;
768778 ocx. register_obligation ( Obligation :: new ( misc_cause, param_env, predicate) ) ;
769779
770780 // Check that all obligations are satisfied by the implementation's
0 commit comments