Skip to main content

rustc_borrowck/diagnostics/
bound_region_errors.rs

1use std::fmt;
2use std::rc::Rc;
3
4use rustc_errors::Diag;
5use rustc_hir::def_id::LocalDefId;
6use rustc_infer::infer::region_constraints::{Constraint, ConstraintKind, RegionConstraintData};
7use rustc_infer::infer::{
8    InferCtxt, RegionResolutionError, RegionVariableOrigin, SubregionOrigin, TyCtxtInferExt as _,
9};
10use rustc_infer::traits::ObligationCause;
11use rustc_infer::traits::query::{
12    CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpDeeplyNormalizeGoal,
13    CanonicalTypeOpNormalizeGoal, CanonicalTypeOpProvePredicateGoal,
14};
15use rustc_middle::ty::error::TypeError;
16use rustc_middle::ty::{
17    self, RePlaceholder, Region, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex,
18};
19use rustc_span::Span;
20use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
21use rustc_trait_selection::error_reporting::infer::nice_region_error::NiceRegionError;
22use rustc_trait_selection::traits::ObligationCtxt;
23use rustc_traits::{type_op_ascribe_user_type_with_span, type_op_prove_predicate_with_cause};
24use tracing::{debug, instrument};
25
26use crate::MirBorrowckCtxt;
27use crate::session_diagnostics::{
28    HigherRankedErrorCause, HigherRankedLifetimeError, HigherRankedSubtypeError,
29};
30
31/// What operation a universe was created for.
32#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for UniverseInfo<'tcx> {
    #[inline]
    fn clone(&self) -> UniverseInfo<'tcx> {
        match self {
            UniverseInfo::RelateTys { expected: __self_0, found: __self_1 } =>
                UniverseInfo::RelateTys {
                    expected: ::core::clone::Clone::clone(__self_0),
                    found: ::core::clone::Clone::clone(__self_1),
                },
            UniverseInfo::TypeOp(__self_0) =>
                UniverseInfo::TypeOp(::core::clone::Clone::clone(__self_0)),
            UniverseInfo::Other => UniverseInfo::Other,
        }
    }
}Clone)]
33pub(crate) enum UniverseInfo<'tcx> {
34    /// Relating two types which have binders.
35    RelateTys { expected: Ty<'tcx>, found: Ty<'tcx> },
36    /// Created from performing a `TypeOp`.
37    TypeOp(Rc<dyn TypeOpInfo<'tcx> + 'tcx>),
38    /// Any other reason.
39    Other,
40}
41
42impl<'tcx> UniverseInfo<'tcx> {
43    pub(crate) fn other() -> UniverseInfo<'tcx> {
44        UniverseInfo::Other
45    }
46
47    pub(crate) fn relate(expected: Ty<'tcx>, found: Ty<'tcx>) -> UniverseInfo<'tcx> {
48        UniverseInfo::RelateTys { expected, found }
49    }
50
51    /// Report an error where an element erroneously made its way into `placeholder`.
52    pub(crate) fn report_erroneous_element(
53        &self,
54        mbcx: &mut MirBorrowckCtxt<'_, '_, 'tcx>,
55        placeholder: ty::PlaceholderRegion<'tcx>,
56        error_element: Option<ty::PlaceholderRegion<'tcx>>,
57        cause: ObligationCause<'tcx>,
58    ) {
59        match *self {
60            UniverseInfo::RelateTys { expected, found } => {
61                let err = mbcx.infcx.err_ctxt().report_mismatched_types(
62                    &cause,
63                    mbcx.infcx.param_env,
64                    expected,
65                    found,
66                    TypeError::RegionsPlaceholderMismatch,
67                );
68                mbcx.buffer_error(err);
69            }
70            UniverseInfo::TypeOp(ref type_op_info) => {
71                type_op_info.report_erroneous_element(mbcx, placeholder, error_element, cause);
72            }
73            UniverseInfo::Other => {
74                // FIXME: This error message isn't great, but it doesn't show
75                // up in the existing UI tests. Consider investigating this
76                // some more.
77                mbcx.buffer_error(
78                    mbcx.dcx().create_err(HigherRankedSubtypeError { span: cause.span }),
79                );
80            }
81        }
82    }
83}
84
85pub(crate) trait ToUniverseInfo<'tcx> {
86    fn to_universe_info(self, base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx>;
87}
88
89impl<'tcx> ToUniverseInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
90    fn to_universe_info(self, base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
91        UniverseInfo::TypeOp(Rc::new(crate::type_check::InstantiateOpaqueType {
92            base_universe: Some(base_universe),
93            ..self
94        }))
95    }
96}
97
98impl<'tcx> ToUniverseInfo<'tcx> for CanonicalTypeOpProvePredicateGoal<'tcx> {
99    fn to_universe_info(self, base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
100        UniverseInfo::TypeOp(Rc::new(PredicateQuery { canonical_query: self, base_universe }))
101    }
102}
103
104impl<'tcx, T: Copy + fmt::Display + TypeFoldable<TyCtxt<'tcx>> + 'tcx> ToUniverseInfo<'tcx>
105    for CanonicalTypeOpNormalizeGoal<'tcx, T>
106{
107    fn to_universe_info(self, base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
108        UniverseInfo::TypeOp(Rc::new(NormalizeQuery { canonical_query: self, base_universe }))
109    }
110}
111
112impl<'tcx, T: Copy + fmt::Display + TypeFoldable<TyCtxt<'tcx>> + 'tcx> ToUniverseInfo<'tcx>
113    for CanonicalTypeOpDeeplyNormalizeGoal<'tcx, T>
114{
115    fn to_universe_info(self, base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
116        UniverseInfo::TypeOp(Rc::new(DeeplyNormalizeQuery { canonical_query: self, base_universe }))
117    }
118}
119
120impl<'tcx> ToUniverseInfo<'tcx> for CanonicalTypeOpAscribeUserTypeGoal<'tcx> {
121    fn to_universe_info(self, base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
122        UniverseInfo::TypeOp(Rc::new(AscribeUserTypeQuery { canonical_query: self, base_universe }))
123    }
124}
125
126impl<'tcx> ToUniverseInfo<'tcx> for ! {
127    fn to_universe_info(self, _base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {
128        self
129    }
130}
131
132#[allow(unused_lifetimes)]
133pub(crate) trait TypeOpInfo<'tcx> {
134    /// Returns an error to be reported if rerunning the type op fails to
135    /// recover the error's cause.
136    fn fallback_error(&self, tcx: TyCtxt<'tcx>, span: Span) -> Diag<'tcx>;
137
138    fn base_universe(&self) -> ty::UniverseIndex;
139
140    fn nice_error<'infcx>(
141        &self,
142        mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
143        cause: ObligationCause<'tcx>,
144        placeholder_region: ty::Region<'tcx>,
145        error_region: Option<ty::Region<'tcx>>,
146    ) -> Option<Diag<'infcx>>;
147
148    /// Constraints require that `error_element` appear in the
149    /// values of `placeholder`, but this cannot be proven to
150    /// hold. Report an error.
151    #[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("report_erroneous_element",
                                    "rustc_borrowck::diagnostics::bound_region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(151u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::bound_region_errors"),
                                    ::tracing_core::field::FieldSet::new(&["placeholder",
                                                    "error_element", "cause"],
                                        ::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,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&placeholder)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&error_element)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&cause)
                                                            as &dyn Value))])
                            })
                } 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;
        }
        {
            let tcx = mbcx.infcx.tcx;
            let base_universe = self.base_universe();
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs:161",
                                    "rustc_borrowck::diagnostics::bound_region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(161u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::bound_region_errors"),
                                    ::tracing_core::field::FieldSet::new(&["base_universe"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&base_universe)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let Some(adjusted_universe) =
                placeholder.universe.as_u32().checked_sub(base_universe.as_u32()) else {
                    mbcx.buffer_error(self.fallback_error(tcx, cause.span));
                    return;
                };
            let placeholder_region =
                ty::Region::new_placeholder(tcx,
                    ty::PlaceholderRegion::new(adjusted_universe.into(),
                        placeholder.bound));
            let error_region =
                error_element.and_then(|e|
                        {
                            let adjusted_universe =
                                e.universe.as_u32().checked_sub(base_universe.as_u32());
                            adjusted_universe.map(|adjusted|
                                    {
                                        ty::Region::new_placeholder(tcx,
                                            ty::PlaceholderRegion::new(adjusted.into(), e.bound))
                                    })
                        });
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs:187",
                                    "rustc_borrowck::diagnostics::bound_region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(187u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::bound_region_errors"),
                                    ::tracing_core::field::FieldSet::new(&["placeholder_region"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&placeholder_region)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let span = cause.span;
            let nice_error =
                self.nice_error(mbcx, cause, placeholder_region,
                    error_region);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs:192",
                                    "rustc_borrowck::diagnostics::bound_region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(192u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::bound_region_errors"),
                                    ::tracing_core::field::FieldSet::new(&["nice_error"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&nice_error)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            mbcx.buffer_error(nice_error.unwrap_or_else(||
                        self.fallback_error(tcx, span)));
        }
    }
}#[instrument(level = "debug", skip(self, mbcx))]
152    fn report_erroneous_element(
153        &self,
154        mbcx: &mut MirBorrowckCtxt<'_, '_, 'tcx>,
155        placeholder: ty::PlaceholderRegion<'tcx>,
156        error_element: Option<ty::PlaceholderRegion<'tcx>>,
157        cause: ObligationCause<'tcx>,
158    ) {
159        let tcx = mbcx.infcx.tcx;
160        let base_universe = self.base_universe();
161        debug!(?base_universe);
162
163        let Some(adjusted_universe) =
164            placeholder.universe.as_u32().checked_sub(base_universe.as_u32())
165        else {
166            mbcx.buffer_error(self.fallback_error(tcx, cause.span));
167            return;
168        };
169
170        let placeholder_region = ty::Region::new_placeholder(
171            tcx,
172            ty::PlaceholderRegion::new(adjusted_universe.into(), placeholder.bound),
173        );
174
175        // FIXME: one day this should just be error_element,
176        // and this method shouldn't do anything.
177        let error_region = error_element.and_then(|e| {
178            let adjusted_universe = e.universe.as_u32().checked_sub(base_universe.as_u32());
179            adjusted_universe.map(|adjusted| {
180                ty::Region::new_placeholder(
181                    tcx,
182                    ty::PlaceholderRegion::new(adjusted.into(), e.bound),
183                )
184            })
185        });
186
187        debug!(?placeholder_region);
188
189        let span = cause.span;
190        let nice_error = self.nice_error(mbcx, cause, placeholder_region, error_region);
191
192        debug!(?nice_error);
193        mbcx.buffer_error(nice_error.unwrap_or_else(|| self.fallback_error(tcx, span)));
194    }
195}
196
197struct PredicateQuery<'tcx> {
198    canonical_query: CanonicalTypeOpProvePredicateGoal<'tcx>,
199    base_universe: ty::UniverseIndex,
200}
201
202impl<'tcx> TypeOpInfo<'tcx> for PredicateQuery<'tcx> {
203    fn fallback_error(&self, tcx: TyCtxt<'tcx>, span: Span) -> Diag<'tcx> {
204        tcx.dcx().create_err(HigherRankedLifetimeError {
205            cause: Some(HigherRankedErrorCause::CouldNotProve {
206                predicate: self.canonical_query.canonical.value.value.predicate.to_string(),
207            }),
208            span,
209        })
210    }
211
212    fn base_universe(&self) -> ty::UniverseIndex {
213        self.base_universe
214    }
215
216    fn nice_error<'infcx>(
217        &self,
218        mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
219        cause: ObligationCause<'tcx>,
220        placeholder_region: ty::Region<'tcx>,
221        error_region: Option<ty::Region<'tcx>>,
222    ) -> Option<Diag<'infcx>> {
223        let (infcx, key, _) =
224            mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
225        let ocx = ObligationCtxt::new(&infcx);
226        type_op_prove_predicate_with_cause(&ocx, key, cause);
227        let diag = try_extract_error_from_fulfill_cx(
228            &ocx,
229            mbcx.mir_def_id(),
230            placeholder_region,
231            error_region,
232        )?
233        .with_dcx(mbcx.dcx());
234        Some(diag)
235    }
236}
237
238struct NormalizeQuery<'tcx, T> {
239    canonical_query: CanonicalTypeOpNormalizeGoal<'tcx, T>,
240    base_universe: ty::UniverseIndex,
241}
242
243impl<'tcx, T> TypeOpInfo<'tcx> for NormalizeQuery<'tcx, T>
244where
245    T: Copy + fmt::Display + TypeFoldable<TyCtxt<'tcx>> + 'tcx,
246{
247    fn fallback_error(&self, tcx: TyCtxt<'tcx>, span: Span) -> Diag<'tcx> {
248        tcx.dcx().create_err(HigherRankedLifetimeError {
249            cause: Some(HigherRankedErrorCause::CouldNotNormalize {
250                value: self.canonical_query.canonical.value.value.value.to_string(),
251            }),
252            span,
253        })
254    }
255
256    fn base_universe(&self) -> ty::UniverseIndex {
257        self.base_universe
258    }
259
260    fn nice_error<'infcx>(
261        &self,
262        mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
263        cause: ObligationCause<'tcx>,
264        placeholder_region: ty::Region<'tcx>,
265        error_region: Option<ty::Region<'tcx>>,
266    ) -> Option<Diag<'infcx>> {
267        let (infcx, key, _) =
268            mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
269        let ocx = ObligationCtxt::new(&infcx);
270
271        // FIXME(lqd): Unify and de-duplicate the following with the actual
272        // `rustc_traits::type_op::type_op_normalize` query to allow the span we need in the
273        // `ObligationCause`. The normalization results are currently different between
274        // `QueryNormalizeExt::query_normalize` used in the query and `normalize` called below:
275        // the former fails to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs`
276        // test. Check after #85499 lands to see if its fixes have erased this difference.
277        let ty::ParamEnvAnd { param_env, value } = key;
278        let _ = ocx.normalize(&cause, param_env, value.value);
279
280        let diag = try_extract_error_from_fulfill_cx(
281            &ocx,
282            mbcx.mir_def_id(),
283            placeholder_region,
284            error_region,
285        )?
286        .with_dcx(mbcx.dcx());
287        Some(diag)
288    }
289}
290
291struct DeeplyNormalizeQuery<'tcx, T> {
292    canonical_query: CanonicalTypeOpDeeplyNormalizeGoal<'tcx, T>,
293    base_universe: ty::UniverseIndex,
294}
295
296impl<'tcx, T> TypeOpInfo<'tcx> for DeeplyNormalizeQuery<'tcx, T>
297where
298    T: Copy + fmt::Display + TypeFoldable<TyCtxt<'tcx>> + 'tcx,
299{
300    fn fallback_error(&self, tcx: TyCtxt<'tcx>, span: Span) -> Diag<'tcx> {
301        tcx.dcx().create_err(HigherRankedLifetimeError {
302            cause: Some(HigherRankedErrorCause::CouldNotNormalize {
303                value: self.canonical_query.canonical.value.value.value.to_string(),
304            }),
305            span,
306        })
307    }
308
309    fn base_universe(&self) -> ty::UniverseIndex {
310        self.base_universe
311    }
312
313    fn nice_error<'infcx>(
314        &self,
315        mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
316        cause: ObligationCause<'tcx>,
317        placeholder_region: ty::Region<'tcx>,
318        error_region: Option<ty::Region<'tcx>>,
319    ) -> Option<Diag<'infcx>> {
320        let (infcx, key, _) =
321            mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
322        let ocx = ObligationCtxt::new(&infcx);
323
324        let ty::ParamEnvAnd { param_env, value } = key;
325        let _ = ocx.deeply_normalize(&cause, param_env, value.value);
326
327        let diag = try_extract_error_from_fulfill_cx(
328            &ocx,
329            mbcx.mir_def_id(),
330            placeholder_region,
331            error_region,
332        )?
333        .with_dcx(mbcx.dcx());
334        Some(diag)
335    }
336}
337
338struct AscribeUserTypeQuery<'tcx> {
339    canonical_query: CanonicalTypeOpAscribeUserTypeGoal<'tcx>,
340    base_universe: ty::UniverseIndex,
341}
342
343impl<'tcx> TypeOpInfo<'tcx> for AscribeUserTypeQuery<'tcx> {
344    fn fallback_error(&self, tcx: TyCtxt<'tcx>, span: Span) -> Diag<'tcx> {
345        // FIXME: This error message isn't great, but it doesn't show up in the existing UI tests,
346        // and is only the fallback when the nice error fails. Consider improving this some more.
347        tcx.dcx().create_err(HigherRankedLifetimeError { cause: None, span })
348    }
349
350    fn base_universe(&self) -> ty::UniverseIndex {
351        self.base_universe
352    }
353
354    fn nice_error<'infcx>(
355        &self,
356        mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
357        cause: ObligationCause<'tcx>,
358        placeholder_region: ty::Region<'tcx>,
359        error_region: Option<ty::Region<'tcx>>,
360    ) -> Option<Diag<'infcx>> {
361        let (infcx, key, _) =
362            mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
363        let ocx = ObligationCtxt::new(&infcx);
364        type_op_ascribe_user_type_with_span(&ocx, key, cause.span).ok()?;
365        let diag = try_extract_error_from_fulfill_cx(
366            &ocx,
367            mbcx.mir_def_id(),
368            placeholder_region,
369            error_region,
370        )?
371        .with_dcx(mbcx.dcx());
372        Some(diag)
373    }
374}
375
376impl<'tcx> TypeOpInfo<'tcx> for crate::type_check::InstantiateOpaqueType<'tcx> {
377    fn fallback_error(&self, tcx: TyCtxt<'tcx>, span: Span) -> Diag<'tcx> {
378        // FIXME: This error message isn't great, but it doesn't show up in the existing UI tests,
379        // and is only the fallback when the nice error fails. Consider improving this some more.
380        tcx.dcx().create_err(HigherRankedLifetimeError { cause: None, span })
381    }
382
383    fn base_universe(&self) -> ty::UniverseIndex {
384        self.base_universe.unwrap()
385    }
386
387    fn nice_error<'infcx>(
388        &self,
389        mbcx: &mut MirBorrowckCtxt<'_, 'infcx, 'tcx>,
390        _cause: ObligationCause<'tcx>,
391        placeholder_region: ty::Region<'tcx>,
392        error_region: Option<ty::Region<'tcx>>,
393    ) -> Option<Diag<'infcx>> {
394        try_extract_error_from_region_constraints(
395            mbcx.infcx,
396            mbcx.mir_def_id(),
397            placeholder_region,
398            error_region,
399            self.region_constraints.as_ref().unwrap(),
400            // We're using the original `InferCtxt` that we
401            // started MIR borrowchecking with, so the region
402            // constraints have already been taken. Use the data from
403            // our `mbcx` instead.
404            |vid| RegionVariableOrigin::Nll(mbcx.regioncx.definitions[vid].origin),
405            |vid| mbcx.regioncx.definitions[vid].universe,
406        )
407    }
408}
409
410#[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("try_extract_error_from_fulfill_cx",
                                    "rustc_borrowck::diagnostics::bound_region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(410u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::bound_region_errors"),
                                    ::tracing_core::field::FieldSet::new(&["generic_param_scope",
                                                    "placeholder_region", "error_region"],
                                        ::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,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&generic_param_scope)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&placeholder_region)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&error_region)
                                                            as &dyn Value))])
                            })
                } 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: Option<Diag<'a>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let _errors = ocx.evaluate_obligations_error_on_ambiguity();
            let region_constraints =
                ocx.infcx.with_region_constraints(|r| r.clone());
            try_extract_error_from_region_constraints(ocx.infcx,
                generic_param_scope, placeholder_region, error_region,
                &region_constraints, |vid| ocx.infcx.region_var_origin(vid),
                |vid|
                    ocx.infcx.universe_of_region(ty::Region::new_var(ocx.infcx.tcx,
                            vid)))
        }
    }
}#[instrument(skip(ocx), level = "debug")]
411fn try_extract_error_from_fulfill_cx<'a, 'tcx>(
412    ocx: &ObligationCtxt<'a, 'tcx>,
413    generic_param_scope: LocalDefId,
414    placeholder_region: ty::Region<'tcx>,
415    error_region: Option<ty::Region<'tcx>>,
416) -> Option<Diag<'a>> {
417    // We generally shouldn't have errors here because the query was
418    // already run, but there's no point using `span_delayed_bug`
419    // when we're going to emit an error here anyway.
420    let _errors = ocx.evaluate_obligations_error_on_ambiguity();
421    let region_constraints = ocx.infcx.with_region_constraints(|r| r.clone());
422    try_extract_error_from_region_constraints(
423        ocx.infcx,
424        generic_param_scope,
425        placeholder_region,
426        error_region,
427        &region_constraints,
428        |vid| ocx.infcx.region_var_origin(vid),
429        |vid| ocx.infcx.universe_of_region(ty::Region::new_var(ocx.infcx.tcx, vid)),
430    )
431}
432
433#[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("try_extract_error_from_region_constraints",
                                    "rustc_borrowck::diagnostics::bound_region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(433u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::bound_region_errors"),
                                    ::tracing_core::field::FieldSet::new(&["generic_param_scope",
                                                    "placeholder_region", "error_region", "region_constraints"],
                                        ::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,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&generic_param_scope)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&placeholder_region)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&error_region)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&region_constraints)
                                                            as &dyn Value))])
                            })
                } 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: Option<Diag<'a>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let placeholder_universe =
                match placeholder_region.kind() {
                    ty::RePlaceholder(p) => p.universe,
                    ty::ReVar(vid) => universe_of_region(vid),
                    _ => ty::UniverseIndex::ROOT,
                };
            let regions_the_same =
                |a_region: Region<'tcx>, b_region: Region<'tcx>|
                    match (a_region.kind(), b_region.kind()) {
                        (RePlaceholder(a_p), RePlaceholder(b_p)) =>
                            a_p.bound == b_p.bound,
                        _ => a_region == b_region,
                    };
            let mut check =
                |c: &Constraint<'tcx>, cause: &SubregionOrigin<'tcx>, exact|
                    match c.kind {
                        ConstraintKind::RegSubReg if
                            ((exact && c.sup == placeholder_region) ||
                                        (!exact && regions_the_same(c.sup, placeholder_region))) &&
                                c.sup != c.sub => {
                            Some((c.sub, cause.clone()))
                        }
                        ConstraintKind::VarSubReg if
                            (exact && c.sup == placeholder_region &&
                                        !universe_of_region(c.sub.as_var()).can_name(placeholder_universe))
                                || (!exact && regions_the_same(c.sup, placeholder_region))
                            => {
                            Some((c.sub, cause.clone()))
                        }
                        _ => None,
                    };
            let mut find_culprit =
                |exact_match: bool|
                    {
                        region_constraints.constraints.iter().find_map(|(constraint,
                                    cause)| check(constraint, cause, exact_match))
                    };
            let (sub_region, cause) =
                find_culprit(true).or_else(|| find_culprit(false))?;
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs:482",
                                    "rustc_borrowck::diagnostics::bound_region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(482u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::bound_region_errors"),
                                    ::tracing_core::field::FieldSet::new(&["message",
                                                    "sub_region"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&format_args!("cause = {0:#?}",
                                                                cause) as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&sub_region)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let error =
                match (error_region, sub_region.kind()) {
                    (Some(error_region), ty::ReVar(vid)) =>
                        RegionResolutionError::SubSupConflict(vid,
                            region_var_origin(vid), cause.clone(), error_region,
                            cause.clone(), placeholder_region,
                            ::alloc::vec::Vec::new()),
                    (Some(error_region), _) => {
                        RegionResolutionError::ConcreteFailure(cause.clone(),
                            error_region, placeholder_region)
                    }
                    (None, ty::ReVar(vid)) =>
                        RegionResolutionError::UpperBoundUniverseConflict(vid,
                            region_var_origin(vid), universe_of_region(vid),
                            cause.clone(), placeholder_region),
                    (None, _) => {
                        RegionResolutionError::ConcreteFailure(cause.clone(),
                            sub_region, placeholder_region)
                    }
                };
            NiceRegionError::new(&infcx.err_ctxt(), generic_param_scope,
                        error).try_report_from_nll().or_else(||
                    {
                        if let SubregionOrigin::Subtype(trace) = cause {
                            Some(infcx.err_ctxt().report_and_explain_type_error(*trace,
                                    infcx.tcx.param_env(generic_param_scope),
                                    TypeError::RegionsPlaceholderMismatch))
                        } else { None }
                    })
        }
    }
}#[instrument(level = "debug", skip(infcx, region_var_origin, universe_of_region))]
434fn try_extract_error_from_region_constraints<'a, 'tcx>(
435    infcx: &'a InferCtxt<'tcx>,
436    generic_param_scope: LocalDefId,
437    placeholder_region: ty::Region<'tcx>,
438    error_region: Option<ty::Region<'tcx>>,
439    region_constraints: &RegionConstraintData<'tcx>,
440    mut region_var_origin: impl FnMut(RegionVid) -> RegionVariableOrigin<'tcx>,
441    mut universe_of_region: impl FnMut(RegionVid) -> UniverseIndex,
442) -> Option<Diag<'a>> {
443    let placeholder_universe = match placeholder_region.kind() {
444        ty::RePlaceholder(p) => p.universe,
445        ty::ReVar(vid) => universe_of_region(vid),
446        _ => ty::UniverseIndex::ROOT,
447    };
448    // Are the two regions the same?
449    let regions_the_same =
450        |a_region: Region<'tcx>, b_region: Region<'tcx>| match (a_region.kind(), b_region.kind()) {
451            (RePlaceholder(a_p), RePlaceholder(b_p)) => a_p.bound == b_p.bound,
452            _ => a_region == b_region,
453        };
454    let mut check = |c: &Constraint<'tcx>, cause: &SubregionOrigin<'tcx>, exact| match c.kind {
455        ConstraintKind::RegSubReg
456            if ((exact && c.sup == placeholder_region)
457                || (!exact && regions_the_same(c.sup, placeholder_region)))
458                && c.sup != c.sub =>
459        {
460            Some((c.sub, cause.clone()))
461        }
462        ConstraintKind::VarSubReg
463            if (exact
464                && c.sup == placeholder_region
465                && !universe_of_region(c.sub.as_var()).can_name(placeholder_universe))
466                || (!exact && regions_the_same(c.sup, placeholder_region)) =>
467        {
468            Some((c.sub, cause.clone()))
469        }
470        _ => None,
471    };
472
473    let mut find_culprit = |exact_match: bool| {
474        region_constraints
475            .constraints
476            .iter()
477            .find_map(|(constraint, cause)| check(constraint, cause, exact_match))
478    };
479
480    let (sub_region, cause) = find_culprit(true).or_else(|| find_culprit(false))?;
481
482    debug!(?sub_region, "cause = {:#?}", cause);
483    let error = match (error_region, sub_region.kind()) {
484        (Some(error_region), ty::ReVar(vid)) => RegionResolutionError::SubSupConflict(
485            vid,
486            region_var_origin(vid),
487            cause.clone(),
488            error_region,
489            cause.clone(),
490            placeholder_region,
491            vec![],
492        ),
493        (Some(error_region), _) => {
494            RegionResolutionError::ConcreteFailure(cause.clone(), error_region, placeholder_region)
495        }
496        // Note universe here is wrong...
497        (None, ty::ReVar(vid)) => RegionResolutionError::UpperBoundUniverseConflict(
498            vid,
499            region_var_origin(vid),
500            universe_of_region(vid),
501            cause.clone(),
502            placeholder_region,
503        ),
504        (None, _) => {
505            RegionResolutionError::ConcreteFailure(cause.clone(), sub_region, placeholder_region)
506        }
507    };
508    NiceRegionError::new(&infcx.err_ctxt(), generic_param_scope, error)
509        .try_report_from_nll()
510        .or_else(|| {
511            if let SubregionOrigin::Subtype(trace) = cause {
512                Some(infcx.err_ctxt().report_and_explain_type_error(
513                    *trace,
514                    infcx.tcx.param_env(generic_param_scope),
515                    TypeError::RegionsPlaceholderMismatch,
516                ))
517            } else {
518                None
519            }
520        })
521}