10
10
11
11
use borrow_check:: { WriteKind , StorageDeadOrDrop } ;
12
12
use borrow_check:: prefixes:: IsPrefixOf ;
13
+ use borrow_check:: nll:: explain_borrow:: BorrowExplanation ;
13
14
use rustc:: middle:: region:: ScopeTree ;
14
- use rustc:: mir:: VarBindingForm ;
15
- use rustc:: mir:: { BindingForm , BorrowKind , ClearCrossCrate , Field , Local } ;
16
- use rustc:: mir:: { FakeReadCause , LocalDecl , LocalKind , Location , Operand , Place } ;
17
- use rustc:: mir:: { ProjectionElem , Rvalue , Statement , StatementKind } ;
15
+ use rustc:: mir:: {
16
+ BindingForm , BorrowKind , ClearCrossCrate , Field , FakeReadCause , Local ,
17
+ LocalDecl , LocalKind , Location , Operand , Place ,
18
+ ProjectionElem , Rvalue , Statement , StatementKind ,
19
+ VarBindingForm ,
20
+ } ;
18
21
use rustc:: ty;
19
22
use rustc_data_structures:: fx:: FxHashSet ;
20
23
use rustc_data_structures:: sync:: Lrc ;
@@ -25,7 +28,6 @@ use super::borrow_set::BorrowData;
25
28
use super :: { Context , MirBorrowckCtxt } ;
26
29
use super :: { InitializationRequiringAction , PrefixSet } ;
27
30
28
- use borrow_check:: nll:: explain_borrow:: BorrowContainsPointReason ;
29
31
use dataflow:: drop_flag_effects;
30
32
use dataflow:: move_paths:: indexes:: MoveOutIndex ;
31
33
use dataflow:: move_paths:: MovePathIndex ;
@@ -226,7 +228,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
226
228
227
229
move_spans. var_span_label ( & mut err, "move occurs due to use in closure" ) ;
228
230
229
- self . explain_why_borrow_contains_point ( context, borrow, None , & mut err) ;
231
+ self . explain_why_borrow_contains_point ( context, borrow, None ) . emit ( self . tcx , & mut err) ;
230
232
err. buffer ( & mut self . errors_buffer ) ;
231
233
}
232
234
@@ -263,7 +265,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
263
265
format ! ( "borrow occurs due to use of `{}` in closure" , desc_place)
264
266
} ) ;
265
267
266
- self . explain_why_borrow_contains_point ( context, borrow, None , & mut err) ;
268
+ self . explain_why_borrow_contains_point ( context, borrow, None ) . emit ( self . tcx , & mut err) ;
267
269
err. buffer ( & mut self . errors_buffer ) ;
268
270
}
269
271
@@ -390,7 +392,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
390
392
) ;
391
393
}
392
394
393
- self . explain_why_borrow_contains_point ( context, issued_borrow, None , & mut err) ;
395
+ self . explain_why_borrow_contains_point ( context, issued_borrow, None )
396
+ . emit ( self . tcx , & mut err) ;
394
397
395
398
err. buffer ( & mut self . errors_buffer ) ;
396
399
}
@@ -445,17 +448,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
445
448
self . access_place_error_reported
446
449
. insert ( ( root_place. clone ( ) , borrow_span) ) ;
447
450
448
- let borrow_reason = self . find_why_borrow_contains_point ( context, borrow) ;
449
-
450
- if let Some ( WriteKind :: StorageDeadOrDrop ( StorageDeadOrDrop :: Destructor ) ) = kind
451
- {
451
+ if let Some ( WriteKind :: StorageDeadOrDrop ( StorageDeadOrDrop :: Destructor ) ) = kind {
452
452
// If a borrow of path `B` conflicts with drop of `D` (and
453
453
// we're not in the uninteresting case where `B` is a
454
454
// prefix of `D`), then report this as a more interesting
455
455
// destructor conflict.
456
456
if !borrow. borrowed_place . is_prefix_of ( place_span. 0 ) {
457
- self . report_borrow_conflicts_with_destructor (
458
- context, borrow, borrow_reason, place_span, kind) ;
457
+ self . report_borrow_conflicts_with_destructor ( context, borrow, place_span, kind) ;
459
458
return ;
460
459
}
461
460
}
@@ -469,7 +468,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
469
468
name,
470
469
& scope_tree,
471
470
& borrow,
472
- borrow_reason,
473
471
drop_span,
474
472
borrow_span,
475
473
kind. map ( |k| ( k, place_span. 0 ) ) ,
@@ -478,7 +476,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
478
476
context,
479
477
& scope_tree,
480
478
& borrow,
481
- borrow_reason,
482
479
drop_span,
483
480
proper_span,
484
481
) ,
@@ -495,16 +492,15 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
495
492
name : & String ,
496
493
scope_tree : & Lrc < ScopeTree > ,
497
494
borrow : & BorrowData < ' tcx > ,
498
- reason : BorrowContainsPointReason < ' tcx > ,
499
495
drop_span : Span ,
500
496
borrow_span : Span ,
501
497
kind_place : Option < ( WriteKind , & Place < ' tcx > ) > ,
502
498
) -> DiagnosticBuilder < ' cx > {
503
499
debug ! (
504
500
"report_local_value_does_not_live_long_enough(\
505
- {:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?} \
501
+ {:?}, {:?}, {:?}, {:?}, {:?}, {:?}\
506
502
)",
507
- context, name, scope_tree, borrow, reason , drop_span, borrow_span
503
+ context, name, scope_tree, borrow, drop_span, borrow_span
508
504
) ;
509
505
510
506
let mut err = self . tcx . path_does_not_live_long_enough (
@@ -519,34 +515,32 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
519
515
format ! ( "`{}` dropped here while still borrowed" , name) ,
520
516
) ;
521
517
522
- self . report_why_borrow_contains_point ( & mut err, reason, kind_place) ;
518
+ self . explain_why_borrow_contains_point ( context, borrow, kind_place)
519
+ . emit ( self . tcx , & mut err) ;
520
+
523
521
err
524
522
}
525
523
526
524
pub ( super ) fn report_borrow_conflicts_with_destructor (
527
525
& mut self ,
528
526
context : Context ,
529
527
borrow : & BorrowData < ' tcx > ,
530
- borrow_reason : BorrowContainsPointReason < ' tcx > ,
531
- place_span : ( & Place < ' tcx > , Span ) ,
528
+ ( place, drop_span) : ( & Place < ' tcx > , Span ) ,
532
529
kind : Option < WriteKind > ,
533
530
) {
534
531
debug ! (
535
532
"report_borrow_conflicts_with_destructor(\
536
- {:?}, {:?}, {:?}, {:?} {:?}\
533
+ {:?}, {:?}, ( {:?}, {:?}), {:?}\
537
534
)",
538
- context, borrow, borrow_reason , place_span , kind,
535
+ context, borrow, place , drop_span , kind,
539
536
) ;
540
537
541
538
let borrow_spans = self . retrieve_borrow_spans ( borrow) ;
542
539
let borrow_span = borrow_spans. var_or_use ( ) ;
543
540
544
541
let mut err = self . tcx . cannot_borrow_across_destructor ( borrow_span, Origin :: Mir ) ;
545
542
546
- let drop_span = place_span. 1 ;
547
-
548
543
let ( what_was_dropped, dropped_ty) = {
549
- let place = place_span. 0 ;
550
544
let desc = match self . describe_place ( place) {
551
545
Some ( name) => format ! ( "`{}`" , name. as_str( ) ) ,
552
546
None => format ! ( "temporary value" ) ,
@@ -571,17 +565,19 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
571
565
} ;
572
566
err. span_label ( drop_span, label) ;
573
567
574
- // Only give this note and suggestion if they could be relevant
575
- match borrow_reason {
576
- BorrowContainsPointReason :: Liveness { ..}
577
- | BorrowContainsPointReason :: DropLiveness { ..} => {
568
+ // Only give this note and suggestion if they could be relevant.
569
+ let explanation = self . explain_why_borrow_contains_point (
570
+ context, borrow, kind. map ( |k| ( k, place) ) ,
571
+ ) ;
572
+ match explanation {
573
+ BorrowExplanation :: UsedLater { ..} |
574
+ BorrowExplanation :: UsedLaterWhenDropped { ..} => {
578
575
err. note ( "consider using a `let` binding to create a longer lived value" ) ;
579
- }
580
- BorrowContainsPointReason :: OutlivesFreeRegion { .. } => ( ) ,
576
+ } ,
577
+ _ => { } ,
581
578
}
582
579
583
- self . report_why_borrow_contains_point (
584
- & mut err, borrow_reason, kind. map ( |k| ( k, place_span. 0 ) ) ) ;
580
+ explanation. emit ( self . tcx , & mut err) ;
585
581
586
582
err. buffer ( & mut self . errors_buffer ) ;
587
583
}
@@ -607,6 +603,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
607
603
"thread-local variables cannot be borrowed beyond the end of the function" ,
608
604
) ;
609
605
err. span_label ( drop_span, "end of enclosing function is here" ) ;
606
+
610
607
err
611
608
}
612
609
@@ -615,15 +612,14 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
615
612
context : Context ,
616
613
scope_tree : & Lrc < ScopeTree > ,
617
614
borrow : & BorrowData < ' tcx > ,
618
- reason : BorrowContainsPointReason < ' tcx > ,
619
615
drop_span : Span ,
620
616
proper_span : Span ,
621
617
) -> DiagnosticBuilder < ' cx > {
622
618
debug ! (
623
619
"report_temporary_value_does_not_live_long_enough(\
624
- {:?}, {:?}, {:?}, {:?}, {:?}, {:?} \
620
+ {:?}, {:?}, {:?}, {:?}, {:?}\
625
621
)",
626
- context, scope_tree, borrow, reason , drop_span, proper_span
622
+ context, scope_tree, borrow, drop_span, proper_span
627
623
) ;
628
624
629
625
let tcx = self . tcx ;
@@ -632,16 +628,18 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
632
628
err. span_label ( proper_span, "temporary value does not live long enough" ) ;
633
629
err. span_label ( drop_span, "temporary value only lives until here" ) ;
634
630
635
- // Only give this note and suggestion if they could be relevant
636
- match reason {
637
- BorrowContainsPointReason :: Liveness { ..}
638
- | BorrowContainsPointReason :: DropLiveness { ..} => {
631
+ let explanation = self . explain_why_borrow_contains_point ( context, borrow, None ) ;
632
+ match explanation {
633
+ BorrowExplanation :: UsedLater ( ..) |
634
+ BorrowExplanation :: UsedLaterInLoop ( ..) |
635
+ BorrowExplanation :: UsedLaterWhenDropped ( ..) => {
636
+ // Only give this note and suggestion if it could be relevant.
639
637
err. note ( "consider using a `let` binding to create a longer lived value" ) ;
640
- }
641
- BorrowContainsPointReason :: OutlivesFreeRegion { .. } => ( ) ,
638
+ } ,
639
+ _ => { } ,
642
640
}
641
+ explanation. emit ( self . tcx , & mut err) ;
643
642
644
- self . report_why_borrow_contains_point ( & mut err, reason, None ) ;
645
643
err
646
644
}
647
645
@@ -749,7 +747,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
749
747
750
748
loan_spans. var_span_label ( & mut err, "borrow occurs due to use in closure" ) ;
751
749
752
- self . explain_why_borrow_contains_point ( context, loan, None , & mut err) ;
750
+ self . explain_why_borrow_contains_point ( context, loan, None ) . emit ( self . tcx , & mut err) ;
753
751
754
752
err. buffer ( & mut self . errors_buffer ) ;
755
753
}
0 commit comments