@@ -863,7 +863,6 @@ enum WriteKind {
863863/// local place can be mutated.
864864//
865865// FIXME: @nikomatsakis suggested that this flag could be removed with the following modifications:
866- // - Merge `check_access_permissions()` and `check_if_reassignment_to_immutable_state()`.
867866// - Split `is_mutable()` into `is_assignable()` (can be directly assigned) and
868867// `is_declared_mutable()`.
869868// - Take flow state into consideration in `is_assignable()` for local variables.
@@ -1132,20 +1131,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11321131 // Write of P[i] or *P requires P init'd.
11331132 self . check_if_assigned_path_is_moved ( location, place_span, flow_state) ;
11341133
1135- // Special case: you can assign an immutable local variable
1136- // (e.g., `x = ...`) so long as it has never been initialized
1137- // before (at this point in the flow).
1138- if let Some ( local) = place_span. 0 . as_local ( ) {
1139- if let Mutability :: Not = self . body . local_decls [ local] . mutability {
1140- // check for reassignments to immutable local variables
1141- self . check_if_reassignment_to_immutable_state (
1142- location, local, place_span, flow_state,
1143- ) ;
1144- return ;
1145- }
1146- }
1147-
1148- // Otherwise, use the normal access permission rules.
11491134 self . access_place (
11501135 location,
11511136 place_span,
@@ -1554,24 +1539,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15541539 }
15551540 }
15561541
1557- fn check_if_reassignment_to_immutable_state (
1558- & mut self ,
1559- location : Location ,
1560- local : Local ,
1561- place_span : ( Place < ' tcx > , Span ) ,
1562- flow_state : & Flows < ' cx , ' tcx > ,
1563- ) {
1564- debug ! ( "check_if_reassignment_to_immutable_state({:?})" , local) ;
1565-
1566- // Check if any of the initializations of `local` have happened yet:
1567- if let Some ( init_index) = self . is_local_ever_initialized ( local, flow_state) {
1568- // And, if so, report an error.
1569- let init = & self . move_data . inits [ init_index] ;
1570- let span = init. span ( & self . body ) ;
1571- self . report_illegal_reassignment ( location, place_span, span, place_span. 0 ) ;
1572- }
1573- }
1574-
15751542 fn check_if_full_path_is_moved (
15761543 & mut self ,
15771544 location : Location ,
@@ -2037,12 +2004,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
20372004 // partial initialization, do not complain about mutability
20382005 // errors except for actual mutation (as opposed to an attempt
20392006 // to do a partial initialization).
2040- let previously_initialized =
2041- self . is_local_ever_initialized ( place. local , flow_state) . is_some ( ) ;
2007+ let previously_initialized = self . is_local_ever_initialized ( place. local , flow_state) ;
20422008
20432009 // at this point, we have set up the error reporting state.
2044- if previously_initialized {
2045- self . report_mutability_error ( place, span, the_place_err, error_access, location) ;
2010+ if let Some ( init_index) = previously_initialized {
2011+ if let ( AccessKind :: Mutate , Some ( _) ) = ( error_access, place. as_local ( ) ) {
2012+ // If this is a mutate access to an immutable local variable with no projections
2013+ // report the error as an illegal reassignment
2014+ let init = & self . move_data . inits [ init_index] ;
2015+ let assigned_span = init. span ( & self . body ) ;
2016+ self . report_illegal_reassignment ( location, ( place, span) , assigned_span, place) ;
2017+ } else {
2018+ self . report_mutability_error ( place, span, the_place_err, error_access, location)
2019+ }
20462020 true
20472021 } else {
20482022 false
0 commit comments