@@ -324,16 +324,16 @@ impl<'tcx> RegionInferenceContext<'tcx> {
324324 match ( category, fr_is_local, outlived_fr_is_local) {
325325 ( ConstraintCategory :: Assignment , true , false ) |
326326 ( ConstraintCategory :: CallArgument , true , false ) =>
327- self . report_escapes_closure_error ( mir, infcx, mir_def_id, fr, outlived_fr,
328- category, span, errors_buffer) ,
327+ self . report_escaping_data_error ( mir, infcx, mir_def_id, fr, outlived_fr,
328+ category, span, errors_buffer) ,
329329 _ =>
330330 self . report_general_error ( mir, infcx, mir_def_id, fr, fr_is_local,
331331 outlived_fr, outlived_fr_is_local,
332332 category, span, errors_buffer) ,
333333 } ;
334334 }
335335
336- fn report_escapes_closure_error (
336+ fn report_escaping_data_error (
337337 & self ,
338338 mir : & Mir < ' tcx > ,
339339 infcx : & InferCtxt < ' _ , ' _ , ' tcx > ,
@@ -348,22 +348,23 @@ impl<'tcx> RegionInferenceContext<'tcx> {
348348 let outlived_fr_name_and_span =
349349 self . get_var_name_and_span_for_region ( infcx. tcx , mir, outlived_fr) ;
350350
351+ let escapes_from = if infcx. tcx . is_closure ( mir_def_id) { "closure" } else { "function" } ;
352+
351353 if fr_name_and_span. is_none ( ) && outlived_fr_name_and_span. is_none ( ) {
352354 return self . report_general_error ( mir, infcx, mir_def_id,
353355 fr, true , outlived_fr, false ,
354356 category, span, errors_buffer) ;
355357 }
356358
357- let mut diag = infcx
358- . tcx
359- . sess
360- . struct_span_err ( span, & format ! ( "borrowed data escapes outside of closure" ) ) ;
359+ let mut diag = infcx. tcx . sess . struct_span_err (
360+ span, & format ! ( "borrowed data escapes outside of {}" , escapes_from) ,
361+ ) ;
361362
362363 if let Some ( ( outlived_fr_name, outlived_fr_span) ) = outlived_fr_name_and_span {
363364 if let Some ( name) = outlived_fr_name {
364365 diag. span_label (
365366 outlived_fr_span,
366- format ! ( "`{}` is declared here, outside of the closure body" , name) ,
367+ format ! ( "`{}` is declared here, outside of the {} body" , name, escapes_from ) ,
367368 ) ;
368369 }
369370 }
@@ -372,13 +373,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
372373 if let Some ( name) = fr_name {
373374 diag. span_label (
374375 fr_span,
375- format ! (
376- "`{}` is a reference that is only valid in the closure body" ,
377- name
378- ) ,
376+ format ! ( "`{}` is a reference that is only valid in the {} body" ,
377+ name, escapes_from) ,
379378 ) ;
380379
381- diag. span_label ( span, format ! ( "`{}` escapes the closure body here" , name) ) ;
380+ diag. span_label ( span, format ! ( "`{}` escapes the {} body here" ,
381+ name, escapes_from) ) ;
382382 }
383383 }
384384
@@ -409,12 +409,14 @@ impl<'tcx> RegionInferenceContext<'tcx> {
409409 let outlived_fr_name = self . give_region_a_name (
410410 infcx, mir, mir_def_id, outlived_fr, counter, & mut diag) ;
411411
412+ let mir_def_name = if infcx. tcx . is_closure ( mir_def_id) { "closure" } else { "function" } ;
413+
412414 match ( category, outlived_fr_is_local, fr_is_local) {
413415 ( ConstraintCategory :: Return , true , _) => {
414416 diag. span_label ( span, format ! (
415- "closure was supposed to return data with lifetime `{}` but it is returning \
417+ "{} was supposed to return data with lifetime `{}` but it is returning \
416418 data with lifetime `{}`",
417- fr_name, outlived_fr_name,
419+ mir_def_name , fr_name, outlived_fr_name,
418420 ) ) ;
419421 } ,
420422 _ => {
0 commit comments