Skip to main content

rustc_trait_selection/error_reporting/infer/nice_region_error/
util.rs

1//! Helper functions corresponding to lifetime errors due to
2//! anonymous regions.
3
4use rustc_hir as hir;
5use rustc_hir::def_id::{DefId, LocalDefId};
6use rustc_middle::ty::{self, Binder, Region, Ty, TyCtxt, TypeFoldable, fold_regions};
7use rustc_span::Span;
8use tracing::instrument;
9
10use crate::error_reporting::infer::nice_region_error::NiceRegionError;
11
12/// Information about the anonymous region we are searching for.
13#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for AnonymousParamInfo<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            &["param", "param_ty", "orig_param_ty", "kind", "param_ty_span",
                        "is_first"];
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.param, &self.param_ty, &self.orig_param_ty, &self.kind,
                        &self.param_ty_span, &&self.is_first];
        ::core::fmt::Formatter::debug_struct_fields_finish(f,
            "AnonymousParamInfo", names, values)
    }
}Debug)]
14pub struct AnonymousParamInfo<'tcx> {
15    /// The parameter corresponding to the anonymous region.
16    pub param: &'tcx hir::Param<'tcx>,
17    /// The type corresponding to the anonymous region parameter.
18    pub param_ty: Ty<'tcx>,
19    /// The original type before region replacement.
20    pub orig_param_ty: Ty<'tcx>,
21    /// The `ty::LateParamRegionKind` corresponding to the anonymous region.
22    pub kind: ty::LateParamRegionKind,
23    /// The `Span` of the parameter type.
24    pub param_ty_span: Span,
25    /// Signals that the argument is the first parameter in the declaration.
26    pub is_first: bool,
27}
28
29// This method walks the Type of the function body parameters using
30// `fold_regions()` function and returns the
31// &hir::Param of the function parameter corresponding to the anonymous
32// region and the Ty corresponding to the named region.
33// Currently only the case where the function declaration consists of
34// one named region and one anonymous region is handled.
35// Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32`
36// Here, we would return the hir::Param for y, we return the type &'a
37// i32, which is the type of y but with the anonymous region replaced
38// with 'a, the corresponding bound region and is_first which is true if
39// the hir::Param is the first parameter in the function declaration.
40#[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("find_param_with_region",
                                    "rustc_trait_selection::error_reporting::infer::nice_region_error::util",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/util.rs"),
                                    ::tracing_core::__macro_support::Option::Some(40u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::nice_region_error::util"),
                                    ::tracing_core::field::FieldSet::new(&["generic_param_scope",
                                                    "anon_region", "replace_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(&anon_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(&replace_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<AnonymousParamInfo<'tcx>> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let (id, kind) =
                match anon_region.kind() {
                    ty::ReLateParam(late_param) =>
                        (late_param.scope, late_param.kind),
                    ty::ReEarlyParam(ebr) => {
                        let region_def =
                            tcx.generics_of(generic_param_scope).region_param(ebr,
                                    tcx).def_id;
                        (tcx.parent(region_def),
                            ty::LateParamRegionKind::Named(region_def))
                    }
                    _ => return None,
                };
            let def_id = id.as_local()?;
            match tcx.hir_node_by_def_id(generic_param_scope) {
                hir::Node::Expr(&hir::Expr {
                    kind: hir::ExprKind::Closure { .. }, .. }) => {
                    return None;
                }
                _ => {}
            }
            let body = tcx.hir_maybe_body_owned_by(def_id)?;
            let owner_id = tcx.hir_body_owner(body.id());
            let fn_decl = tcx.hir_fn_decl_by_hir_id(owner_id)?;
            let poly_fn_sig =
                tcx.fn_sig(id).instantiate_identity().skip_norm_wip();
            let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
            body.params.iter().take(if fn_sig.c_variadic() {
                            fn_sig.inputs().len()
                        } else {
                            match (&fn_sig.inputs().len(), &body.params.len()) {
                                (left_val, right_val) => {
                                    if !(*left_val == *right_val) {
                                        let kind = ::core::panicking::AssertKind::Eq;
                                        ::core::panicking::assert_failed(kind, &*left_val,
                                            &*right_val, ::core::option::Option::None);
                                    }
                                }
                            };
                            body.params.len()
                        }).enumerate().find_map(|(index, param)|
                    {
                        let ty = fn_sig.inputs()[index];
                        let mut found_anon_region = false;
                        let new_param_ty =
                            fold_regions(tcx, ty,
                                |r, _|
                                    {
                                        if r == anon_region {
                                            found_anon_region = true;
                                            replace_region
                                        } else { r }
                                    });
                        found_anon_region.then(||
                                {
                                    let ty_hir_id = fn_decl.inputs[index].hir_id;
                                    let param_ty_span = tcx.hir_span(ty_hir_id);
                                    let is_first = index == 0;
                                    AnonymousParamInfo {
                                        param,
                                        param_ty: new_param_ty,
                                        orig_param_ty: ty,
                                        param_ty_span,
                                        kind,
                                        is_first,
                                    }
                                })
                    })
        }
    }
}#[instrument(skip(tcx), level = "debug")]
41pub fn find_param_with_region<'tcx>(
42    tcx: TyCtxt<'tcx>,
43    generic_param_scope: LocalDefId,
44    anon_region: Region<'tcx>,
45    replace_region: Region<'tcx>,
46) -> Option<AnonymousParamInfo<'tcx>> {
47    let (id, kind) = match anon_region.kind() {
48        ty::ReLateParam(late_param) => (late_param.scope, late_param.kind),
49        ty::ReEarlyParam(ebr) => {
50            let region_def = tcx.generics_of(generic_param_scope).region_param(ebr, tcx).def_id;
51            (tcx.parent(region_def), ty::LateParamRegionKind::Named(region_def))
52        }
53        _ => return None, // not a free region
54    };
55
56    let def_id = id.as_local()?;
57
58    // FIXME: use def_kind
59    // Don't perform this on closures
60    match tcx.hir_node_by_def_id(generic_param_scope) {
61        hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
62            return None;
63        }
64        _ => {}
65    }
66
67    let body = tcx.hir_maybe_body_owned_by(def_id)?;
68
69    let owner_id = tcx.hir_body_owner(body.id());
70    let fn_decl = tcx.hir_fn_decl_by_hir_id(owner_id)?;
71    let poly_fn_sig = tcx.fn_sig(id).instantiate_identity().skip_norm_wip();
72
73    let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
74    body.params
75        .iter()
76        .take(if fn_sig.c_variadic() {
77            fn_sig.inputs().len()
78        } else {
79            assert_eq!(fn_sig.inputs().len(), body.params.len());
80            body.params.len()
81        })
82        .enumerate()
83        .find_map(|(index, param)| {
84            // May return None; sometimes the tables are not yet populated.
85            let ty = fn_sig.inputs()[index];
86            let mut found_anon_region = false;
87            let new_param_ty = fold_regions(tcx, ty, |r, _| {
88                if r == anon_region {
89                    found_anon_region = true;
90                    replace_region
91                } else {
92                    r
93                }
94            });
95            found_anon_region.then(|| {
96                let ty_hir_id = fn_decl.inputs[index].hir_id;
97                let param_ty_span = tcx.hir_span(ty_hir_id);
98                let is_first = index == 0;
99                AnonymousParamInfo {
100                    param,
101                    param_ty: new_param_ty,
102                    orig_param_ty: ty,
103                    param_ty_span,
104                    kind,
105                    is_first,
106                }
107            })
108        })
109}
110
111impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
112    pub(super) fn find_param_with_region(
113        &self,
114        anon_region: Region<'tcx>,
115        replace_region: Region<'tcx>,
116    ) -> Option<AnonymousParamInfo<'tcx>> {
117        find_param_with_region(self.tcx(), self.generic_param_scope, anon_region, replace_region)
118    }
119
120    // Here, we check for the case where the anonymous region
121    // is in the return type as written by the user.
122    // FIXME(#42703) - Need to handle certain cases here.
123    pub(super) fn is_return_type_anon(
124        &self,
125        scope_def_id: LocalDefId,
126        region_def_id: DefId,
127        hir_sig: &hir::FnSig<'_>,
128    ) -> Option<Span> {
129        let fn_ty = self.tcx().type_of(scope_def_id).instantiate_identity().skip_norm_wip();
130        if let ty::FnDef(_, _) = fn_ty.kind() {
131            let ret_ty = fn_ty.fn_sig(self.tcx()).output();
132            let span = hir_sig.decl.output.span();
133            let future_output = if hir_sig.header.is_async() {
134                ret_ty.map_bound(|ty| self.cx.get_impl_future_output_ty(ty)).transpose()
135            } else {
136                None
137            };
138            return match future_output {
139                Some(output) if self.includes_region(output, region_def_id) => Some(span),
140                None if self.includes_region(ret_ty, region_def_id) => Some(span),
141                _ => None,
142            };
143        }
144        None
145    }
146
147    fn includes_region(
148        &self,
149        ty: Binder<'tcx, impl TypeFoldable<TyCtxt<'tcx>>>,
150        region_def_id: DefId,
151    ) -> bool {
152        let late_bound_regions = self.tcx().collect_referenced_late_bound_regions(ty);
153        // We are only checking is any region meets the condition so order doesn't matter
154        #[allow(rustc::potential_query_instability)]
155        late_bound_regions.iter().any(|r| match *r {
156            ty::BoundRegionKind::Named(def_id) => def_id == region_def_id,
157            _ => false,
158        })
159    }
160
161    // Here we check for the case where anonymous region
162    // corresponds to self and if yes, we display E0312.
163    // FIXME(#42700) - Need to format self properly to
164    // enable E0621 for it.
165    pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: LocalDefId) -> bool {
166        is_first
167            && self
168                .tcx()
169                .opt_associated_item(scope_def_id.to_def_id())
170                .is_some_and(|i| i.is_method())
171    }
172}