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_type_ir::CallerBoundsIterator;
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
23{}
#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("explicit_outlives_bounds",
                                    "rustc_infer::infer::outlives", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("/rustc-dev/f5cedb83a788ea9581097abc36a2a8503a7f541c/compiler/rustc_infer/src/infer/outlives/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(23u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::outlives"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set_all(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: _ = loop {};
            return __tracing_attr_fake_return;
        }
        {
            param_env.caller_bounds().non_trait_clauses().iter().filter_map(ty::Clause::as_region_outlives_clause).filter_map(ty::Binder::no_bound_vars).map(|ty::OutlivesClause(r_a,
                        r_b)| OutlivesBound::RegionSubRegion(r_b, r_a))
        }
    }
}#[instrument(level = "debug", skip(param_env))]
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        .non_trait_clauses()
30        .iter()
31        .filter_map(ty::Clause::as_region_outlives_clause)
32        .filter_map(ty::Binder::no_bound_vars)
33        .map(|ty::OutlivesClause(r_a, r_b)| OutlivesBound::RegionSubRegion(r_b, r_a))
34}
35
36impl<'tcx> InferCtxt<'tcx> {
37    /// Process the region constraints and return any errors that
38    /// result. After this, no more unification operations should be
39    /// done -- or the compiler will panic -- but it is legal to use
40    /// `deeply_resolve_ignoring_regions` as well as `fully_resolve`.
41    ///
42    /// Don't call this directly unless you know what you're doing.
43    /// You probably want to use `resolve_regions` instead.
44    #[must_use]
45    pub fn resolve_regions_with_outlives_env(
46        &self,
47        outlives_env: &OutlivesEnvironment<'tcx>,
48    ) -> Vec<RegionResolutionError<'tcx>> {
49        self.process_registered_region_obligations(outlives_env);
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        // FIXME(-Zassumptions-in-binders): Handle solver_data
64        storage.legacy_data.constraints = storage
65            .legacy_data
66            .constraints
67            .iter()
68            .flat_map(|(constraint, origin)| {
69                constraint.iter_outlives().zip(iter::repeat_with(|| origin.clone()))
70            })
71            .collect();
72
73        // Filter out any region-region outlives assumptions that are implied by
74        // coroutine well-formedness.
75        if self.tcx.sess.opts.unstable_opts.higher_ranked_assumptions {
76            storage.legacy_data.constraints.retain(|(c, _)| match c.kind {
77                ConstraintKind::RegSubReg => !outlives_env
78                    .higher_ranked_assumptions()
79                    .contains(&ty::OutlivesClause(c.sup.into(), c.sub)),
80
81                ConstraintKind::VarSubVar
82                | ConstraintKind::RegSubVar
83                | ConstraintKind::VarSubReg => true,
84
85                ConstraintKind::VarEqVar | ConstraintKind::VarEqReg | ConstraintKind::RegEqReg => {
86                    ::core::panicking::unreachable_default_msg();unreachable!();
87                }
88            });
89        }
90
91        let region_rels = &RegionRelations::new(self.tcx, outlives_env.free_region_map());
92
93        let (lexical_region_resolutions, errors) =
94            lexical_region_resolve::resolve(region_rels, storage.var_infos, storage.legacy_data);
95
96        let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
97        if !old_value.is_none() {
    ::core::panicking::panic("assertion failed: old_value.is_none()")
};assert!(old_value.is_none());
98
99        errors
100    }
101
102    /// Obtains (and clears) the current set of region
103    /// constraints. The inference context is still usable: further
104    /// unifications will simply add new constraints.
105    ///
106    /// This method is not meant to be used with normal lexical region
107    /// resolution. Rather, it is used in the NLL mode as a kind of
108    /// interim hack: basically we run normal type-check and generate
109    /// region constraints as normal, but then we take them and
110    /// translate them into the form that the NLL solver
111    /// understands. See the NLL module for mode details.
112    pub fn take_and_reset_region_constraints(&self) -> RegionConstraintData<'tcx> {
113        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!(
114            self.inner.borrow().region_obligations.is_empty(),
115            "region_obligations not empty: {:#?}",
116            self.inner.borrow().region_obligations
117        );
118        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!(
119            self.inner.borrow().region_assumptions.is_empty(),
120            "region_assumptions not empty: {:#?}",
121            self.inner.borrow().region_assumptions
122        );
123
124        self.inner.borrow_mut().unwrap_region_constraints().take_and_reset_data()
125    }
126
127    /// Gives temporary access to the region constraint data.
128    pub fn with_region_constraints<R>(
129        &self,
130        op: impl FnOnce(&RegionConstraintData<'tcx>) -> R,
131    ) -> R {
132        let mut inner = self.inner.borrow_mut();
133        // FIXME(-Zassumptions-on-binders): Handle solver_data
134        op(inner.unwrap_region_constraints().legacy_data())
135    }
136}