2022-09-14 23:07:19 +08:00
|
|
|
#![deny(rustc::untranslatable_diagnostic)]
|
|
|
|
#![deny(rustc::diagnostic_outside_of_impl)]
|
|
|
|
|
2018-07-02 11:29:39 -04:00
|
|
|
use rustc_data_structures::graph::scc::Sccs;
|
2020-01-05 15:01:00 +00:00
|
|
|
use rustc_index::vec::IndexVec;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::mir::ConstraintCategory;
|
2021-05-13 09:30:14 -04:00
|
|
|
use rustc_middle::ty::{RegionVid, VarianceDiagInfo};
|
2022-04-19 20:53:35 -04:00
|
|
|
use rustc_span::Span;
|
2018-06-27 16:46:27 -04:00
|
|
|
use std::fmt;
|
2019-06-04 18:05:23 -04:00
|
|
|
use std::ops::Index;
|
2018-06-27 16:25:41 -04:00
|
|
|
|
2020-12-30 18:48:40 +01:00
|
|
|
use crate::type_check::Locations;
|
2019-11-29 19:48:29 -06:00
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) mod graph;
|
2018-07-02 11:29:39 -04:00
|
|
|
|
2019-06-04 17:39:12 -04:00
|
|
|
/// A set of NLL region constraints. These include "outlives"
|
|
|
|
/// constraints of the form `R1: R2`. Each constraint is identified by
|
|
|
|
/// a unique `OutlivesConstraintIndex` and you can index into the set
|
|
|
|
/// (`constraint_set[i]`) to access the constraint details.
|
2018-06-27 16:25:41 -04:00
|
|
|
#[derive(Clone, Default)]
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) struct OutlivesConstraintSet<'tcx> {
|
2021-05-13 09:30:14 -04:00
|
|
|
outlives: IndexVec<OutlivesConstraintIndex, OutlivesConstraint<'tcx>>,
|
2018-06-27 16:25:41 -04:00
|
|
|
}
|
|
|
|
|
2021-05-13 09:30:14 -04:00
|
|
|
impl<'tcx> OutlivesConstraintSet<'tcx> {
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn push(&mut self, constraint: OutlivesConstraint<'tcx>) {
|
2022-08-30 21:50:22 -04:00
|
|
|
debug!("OutlivesConstraintSet::push({:?})", constraint);
|
2018-06-27 17:47:55 -04:00
|
|
|
if constraint.sup == constraint.sub {
|
2018-06-27 16:25:41 -04:00
|
|
|
// 'a: 'a is pretty uninteresting
|
|
|
|
return;
|
|
|
|
}
|
2019-06-04 17:39:12 -04:00
|
|
|
self.outlives.push(constraint);
|
2018-06-27 16:25:41 -04:00
|
|
|
}
|
2018-07-02 11:29:39 -04:00
|
|
|
|
2018-08-07 14:14:38 -04:00
|
|
|
/// Constructs a "normal" graph from the constraint set; the graph makes it
|
2018-08-07 10:07:35 -04:00
|
|
|
/// easy to find the constraints affecting a particular region.
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
/// N.B., this graph contains a "frozen" view of the current
|
2019-06-05 10:17:30 -04:00
|
|
|
/// constraints. Any new constraints added to the `OutlivesConstraintSet`
|
2018-08-07 10:07:35 -04:00
|
|
|
/// after the graph is built will not be present in the graph.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn graph(&self, num_region_vars: usize) -> graph::NormalConstraintGraph {
|
2018-08-07 14:14:38 -04:00
|
|
|
graph::ConstraintGraph::new(graph::Normal, self, num_region_vars)
|
2018-07-02 11:29:39 -04:00
|
|
|
}
|
|
|
|
|
2018-08-07 14:50:36 -04:00
|
|
|
/// Like `graph`, but constraints a reverse graph where `R1: R2`
|
|
|
|
/// represents an edge `R2 -> R1`.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn reverse_graph(&self, num_region_vars: usize) -> graph::ReverseConstraintGraph {
|
2018-08-07 14:50:36 -04:00
|
|
|
graph::ConstraintGraph::new(graph::Reverse, self, num_region_vars)
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Computes cycles (SCCs) in the graph of regions. In particular,
|
2018-07-02 11:29:39 -04:00
|
|
|
/// find all regions R1, R2 such that R1: R2 and R2: R1 and group
|
|
|
|
/// them into an SCC, and find the relationships between SCCs.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn compute_sccs(
|
2018-07-02 11:29:39 -04:00
|
|
|
&self,
|
2018-08-07 14:14:38 -04:00
|
|
|
constraint_graph: &graph::NormalConstraintGraph,
|
2018-08-26 22:50:57 -04:00
|
|
|
static_region: RegionVid,
|
2018-07-02 11:29:39 -04:00
|
|
|
) -> Sccs<RegionVid, ConstraintSccIndex> {
|
2018-08-26 22:50:57 -04:00
|
|
|
let region_graph = &constraint_graph.region_graph(self, static_region);
|
2018-07-02 11:29:39 -04:00
|
|
|
Sccs::new(region_graph)
|
|
|
|
}
|
2019-06-04 18:05:23 -04:00
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn outlives(&self) -> &IndexVec<OutlivesConstraintIndex, OutlivesConstraint<'tcx>> {
|
2019-06-04 18:05:23 -04:00
|
|
|
&self.outlives
|
|
|
|
}
|
2018-06-27 16:25:41 -04:00
|
|
|
}
|
|
|
|
|
2021-05-13 09:30:14 -04:00
|
|
|
impl<'tcx> Index<OutlivesConstraintIndex> for OutlivesConstraintSet<'tcx> {
|
|
|
|
type Output = OutlivesConstraint<'tcx>;
|
2018-06-27 18:50:48 -04:00
|
|
|
|
2019-06-04 18:05:23 -04:00
|
|
|
fn index(&self, i: OutlivesConstraintIndex) -> &Self::Output {
|
|
|
|
&self.outlives[i]
|
2018-07-02 11:29:39 -04:00
|
|
|
}
|
2018-06-27 18:50:48 -04:00
|
|
|
}
|
|
|
|
|
2022-08-29 21:39:53 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
2021-05-13 09:30:14 -04:00
|
|
|
pub struct OutlivesConstraint<'tcx> {
|
2018-06-27 16:46:27 -04:00
|
|
|
// NB. The ordering here is not significant for correctness, but
|
|
|
|
// it is for convenience. Before we dump the constraints in the
|
|
|
|
// debugging logs, we sort them, and we'd like the "super region"
|
|
|
|
// to be first, etc. (In particular, span should remain last.)
|
|
|
|
/// The region SUP must outlive SUB...
|
|
|
|
pub sup: RegionVid,
|
|
|
|
|
|
|
|
/// Region that must be outlived.
|
|
|
|
pub sub: RegionVid,
|
|
|
|
|
|
|
|
/// Where did this constraint arise?
|
2018-06-04 12:25:12 -04:00
|
|
|
pub locations: Locations,
|
2018-09-15 18:30:14 +01:00
|
|
|
|
2022-04-19 20:53:35 -04:00
|
|
|
/// The `Span` associated with the creation of this constraint.
|
|
|
|
/// This should be used in preference to obtaining the span from
|
|
|
|
/// `locations`, since the `locations` may give a poor span
|
|
|
|
/// in some cases (e.g. converting a constraint from a promoted).
|
|
|
|
pub span: Span,
|
|
|
|
|
2018-09-15 18:30:14 +01:00
|
|
|
/// What caused this constraint?
|
2022-10-27 16:15:11 +00:00
|
|
|
pub category: ConstraintCategory<'tcx>,
|
2021-05-13 09:30:14 -04:00
|
|
|
|
|
|
|
/// Variance diagnostic information
|
|
|
|
pub variance_info: VarianceDiagInfo<'tcx>,
|
2022-10-03 12:14:48 +03:00
|
|
|
|
|
|
|
/// If this constraint is promoted from closure requirements.
|
|
|
|
pub from_closure: bool,
|
2018-06-27 16:46:27 -04:00
|
|
|
}
|
|
|
|
|
2021-05-13 09:30:14 -04:00
|
|
|
impl<'tcx> fmt::Debug for OutlivesConstraint<'tcx> {
|
2019-02-08 06:28:15 +09:00
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-05-13 09:30:14 -04:00
|
|
|
write!(
|
|
|
|
formatter,
|
2022-08-29 02:00:08 -04:00
|
|
|
"({:?}: {:?}) due to {:?} ({:?}) ({:?})",
|
|
|
|
self.sup, self.sub, self.locations, self.variance_info, self.category,
|
2021-05-13 09:30:14 -04:00
|
|
|
)
|
2018-06-27 16:46:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::newtype_index! {
|
2022-12-18 21:37:38 +01:00
|
|
|
#[debug_format = "OutlivesConstraintIndex({})"]
|
2019-06-04 17:39:12 -04:00
|
|
|
pub struct OutlivesConstraintIndex {
|
2018-07-25 13:41:32 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-01 06:11:08 -04:00
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
rustc_index::newtype_index! {
|
2022-12-18 21:37:38 +01:00
|
|
|
#[debug_format = "ConstraintSccIndex({})"]
|
2018-07-25 13:41:32 +03:00
|
|
|
pub struct ConstraintSccIndex {
|
|
|
|
}
|
|
|
|
}
|