@@ -863,7 +863,6 @@ enum WriteKind {
863
863
/// local place can be mutated.
864
864
//
865
865
// 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()`.
867
866
// - Split `is_mutable()` into `is_assignable()` (can be directly assigned) and
868
867
// `is_declared_mutable()`.
869
868
// - Take flow state into consideration in `is_assignable()` for local variables.
@@ -1132,20 +1131,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
1132
1131
// Write of P[i] or *P requires P init'd.
1133
1132
self . check_if_assigned_path_is_moved ( location, place_span, flow_state) ;
1134
1133
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.
1149
1134
self . access_place (
1150
1135
location,
1151
1136
place_span,
@@ -1554,24 +1539,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
1554
1539
}
1555
1540
}
1556
1541
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
-
1575
1542
fn check_if_full_path_is_moved (
1576
1543
& mut self ,
1577
1544
location : Location ,
@@ -2037,12 +2004,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
2037
2004
// partial initialization, do not complain about mutability
2038
2005
// errors except for actual mutation (as opposed to an attempt
2039
2006
// 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) ;
2042
2008
2043
2009
// 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
+ }
2046
2020
true
2047
2021
} else {
2048
2022
false
0 commit comments