Skip to content

Commit

Permalink
Ensure that depth first search does not get stuck in cycles.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtwco committed May 27, 2018
1 parent 0eeebe1 commit 24ee506
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/librustc_mir/dataflow/impls/borrows.rs
Expand Up @@ -59,9 +59,16 @@ fn precompute_borrows_out_of_scope<'a, 'tcx>(
borrows_out_of_scope_at_location: &mut FxHashMap<Location, Vec<BorrowIndex>>,
borrow_index: BorrowIndex,
borrow_region: RegionVid,
location: Location
location: Location,
visited_locations: &mut Vec<Location>
) {
// Start by dealing with the current location.
// Check if we have already visited this location and skip
// it if we have - avoids infinite loops.
if visited_locations.contains(&location) { return; }
visited_locations.push(location.clone());

// Next, add the borrow index to the current location's vector if the region does
// not contain the point at that location (or create a new vector if required).
if !regioncx.region_contains_point(borrow_region, location) {
borrows_out_of_scope_at_location
.entry(location.clone())
Expand All @@ -80,57 +87,65 @@ fn precompute_borrows_out_of_scope<'a, 'tcx>(
TerminatorKind::FalseUnwind { real_target: target, .. } => {
precompute_borrows_out_of_scope(
mir, regioncx, borrows_out_of_scope_at_location,
borrow_index, borrow_region, target.start_location()
borrow_index, borrow_region, target.start_location(),
visited_locations
);
},
TerminatorKind::SwitchInt { ref targets, .. } => {
for block in targets {
precompute_borrows_out_of_scope(
mir, regioncx, borrows_out_of_scope_at_location,
borrow_index, borrow_region, block.start_location()
borrow_index, borrow_region, block.start_location(),
visited_locations
);
}
},
TerminatorKind::Drop { target, unwind, .. } |
TerminatorKind::DropAndReplace { target, unwind, .. } => {
precompute_borrows_out_of_scope(
mir, regioncx, borrows_out_of_scope_at_location,
borrow_index, borrow_region, target.start_location()
borrow_index, borrow_region, target.start_location(),
visited_locations
);

if let Some(unwind_block) = unwind {
precompute_borrows_out_of_scope(
mir, regioncx, borrows_out_of_scope_at_location,
borrow_index, borrow_region, unwind_block.start_location()
borrow_index, borrow_region, unwind_block.start_location(),
visited_locations
);
}
},
TerminatorKind::Call { ref destination, cleanup, .. } => {
if let Some((_, block)) = destination {
precompute_borrows_out_of_scope(
mir, regioncx, borrows_out_of_scope_at_location,
borrow_index, borrow_region, block.start_location()
borrow_index, borrow_region, block.start_location(),
visited_locations
);
}

if let Some(block) = cleanup {
precompute_borrows_out_of_scope(
mir, regioncx, borrows_out_of_scope_at_location,
borrow_index, borrow_region, block.start_location()
borrow_index, borrow_region, block.start_location(),
visited_locations
);
}
},
TerminatorKind::Assert { target, cleanup, .. } |
TerminatorKind::Yield { resume: target, drop: cleanup, .. } => {
precompute_borrows_out_of_scope(
mir, regioncx, borrows_out_of_scope_at_location,
borrow_index, borrow_region, target.start_location()
borrow_index, borrow_region, target.start_location(),
visited_locations
);

if let Some(block) = cleanup {
precompute_borrows_out_of_scope(
mir, regioncx, borrows_out_of_scope_at_location,
borrow_index, borrow_region, block.start_location()
borrow_index, borrow_region, block.start_location(),
visited_locations
);
}
},
Expand All @@ -142,7 +157,7 @@ fn precompute_borrows_out_of_scope<'a, 'tcx>(
} else {
precompute_borrows_out_of_scope(mir, regioncx, borrows_out_of_scope_at_location,
borrow_index, borrow_region,
location.successor_within_block());
location.successor_within_block(), visited_locations);
}
}

Expand All @@ -167,7 +182,8 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {

precompute_borrows_out_of_scope(mir, &nonlexical_regioncx,
&mut borrows_out_of_scope_at_location,
borrow_index, borrow_region, location);
borrow_index, borrow_region, location,
&mut Vec::new());
}

Borrows {
Expand Down

0 comments on commit 24ee506

Please sign in to comment.