@@ -330,19 +330,36 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
330330
331331 let span = tcx. def_span ( def_id) ;
332332
333- // If there are required region bounds, we can use them.
334- if opaque_defn. has_required_region_bounds {
335- let bounds = tcx. explicit_item_bounds ( def_id) ;
336- debug ! ( "{:#?}" , bounds) ;
337- let bounds: Vec < _ > =
338- bounds. iter ( ) . map ( |( bound, _) | bound. subst ( tcx, opaque_type_key. substs ) ) . collect ( ) ;
339- debug ! ( "{:#?}" , bounds) ;
340- let opaque_type = tcx. mk_opaque ( def_id, opaque_type_key. substs ) ;
341-
342- let required_region_bounds =
343- required_region_bounds ( tcx, opaque_type, bounds. into_iter ( ) ) ;
344- debug_assert ! ( !required_region_bounds. is_empty( ) ) ;
333+ // Check if the `impl Trait` bounds include region bounds.
334+ // For example, this would be true for:
335+ //
336+ // fn foo<'a, 'b, 'c>() -> impl Trait<'c> + 'a + 'b
337+ //
338+ // but false for:
339+ //
340+ // fn foo<'c>() -> impl Trait<'c>
341+ //
342+ // unless `Trait` was declared like:
343+ //
344+ // trait Trait<'c>: 'c
345+ //
346+ // in which case it would be true.
347+ //
348+ // This is used during regionck to decide whether we need to
349+ // impose any additional constraints to ensure that region
350+ // variables in `concrete_ty` wind up being constrained to
351+ // something from `substs` (or, at minimum, things that outlive
352+ // the fn body). (Ultimately, writeback is responsible for this
353+ // check.)
354+ let bounds = tcx. explicit_item_bounds ( def_id) ;
355+ debug ! ( "{:#?}" , bounds) ;
356+ let bounds: Vec < _ > =
357+ bounds. iter ( ) . map ( |( bound, _) | bound. subst ( tcx, opaque_type_key. substs ) ) . collect ( ) ;
358+ debug ! ( "{:#?}" , bounds) ;
359+ let opaque_type = tcx. mk_opaque ( def_id, opaque_type_key. substs ) ;
345360
361+ let required_region_bounds = required_region_bounds ( tcx, opaque_type, bounds. into_iter ( ) ) ;
362+ if !required_region_bounds. is_empty ( ) {
346363 for required_region in required_region_bounds {
347364 concrete_ty. visit_with ( & mut ConstrainOpaqueTypeRegionVisitor {
348365 op : |r| self . sub_regions ( infer:: CallReturn ( span) , required_region, r) ,
@@ -979,9 +996,6 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
979996
980997 debug ! ( "instantiate_opaque_types: bounds={:?}" , bounds) ;
981998
982- let required_region_bounds = required_region_bounds ( tcx, ty, bounds. iter ( ) . copied ( ) ) ;
983- debug ! ( "instantiate_opaque_types: required_region_bounds={:?}" , required_region_bounds) ;
984-
985999 // Make sure that we are in fact defining the *entire* type
9861000 // (e.g., `type Foo<T: Bound> = impl Bar;` needs to be
9871001 // defined by a function like `fn foo<T: Bound>() -> Foo<T>`).
@@ -997,13 +1011,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
9971011
9981012 self . opaque_types . insert (
9991013 OpaqueTypeKey { def_id, substs } ,
1000- OpaqueTypeDecl {
1001- opaque_type : ty,
1002- definition_span,
1003- concrete_ty : ty_var,
1004- has_required_region_bounds : !required_region_bounds. is_empty ( ) ,
1005- origin,
1006- } ,
1014+ OpaqueTypeDecl { opaque_type : ty, definition_span, concrete_ty : ty_var, origin } ,
10071015 ) ;
10081016 debug ! ( "instantiate_opaque_types: ty_var={:?}" , ty_var) ;
10091017
0 commit comments