]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs
New upstream version 1.48.0~beta.8+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 rustc_hir as hir;
6 use rustc_hir::def_id::LocalDefId;
7 use rustc_middle::ty::{self, DefIdTree, Region, Ty};
8 use rustc_span::Span;
9
10 /// Information about the anonymous region we are searching for.
11 #[derive(Debug)]
12 pub(super) struct AnonymousParamInfo<'tcx> {
13 /// The parameter corresponding to the anonymous region.
14 pub param: &'tcx hir::Param<'tcx>,
15 /// The type corresponding to the anonymous region parameter.
16 pub param_ty: Ty<'tcx>,
17 /// The ty::BoundRegion corresponding to the anonymous region.
18 pub bound_region: ty::BoundRegion,
19 /// The `Span` of the parameter type.
20 pub param_ty_span: Span,
21 /// Signals that the argument is the first parameter in the declaration.
22 pub is_first: bool,
23 }
24
25 impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
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 pub(super) fn find_param_with_region(
38 &self,
39 anon_region: Region<'tcx>,
40 replace_region: Region<'tcx>,
41 ) -> Option<AnonymousParamInfo<'_>> {
42 let (id, bound_region) = match *anon_region {
43 ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
44 ty::ReEarlyBound(ebr) => (
45 self.tcx().parent(ebr.def_id).unwrap(),
46 ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
47 ),
48 _ => return None, // not a free region
49 };
50
51 let hir = &self.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 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);
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
67 } else {
68 r
69 }
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 })
86 }
87
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.
91 pub(super) fn is_return_type_anon(
92 &self,
93 scope_def_id: LocalDefId,
94 br: ty::BoundRegion,
95 decl: &hir::FnDecl<'_>,
96 ) -> Option<Span> {
97 let ret_ty = self.tcx().type_of(scope_def_id);
98 if let ty::FnDef(_, _) = ret_ty.kind() {
99 let sig = ret_ty.fn_sig(self.tcx());
100 let late_bound_regions =
101 self.tcx().collect_referenced_late_bound_regions(&sig.output());
102 if late_bound_regions.iter().any(|r| *r == br) {
103 return Some(decl.output.span());
104 }
105 }
106 None
107 }
108
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.
113 pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: LocalDefId) -> bool {
114 is_first
115 && self
116 .tcx()
117 .opt_associated_item(scope_def_id.to_def_id())
118 .map(|i| i.fn_has_self_parameter)
119 == Some(true)
120 }
121 }