Skip to content

Commit d0416cc

Browse files
committed
Add helper method to determine if local had ever been initialized at current point in flow.
1 parent bc61541 commit d0416cc

File tree

1 file changed

+25
-19
lines changed
  • src/librustc_mir/borrow_check

1 file changed

+25
-19
lines changed

Diff for: src/librustc_mir/borrow_check/mod.rs

+25-19
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ use std::collections::BTreeMap;
3636

3737
use syntax_pos::Span;
3838

39-
use dataflow::indexes::BorrowIndex;
40-
use dataflow::move_paths::{HasMoveData, LookupResult, MoveData, MoveError, MovePathIndex};
41-
use dataflow::move_paths::indexes::MoveOutIndex;
39+
use dataflow::indexes::{BorrowIndex, InitIndex, MoveOutIndex, MovePathIndex};
40+
use dataflow::move_paths::{HasMoveData, LookupResult, MoveData, MoveError};
4241
use dataflow::Borrows;
4342
use dataflow::DataflowResultsConsumer;
4443
use dataflow::FlowAtLocation;
@@ -1442,10 +1441,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
14421441
debug!("check_if_reassignment_to_immutable_state({:?})", local);
14431442

14441443
// Check if any of the initializiations of `local` have happened yet:
1445-
let mpi = self.move_data.rev_lookup.find_local(local);
1446-
let init_indices = &self.move_data.init_path_map[mpi];
1447-
let first_init_index = init_indices.iter().find(|&ii| flow_state.ever_inits.contains(*ii));
1448-
if let Some(&init_index) = first_init_index {
1444+
if let Some(init_index) = self.is_local_ever_initialized(local, flow_state) {
14491445
// And, if so, report an error.
14501446
let init = &self.move_data.inits[init_index];
14511447
let span = init.span(&self.mir);
@@ -1889,6 +1885,21 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18891885
return true;
18901886
}
18911887

1888+
fn is_local_ever_initialized(&self,
1889+
local: Local,
1890+
flow_state: &Flows<'cx, 'gcx, 'tcx>)
1891+
-> Option<InitIndex>
1892+
{
1893+
let mpi = self.move_data.rev_lookup.find_local(local);
1894+
let ii = &self.move_data.init_path_map[mpi];
1895+
for &index in ii {
1896+
if flow_state.ever_inits.contains(index) {
1897+
return Some(index);
1898+
}
1899+
}
1900+
return None;
1901+
}
1902+
18921903
/// Adds the place into the used mutable variables set
18931904
fn add_used_mut<'d>(
18941905
&mut self,
@@ -1900,18 +1911,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
19001911
place: Place::Local(local),
19011912
is_local_mutation_allowed,
19021913
} => {
1903-
if is_local_mutation_allowed != LocalMutationIsAllowed::Yes {
1904-
// If the local may be initialized, and it is now currently being
1905-
// mutated, then it is justified to be annotated with the `mut`
1906-
// keyword, since the mutation may be a possible reassignment.
1907-
let mpi = self.move_data.rev_lookup.find_local(*local);
1908-
let ii = &self.move_data.init_path_map[mpi];
1909-
for &index in ii {
1910-
if flow_state.ever_inits.contains(index) {
1911-
self.used_mut.insert(*local);
1912-
break;
1913-
}
1914-
}
1914+
// If the local may have been initialized, and it is now currently being
1915+
// mutated, then it is justified to be annotated with the `mut`
1916+
// keyword, since the mutation may be a possible reassignment.
1917+
if is_local_mutation_allowed != LocalMutationIsAllowed::Yes &&
1918+
self.is_local_ever_initialized(*local, flow_state).is_some()
1919+
{
1920+
self.used_mut.insert(*local);
19151921
}
19161922
}
19171923
RootPlace {

0 commit comments

Comments
 (0)