From 4e5fe71ca65020642361c4d3b3a3c2401864d5a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 13 Nov 2023 14:39:32 +0000 Subject: [PATCH 1/9] stop referring to a region as a "row" in liveness values --- .../rustc_borrowck/src/region_infer/values.rs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 38452df32e9ed..bedf130a866b2 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -138,34 +138,34 @@ impl LivenessValues { /// Adds the given element to the value for the given region. Returns whether /// the element is newly added (i.e., was not already present). - pub(crate) fn add_element(&mut self, row: N, location: Location) -> bool { - debug!("LivenessValues::add(r={:?}, location={:?})", row, location); + pub(crate) fn add_element(&mut self, region: N, location: Location) -> bool { + debug!("LivenessValues::add_element(region={:?}, location={:?})", region, location); let index = self.elements.point_from_location(location); - self.points.insert(row, index) + self.points.insert(region, index) } /// Adds all the elements in the given bit array into the given /// region. Returns whether any of them are newly added. - pub(crate) fn add_elements(&mut self, row: N, locations: &IntervalSet) -> bool { - debug!("LivenessValues::add_elements(row={:?}, locations={:?})", row, locations); - self.points.union_row(row, locations) + pub(crate) fn add_elements(&mut self, region: N, locations: &IntervalSet) -> bool { + debug!("LivenessValues::add_elements(region={:?}, locations={:?})", region, locations); + self.points.union_row(region, locations) } - /// Adds all the control-flow points to the values for `r`. - pub(crate) fn add_all_points(&mut self, row: N) { - self.points.insert_all_into_row(row); + /// Records `region` as being live at all the control-flow points. + pub(crate) fn add_all_points(&mut self, region: N) { + self.points.insert_all_into_row(region); } - /// Returns `true` if the region `r` contains the given element. - pub(crate) fn contains(&self, row: N, location: Location) -> bool { + /// Returns `true` if the region `region` contains the given element. + pub(crate) fn contains(&self, region: N, location: Location) -> bool { let index = self.elements.point_from_location(location); - self.points.row(row).is_some_and(|r| r.contains(index)) + self.points.row(region).is_some_and(|r| r.contains(index)) } - /// Returns an iterator of all the elements contained by the region `r` - pub(crate) fn get_elements(&self, row: N) -> impl Iterator + '_ { + /// Returns an iterator of all the elements contained by `region`. + pub(crate) fn get_elements(&self, region: N) -> impl Iterator + '_ { self.points - .row(row) + .row(region) .into_iter() .flat_map(|set| set.iter()) .take_while(move |&p| self.elements.point_in_range(p)) @@ -173,8 +173,8 @@ impl LivenessValues { } /// Returns a "pretty" string value of the region. Meant for debugging. - pub(crate) fn region_value_str(&self, r: N) -> String { - region_value_str(self.get_elements(r).map(RegionElement::Location)) + pub(crate) fn region_value_str(&self, region: N) -> String { + region_value_str(self.get_elements(region).map(RegionElement::Location)) } #[inline] From 3e3e7a023fa7b4d3ea11009565b09b1ec64c2b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 13 Nov 2023 14:44:17 +0000 Subject: [PATCH 2/9] iterate over regions and not "rows" in liveness values --- compiler/rustc_borrowck/src/region_infer/mod.rs | 2 +- compiler/rustc_borrowck/src/region_infer/values.rs | 2 +- compiler/rustc_borrowck/src/type_check/mod.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index b1f91a0562822..aeb9f935ecc17 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -359,7 +359,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { let mut scc_values = RegionValues::new(elements, universal_regions.len(), &placeholder_indices); - for region in liveness_constraints.rows() { + for region in liveness_constraints.regions() { let scc = constraint_sccs.scc(region); scc_values.merge_liveness(scc, region, &liveness_constraints); } diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index bedf130a866b2..48bfd6bd13241 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -132,7 +132,7 @@ impl LivenessValues { } /// Iterate through each region that has a value in this set. - pub(crate) fn rows(&self) -> impl Iterator { + pub(crate) fn regions(&self) -> impl Iterator { self.points.rows() } diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 608d010394f66..96c23cfaf77d2 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -592,7 +592,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> { } self.cx.borrowck_context.constraints.outlives_constraints.push(constraint) } - for region in liveness_constraints.rows() { + for region in liveness_constraints.regions() { // If the region is live at at least one location in the promoted MIR, // then add a liveness constraint to the main MIR for this region // at the location provided as an argument to this method From fb94626431e2637306aede1095cb66e6bbb43777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 13 Nov 2023 14:49:16 +0000 Subject: [PATCH 3/9] add locations instead of "element"s, and remove unused return value --- compiler/rustc_borrowck/src/constraint_generation.rs | 2 +- compiler/rustc_borrowck/src/region_infer/values.rs | 11 +++++------ compiler/rustc_borrowck/src/type_check/mod.rs | 6 +++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_borrowck/src/constraint_generation.rs b/compiler/rustc_borrowck/src/constraint_generation.rs index 1f642099f0899..02b3bed94bfcd 100644 --- a/compiler/rustc_borrowck/src/constraint_generation.rs +++ b/compiler/rustc_borrowck/src/constraint_generation.rs @@ -167,7 +167,7 @@ impl<'cx, 'tcx> ConstraintGeneration<'cx, 'tcx> { self.infcx.tcx.for_each_free_region(&live_ty, |live_region| { let vid = live_region.as_var(); - self.liveness_constraints.add_element(vid, location); + self.liveness_constraints.add_location(vid, location); }); } diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 48bfd6bd13241..03630f0e2bcbc 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -136,12 +136,11 @@ impl LivenessValues { self.points.rows() } - /// Adds the given element to the value for the given region. Returns whether - /// the element is newly added (i.e., was not already present). - pub(crate) fn add_element(&mut self, region: N, location: Location) -> bool { - debug!("LivenessValues::add_element(region={:?}, location={:?})", region, location); - let index = self.elements.point_from_location(location); - self.points.insert(region, index) + /// Records `region` as being live at the given `location`. + pub(crate) fn add_location(&mut self, region: N, location: Location) { + debug!("LivenessValues::add_location(region={:?}, location={:?})", region, location); + let point = self.elements.point_from_location(location); + self.points.insert(region, point); } /// Adds all the elements in the given bit array into the given diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 96c23cfaf77d2..f00e41e08863a 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -318,7 +318,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> { .borrowck_context .constraints .liveness_constraints - .add_element(live_region_vid, location); + .add_location(live_region_vid, location); }); // HACK(compiler-errors): Constants that are gathered into Body.required_consts @@ -601,7 +601,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> { .borrowck_context .constraints .liveness_constraints - .add_element(region, location); + .add_location(region, location); } } } @@ -1443,7 +1443,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { self.borrowck_context .constraints .liveness_constraints - .add_element(region_vid, term_location); + .add_location(region_vid, term_location); } self.check_call_inputs(body, term, func, &sig, args, term_location, *call_source); From c8feeb6ced8b934fea40d5d4e99527e6bb6e8368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 13 Nov 2023 14:54:07 +0000 Subject: [PATCH 4/9] refer to points and not "elements", and remove unused return value --- compiler/rustc_borrowck/src/region_infer/values.rs | 9 ++++----- compiler/rustc_borrowck/src/type_check/liveness/trace.rs | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 03630f0e2bcbc..2e15dee0c81f6 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -143,11 +143,10 @@ impl LivenessValues { self.points.insert(region, point); } - /// Adds all the elements in the given bit array into the given - /// region. Returns whether any of them are newly added. - pub(crate) fn add_elements(&mut self, region: N, locations: &IntervalSet) -> bool { - debug!("LivenessValues::add_elements(region={:?}, locations={:?})", region, locations); - self.points.union_row(region, locations) + /// Records `region` as being live at all the given `points`. + pub(crate) fn add_points(&mut self, region: N, points: &IntervalSet) { + debug!("LivenessValues::add_points(region={:?}, points={:?})", region, points); + self.points.union_row(region, points); } /// Records `region` as being live at all the control-flow points. diff --git a/compiler/rustc_borrowck/src/type_check/liveness/trace.rs b/compiler/rustc_borrowck/src/type_check/liveness/trace.rs index e616449ccd412..2cfbe8a387465 100644 --- a/compiler/rustc_borrowck/src/type_check/liveness/trace.rs +++ b/compiler/rustc_borrowck/src/type_check/liveness/trace.rs @@ -618,7 +618,7 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> { .borrowck_context .constraints .liveness_constraints - .add_elements(live_region_vid, live_at); + .add_points(live_region_vid, live_at); // There can only be inflowing loans for this region when we are using // `-Zpolonius=next`. From 23bc9c6a624b2a8a517f597341e8277c6dce85e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 13 Nov 2023 14:58:16 +0000 Subject: [PATCH 5/9] regions do not contain liveness elements --- compiler/rustc_borrowck/src/region_infer/mod.rs | 8 ++++---- compiler/rustc_borrowck/src/region_infer/values.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index aeb9f935ecc17..b9a824c1401af 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -1963,15 +1963,15 @@ impl<'tcx> RegionInferenceContext<'tcx> { None } - /// Finds some region R such that `fr1: R` and `R` is live at `elem`. + /// Finds some region R such that `fr1: R` and `R` is live at `location`. #[instrument(skip(self), level = "trace", ret)] - pub(crate) fn find_sub_region_live_at(&self, fr1: RegionVid, elem: Location) -> RegionVid { + pub(crate) fn find_sub_region_live_at(&self, fr1: RegionVid, location: Location) -> RegionVid { trace!(scc = ?self.constraint_sccs.scc(fr1)); trace!(universe = ?self.scc_universes[self.constraint_sccs.scc(fr1)]); self.find_constraint_paths_between_regions(fr1, |r| { - // First look for some `r` such that `fr1: r` and `r` is live at `elem` + // First look for some `r` such that `fr1: r` and `r` is live at `location` trace!(?r, liveness_constraints=?self.liveness_constraints.region_value_str(r)); - self.liveness_constraints.contains(r, elem) + self.liveness_constraints.is_live_at(r, location) }) .or_else(|| { // If we fail to find that, we may find some `r` such that diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 2e15dee0c81f6..14df7d0553bd5 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -154,10 +154,10 @@ impl LivenessValues { self.points.insert_all_into_row(region); } - /// Returns `true` if the region `region` contains the given element. - pub(crate) fn contains(&self, region: N, location: Location) -> bool { - let index = self.elements.point_from_location(location); - self.points.row(region).is_some_and(|r| r.contains(index)) + /// Returns whether `region` is marked live at the given `location`. + pub(crate) fn is_live_at(&self, region: N, location: Location) -> bool { + let point = self.elements.point_from_location(location); + self.points.row(region).is_some_and(|r| r.contains(point)) } /// Returns an iterator of all the elements contained by `region`. From b4c7d1dd05ed839ab80d5fe0452d890743a7ae5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 13 Nov 2023 15:07:35 +0000 Subject: [PATCH 6/9] fix doc --- compiler/rustc_borrowck/src/region_infer/values.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 14df7d0553bd5..502e884b531d2 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -116,17 +116,15 @@ pub(crate) enum RegionElement { PlaceholderRegion(ty::PlaceholderRegion), } -/// When we initially compute liveness, we use an interval matrix storing -/// liveness ranges for each region-vid. +/// Records the CFG locations where each region is live. When we initially compute liveness, we use +/// an interval matrix storing liveness ranges for each region-vid. pub(crate) struct LivenessValues { elements: Rc, points: SparseIntervalMatrix, } impl LivenessValues { - /// Creates a new set of "region values" that tracks causal information. - /// Each of the regions in num_region_variables will be initialized with an - /// empty set of points and no causal information. + /// Create an empty map of regions to locations where they're live. pub(crate) fn new(elements: Rc) -> Self { Self { points: SparseIntervalMatrix::new(elements.num_points), elements } } From 79c5e913d3de11566cf60fa1d069141639ee913f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 13 Nov 2023 15:18:47 +0000 Subject: [PATCH 7/9] introduce `is_live_anywhere` instead of peeking into points and refactor misnamed `get_elements` --- .../rustc_borrowck/src/region_infer/values.rs | 16 +++++++++++----- compiler/rustc_borrowck/src/type_check/mod.rs | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 502e884b531d2..d0e24f7817ad4 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -158,19 +158,25 @@ impl LivenessValues { self.points.row(region).is_some_and(|r| r.contains(point)) } - /// Returns an iterator of all the elements contained by `region`. - pub(crate) fn get_elements(&self, region: N) -> impl Iterator + '_ { + /// Returns whether `region` is marked live at any location. + pub(crate) fn is_live_anywhere(&self, region: N) -> bool { + self.live_points(region).next().is_some() + } + + /// Returns an iterator of all the points where `region` is live. + fn live_points(&self, region: N) -> impl Iterator + '_ { self.points .row(region) .into_iter() .flat_map(|set| set.iter()) - .take_while(move |&p| self.elements.point_in_range(p)) - .map(move |p| self.elements.to_location(p)) + .take_while(|&p| self.elements.point_in_range(p)) } /// Returns a "pretty" string value of the region. Meant for debugging. pub(crate) fn region_value_str(&self, region: N) -> String { - region_value_str(self.get_elements(region).map(RegionElement::Location)) + region_value_str( + self.live_points(region).map(|p| RegionElement::Location(self.elements.to_location(p))), + ) } #[inline] diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index f00e41e08863a..e23f633bea2b6 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -596,7 +596,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> { // If the region is live at at least one location in the promoted MIR, // then add a liveness constraint to the main MIR for this region // at the location provided as an argument to this method - if liveness_constraints.get_elements(region).next().is_some() { + if liveness_constraints.is_live_anywhere(region) { self.cx .borrowck_context .constraints From 84a002cc4299b387a5f4b234f50155f8ea3c900d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 13 Nov 2023 15:28:26 +0000 Subject: [PATCH 8/9] `LivenessValues` does not need to be generic over regions --- .../src/constraint_generation.rs | 6 ++--- .../rustc_borrowck/src/region_infer/mod.rs | 4 ++-- .../rustc_borrowck/src/region_infer/values.rs | 24 +++++++++---------- compiler/rustc_borrowck/src/type_check/mod.rs | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_borrowck/src/constraint_generation.rs b/compiler/rustc_borrowck/src/constraint_generation.rs index 02b3bed94bfcd..21d367c40cb9c 100644 --- a/compiler/rustc_borrowck/src/constraint_generation.rs +++ b/compiler/rustc_borrowck/src/constraint_generation.rs @@ -9,7 +9,7 @@ use rustc_middle::mir::{ }; use rustc_middle::ty::visit::TypeVisitable; use rustc_middle::ty::GenericArgsRef; -use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt}; +use rustc_middle::ty::{self, Ty, TyCtxt}; use crate::{ borrow_set::BorrowSet, facts::AllFacts, location::LocationTable, places_conflict, @@ -18,7 +18,7 @@ use crate::{ pub(super) fn generate_constraints<'tcx>( infcx: &InferCtxt<'tcx>, - liveness_constraints: &mut LivenessValues, + liveness_constraints: &mut LivenessValues, all_facts: &mut Option, location_table: &LocationTable, body: &Body<'tcx>, @@ -43,7 +43,7 @@ struct ConstraintGeneration<'cg, 'tcx> { infcx: &'cg InferCtxt<'tcx>, all_facts: &'cg mut Option, location_table: &'cg LocationTable, - liveness_constraints: &'cg mut LivenessValues, + liveness_constraints: &'cg mut LivenessValues, borrow_set: &'cg BorrowSet<'tcx>, body: &'cg Body<'tcx>, } diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index b9a824c1401af..ff125a0274516 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -59,7 +59,7 @@ pub struct RegionInferenceContext<'tcx> { /// regions, these start out empty and steadily grow, though for /// each universally quantified region R they start out containing /// the entire CFG and `end(R)`. - liveness_constraints: LivenessValues, + liveness_constraints: LivenessValues, /// The outlives constraints computed by the type-check. constraints: Frozen>, @@ -332,7 +332,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { member_constraints_in: MemberConstraintSet<'tcx, RegionVid>, universe_causes: FxIndexMap>, type_tests: Vec>, - liveness_constraints: LivenessValues, + liveness_constraints: LivenessValues, elements: &Rc, live_loans: SparseBitMatrix, ) -> Self { diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index d0e24f7817ad4..e73872bf66ae2 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -118,53 +118,53 @@ pub(crate) enum RegionElement { /// Records the CFG locations where each region is live. When we initially compute liveness, we use /// an interval matrix storing liveness ranges for each region-vid. -pub(crate) struct LivenessValues { +pub(crate) struct LivenessValues { elements: Rc, - points: SparseIntervalMatrix, + points: SparseIntervalMatrix, } -impl LivenessValues { +impl LivenessValues { /// Create an empty map of regions to locations where they're live. pub(crate) fn new(elements: Rc) -> Self { Self { points: SparseIntervalMatrix::new(elements.num_points), elements } } /// Iterate through each region that has a value in this set. - pub(crate) fn regions(&self) -> impl Iterator { + pub(crate) fn regions(&self) -> impl Iterator { self.points.rows() } /// Records `region` as being live at the given `location`. - pub(crate) fn add_location(&mut self, region: N, location: Location) { + pub(crate) fn add_location(&mut self, region: RegionVid, location: Location) { debug!("LivenessValues::add_location(region={:?}, location={:?})", region, location); let point = self.elements.point_from_location(location); self.points.insert(region, point); } /// Records `region` as being live at all the given `points`. - pub(crate) fn add_points(&mut self, region: N, points: &IntervalSet) { + pub(crate) fn add_points(&mut self, region: RegionVid, points: &IntervalSet) { debug!("LivenessValues::add_points(region={:?}, points={:?})", region, points); self.points.union_row(region, points); } /// Records `region` as being live at all the control-flow points. - pub(crate) fn add_all_points(&mut self, region: N) { + pub(crate) fn add_all_points(&mut self, region: RegionVid) { self.points.insert_all_into_row(region); } /// Returns whether `region` is marked live at the given `location`. - pub(crate) fn is_live_at(&self, region: N, location: Location) -> bool { + pub(crate) fn is_live_at(&self, region: RegionVid, location: Location) -> bool { let point = self.elements.point_from_location(location); self.points.row(region).is_some_and(|r| r.contains(point)) } /// Returns whether `region` is marked live at any location. - pub(crate) fn is_live_anywhere(&self, region: N) -> bool { + pub(crate) fn is_live_anywhere(&self, region: RegionVid) -> bool { self.live_points(region).next().is_some() } /// Returns an iterator of all the points where `region` is live. - fn live_points(&self, region: N) -> impl Iterator + '_ { + fn live_points(&self, region: RegionVid) -> impl Iterator + '_ { self.points .row(region) .into_iter() @@ -173,7 +173,7 @@ impl LivenessValues { } /// Returns a "pretty" string value of the region. Meant for debugging. - pub(crate) fn region_value_str(&self, region: N) -> String { + pub(crate) fn region_value_str(&self, region: RegionVid) -> String { region_value_str( self.live_points(region).map(|p| RegionElement::Location(self.elements.to_location(p))), ) @@ -309,7 +309,7 @@ impl RegionValues { /// `self[to] |= values[from]`, essentially: that is, take all the /// elements for the region `from` from `values` and add them to /// the region `to` in `self`. - pub(crate) fn merge_liveness(&mut self, to: N, from: M, values: &LivenessValues) { + pub(crate) fn merge_liveness(&mut self, to: N, from: RegionVid, values: &LivenessValues) { if let Some(set) = values.points.row(from) { self.points.union_row(to, set); } diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index e23f633bea2b6..f0e35bdb75f55 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -899,7 +899,7 @@ pub(crate) struct MirTypeckRegionConstraints<'tcx> { /// not otherwise appear in the MIR -- in particular, the /// late-bound regions that it instantiates at call-sites -- and /// hence it must report on their liveness constraints. - pub(crate) liveness_constraints: LivenessValues, + pub(crate) liveness_constraints: LivenessValues, pub(crate) outlives_constraints: OutlivesConstraintSet<'tcx>, From d203476c4f25895205dfaeb11519339b2ecd1867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Mon, 13 Nov 2023 15:35:04 +0000 Subject: [PATCH 9/9] rename debugging support functions --- .../rustc_borrowck/src/region_infer/dump_mir.rs | 2 +- compiler/rustc_borrowck/src/region_infer/mod.rs | 2 +- .../rustc_borrowck/src/region_infer/values.rs | 17 ++++++++++------- .../src/type_check/liveness/trace.rs | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/dump_mir.rs b/compiler/rustc_borrowck/src/region_infer/dump_mir.rs index 4d620ac9de615..cfbb2766c3397 100644 --- a/compiler/rustc_borrowck/src/region_infer/dump_mir.rs +++ b/compiler/rustc_borrowck/src/region_infer/dump_mir.rs @@ -67,7 +67,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { with_msg: &mut dyn FnMut(&str) -> io::Result<()>, ) -> io::Result<()> { for region in self.definitions.indices() { - let value = self.liveness_constraints.region_value_str(region); + let value = self.liveness_constraints.pretty_print_live_points(region); if value != "{}" { with_msg(&format!("{region:?} live at {value}"))?; } diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index ff125a0274516..f25376c248fbf 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -1970,7 +1970,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { trace!(universe = ?self.scc_universes[self.constraint_sccs.scc(fr1)]); self.find_constraint_paths_between_regions(fr1, |r| { // First look for some `r` such that `fr1: r` and `r` is live at `location` - trace!(?r, liveness_constraints=?self.liveness_constraints.region_value_str(r)); + trace!(?r, liveness_constraints=?self.liveness_constraints.pretty_print_live_points(r)); self.liveness_constraints.is_live_at(r, location) }) .or_else(|| { diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index e73872bf66ae2..93f5fbd003961 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -172,9 +172,10 @@ impl LivenessValues { .take_while(|&p| self.elements.point_in_range(p)) } - /// Returns a "pretty" string value of the region. Meant for debugging. - pub(crate) fn region_value_str(&self, region: RegionVid) -> String { - region_value_str( + /// For debugging purposes, returns a pretty-printed string of the points where the `region` is + /// live. + pub(crate) fn pretty_print_live_points(&self, region: RegionVid) -> String { + pretty_print_region_elements( self.live_points(region).map(|p| RegionElement::Location(self.elements.to_location(p))), ) } @@ -378,7 +379,7 @@ impl RegionValues { /// Returns a "pretty" string value of the region. Meant for debugging. pub(crate) fn region_value_str(&self, r: N) -> String { - region_value_str(self.elements_contained_in(r)) + pretty_print_region_elements(self.elements_contained_in(r)) } } @@ -422,11 +423,12 @@ impl ToElementIndex for ty::PlaceholderRegion { } } -pub(crate) fn location_set_str( +/// For debugging purposes, returns a pretty-printed string of the given points. +pub(crate) fn pretty_print_points( elements: &RegionValueElements, points: impl IntoIterator, ) -> String { - region_value_str( + pretty_print_region_elements( points .into_iter() .take_while(|&p| elements.point_in_range(p)) @@ -435,7 +437,8 @@ pub(crate) fn location_set_str( ) } -fn region_value_str(elements: impl IntoIterator) -> String { +/// For debugging purposes, returns a pretty-printed string of the given region elements. +fn pretty_print_region_elements(elements: impl IntoIterator) -> String { let mut result = String::new(); result.push('{'); diff --git a/compiler/rustc_borrowck/src/type_check/liveness/trace.rs b/compiler/rustc_borrowck/src/type_check/liveness/trace.rs index 2cfbe8a387465..2ef4cf6768235 100644 --- a/compiler/rustc_borrowck/src/type_check/liveness/trace.rs +++ b/compiler/rustc_borrowck/src/type_check/liveness/trace.rs @@ -550,7 +550,7 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> { dropped_local, dropped_ty, drop_locations, - values::location_set_str(self.elements, live_at.iter()), + values::pretty_print_points(self.elements, live_at.iter()), ); let drop_data = self.drop_data.entry(dropped_ty).or_insert_with({ @@ -599,7 +599,7 @@ impl<'tcx> LivenessContext<'_, '_, '_, 'tcx> { debug!("make_all_regions_live(value={:?})", value); debug!( "make_all_regions_live: live_at={}", - values::location_set_str(elements, live_at.iter()), + values::pretty_print_points(elements, live_at.iter()), ); // When using `-Zpolonius=next`, we want to record the loans that flow into this value's