rustc_trait_selection/error_reporting/infer/nice_region_error/
util.rs1use 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#[derive(Debug)]
14pub struct AnonymousParamInfo<'tcx> {
15 pub param: &'tcx hir::Param<'tcx>,
17 pub param_ty: Ty<'tcx>,
19 pub kind: ty::LateParamRegionKind,
21 pub param_ty_span: Span,
23 pub is_first: bool,
25}
26
27#[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.kind() {
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))
50 }
51 _ => return None, };
53
54 let def_id = id.as_local()?;
55
56 match tcx.hir_node_by_def_id(generic_param_scope) {
59 hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
60 return None;
61 }
62 _ => {}
63 }
64
65 let body = tcx.hir_maybe_body_owned_by(def_id)?;
66
67 let owner_id = tcx.hir_body_owner(body.id());
68 let fn_decl = tcx.hir_fn_decl_by_hir_id(owner_id)?;
69 let poly_fn_sig = tcx.fn_sig(id).instantiate_identity();
70
71 let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
72 body.params
73 .iter()
74 .take(if fn_sig.c_variadic {
75 fn_sig.inputs().len()
76 } else {
77 assert_eq!(fn_sig.inputs().len(), body.params.len());
78 body.params.len()
79 })
80 .enumerate()
81 .find_map(|(index, param)| {
82 let ty = fn_sig.inputs()[index];
84 let mut found_anon_region = false;
85 let new_param_ty = fold_regions(tcx, ty, |r, _| {
86 if r == anon_region {
87 found_anon_region = true;
88 replace_region
89 } else {
90 r
91 }
92 });
93 found_anon_region.then(|| {
94 let ty_hir_id = fn_decl.inputs[index].hir_id;
95 let param_ty_span = tcx.hir_span(ty_hir_id);
96 let is_first = index == 0;
97 AnonymousParamInfo { param, param_ty: new_param_ty, param_ty_span, kind, is_first }
98 })
99 })
100}
101
102impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
103 pub(super) fn find_param_with_region(
104 &self,
105 anon_region: Region<'tcx>,
106 replace_region: Region<'tcx>,
107 ) -> Option<AnonymousParamInfo<'tcx>> {
108 find_param_with_region(self.tcx(), self.generic_param_scope, anon_region, replace_region)
109 }
110
111 pub(super) fn is_return_type_anon(
115 &self,
116 scope_def_id: LocalDefId,
117 region_def_id: DefId,
118 hir_sig: &hir::FnSig<'_>,
119 ) -> Option<Span> {
120 let fn_ty = self.tcx().type_of(scope_def_id).instantiate_identity();
121 if let ty::FnDef(_, _) = fn_ty.kind() {
122 let ret_ty = fn_ty.fn_sig(self.tcx()).output();
123 let span = hir_sig.decl.output.span();
124 let future_output = if hir_sig.header.is_async() {
125 ret_ty.map_bound(|ty| self.cx.get_impl_future_output_ty(ty)).transpose()
126 } else {
127 None
128 };
129 return match future_output {
130 Some(output) if self.includes_region(output, region_def_id) => Some(span),
131 None if self.includes_region(ret_ty, region_def_id) => Some(span),
132 _ => None,
133 };
134 }
135 None
136 }
137
138 fn includes_region(
139 &self,
140 ty: Binder<'tcx, impl TypeFoldable<TyCtxt<'tcx>>>,
141 region_def_id: DefId,
142 ) -> bool {
143 let late_bound_regions = self.tcx().collect_referenced_late_bound_regions(ty);
144 #[allow(rustc::potential_query_instability)]
146 late_bound_regions.iter().any(|r| match *r {
147 ty::BoundRegionKind::Named(def_id) => def_id == region_def_id,
148 _ => false,
149 })
150 }
151
152 pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: LocalDefId) -> bool {
157 is_first
158 && self
159 .tcx()
160 .opt_associated_item(scope_def_id.to_def_id())
161 .is_some_and(|i| i.is_method())
162 }
163}