1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::mir::ConstraintCategory;
12 use rustc::ty::RegionVid;
13 use rustc_data_structures::graph::scc::Sccs;
14 use rustc_data_structures::indexed_vec::{Idx, IndexVec};
15 use borrow_check::nll::type_check::Locations;
22 #[derive(Clone, Default)]
23 crate struct ConstraintSet {
24 constraints: IndexVec<ConstraintIndex, OutlivesConstraint>,
28 crate fn push(&mut self, constraint: OutlivesConstraint) {
30 "ConstraintSet::push({:?}: {:?} @ {:?}",
31 constraint.sup, constraint.sub, constraint.locations
33 if constraint.sup == constraint.sub {
34 // 'a: 'a is pretty uninteresting
37 self.constraints.push(constraint);
40 /// Constructs a "normal" graph from the constraint set; the graph makes it
41 /// easy to find the constraints affecting a particular region.
43 /// NB: This graph contains a "frozen" view of the current
44 /// constraints. any new constraints added to the `ConstraintSet`
45 /// after the graph is built will not be present in the graph.
46 crate fn graph(&self, num_region_vars: usize) -> graph::NormalConstraintGraph {
47 graph::ConstraintGraph::new(graph::Normal, self, num_region_vars)
50 /// Like `graph`, but constraints a reverse graph where `R1: R2`
51 /// represents an edge `R2 -> R1`.
52 crate fn reverse_graph(&self, num_region_vars: usize) -> graph::ReverseConstraintGraph {
53 graph::ConstraintGraph::new(graph::Reverse, self, num_region_vars)
56 /// Compute cycles (SCCs) in the graph of regions. In particular,
57 /// find all regions R1, R2 such that R1: R2 and R2: R1 and group
58 /// them into an SCC, and find the relationships between SCCs.
59 crate fn compute_sccs(
61 constraint_graph: &graph::NormalConstraintGraph,
62 static_region: RegionVid,
63 ) -> Sccs<RegionVid, ConstraintSccIndex> {
64 let region_graph = &constraint_graph.region_graph(self, static_region);
65 Sccs::new(region_graph)
69 impl Deref for ConstraintSet {
70 type Target = IndexVec<ConstraintIndex, OutlivesConstraint>;
72 fn deref(&self) -> &Self::Target {
77 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
78 pub struct OutlivesConstraint {
79 // NB. The ordering here is not significant for correctness, but
80 // it is for convenience. Before we dump the constraints in the
81 // debugging logs, we sort them, and we'd like the "super region"
82 // to be first, etc. (In particular, span should remain last.)
83 /// The region SUP must outlive SUB...
86 /// Region that must be outlived.
89 /// Where did this constraint arise?
90 pub locations: Locations,
92 /// What caused this constraint?
93 pub category: ConstraintCategory,
96 impl fmt::Debug for OutlivesConstraint {
97 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
100 "({:?}: {:?}) due to {:?}",
101 self.sup, self.sub, self.locations
107 pub struct ConstraintIndex {
108 DEBUG_FORMAT = "ConstraintIndex({})"
113 pub struct ConstraintSccIndex {
114 DEBUG_FORMAT = "ConstraintSccIndex({})"