@@ -24,6 +24,7 @@ use super::borrow_set::BorrowData;
2424use super :: { Context , MirBorrowckCtxt } ;
2525use super :: { InitializationRequiringAction , PrefixSet } ;
2626
27+ use borrow_check:: nll:: explain_borrow:: BorrowContainsPointReason ;
2728use dataflow:: drop_flag_effects;
2829use dataflow:: move_paths:: indexes:: MoveOutIndex ;
2930use dataflow:: move_paths:: MovePathIndex ;
@@ -409,6 +410,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
409410 self . access_place_error_reported
410411 . insert ( ( root_place. clone ( ) , borrow_span) ) ;
411412
413+ let borrow_reason = self . find_why_borrow_contains_point ( context, borrow) ;
414+
412415 let mut err = match & self . describe_place ( & borrow. borrowed_place ) {
413416 Some ( _) if self . is_place_thread_local ( root_place) => {
414417 self . report_thread_local_value_does_not_live_long_enough ( drop_span, borrow_span)
@@ -418,17 +421,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
418421 name,
419422 & scope_tree,
420423 & borrow,
424+ borrow_reason,
421425 drop_span,
422426 borrow_span,
423- proper_span,
424427 kind. map ( |k| ( k, place_span. 0 ) ) ,
425428 ) ,
426429 None => self . report_temporary_value_does_not_live_long_enough (
427430 context,
428431 & scope_tree,
429432 & borrow,
433+ borrow_reason,
430434 drop_span,
431- borrow_span,
432435 proper_span,
433436 ) ,
434437 } ;
@@ -444,16 +447,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
444447 name : & String ,
445448 scope_tree : & Lrc < ScopeTree > ,
446449 borrow : & BorrowData < ' tcx > ,
450+ reason : BorrowContainsPointReason < ' tcx > ,
447451 drop_span : Span ,
448452 borrow_span : Span ,
449- _proper_span : Span ,
450453 kind_place : Option < ( WriteKind , & Place < ' tcx > ) > ,
451454 ) -> DiagnosticBuilder < ' cx > {
452455 debug ! (
453456 "report_local_value_does_not_live_long_enough(\
454- {:?}, {:?}, {:?}, {:?}, {:?}, {:?}\
457+ {:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?} \
455458 )",
456- context, name, scope_tree, borrow, drop_span, borrow_span
459+ context, name, scope_tree, borrow, reason , drop_span, borrow_span
457460 ) ;
458461
459462 let mut err = self . tcx . path_does_not_live_long_enough (
@@ -468,7 +471,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
468471 format ! ( "`{}` dropped here while still borrowed" , name) ,
469472 ) ;
470473
471- self . explain_why_borrow_contains_point ( context , borrow , kind_place , & mut err ) ;
474+ self . report_why_borrow_contains_point ( & mut err , reason , kind_place ) ;
472475 err
473476 }
474477
@@ -501,15 +504,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
501504 context : Context ,
502505 scope_tree : & Lrc < ScopeTree > ,
503506 borrow : & BorrowData < ' tcx > ,
507+ reason : BorrowContainsPointReason < ' tcx > ,
504508 drop_span : Span ,
505- _borrow_span : Span ,
506509 proper_span : Span ,
507510 ) -> DiagnosticBuilder < ' cx > {
508511 debug ! (
509512 "report_temporary_value_does_not_live_long_enough(\
510- {:?}, {:?}, {:?}, {:?}, {:?}\
513+ {:?}, {:?}, {:?}, {:?}, {:?}, {:?} \
511514 )",
512- context, scope_tree, borrow, drop_span, proper_span
515+ context, scope_tree, borrow, reason , drop_span, proper_span
513516 ) ;
514517
515518 let tcx = self . tcx ;
@@ -518,7 +521,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
518521 err. span_label ( proper_span, "temporary value does not live long enough" ) ;
519522 err. span_label ( drop_span, "temporary value only lives until here" ) ;
520523
521- self . explain_why_borrow_contains_point ( context, borrow, None , & mut err) ;
524+ // Only give this note and suggestion if they could be relevant
525+ match reason {
526+ BorrowContainsPointReason :: Liveness { ..}
527+ | BorrowContainsPointReason :: DropLiveness { ..} => {
528+ err. note ( "consider using a `let` binding to create a longer lived value" ) ;
529+ }
530+ BorrowContainsPointReason :: OutlivesFreeRegion { ..} => ( ) ,
531+ }
532+
533+ self . report_why_borrow_contains_point ( & mut err, reason, None ) ;
522534 err
523535 }
524536
0 commit comments