@@ -881,7 +881,6 @@ enum WriteKind {
881
881
/// local place can be mutated.
882
882
//
883
883
// FIXME: @nikomatsakis suggested that this flag could be removed with the following modifications:
884
- // - Merge `check_access_permissions()` and `check_if_reassignment_to_immutable_state()`.
885
884
// - Split `is_mutable()` into `is_assignable()` (can be directly assigned) and
886
885
// `is_declared_mutable()`.
887
886
// - Take flow state into consideration in `is_assignable()` for local variables.
@@ -1150,20 +1149,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
1150
1149
// Write of P[i] or *P requires P init'd.
1151
1150
self . check_if_assigned_path_is_moved ( location, place_span, flow_state) ;
1152
1151
1153
- // Special case: you can assign an immutable local variable
1154
- // (e.g., `x = ...`) so long as it has never been initialized
1155
- // before (at this point in the flow).
1156
- if let Some ( local) = place_span. 0 . as_local ( ) {
1157
- if let Mutability :: Not = self . body . local_decls [ local] . mutability {
1158
- // check for reassignments to immutable local variables
1159
- self . check_if_reassignment_to_immutable_state (
1160
- location, local, place_span, flow_state,
1161
- ) ;
1162
- return ;
1163
- }
1164
- }
1165
-
1166
- // Otherwise, use the normal access permission rules.
1167
1152
self . access_place (
1168
1153
location,
1169
1154
place_span,
@@ -1572,24 +1557,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
1572
1557
}
1573
1558
}
1574
1559
1575
- fn check_if_reassignment_to_immutable_state (
1576
- & mut self ,
1577
- location : Location ,
1578
- local : Local ,
1579
- place_span : ( Place < ' tcx > , Span ) ,
1580
- flow_state : & Flows < ' cx , ' tcx > ,
1581
- ) {
1582
- debug ! ( "check_if_reassignment_to_immutable_state({:?})" , local) ;
1583
-
1584
- // Check if any of the initializations of `local` have happened yet:
1585
- if let Some ( init_index) = self . is_local_ever_initialized ( local, flow_state) {
1586
- // And, if so, report an error.
1587
- let init = & self . move_data . inits [ init_index] ;
1588
- let span = init. span ( & self . body ) ;
1589
- self . report_illegal_reassignment ( location, place_span, span, place_span. 0 ) ;
1590
- }
1591
- }
1592
-
1593
1560
fn check_if_full_path_is_moved (
1594
1561
& mut self ,
1595
1562
location : Location ,
@@ -2056,12 +2023,21 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
2056
2023
// partial initialization, do not complain about mutability
2057
2024
// errors except for actual mutation (as opposed to an attempt
2058
2025
// to do a partial initialization).
2059
- let previously_initialized =
2060
- self . is_local_ever_initialized ( place. local , flow_state) . is_some ( ) ;
2026
+ let previously_initialized = self . is_local_ever_initialized ( place. local , flow_state) ;
2061
2027
2062
2028
// at this point, we have set up the error reporting state.
2063
- if previously_initialized {
2064
- self . report_mutability_error ( place, span, the_place_err, error_access, location) ;
2029
+ if let Some ( init_index) = previously_initialized {
2030
+ match ( error_access, is_local_mutation_allowed, place. as_local ( ) ) {
2031
+ // emit better diagnostic for reassignment to immutable local variables
2032
+ ( AccessKind :: Mutate , LocalMutationIsAllowed :: No , Some ( _) ) => {
2033
+ let init = & self . move_data . inits [ init_index] ;
2034
+ let assigned_span = init. span ( & self . body ) ;
2035
+ self . report_illegal_reassignment ( location, ( place, span) , assigned_span, place) ;
2036
+ }
2037
+ _ => {
2038
+ self . report_mutability_error ( place, span, the_place_err, error_access, location)
2039
+ }
2040
+ }
2065
2041
true
2066
2042
} else {
2067
2043
false
0 commit comments