Skip to main content

rustc_infer/infer/outlives/
mod.rs

1//! Various code related to computing outlives relations.
2
3use std::iter;
4
5use rustc_data_structures::undo_log::UndoLogs;
6use rustc_middle::traits::query::OutlivesBound;
7use rustc_middle::ty;
8use rustc_span::Span;
9use tracing::instrument;
10
11use self::env::OutlivesEnvironment;
12use super::region_constraints::{RegionConstraintData, UndoLog};
13use super::{InferCtxt, RegionResolutionError};
14use crate::infer::free_regions::RegionRelations;
15use crate::infer::lexical_region_resolve;
16use crate::infer::region_constraints::ConstraintKind;
17
18pub mod env;
19pub mod obligations;
20pub mod test_type_match;
21pub(crate) mod verify;
22
23x;#[instrument(level = "debug", skip(param_env), ret)]
24pub fn explicit_outlives_bounds<'tcx>(
25    param_env: ty::ParamEnv<'tcx>,
26) -> impl Iterator<Item = OutlivesBound<'tcx>> {
27    param_env
28        .caller_bounds()
29        .into_iter()
30        .filter_map(ty::Clause::as_region_outlives_clause)
31        .filter_map(ty::Binder::no_bound_vars)
32        .map(|ty::OutlivesClause(r_a, r_b)| OutlivesBound::RegionSubRegion(r_b, r_a))
33}
34
35impl<'tcx> InferCtxt<'tcx> {
36    /// Process the region constraints and return any errors that
37    /// result. After this, no more unification operations should be
38    /// done -- or the compiler will panic -- but it is legal to use
39    /// `resolve_vars_if_possible` as well as `fully_resolve`.
40    ///
41    /// Don't call this directly unless you know what you're doing.
42    /// You probably want to use `resolve_regions` instead.
43    #[must_use]
44    pub fn resolve_regions_with_outlives_env(
45        &self,
46        outlives_env: &OutlivesEnvironment<'tcx>,
47        span: Span,
48    ) -> Vec<RegionResolutionError<'tcx>> {
49        self.process_registered_region_obligations(outlives_env, span);
50
51        let mut storage = {
52            let mut inner = self.inner.borrow_mut();
53            let inner = &mut *inner;
54            if !(self.tainted_by_errors().is_some() ||
            inner.region_obligations.is_empty()) {
    {
        ::core::panicking::panic_fmt(format_args!("region_obligations not empty: {0:#?}",
                inner.region_obligations));
    }
};assert!(
55                self.tainted_by_errors().is_some() || inner.region_obligations.is_empty(),
56                "region_obligations not empty: {:#?}",
57                inner.region_obligations,
58            );
59            if !!UndoLogs::<UndoLog<'_>>::in_snapshot(&inner.undo_log) {
    ::core::panicking::panic("assertion failed: !UndoLogs::<UndoLog<\'_>>::in_snapshot(&inner.undo_log)")
};assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&inner.undo_log));
60            inner.region_constraint_storage.take().expect("regions already resolved")
61        };
62
63        storage.data.constraints = storage
64            .data
65            .constraints
66            .iter()
67            .flat_map(|(constraint, origin)| {
68                constraint.iter_outlives().zip(iter::repeat_with(|| origin.clone()))
69            })
70            .collect();
71
72        // Filter out any region-region outlives assumptions that are implied by
73        // coroutine well-formedness.
74        if self.tcx.sess.opts.unstable_opts.higher_ranked_assumptions {
75            storage.data.constraints.retain(|(c, _)| match c.kind {
76                ConstraintKind::RegSubReg => !outlives_env
77                    .higher_ranked_assumptions()
78                    .contains(&ty::OutlivesClause(c.sup.into(), c.sub)),
79
80                ConstraintKind::VarSubVar
81                | ConstraintKind::RegSubVar
82                | ConstraintKind::VarSubReg => true,
83
84                ConstraintKind::VarEqVar | ConstraintKind::VarEqReg | ConstraintKind::RegEqReg => {
85                    ::core::panicking::panic("internal error: entered unreachable code");unreachable!();
86                }
87            });
88        }
89
90        let region_rels = &RegionRelations::new(self.tcx, outlives_env.free_region_map());
91
92        let (lexical_region_resolutions, errors) =
93            lexical_region_resolve::resolve(region_rels, storage.var_infos, storage.data);
94
95        let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
96        if !old_value.is_none() {
    ::core::panicking::panic("assertion failed: old_value.is_none()")
};assert!(old_value.is_none());
97
98        errors
99    }
100
101    /// Obtains (and clears) the current set of region
102    /// constraints. The inference context is still usable: further
103    /// unifications will simply add new constraints.
104    ///
105    /// This method is not meant to be used with normal lexical region
106    /// resolution. Rather, it is used in the NLL mode as a kind of
107    /// interim hack: basically we run normal type-check and generate
108    /// region constraints as normal, but then we take them and
109    /// translate them into the form that the NLL solver
110    /// understands. See the NLL module for mode details.
111    pub fn take_and_reset_region_constraints(&self) -> RegionConstraintData<'tcx> {
112        if !self.inner.borrow().region_obligations.is_empty() {
    {
        ::core::panicking::panic_fmt(format_args!("region_obligations not empty: {0:#?}",
                self.inner.borrow().region_obligations));
    }
};assert!(
113            self.inner.borrow().region_obligations.is_empty(),
114            "region_obligations not empty: {:#?}",
115            self.inner.borrow().region_obligations
116        );
117        if !self.inner.borrow().region_assumptions.is_empty() {
    {
        ::core::panicking::panic_fmt(format_args!("region_assumptions not empty: {0:#?}",
                self.inner.borrow().region_assumptions));
    }
};assert!(
118            self.inner.borrow().region_assumptions.is_empty(),
119            "region_assumptions not empty: {:#?}",
120            self.inner.borrow().region_assumptions
121        );
122
123        self.inner.borrow_mut().unwrap_region_constraints().take_and_reset_data()
124    }
125
126    /// Gives temporary access to the region constraint data.
127    pub fn with_region_constraints<R>(
128        &self,
129        op: impl FnOnce(&RegionConstraintData<'tcx>) -> R,
130    ) -> R {
131        let mut inner = self.inner.borrow_mut();
132        op(inner.unwrap_region_constraints().data())
133    }
134}