@@ -813,17 +813,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
813
813
if * outlived_f != ty:: ReStatic {
814
814
return ;
815
815
}
816
+ let suitable_region = self . infcx . tcx . is_suitable_region ( f) ;
817
+ let Some ( suitable_region) = suitable_region else { return ; } ;
816
818
817
- let fn_returns = self
818
- . infcx
819
- . tcx
820
- . is_suitable_region ( f)
821
- . map ( |r| self . infcx . tcx . return_type_impl_or_dyn_traits ( r. def_id ) )
822
- . unwrap_or_default ( ) ;
823
-
824
- if fn_returns. is_empty ( ) {
825
- return ;
826
- }
819
+ let fn_returns = self . infcx . tcx . return_type_impl_or_dyn_traits ( suitable_region. def_id ) ;
827
820
828
821
let param = if let Some ( param) = find_param_with_region ( self . infcx . tcx , f, outlived_f) {
829
822
param
@@ -839,15 +832,43 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
839
832
} ;
840
833
let captures = format ! ( "captures data from {arg}" ) ;
841
834
842
- return nice_region_error:: suggest_new_region_bound (
843
- self . infcx . tcx ,
844
- diag,
845
- fn_returns,
846
- lifetime. to_string ( ) ,
847
- Some ( arg) ,
848
- captures,
849
- Some ( ( param. param_ty_span , param. param_ty . to_string ( ) ) ) ,
850
- self . infcx . tcx . is_suitable_region ( f) . map ( |r| r. def_id ) ,
835
+ if !fn_returns. is_empty ( ) {
836
+ nice_region_error:: suggest_new_region_bound (
837
+ self . infcx . tcx ,
838
+ diag,
839
+ fn_returns,
840
+ lifetime. to_string ( ) ,
841
+ Some ( arg) ,
842
+ captures,
843
+ Some ( ( param. param_ty_span , param. param_ty . to_string ( ) ) ) ,
844
+ Some ( suitable_region. def_id ) ,
845
+ ) ;
846
+ return ;
847
+ }
848
+
849
+ let Some ( ( alias_tys, alias_span) ) = self
850
+ . infcx
851
+ . tcx
852
+ . return_type_impl_or_dyn_traits_with_type_alias ( suitable_region. def_id ) else { return ; } ;
853
+
854
+ // in case the return type of the method is a type alias
855
+ let mut spans_suggs: Vec < _ > = Vec :: new ( ) ;
856
+ for alias_ty in alias_tys {
857
+ if alias_ty. span . desugaring_kind ( ) . is_some ( ) {
858
+ // Skip `async` desugaring `impl Future`.
859
+ ( )
860
+ }
861
+ if let TyKind :: TraitObject ( _, lt, _) = alias_ty. kind {
862
+ spans_suggs. push ( ( lt. ident . span . shrink_to_hi ( ) , " + 'a" . to_string ( ) ) ) ;
863
+ }
864
+ }
865
+ spans_suggs. push ( ( alias_span. shrink_to_hi ( ) , "<'a>" . to_string ( ) ) ) ;
866
+ diag. multipart_suggestion_verbose (
867
+ & format ! (
868
+ "to declare that the trait object {captures}, you can add a lifetime parameter `'a` in the type alias"
869
+ ) ,
870
+ spans_suggs,
871
+ Applicability :: MaybeIncorrect ,
851
872
) ;
852
873
}
853
874
}
0 commit comments