@@ -24,6 +24,7 @@ use super::borrow_set::BorrowData;
24
24
use super :: { Context , MirBorrowckCtxt } ;
25
25
use super :: { InitializationRequiringAction , PrefixSet } ;
26
26
27
+ use borrow_check:: nll:: explain_borrow:: BorrowContainsPointReason ;
27
28
use dataflow:: drop_flag_effects;
28
29
use dataflow:: move_paths:: indexes:: MoveOutIndex ;
29
30
use dataflow:: move_paths:: MovePathIndex ;
@@ -409,6 +410,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
409
410
self . access_place_error_reported
410
411
. insert ( ( root_place. clone ( ) , borrow_span) ) ;
411
412
413
+ let borrow_reason = self . find_why_borrow_contains_point ( context, borrow) ;
414
+
412
415
let mut err = match & self . describe_place ( & borrow. borrowed_place ) {
413
416
Some ( _) if self . is_place_thread_local ( root_place) => {
414
417
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> {
418
421
name,
419
422
& scope_tree,
420
423
& borrow,
424
+ borrow_reason,
421
425
drop_span,
422
426
borrow_span,
423
- proper_span,
424
427
kind. map ( |k| ( k, place_span. 0 ) ) ,
425
428
) ,
426
429
None => self . report_temporary_value_does_not_live_long_enough (
427
430
context,
428
431
& scope_tree,
429
432
& borrow,
433
+ borrow_reason,
430
434
drop_span,
431
- borrow_span,
432
435
proper_span,
433
436
) ,
434
437
} ;
@@ -444,16 +447,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
444
447
name : & String ,
445
448
scope_tree : & Lrc < ScopeTree > ,
446
449
borrow : & BorrowData < ' tcx > ,
450
+ reason : BorrowContainsPointReason < ' tcx > ,
447
451
drop_span : Span ,
448
452
borrow_span : Span ,
449
- _proper_span : Span ,
450
453
kind_place : Option < ( WriteKind , & Place < ' tcx > ) > ,
451
454
) -> DiagnosticBuilder < ' cx > {
452
455
debug ! (
453
456
"report_local_value_does_not_live_long_enough(\
454
- {:?}, {:?}, {:?}, {:?}, {:?}, {:?}\
457
+ {:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?} \
455
458
)",
456
- context, name, scope_tree, borrow, drop_span, borrow_span
459
+ context, name, scope_tree, borrow, reason , drop_span, borrow_span
457
460
) ;
458
461
459
462
let mut err = self . tcx . path_does_not_live_long_enough (
@@ -468,7 +471,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
468
471
format ! ( "`{}` dropped here while still borrowed" , name) ,
469
472
) ;
470
473
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 ) ;
472
475
err
473
476
}
474
477
@@ -501,15 +504,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
501
504
context : Context ,
502
505
scope_tree : & Lrc < ScopeTree > ,
503
506
borrow : & BorrowData < ' tcx > ,
507
+ reason : BorrowContainsPointReason < ' tcx > ,
504
508
drop_span : Span ,
505
- _borrow_span : Span ,
506
509
proper_span : Span ,
507
510
) -> DiagnosticBuilder < ' cx > {
508
511
debug ! (
509
512
"report_temporary_value_does_not_live_long_enough(\
510
- {:?}, {:?}, {:?}, {:?}, {:?}\
513
+ {:?}, {:?}, {:?}, {:?}, {:?}, {:?} \
511
514
)",
512
- context, scope_tree, borrow, drop_span, proper_span
515
+ context, scope_tree, borrow, reason , drop_span, proper_span
513
516
) ;
514
517
515
518
let tcx = self . tcx ;
@@ -518,7 +521,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
518
521
err. span_label ( proper_span, "temporary value does not live long enough" ) ;
519
522
err. span_label ( drop_span, "temporary value only lives until here" ) ;
520
523
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 ) ;
522
534
err
523
535
}
524
536
0 commit comments