Skip to content

Commit

Permalink
[nll] Refactor the Edges iterator to return OutlivesConstraints
Browse files Browse the repository at this point in the history
Part of #53178
  • Loading branch information
wesleywiser committed Sep 6, 2018
1 parent a8c11d2 commit db01b67
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
18 changes: 11 additions & 7 deletions src/librustc_mir/borrow_check/nll/constraints/graph.rs
Expand Up @@ -103,27 +103,33 @@ 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,
}
}
}

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
}
Expand Down Expand Up @@ -154,22 +160,20 @@ 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>,
}

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))
}
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +54,7 @@ impl fmt::Display for ConstraintCategory {
#[derive(Copy, Clone, PartialEq, Eq)]
enum Trace {
StartRegion,
FromConstraint(ConstraintIndex),
FromOutlivesConstraint(OutlivesConstraint),
NotVisited,
}

Expand All @@ -80,12 +81,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
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<_>>()
);
Expand Down Expand Up @@ -121,7 +121,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// 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);

Expand Down Expand Up @@ -164,7 +164,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
&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;

Expand All @@ -185,9 +185,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
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 => {
Expand All @@ -201,11 +201,11 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// 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);
}
}
Expand All @@ -216,8 +216,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {

/// 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
Expand All @@ -232,19 +231,18 @@ impl<'tcx> RegionInferenceContext<'tcx> {
/// 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
.locations
.from_location()
.unwrap_or(Location::START);

if !self.constraint_is_interesting(index) {
if !self.constraint_is_interesting(constraint) {
return (ConstraintCategory::Boring, span);
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/nll/region_infer/mod.rs
Expand Up @@ -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;
Expand Down

0 comments on commit db01b67

Please sign in to comment.