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
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/cea272fa356e94bd2ee2cadf376630aa0683867a/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().into_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        .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}