rustc_borrowck/constraints/
mod.rs

1use std::fmt;
2use std::ops::Index;
3
4use rustc_index::{IndexSlice, IndexVec};
5use rustc_middle::mir::ConstraintCategory;
6use rustc_middle::ty::{RegionVid, TyCtxt, VarianceDiagInfo};
7use rustc_span::Span;
8use tracing::debug;
9
10use crate::type_check::Locations;
11
12pub(crate) mod graph;
13
14/// A set of NLL region constraints. These include "outlives"
15/// constraints of the form `R1: R2`. Each constraint is identified by
16/// a unique `OutlivesConstraintIndex` and you can index into the set
17/// (`constraint_set[i]`) to access the constraint details.
18#[derive(Clone, Debug, Default)]
19pub(crate) struct OutlivesConstraintSet<'tcx> {
20    outlives: IndexVec<OutlivesConstraintIndex, OutlivesConstraint<'tcx>>,
21}
22
23impl<'tcx> OutlivesConstraintSet<'tcx> {
24    pub(crate) fn push(&mut self, constraint: OutlivesConstraint<'tcx>) {
25        debug!("OutlivesConstraintSet::push({:?})", constraint);
26        if constraint.sup == constraint.sub {
27            // 'a: 'a is pretty uninteresting
28            return;
29        }
30        self.outlives.push(constraint);
31    }
32
33    /// Constructs a "normal" graph from the constraint set; the graph makes it
34    /// easy to find the constraints affecting a particular region.
35    ///
36    /// N.B., this graph contains a "frozen" view of the current
37    /// constraints. Any new constraints added to the `OutlivesConstraintSet`
38    /// after the graph is built will not be present in the graph.
39    pub(crate) fn graph(&self, num_region_vars: usize) -> graph::NormalConstraintGraph {
40        graph::ConstraintGraph::new(graph::Normal, self, num_region_vars)
41    }
42
43    /// Like `graph`, but constraints a reverse graph where `R1: R2`
44    /// represents an edge `R2 -> R1`.
45    pub(crate) fn reverse_graph(&self, num_region_vars: usize) -> graph::ReverseConstraintGraph {
46        graph::ConstraintGraph::new(graph::Reverse, self, num_region_vars)
47    }
48
49    pub(crate) fn outlives(
50        &self,
51    ) -> &IndexSlice<OutlivesConstraintIndex, OutlivesConstraint<'tcx>> {
52        &self.outlives
53    }
54}
55
56impl<'tcx> Index<OutlivesConstraintIndex> for OutlivesConstraintSet<'tcx> {
57    type Output = OutlivesConstraint<'tcx>;
58
59    fn index(&self, i: OutlivesConstraintIndex) -> &Self::Output {
60        &self.outlives[i]
61    }
62}
63
64#[derive(Copy, Clone, PartialEq, Eq)]
65pub struct OutlivesConstraint<'tcx> {
66    // NB. The ordering here is not significant for correctness, but
67    // it is for convenience. Before we dump the constraints in the
68    // debugging logs, we sort them, and we'd like the "super region"
69    // to be first, etc. (In particular, span should remain last.)
70    /// The region SUP must outlive SUB...
71    pub sup: RegionVid,
72
73    /// Region that must be outlived.
74    pub sub: RegionVid,
75
76    /// Where did this constraint arise?
77    pub locations: Locations,
78
79    /// The `Span` associated with the creation of this constraint.
80    /// This should be used in preference to obtaining the span from
81    /// `locations`, since the `locations` may give a poor span
82    /// in some cases (e.g. converting a constraint from a promoted).
83    pub span: Span,
84
85    /// What caused this constraint?
86    pub category: ConstraintCategory<'tcx>,
87
88    /// Variance diagnostic information
89    pub variance_info: VarianceDiagInfo<TyCtxt<'tcx>>,
90
91    /// If this constraint is promoted from closure requirements.
92    pub from_closure: bool,
93}
94
95impl<'tcx> fmt::Debug for OutlivesConstraint<'tcx> {
96    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
97        write!(
98            formatter,
99            "({:?}: {:?}) due to {:?} ({:?}) ({:?})",
100            self.sup, self.sub, self.locations, self.variance_info, self.category,
101        )
102    }
103}
104
105rustc_index::newtype_index! {
106    #[debug_format = "OutlivesConstraintIndex({})"]
107    pub(crate) struct OutlivesConstraintIndex {}
108}
109
110rustc_index::newtype_index! {
111    #[orderable]
112    #[debug_format = "ConstraintSccIndex({})"]
113    pub struct ConstraintSccIndex {}
114}