From: Wesley Wiser Date: Sat, 25 Aug 2018 03:11:44 +0000 (-0400) Subject: [nll] Refactor the `Edges` iterator to return `OutlivesConstraints` X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=db01b6789db9b4f89209ea6e28781dfd66eebbcb;p=rust.git [nll] Refactor the `Edges` iterator to return `OutlivesConstraints` Part of #53178 --- diff --git a/src/librustc_mir/borrow_check/nll/constraints/graph.rs b/src/librustc_mir/borrow_check/nll/constraints/graph.rs index 1a1094b570b..7cf94ec84dc 100644 --- a/src/librustc_mir/borrow_check/nll/constraints/graph.rs +++ b/src/librustc_mir/borrow_check/nll/constraints/graph.rs @@ -103,10 +103,15 @@ impl ConstraintGraph { } /// Given a region `R`, iterate over all constraints `R: R1`. - crate fn outgoing_edges(&self, region_sup: RegionVid) -> Edges<'_, D> { + crate fn outgoing_edges<'a>( + &'a self, + region_sup: RegionVid, + constraints: &'a ConstraintSet, + ) -> Edges<'a, D> { let first = self.first_constraints[region_sup]; Edges { graph: self, + constraints, pointer: first, } } @@ -114,16 +119,17 @@ impl ConstraintGraph { crate struct Edges<'s, D: ConstraintGraphDirecton> { graph: &'s ConstraintGraph, + constraints: &'s ConstraintSet, pointer: Option, } impl<'s, D: ConstraintGraphDirecton> Iterator for Edges<'s, D> { - type Item = ConstraintIndex; + type Item = OutlivesConstraint; fn next(&mut self) -> Option { if let Some(p) = self.pointer { self.pointer = self.graph.next_constraints[p]; - Some(p) + Some(self.constraints[p]) } else { None } @@ -154,14 +160,12 @@ impl<'s, D: ConstraintGraphDirecton> RegionGraph<'s, D> { /// there exists a constraint `R: R1`. crate fn outgoing_regions(&self, region_sup: RegionVid) -> Successors<'_, D> { Successors { - set: self.set, - edges: self.constraint_graph.outgoing_edges(region_sup), + edges: self.constraint_graph.outgoing_edges(region_sup, self.set), } } } crate struct Successors<'s, D: ConstraintGraphDirecton> { - set: &'s ConstraintSet, edges: Edges<'s, D>, } @@ -169,7 +173,7 @@ impl<'s, D: ConstraintGraphDirecton> Iterator for Successors<'s, D> { type Item = RegionVid; fn next(&mut self) -> Option { - self.edges.next().map(|c| D::end_region(&self.set[c])) + self.edges.next().map(|c| D::end_region(&c)) } } diff --git a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs index ca208a43431..2a541b6474f 100644 --- a/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs +++ b/src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs @@ -8,7 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use borrow_check::nll::region_infer::{ConstraintIndex, RegionInferenceContext}; +use borrow_check::nll::constraints::OutlivesConstraint; +use borrow_check::nll::region_infer::RegionInferenceContext; use borrow_check::nll::type_check::Locations; use rustc::hir::def_id::DefId; use rustc::infer::error_reporting::nice_region_error::NiceRegionError; @@ -53,7 +54,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { #[derive(Copy, Clone, PartialEq, Eq)] enum Trace { StartRegion, - FromConstraint(ConstraintIndex), + FromOutlivesConstraint(OutlivesConstraint), NotVisited, } @@ -80,12 +81,11 @@ fn best_blame_constraint( debug!( "best_blame_constraint: path={:#?}", path.iter() - .map(|&ci| format!( - "{:?}: {:?} ({:?}: {:?})", - ci, - &self.constraints[ci], - self.constraint_sccs.scc(self.constraints[ci].sup), - self.constraint_sccs.scc(self.constraints[ci].sub), + .map(|&c| format!( + "{:?} ({:?}: {:?})", + c, + self.constraint_sccs.scc(c.sup), + self.constraint_sccs.scc(c.sub), )) .collect::>() ); @@ -121,7 +121,7 @@ fn best_blame_constraint( // highlight (e.g., a call site or something). let target_scc = self.constraint_sccs.scc(target_region); let best_choice = (0..path.len()).rev().find(|&i| { - let constraint = &self.constraints[path[i]]; + let constraint = path[i]; let constraint_sup_scc = self.constraint_sccs.scc(constraint.sup); @@ -164,7 +164,7 @@ fn find_constraint_paths_between_regions( &self, from_region: RegionVid, target_test: impl Fn(RegionVid) -> bool, - ) -> Option<(Vec, RegionVid)> { + ) -> Option<(Vec, RegionVid)> { let mut context = IndexVec::from_elem(Trace::NotVisited, &self.definitions); context[from_region] = Trace::StartRegion; @@ -185,9 +185,9 @@ fn find_constraint_paths_between_regions( Trace::NotVisited => { bug!("found unvisited region {:?} on path to {:?}", p, r) } - Trace::FromConstraint(c) => { + Trace::FromOutlivesConstraint(c) => { result.push(c); - p = self.constraints[c].sup; + p = c.sup; } Trace::StartRegion => { @@ -201,11 +201,11 @@ fn find_constraint_paths_between_regions( // Otherwise, walk over the outgoing constraints and // enqueue any regions we find, keeping track of how we // reached them. - for constraint in self.constraint_graph.outgoing_edges(r) { - assert_eq!(self.constraints[constraint].sup, r); - let sub_region = self.constraints[constraint].sub; + for constraint in self.constraint_graph.outgoing_edges(r, &self.constraints) { + assert_eq!(constraint.sup, r); + let sub_region = constraint.sub; if let Trace::NotVisited = context[sub_region] { - context[sub_region] = Trace::FromConstraint(constraint); + context[sub_region] = Trace::FromOutlivesConstraint(constraint); deque.push_back(sub_region); } } @@ -216,8 +216,7 @@ fn find_constraint_paths_between_regions( /// This function will return true if a constraint is interesting and false if a constraint /// is not. It is useful in filtering constraint paths to only interesting points. - fn constraint_is_interesting(&self, index: ConstraintIndex) -> bool { - let constraint = self.constraints[index]; + fn constraint_is_interesting(&self, constraint: OutlivesConstraint) -> bool { debug!( "constraint_is_interesting: locations={:?} constraint={:?}", constraint.locations, constraint @@ -232,11 +231,10 @@ fn constraint_is_interesting(&self, index: ConstraintIndex) -> bool { /// This function classifies a constraint from a location. fn classify_constraint( &self, - index: ConstraintIndex, + constraint: OutlivesConstraint, mir: &Mir<'tcx>, tcx: TyCtxt<'_, '_, 'tcx>, ) -> (ConstraintCategory, Span) { - let constraint = self.constraints[index]; debug!("classify_constraint: constraint={:?}", constraint); let span = constraint.locations.span(mir); let location = constraint @@ -244,7 +242,7 @@ fn classify_constraint( .from_location() .unwrap_or(Location::START); - if !self.constraint_is_interesting(index) { + if !self.constraint_is_interesting(constraint) { return (ConstraintCategory::Boring, span); } diff --git a/src/librustc_mir/borrow_check/nll/region_infer/mod.rs b/src/librustc_mir/borrow_check/nll/region_infer/mod.rs index ff68b5987e8..4fe39ba3c95 100644 --- a/src/librustc_mir/borrow_check/nll/region_infer/mod.rs +++ b/src/librustc_mir/borrow_check/nll/region_infer/mod.rs @@ -11,7 +11,7 @@ use super::universal_regions::UniversalRegions; use borrow_check::nll::constraints::graph::NormalConstraintGraph; use borrow_check::nll::constraints::{ - ConstraintIndex, ConstraintSccIndex, ConstraintSet, OutlivesConstraint, + ConstraintSccIndex, ConstraintSet, OutlivesConstraint, }; use borrow_check::nll::region_infer::values::{RegionElement, ToElementIndex}; use borrow_check::nll::type_check::free_region_relations::UniversalRegionRelations;