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::{NoSolution, OutlivesBound};
7use rustc_middle::ty;
8use tracing::instrument;
9
10use self::env::OutlivesEnvironment;
11use super::region_constraints::{RegionConstraintData, UndoLog};
12use super::{InferCtxt, RegionResolutionError, SubregionOrigin};
13use crate::infer::free_regions::RegionRelations;
14use crate::infer::lexical_region_resolve;
15use crate::infer::region_constraints::ConstraintKind;
16
17pub mod env;
18pub mod for_liveness;
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::OutlivesPredicate(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    /// If you are in a crate that has access to `rustc_trait_selection`,
42    /// then it's probably better to use `resolve_regions`,
43    /// which knows how to normalize registered region obligations.
44    #[must_use]
45    pub fn resolve_regions_with_normalize(
46        &self,
47        outlives_env: &OutlivesEnvironment<'tcx>,
48        deeply_normalize_ty: impl Fn(
49            ty::PolyTypeOutlivesPredicate<'tcx>,
50            SubregionOrigin<'tcx>,
51        ) -> Result<ty::PolyTypeOutlivesPredicate<'tcx>, NoSolution>,
52    ) -> Vec<RegionResolutionError<'tcx>> {
53        match self.process_registered_region_obligations(outlives_env, deeply_normalize_ty) {
54            Ok(()) => {}
55            Err((clause, origin)) => {
56                return ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [RegionResolutionError::CannotNormalize(clause, origin)]))vec![RegionResolutionError::CannotNormalize(clause, origin)];
57            }
58        };
59
60        let mut storage = {
61            let mut inner = self.inner.borrow_mut();
62            let inner = &mut *inner;
63            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!(
64                self.tainted_by_errors().is_some() || inner.region_obligations.is_empty(),
65                "region_obligations not empty: {:#?}",
66                inner.region_obligations,
67            );
68            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));
69            inner.region_constraint_storage.take().expect("regions already resolved")
70        };
71
72        storage.data.constraints = storage
73            .data
74            .constraints
75            .iter()
76            .flat_map(|(constraint, origin)| {
77                constraint.iter_outlives().zip(iter::repeat_with(|| origin.clone()))
78            })
79            .collect();
80
81        // Filter out any region-region outlives assumptions that are implied by
82        // coroutine well-formedness.
83        if self.tcx.sess.opts.unstable_opts.higher_ranked_assumptions {
84            storage.data.constraints.retain(|(c, _)| match c.kind {
85                ConstraintKind::RegSubReg => !outlives_env
86                    .higher_ranked_assumptions()
87                    .contains(&ty::OutlivesPredicate(c.sup.into(), c.sub)),
88
89                ConstraintKind::VarSubVar
90                | ConstraintKind::RegSubVar
91                | ConstraintKind::VarSubReg => true,
92
93                ConstraintKind::VarEqVar | ConstraintKind::VarEqReg | ConstraintKind::RegEqReg => {
94                    ::core::panicking::panic("internal error: entered unreachable code");unreachable!();
95                }
96            });
97        }
98
99        let region_rels = &RegionRelations::new(self.tcx, outlives_env.free_region_map());
100
101        let (lexical_region_resolutions, errors) =
102            lexical_region_resolve::resolve(region_rels, storage.var_infos, storage.data);
103
104        let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
105        if !old_value.is_none() {
    ::core::panicking::panic("assertion failed: old_value.is_none()")
};assert!(old_value.is_none());
106
107        errors
108    }
109
110    /// Obtains (and clears) the current set of region
111    /// constraints. The inference context is still usable: further
112    /// unifications will simply add new constraints.
113    ///
114    /// This method is not meant to be used with normal lexical region
115    /// resolution. Rather, it is used in the NLL mode as a kind of
116    /// interim hack: basically we run normal type-check and generate
117    /// region constraints as normal, but then we take them and
118    /// translate them into the form that the NLL solver
119    /// understands. See the NLL module for mode details.
120    pub fn take_and_reset_region_constraints(&self) -> RegionConstraintData<'tcx> {
121        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!(
122            self.inner.borrow().region_obligations.is_empty(),
123            "region_obligations not empty: {:#?}",
124            self.inner.borrow().region_obligations
125        );
126        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!(
127            self.inner.borrow().region_assumptions.is_empty(),
128            "region_assumptions not empty: {:#?}",
129            self.inner.borrow().region_assumptions
130        );
131
132        self.inner.borrow_mut().unwrap_region_constraints().take_and_reset_data()
133    }
134
135    /// Gives temporary access to the region constraint data.
136    pub fn with_region_constraints<R>(
137        &self,
138        op: impl FnOnce(&RegionConstraintData<'tcx>) -> R,
139    ) -> R {
140        let mut inner = self.inner.borrow_mut();
141        op(inner.unwrap_region_constraints().data())
142    }
143}