]> git.proxmox.com Git - rustc.git/blame - src/librustc_infer/infer/error_reporting/nice_region_error/util.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_infer / infer / error_reporting / nice_region_error / util.rs
CommitLineData
3b2f2976
XL
1//! Helper functions corresponding to lifetime errors due to
2//! anonymous regions.
b7449926 3
9fa01778 4use crate::infer::error_reporting::nice_region_error::NiceRegionError;
dfeec247 5use rustc_hir as hir;
f035d41b 6use rustc_hir::def_id::LocalDefId;
ba9703b0 7use rustc_middle::ty::{self, DefIdTree, Region, Ty};
dfeec247 8use rustc_span::Span;
3b2f2976 9
3dfed10e 10/// Information about the anonymous region we are searching for.
ea8adc8c 11#[derive(Debug)]
e1599b0c 12pub(super) struct AnonymousParamInfo<'tcx> {
3dfed10e 13 /// The parameter corresponding to the anonymous region.
dfeec247 14 pub param: &'tcx hir::Param<'tcx>,
3dfed10e 15 /// The type corresponding to the anonymous region parameter.
e1599b0c 16 pub param_ty: Ty<'tcx>,
3dfed10e 17 /// The ty::BoundRegion corresponding to the anonymous region.
3b2f2976 18 pub bound_region: ty::BoundRegion,
3dfed10e 19 /// The `Span` of the parameter type.
dfeec247 20 pub param_ty_span: Span,
3dfed10e 21 /// Signals that the argument is the first parameter in the declaration.
3b2f2976
XL
22 pub is_first: bool,
23}
24
dc9dc135 25impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
e1599b0c 26 // This method walks the Type of the function body parameters using
3b2f2976 27 // `fold_regions()` function and returns the
e1599b0c 28 // &hir::Param of the function parameter corresponding to the anonymous
3b2f2976
XL
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`
e1599b0c 33 // Here, we would return the hir::Param for y, we return the type &'a
3b2f2976
XL
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
e1599b0c
XL
36 // the hir::Param is the first parameter in the function declaration.
37 pub(super) fn find_param_with_region(
ff7c6d11
XL
38 &self,
39 anon_region: Region<'tcx>,
40 replace_region: Region<'tcx>,
e1599b0c 41 ) -> Option<AnonymousParamInfo<'_>> {
ea8adc8c
XL
42 let (id, bound_region) = match *anon_region {
43 ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
48663c56 44 ty::ReEarlyBound(ebr) => (
532ac7d7 45 self.tcx().parent(ebr.def_id).unwrap(),
ff7c6d11
XL
46 ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
47 ),
ea8adc8c
XL
48 _ => return None, // not a free region
49 };
50
9fa01778 51 let hir = &self.tcx().hir();
3dfed10e 52 let hir_id = hir.local_def_id_to_hir_id(id.as_local()?);
ba9703b0
XL
53 let body_id = hir.maybe_body_owned_by(hir_id)?;
54 let body = hir.body(body_id);
55 let owner_id = hir.body_owner(body_id);
56 let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
57 let poly_fn_sig = self.tcx().fn_sig(id);
58 let fn_sig = self.tcx().liberate_late_bound_regions(id, &poly_fn_sig);
f9f354fc
XL
59 body.params.iter().enumerate().find_map(|(index, param)| {
60 // May return None; sometimes the tables are not yet populated.
61 let ty = fn_sig.inputs()[index];
62 let mut found_anon_region = false;
63 let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
64 if *r == *anon_region {
65 found_anon_region = true;
66 replace_region
3b2f2976 67 } else {
f9f354fc 68 r
3b2f2976 69 }
f9f354fc
XL
70 });
71 if found_anon_region {
72 let ty_hir_id = fn_decl.inputs[index].hir_id;
73 let param_ty_span = hir.span(ty_hir_id);
74 let is_first = index == 0;
75 Some(AnonymousParamInfo {
76 param,
77 param_ty: new_param_ty,
78 param_ty_span,
79 bound_region,
80 is_first,
81 })
82 } else {
83 None
84 }
85 })
3b2f2976
XL
86 }
87
3b2f2976
XL
88 // Here, we check for the case where the anonymous region
89 // is in the return type.
90 // FIXME(#42703) - Need to handle certain cases here.
ff7c6d11
XL
91 pub(super) fn is_return_type_anon(
92 &self,
f035d41b 93 scope_def_id: LocalDefId,
ff7c6d11 94 br: ty::BoundRegion,
dfeec247 95 decl: &hir::FnDecl<'_>,
ff7c6d11 96 ) -> Option<Span> {
9fa01778 97 let ret_ty = self.tcx().type_of(scope_def_id);
e74abb32 98 if let ty::FnDef(_, _) = ret_ty.kind {
9fa01778 99 let sig = ret_ty.fn_sig(self.tcx());
dfeec247
XL
100 let late_bound_regions =
101 self.tcx().collect_referenced_late_bound_regions(&sig.output());
0bf4aa26
XL
102 if late_bound_regions.iter().any(|r| *r == br) {
103 return Some(decl.output.span());
3b2f2976 104 }
3b2f2976 105 }
ea8adc8c 106 None
3b2f2976 107 }
8faf50e0 108
3b2f2976
XL
109 // Here we check for the case where anonymous region
110 // corresponds to self and if yes, we display E0312.
111 // FIXME(#42700) - Need to format self properly to
112 // enable E0621 for it.
f035d41b 113 pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: LocalDefId) -> bool {
ff7c6d11 114 is_first
f035d41b
XL
115 && self
116 .tcx()
117 .opt_associated_item(scope_def_id.to_def_id())
118 .map(|i| i.fn_has_self_parameter)
dfeec247 119 == Some(true)
3b2f2976 120 }
3b2f2976 121}