@@ -35,7 +35,7 @@ use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
3535use rustc_middle:: ty:: query:: Providers ;
3636use rustc_middle:: ty:: { self , CapturedPlace , ParamEnv , RegionVid , TyCtxt } ;
3737use rustc_session:: lint:: builtin:: UNUSED_MUT ;
38- use rustc_span:: { Span , Symbol } ;
38+ use rustc_span:: { DesugaringKind , Span , Symbol } ;
3939
4040use either:: Either ;
4141use smallvec:: SmallVec ;
@@ -340,7 +340,6 @@ fn do_mir_borrowck<'tcx>(
340340 next_region_name : RefCell :: new ( 1 ) ,
341341 polonius_output : None ,
342342 errors,
343- replaces : Default :: default ( ) ,
344343 } ;
345344 promoted_mbcx. report_move_errors ( move_errors) ;
346345 errors = promoted_mbcx. errors ;
@@ -372,7 +371,6 @@ fn do_mir_borrowck<'tcx>(
372371 next_region_name : RefCell :: new ( 1 ) ,
373372 polonius_output,
374373 errors,
375- replaces : Default :: default ( ) ,
376374 } ;
377375
378376 // Compute and report region errors, if any.
@@ -555,10 +553,6 @@ struct MirBorrowckCtxt<'cx, 'tcx> {
555553 polonius_output : Option < Rc < PoloniusOutput > > ,
556554
557555 errors : error:: BorrowckErrors < ' tcx > ,
558-
559- /// Record the places were a replace happens so that we can use the
560- /// correct access depth in the assignment for better diagnostic
561- replaces : FxHashSet < Location > ,
562556}
563557
564558// Check that:
@@ -583,10 +577,8 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
583577 match & stmt. kind {
584578 StatementKind :: Assign ( box ( lhs, rhs) ) => {
585579 self . consume_rvalue ( location, ( rhs, span) , flow_state) ;
586- // In case of a replace the drop access check is skipped for better diagnostic but we need
587- // to use a stricter access depth here
588- let access_depth = if self . replaces . contains ( & location) { AccessDepth :: Drop } else { Shallow ( None ) } ;
589- self . mutate_place ( location, ( * lhs, span) , access_depth, flow_state) ;
580+
581+ self . mutate_place ( location, ( * lhs, span) , AccessDepth :: Shallow ( None ) , flow_state) ;
590582 }
591583 StatementKind :: FakeRead ( box ( _, place) ) => {
592584 // Read for match doesn't access any memory and is used to
@@ -652,28 +644,13 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
652644 TerminatorKind :: SwitchInt { discr, targets : _ } => {
653645 self . consume_operand ( loc, ( discr, span) , flow_state) ;
654646 }
655- TerminatorKind :: Drop { place, target, unwind, is_replace } => {
647+ TerminatorKind :: Drop { place, target : _ , unwind : _ , is_replace : _ } => {
656648 debug ! (
657649 "visit_terminator_drop \
658650 loc: {:?} term: {:?} place: {:?} span: {:?}",
659651 loc, term, place, span
660652 ) ;
661653
662- // In case of a replace, it's more user friendly to report a problem with the explicit
663- // assignment than the implicit drop.
664- // Simply skip this access and rely on the assignment to report any error.
665- if * is_replace {
666- // An assignment `x = ...` is usually a shallow access, but in the case of a replace
667- // the drop could access internal references depending on the drop implementation.
668- // Since we're skipping the drop access, we need to mark the access depth
669- // of the assignment as AccessDepth::Drop.
670- self . replaces . insert ( Location { block : * target, statement_index : 0 } ) ;
671- if let Some ( unwind) = unwind {
672- self . replaces . insert ( Location { block : * unwind, statement_index : 0 } ) ;
673- }
674- return ;
675- }
676-
677654 self . access_place (
678655 loc,
679656 ( * place, span) ,
@@ -1112,13 +1089,22 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11121089 this. report_conflicting_borrow ( location, place_span, bk, borrow) ;
11131090 this. buffer_error ( err) ;
11141091 }
1115- WriteKind :: StorageDeadOrDrop => this
1116- . report_borrowed_value_does_not_live_long_enough (
1117- location,
1118- borrow,
1119- place_span,
1120- Some ( kind) ,
1121- ) ,
1092+ WriteKind :: StorageDeadOrDrop => {
1093+ if let Some ( DesugaringKind :: Replace ) = place_span. 1 . desugaring_kind ( ) {
1094+ // If this is a drop triggered by a reassignment, it's more user friendly
1095+ // to report a problem with the explicit assignment than the implicit drop.
1096+ this. report_illegal_mutation_of_borrowed (
1097+ location, place_span, borrow,
1098+ )
1099+ } else {
1100+ this. report_borrowed_value_does_not_live_long_enough (
1101+ location,
1102+ borrow,
1103+ place_span,
1104+ Some ( kind) ,
1105+ )
1106+ }
1107+ }
11221108 WriteKind :: Mutate => {
11231109 this. report_illegal_mutation_of_borrowed ( location, place_span, borrow)
11241110 }
0 commit comments