Skip to content

Commit 24ee506

Browse files
committed
Ensure that depth first search does not get stuck in cycles.
1 parent 0eeebe1 commit 24ee506

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/librustc_mir/dataflow/impls/borrows.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ fn precompute_borrows_out_of_scope<'a, 'tcx>(
5959
borrows_out_of_scope_at_location: &mut FxHashMap<Location, Vec<BorrowIndex>>,
6060
borrow_index: BorrowIndex,
6161
borrow_region: RegionVid,
62-
location: Location
62+
location: Location,
63+
visited_locations: &mut Vec<Location>
6364
) {
64-
// Start by dealing with the current location.
65+
// Check if we have already visited this location and skip
66+
// it if we have - avoids infinite loops.
67+
if visited_locations.contains(&location) { return; }
68+
visited_locations.push(location.clone());
69+
70+
// Next, add the borrow index to the current location's vector if the region does
71+
// not contain the point at that location (or create a new vector if required).
6572
if !regioncx.region_contains_point(borrow_region, location) {
6673
borrows_out_of_scope_at_location
6774
.entry(location.clone())
@@ -80,57 +87,65 @@ fn precompute_borrows_out_of_scope<'a, 'tcx>(
8087
TerminatorKind::FalseUnwind { real_target: target, .. } => {
8188
precompute_borrows_out_of_scope(
8289
mir, regioncx, borrows_out_of_scope_at_location,
83-
borrow_index, borrow_region, target.start_location()
90+
borrow_index, borrow_region, target.start_location(),
91+
visited_locations
8492
);
8593
},
8694
TerminatorKind::SwitchInt { ref targets, .. } => {
8795
for block in targets {
8896
precompute_borrows_out_of_scope(
8997
mir, regioncx, borrows_out_of_scope_at_location,
90-
borrow_index, borrow_region, block.start_location()
98+
borrow_index, borrow_region, block.start_location(),
99+
visited_locations
91100
);
92101
}
93102
},
94103
TerminatorKind::Drop { target, unwind, .. } |
95104
TerminatorKind::DropAndReplace { target, unwind, .. } => {
96105
precompute_borrows_out_of_scope(
97106
mir, regioncx, borrows_out_of_scope_at_location,
98-
borrow_index, borrow_region, target.start_location()
107+
borrow_index, borrow_region, target.start_location(),
108+
visited_locations
99109
);
100110

101111
if let Some(unwind_block) = unwind {
102112
precompute_borrows_out_of_scope(
103113
mir, regioncx, borrows_out_of_scope_at_location,
104-
borrow_index, borrow_region, unwind_block.start_location()
114+
borrow_index, borrow_region, unwind_block.start_location(),
115+
visited_locations
105116
);
106117
}
107118
},
108119
TerminatorKind::Call { ref destination, cleanup, .. } => {
109120
if let Some((_, block)) = destination {
110121
precompute_borrows_out_of_scope(
111122
mir, regioncx, borrows_out_of_scope_at_location,
112-
borrow_index, borrow_region, block.start_location()
123+
borrow_index, borrow_region, block.start_location(),
124+
visited_locations
113125
);
114126
}
115127

116128
if let Some(block) = cleanup {
117129
precompute_borrows_out_of_scope(
118130
mir, regioncx, borrows_out_of_scope_at_location,
119-
borrow_index, borrow_region, block.start_location()
131+
borrow_index, borrow_region, block.start_location(),
132+
visited_locations
120133
);
121134
}
122135
},
123136
TerminatorKind::Assert { target, cleanup, .. } |
124137
TerminatorKind::Yield { resume: target, drop: cleanup, .. } => {
125138
precompute_borrows_out_of_scope(
126139
mir, regioncx, borrows_out_of_scope_at_location,
127-
borrow_index, borrow_region, target.start_location()
140+
borrow_index, borrow_region, target.start_location(),
141+
visited_locations
128142
);
129143

130144
if let Some(block) = cleanup {
131145
precompute_borrows_out_of_scope(
132146
mir, regioncx, borrows_out_of_scope_at_location,
133-
borrow_index, borrow_region, block.start_location()
147+
borrow_index, borrow_region, block.start_location(),
148+
visited_locations
134149
);
135150
}
136151
},
@@ -142,7 +157,7 @@ fn precompute_borrows_out_of_scope<'a, 'tcx>(
142157
} else {
143158
precompute_borrows_out_of_scope(mir, regioncx, borrows_out_of_scope_at_location,
144159
borrow_index, borrow_region,
145-
location.successor_within_block());
160+
location.successor_within_block(), visited_locations);
146161
}
147162
}
148163

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

168183
precompute_borrows_out_of_scope(mir, &nonlexical_regioncx,
169184
&mut borrows_out_of_scope_at_location,
170-
borrow_index, borrow_region, location);
185+
borrow_index, borrow_region, location,
186+
&mut Vec::new());
171187
}
172188

173189
Borrows {

0 commit comments

Comments
 (0)