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