]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / compiler / rustc_infer / src / infer / error_reporting / nice_region_error / util.rs
1 //! Helper functions corresponding to lifetime errors due to
2 //! anonymous regions.
3
4 use crate::infer::error_reporting::nice_region_error::NiceRegionError;
5 use crate::infer::TyCtxt;
6 use rustc_hir as hir;
7 use rustc_hir::def_id::LocalDefId;
8 use rustc_middle::ty::{self, Binder, DefIdTree, Region, Ty, TypeFoldable};
9 use rustc_span::Span;
10
11 /// Information about the anonymous region we are searching for.
12 #[derive(Debug)]
13 pub struct AnonymousParamInfo<'tcx> {
14 /// The parameter corresponding to the anonymous region.
15 pub param: &'tcx hir::Param<'tcx>,
16 /// The type corresponding to the anonymous region parameter.
17 pub param_ty: Ty<'tcx>,
18 /// The ty::BoundRegionKind corresponding to the anonymous region.
19 pub bound_region: ty::BoundRegionKind,
20 /// The `Span` of the parameter type.
21 pub param_ty_span: Span,
22 /// Signals that the argument is the first parameter in the declaration.
23 pub is_first: bool,
24 }
25
26 // This method walks the Type of the function body parameters using
27 // `fold_regions()` function and returns the
28 // &hir::Param of the function parameter corresponding to the anonymous
29 // region and the Ty corresponding to the named region.
30 // Currently only the case where the function declaration consists of
31 // one named region and one anonymous region is handled.
32 // Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32`
33 // Here, we would return the hir::Param for y, we return the type &'a
34 // i32, which is the type of y but with the anonymous region replaced
35 // with 'a, the corresponding bound region and is_first which is true if
36 // the hir::Param is the first parameter in the function declaration.
37 #[instrument(skip(tcx), level = "debug")]
38 pub fn find_param_with_region<'tcx>(
39 tcx: TyCtxt<'tcx>,
40 anon_region: Region<'tcx>,
41 replace_region: Region<'tcx>,
42 ) -> Option<AnonymousParamInfo<'tcx>> {
43 let (id, bound_region) = match *anon_region {
44 ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
45 ty::ReEarlyBound(ebr) => {
46 (tcx.parent(ebr.def_id), ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name))
47 }
48 _ => return None, // not a free region
49 };
50
51 let hir = &tcx.hir();
52 let hir_id = hir.local_def_id_to_hir_id(id.as_local()?);
53 let body_id = hir.maybe_body_owned_by(hir_id)?;
54 let body = hir.body(body_id);
55
56 // Don't perform this on closures
57 match hir.get(hir_id) {
58 hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
59 return None;
60 }
61 _ => {}
62 }
63
64 let owner_id = hir.body_owner(body_id);
65 let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
66 let poly_fn_sig = tcx.fn_sig(id);
67
68 let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
69 body.params
70 .iter()
71 .take(if fn_sig.c_variadic {
72 fn_sig.inputs().len()
73 } else {
74 assert_eq!(fn_sig.inputs().len(), body.params.len());
75 body.params.len()
76 })
77 .enumerate()
78 .find_map(|(index, param)| {
79 // May return None; sometimes the tables are not yet populated.
80 let ty = fn_sig.inputs()[index];
81 let mut found_anon_region = false;
82 let new_param_ty = tcx.fold_regions(ty, &mut false, |r, _| {
83 if r == anon_region {
84 found_anon_region = true;
85 replace_region
86 } else {
87 r
88 }
89 });
90 if found_anon_region {
91 let ty_hir_id = fn_decl.inputs[index].hir_id;
92 let param_ty_span = hir.span(ty_hir_id);
93 let is_first = index == 0;
94 Some(AnonymousParamInfo {
95 param,
96 param_ty: new_param_ty,
97 param_ty_span,
98 bound_region,
99 is_first,
100 })
101 } else {
102 None
103 }
104 })
105 }
106
107 impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
108 pub(super) fn find_param_with_region(
109 &self,
110 anon_region: Region<'tcx>,
111 replace_region: Region<'tcx>,
112 ) -> Option<AnonymousParamInfo<'tcx>> {
113 find_param_with_region(self.tcx(), anon_region, replace_region)
114 }
115
116 // Here, we check for the case where the anonymous region
117 // is in the return type as written by the user.
118 // FIXME(#42703) - Need to handle certain cases here.
119 pub(super) fn is_return_type_anon(
120 &self,
121 scope_def_id: LocalDefId,
122 br: ty::BoundRegionKind,
123 hir_sig: &hir::FnSig<'_>,
124 ) -> Option<Span> {
125 let fn_ty = self.tcx().type_of(scope_def_id);
126 if let ty::FnDef(_, _) = fn_ty.kind() {
127 let ret_ty = fn_ty.fn_sig(self.tcx()).output();
128 let span = hir_sig.decl.output.span();
129 let future_output = if hir_sig.header.is_async() {
130 ret_ty.map_bound(|ty| self.infcx.get_impl_future_output_ty(ty)).transpose()
131 } else {
132 None
133 };
134 return match future_output {
135 Some(output) if self.includes_region(output, br) => Some(span),
136 None if self.includes_region(ret_ty, br) => Some(span),
137 _ => None,
138 };
139 }
140 None
141 }
142
143 fn includes_region(
144 &self,
145 ty: Binder<'tcx, impl TypeFoldable<'tcx>>,
146 region: ty::BoundRegionKind,
147 ) -> bool {
148 let late_bound_regions = self.tcx().collect_referenced_late_bound_regions(&ty);
149 late_bound_regions.iter().any(|r| *r == region)
150 }
151
152 // Here we check for the case where anonymous region
153 // corresponds to self and if yes, we display E0312.
154 // FIXME(#42700) - Need to format self properly to
155 // enable E0621 for it.
156 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 .map(|i| i.fn_has_self_parameter)
162 == Some(true)
163 }
164 }