extract `region_value_str` helper

This commit is contained in:
Niko Matsakis 2018-07-23 22:14:56 +03:00
parent f277b394af
commit 2fda456ddf
3 changed files with 73 additions and 77 deletions

View File

@ -12,7 +12,7 @@ use borrow_check::borrow_set::BorrowSet;
use borrow_check::location::LocationTable;
use borrow_check::nll::ToRegionVid;
use borrow_check::nll::facts::AllFacts;
use borrow_check::nll::region_infer::values::{RegionValueElements, RegionValues};
use borrow_check::nll::region_infer::values::RegionValues;
use rustc::infer::InferCtxt;
use rustc::mir::visit::TyContext;
use rustc::mir::visit::Visitor;
@ -24,7 +24,6 @@ use rustc::ty::{self, CanonicalTy, ClosureSubsts, GeneratorSubsts, RegionVid};
pub(super) fn generate_constraints<'cx, 'gcx, 'tcx>(
infcx: &InferCtxt<'cx, 'gcx, 'tcx>,
elements: &RegionValueElements,
liveness_constraints: &mut RegionValues<RegionVid>,
all_facts: &mut Option<AllFacts>,
location_table: &LocationTable,
@ -37,7 +36,6 @@ pub(super) fn generate_constraints<'cx, 'gcx, 'tcx>(
liveness_constraints,
location_table,
all_facts,
elements,
};
for (bb, data) in mir.basic_blocks().iter_enumerated() {
@ -52,7 +50,6 @@ struct ConstraintGeneration<'cg, 'cx: 'cg, 'gcx: 'tcx, 'tcx: 'cx> {
location_table: &'cg LocationTable,
liveness_constraints: &'cg mut RegionValues<RegionVid>,
borrow_set: &'cg BorrowSet<'tcx>,
elements: &'cg RegionValueElements,
}
impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx, 'tcx> {
@ -205,7 +202,7 @@ impl<'cx, 'cg, 'gcx, 'tcx> ConstraintGeneration<'cx, 'cg, 'gcx, 'tcx> {
.tcx
.for_each_free_region(&live_ty, |live_region| {
let vid = live_region.to_region_vid();
self.liveness_constraints.add_element(&self.elements, vid, location);
self.liveness_constraints.add_element(vid, location);
});
}
}

View File

@ -143,7 +143,6 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
constraint_generation::generate_constraints(
infcx,
&elements,
&mut liveness_constraints,
&mut all_facts,
location_table,

View File

@ -192,11 +192,7 @@ impl<N: Idx> RegionValues<N> {
/// True if `sup_region` contains all the CFG points that
/// `sub_region` contains. Ignores universal regions.
crate fn contains_points(
&self,
sup_region: N,
sub_region: N,
) -> bool {
crate fn contains_points(&self, sup_region: N, sub_region: N) -> bool {
// This could be done faster by comparing the bitsets. But I
// am lazy.
if let Some(sub_row) = self.points.row(sub_region) {
@ -252,72 +248,7 @@ impl<N: Idx> RegionValues<N> {
/// Returns a "pretty" string value of the region. Meant for debugging.
crate fn region_value_str(&self, r: N) -> String {
let mut result = String::new();
result.push_str("{");
// Set to Some(l1, l2) when we have observed all the locations
// from l1..=l2 (inclusive) but not yet printed them. This
// gets extended if we then see l3 where l3 is the successor
// to l2.
let mut open_location: Option<(Location, Location)> = None;
let mut sep = "";
let mut push_sep = |s: &mut String| {
s.push_str(sep);
sep = ", ";
};
for element in self.elements_contained_in(r) {
match element {
RegionElement::Location(l) => {
if let Some((location1, location2)) = open_location {
if location2.block == l.block
&& location2.statement_index == l.statement_index - 1
{
open_location = Some((location1, l));
continue;
}
push_sep(&mut result);
Self::push_location_range(&mut result, location1, location2);
}
open_location = Some((l, l));
}
RegionElement::RootUniversalRegion(fr) => {
if let Some((location1, location2)) = open_location {
push_sep(&mut result);
Self::push_location_range(&mut result, location1, location2);
open_location = None;
}
push_sep(&mut result);
result.push_str(&format!("{:?}", fr));
}
}
}
if let Some((location1, location2)) = open_location {
push_sep(&mut result);
Self::push_location_range(&mut result, location1, location2);
}
result.push_str("}");
result
}
fn push_location_range(str: &mut String, location1: Location, location2: Location) {
if location1 == location2 {
str.push_str(&format!("{:?}", location1));
} else {
assert_eq!(location1.block, location2.block);
str.push_str(&format!(
"{:?}[{}..={}]",
location1.block, location1.statement_index, location2.statement_index
));
}
region_value_str(self.elements_contained_in(r))
}
}
@ -372,3 +303,72 @@ impl ToElementIndex for RegionVid {
values.free_regions.contains(row, self)
}
}
crate fn region_value_str(elements: impl IntoIterator<Item = RegionElement>) -> String {
let mut result = String::new();
result.push_str("{");
// Set to Some(l1, l2) when we have observed all the locations
// from l1..=l2 (inclusive) but not yet printed them. This
// gets extended if we then see l3 where l3 is the successor
// to l2.
let mut open_location: Option<(Location, Location)> = None;
let mut sep = "";
let mut push_sep = |s: &mut String| {
s.push_str(sep);
sep = ", ";
};
for element in elements {
match element {
RegionElement::Location(l) => {
if let Some((location1, location2)) = open_location {
if location2.block == l.block
&& location2.statement_index == l.statement_index - 1
{
open_location = Some((location1, l));
continue;
}
push_sep(&mut result);
push_location_range(&mut result, location1, location2);
}
open_location = Some((l, l));
}
RegionElement::RootUniversalRegion(fr) => {
if let Some((location1, location2)) = open_location {
push_sep(&mut result);
push_location_range(&mut result, location1, location2);
open_location = None;
}
push_sep(&mut result);
result.push_str(&format!("{:?}", fr));
}
}
}
if let Some((location1, location2)) = open_location {
push_sep(&mut result);
push_location_range(&mut result, location1, location2);
}
result.push_str("}");
return result;
fn push_location_range(str: &mut String, location1: Location, location2: Location) {
if location1 == location2 {
str.push_str(&format!("{:?}", location1));
} else {
assert_eq!(location1.block, location2.block);
str.push_str(&format!(
"{:?}[{}..={}]",
location1.block, location1.statement_index, location2.statement_index
));
}
}
}