Skip to main content

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(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for OutlivesConstraintSet<'tcx> {
    #[inline]
    fn clone(&self) -> OutlivesConstraintSet<'tcx> {
        OutlivesConstraintSet {
            outlives: ::core::clone::Clone::clone(&self.outlives),
        }
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for OutlivesConstraintSet<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "OutlivesConstraintSet", "outlives", &&self.outlives)
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::default::Default for OutlivesConstraintSet<'tcx> {
    #[inline]
    fn default() -> OutlivesConstraintSet<'tcx> {
        OutlivesConstraintSet {
            outlives: ::core::default::Default::default(),
        }
    }
}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        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/constraints/mod.rs:25",
                        "rustc_borrowck::constraints", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/constraints/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(25u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::constraints"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("OutlivesConstraintSet::push({0:?})",
                                                    constraint) as &dyn Value))])
            });
    } else { ; }
};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(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for OutlivesConstraint<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for OutlivesConstraint<'tcx> {
    #[inline]
    fn clone(&self) -> OutlivesConstraint<'tcx> {
        let _: ::core::clone::AssertParamIsClone<RegionVid>;
        let _: ::core::clone::AssertParamIsClone<Locations>;
        let _: ::core::clone::AssertParamIsClone<Span>;
        let _: ::core::clone::AssertParamIsClone<ConstraintCategory<'tcx>>;
        let _:
                ::core::clone::AssertParamIsClone<VarianceDiagInfo<TyCtxt<'tcx>>>;
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for OutlivesConstraint<'tcx> {
    #[inline]
    fn eq(&self, other: &OutlivesConstraint<'tcx>) -> bool {
        self.from_closure == other.from_closure && self.sup == other.sup &&
                            self.sub == other.sub && self.locations == other.locations
                    && self.span == other.span &&
                self.category == other.category &&
            self.variance_info == other.variance_info
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for OutlivesConstraint<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<RegionVid>;
        let _: ::core::cmp::AssertParamIsEq<Locations>;
        let _: ::core::cmp::AssertParamIsEq<Span>;
        let _: ::core::cmp::AssertParamIsEq<ConstraintCategory<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<VarianceDiagInfo<TyCtxt<'tcx>>>;
        let _: ::core::cmp::AssertParamIsEq<bool>;
    }
}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        formatter.write_fmt(format_args!("({0:?}: {1:?}) due to {2:?} ({3:?}) ({4:?})",
        self.sup, self.sub, self.locations, self.variance_info,
        self.category))write!(
98            formatter,
99            "({:?}: {:?}) due to {:?} ({:?}) ({:?})",
100            self.sup, self.sub, self.locations, self.variance_info, self.category,
101        )
102    }
103}
104
105impl ::std::fmt::Debug for OutlivesConstraintIndex {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
        fmt.write_fmt(format_args!("OutlivesConstraintIndex({0})",
                self.as_u32()))
    }
}rustc_index::newtype_index! {
106    #[debug_format = "OutlivesConstraintIndex({})"]
107    pub(crate) struct OutlivesConstraintIndex {}
108}
109
110impl ::std::fmt::Debug for ConstraintSccIndex {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
        fmt.write_fmt(format_args!("ConstraintSccIndex({0})", self.as_u32()))
    }
}rustc_index::newtype_index! {
111    #[orderable]
112    #[debug_format = "ConstraintSccIndex({})"]
113    pub struct ConstraintSccIndex {}
114}