@@ -1024,13 +1024,14 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10241024 }
10251025
10261026 err_out_of_scope( super_scope, sub_scope, cause) => {
1027- let ( value_kind, value_msg) = match err. cmt . cat {
1027+ let ( value_kind, value_msg, is_temporary ) = match err. cmt . cat {
10281028 mc:: Categorization :: Rvalue ( _) =>
1029- ( "temporary value" , "temporary value created here" ) ,
1029+ ( "temporary value" , "temporary value created here" , true ) ,
10301030 _ =>
1031- ( "borrowed value" , "does not live long enough" )
1031+ ( "borrowed value" , "does not live long enough" , false )
10321032 } ;
1033- match cause {
1033+
1034+ let is_closure = match cause {
10341035 euv:: ClosureCapture ( s) => {
10351036 // The primary span starts out as the closure creation point.
10361037 // Change the primary span here to highlight the use of the variable
@@ -1041,21 +1042,36 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10411042 db. span = MultiSpan :: from_span ( s) ;
10421043 db. span_label ( primary, & format ! ( "capture occurs here" ) ) ;
10431044 db. span_label ( s, & value_msg) ;
1045+ true
10441046 }
1045- None => ( )
1047+ None => false
10461048 }
10471049 }
10481050 _ => {
10491051 db. span_label ( error_span, & value_msg) ;
1052+ false
10501053 }
1051- }
1054+ } ;
10521055
10531056 let sub_span = self . region_end_span ( sub_scope) ;
10541057 let super_span = self . region_end_span ( super_scope) ;
10551058
10561059 match ( sub_span, super_span) {
10571060 ( Some ( s1) , Some ( s2) ) if s1 == s2 => {
1058- db. span_label ( s1, & format ! ( "{} dropped before borrower" , value_kind) ) ;
1061+ if !is_temporary && !is_closure {
1062+ db. span = MultiSpan :: from_span ( s1) ;
1063+ db. span_label ( error_span, & format ! ( "borrow occurs here" ) ) ;
1064+ let msg = match opt_loan_path ( & err. cmt ) {
1065+ None => "borrowed value" . to_string ( ) ,
1066+ Some ( lp) => {
1067+ format ! ( "`{}`" , self . loan_path_to_string( & lp) )
1068+ }
1069+ } ;
1070+ db. span_label ( s1,
1071+ & format ! ( "{} dropped here while still borrowed" , msg) ) ;
1072+ } else {
1073+ db. span_label ( s1, & format ! ( "{} dropped before borrower" , value_kind) ) ;
1074+ }
10591075 db. note ( "values in a scope are dropped in the opposite order \
10601076 they are created") ;
10611077 }
0 commit comments