]> git.lizzy.rs Git - rust.git/commitdiff
[nll] Refactor the `Edges` iterator to return `OutlivesConstraints`
authorWesley Wiser <wwiser@gmail.com>
Sat, 25 Aug 2018 03:11:44 +0000 (23:11 -0400)
committerWesley Wiser <wwiser@gmail.com>
Thu, 6 Sep 2018 23:58:22 +0000 (19:58 -0400)
Part of #53178

src/librustc_mir/borrow_check/nll/constraints/graph.rs
src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs
src/librustc_mir/borrow_check/nll/region_infer/mod.rs

index 1a1094b570bd107ca5de46a493a8874f75eb554a..7cf94ec84dc935eac95218de674fd446df762467 100644 (file)
@@ -103,10 +103,15 @@ impl<D: ConstraintGraphDirecton> ConstraintGraph<D> {
     }
 
     /// 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<D: ConstraintGraphDirecton> ConstraintGraph<D> {
 
 crate struct Edges<'s, D: ConstraintGraphDirecton> {
     graph: &'s ConstraintGraph<D>,
+    constraints: &'s ConstraintSet,
     pointer: Option<ConstraintIndex>,
 }
 
 impl<'s, D: ConstraintGraphDirecton> Iterator for Edges<'s, D> {
-    type Item = ConstraintIndex;
+    type Item = OutlivesConstraint;
 
     fn next(&mut self) -> Option<Self::Item> {
         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::Item> {
-        self.edges.next().map(|c| D::end_region(&self.set[c]))
+        self.edges.next().map(|c| D::end_region(&c))
     }
 }
 
index ca208a434314f5161d61cee8bf9c5ebda0ca779f..2a541b6474f583fc71648189587de2bd55a69358 100644 (file)
@@ -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::<Vec<_>>()
         );
@@ -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<ConstraintIndex>, RegionVid)> {
+    ) -> Option<(Vec<OutlivesConstraint>, 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);
         }
 
index ff68b5987e85a21d2ec9072e21c4dbb99f5a8b61..4fe39ba3c9520bea74aed78160f7fa822c31e835 100644 (file)
@@ -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;