Skip to content

Commit cef59aa

Browse files
committed
make RegionValues generic over its domain
We used to store one value per RegionVid; we will soon be storing one value per SCC.
1 parent 0e286bb commit cef59aa

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ pub struct RegionInferenceContext<'tcx> {
5353
/// regions, these start out empty and steadily grow, though for
5454
/// each universally quantified region R they start out containing
5555
/// the entire CFG and `end(R)`.
56-
liveness_constraints: RegionValues,
56+
liveness_constraints: RegionValues<RegionVid>,
5757

5858
/// The final inferred values of the inference variables; `None`
5959
/// until `solve` is invoked.
60-
inferred_values: Option<RegionValues>,
60+
inferred_values: Option<RegionValues<RegionVid>>,
6161

6262
/// The constraints we have accumulated and used during solving.
6363
constraints: ConstraintSet,
@@ -405,7 +405,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
405405
self.inferred_values = Some(inferred_values);
406406
}
407407

408-
fn compute_region_values(&self, _mir: &Mir<'tcx>) -> RegionValues {
408+
fn compute_region_values(&self, _mir: &Mir<'tcx>) -> RegionValues<RegionVid> {
409409
debug!("compute_region_values()");
410410
debug!("compute_region_values: constraints={:#?}", {
411411
let mut constraints: Vec<_> = self.constraints.iter().collect();

src/librustc_mir/borrow_check/nll/region_infer/values.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,20 @@ impl ToElementIndex for RegionElementIndex {
180180
/// compact `SparseBitMatrix` representation, with one row per region
181181
/// variable. The columns consist of either universal regions or
182182
/// points in the CFG.
183-
pub(super) struct RegionValues {
183+
pub(super) struct RegionValues<N: Idx> {
184184
elements: Rc<RegionValueElements>,
185-
matrix: SparseBitMatrix<RegionVid, RegionElementIndex>,
185+
matrix: SparseBitMatrix<N, RegionElementIndex>,
186186

187187
/// If cause tracking is enabled, maps from a pair (r, e)
188188
/// consisting of a region `r` that contains some element `e` to
189189
/// the reason that the element is contained. There should be an
190190
/// entry for every bit set to 1 in `SparseBitMatrix`.
191-
causes: Option<CauseMap>,
191+
causes: Option<CauseMap<N>>,
192192
}
193193

194-
type CauseMap = FxHashMap<(RegionVid, RegionElementIndex), Cause>;
194+
type CauseMap<N> = FxHashMap<(N, RegionElementIndex), Cause>;
195195

196-
impl RegionValues {
196+
impl<N: Idx> RegionValues<N> {
197197
/// Creates a new set of "region values" that tracks causal information.
198198
/// Each of the regions in num_region_variables will be initialized with an
199199
/// empty set of points and no causal information.
@@ -206,7 +206,7 @@ impl RegionValues {
206206
Self {
207207
elements: elements.clone(),
208208
matrix: SparseBitMatrix::new(
209-
RegionVid::new(num_region_variables),
209+
N::new(num_region_variables),
210210
RegionElementIndex::new(elements.num_elements()),
211211
),
212212
causes: Some(CauseMap::default()),
@@ -235,7 +235,7 @@ impl RegionValues {
235235
/// the element is newly added (i.e., was not already present).
236236
pub(super) fn add_element<E: ToElementIndex>(
237237
&mut self,
238-
r: RegionVid,
238+
r: N,
239239
elem: E,
240240
cause: &Cause,
241241
) -> bool {
@@ -245,18 +245,20 @@ impl RegionValues {
245245

246246
/// Add all elements in `r_from` to `r_to` (because e.g. `r_to:
247247
/// r_from`).
248-
pub(super) fn add_region(&mut self, r_to: RegionVid, r_from: RegionVid) -> bool {
248+
pub(super) fn add_region(&mut self, r_to: N, r_from: N) -> bool {
249249
self.matrix.merge(r_from, r_to)
250250
}
251251

252252
/// Internal method to add an element to a region.
253253
///
254254
/// Takes a "lazy" cause -- this function will return the cause, but it will only
255255
/// be invoked if cause tracking is enabled.
256-
fn add_internal<F>(&mut self, r: RegionVid, i: RegionElementIndex, make_cause: F) -> bool
257-
where
258-
F: FnOnce(&CauseMap) -> Cause,
259-
{
256+
fn add_internal(
257+
&mut self,
258+
r: N,
259+
i: RegionElementIndex,
260+
make_cause: impl FnOnce(&CauseMap<N>) -> Cause,
261+
) -> bool {
260262
if self.matrix.add(r, i) {
261263
debug!("add(r={:?}, i={:?})", r, self.elements.to_element(i));
262264

@@ -284,14 +286,14 @@ impl RegionValues {
284286
}
285287

286288
/// True if the region `r` contains the given element.
287-
pub(super) fn contains<E: ToElementIndex>(&self, r: RegionVid, elem: E) -> bool {
289+
pub(super) fn contains<E: ToElementIndex>(&self, r: N, elem: E) -> bool {
288290
let i = self.elements.index(elem);
289291
self.matrix.contains(r, i)
290292
}
291293

292294
/// True if `sup_region` contains all the CFG points that
293295
/// `sub_region` contains. Ignores universal regions.
294-
pub(super) fn contains_points(&self, sup_region: RegionVid, sub_region: RegionVid) -> bool {
296+
pub(super) fn contains_points(&self, sup_region: N, sub_region: N) -> bool {
295297
// This could be done faster by comparing the bitsets. But I
296298
// am lazy.
297299
self.element_indices_contained_in(sub_region)
@@ -304,15 +306,15 @@ impl RegionValues {
304306
/// `elements_contained_in`.
305307
pub(super) fn element_indices_contained_in<'a>(
306308
&'a self,
307-
r: RegionVid,
309+
r: N,
308310
) -> impl Iterator<Item = RegionElementIndex> + 'a {
309311
self.matrix.iter(r).map(move |i| i)
310312
}
311313

312314
/// Returns just the universal regions that are contained in a given region's value.
313315
pub(super) fn universal_regions_outlived_by<'a>(
314316
&'a self,
315-
r: RegionVid,
317+
r: N,
316318
) -> impl Iterator<Item = RegionVid> + 'a {
317319
self.element_indices_contained_in(r)
318320
.map(move |i| self.elements.to_universal_region(i))
@@ -323,14 +325,14 @@ impl RegionValues {
323325
/// Returns all the elements contained in a given region's value.
324326
pub(super) fn elements_contained_in<'a>(
325327
&'a self,
326-
r: RegionVid,
328+
r: N,
327329
) -> impl Iterator<Item = RegionElement> + 'a {
328330
self.element_indices_contained_in(r)
329331
.map(move |r| self.elements.to_element(r))
330332
}
331333

332334
/// Returns a "pretty" string value of the region. Meant for debugging.
333-
pub(super) fn region_value_str(&self, r: RegionVid) -> String {
335+
pub(super) fn region_value_str(&self, r: N) -> String {
334336
let mut result = String::new();
335337
result.push_str("{");
336338

@@ -404,7 +406,7 @@ impl RegionValues {
404406
///
405407
/// Returns None if cause tracking is disabled or `elem` is not
406408
/// actually found in `r`.
407-
pub(super) fn cause<T: ToElementIndex>(&self, r: RegionVid, elem: T) -> Option<Cause> {
409+
pub(super) fn cause<T: ToElementIndex>(&self, r: N, elem: T) -> Option<Cause> {
408410
let index = self.elements.index(elem);
409411
if let Some(causes) = &self.causes {
410412
causes.get(&(r, index)).cloned()

0 commit comments

Comments
 (0)