1 //! Helper functions corresponding to lifetime errors due to
4 use crate::infer::error_reporting::nice_region_error::NiceRegionError;
5 use rustc::ty::{self, DefIdTree, Region, Ty};
7 use rustc_hir::def_id::DefId;
10 // The struct contains the information about the anonymous region
11 // we are searching for.
13 pub(super) struct AnonymousParamInfo<'tcx> {
14 // the parameter corresponding to the anonymous region
15 pub param: &'tcx hir::Param<'tcx>,
16 // the type corresponding to the anonymopus region parameter
17 pub param_ty: Ty<'tcx>,
18 // the ty::BoundRegion corresponding to the anonymous region
19 pub bound_region: ty::BoundRegion,
20 // param_ty_span contains span of parameter type
21 pub param_ty_span: Span,
22 // corresponds to id the argument is the first parameter
27 impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
28 // This method walks the Type of the function body parameters using
29 // `fold_regions()` function and returns the
30 // &hir::Param of the function parameter corresponding to the anonymous
31 // region and the Ty corresponding to the named region.
32 // Currently only the case where the function declaration consists of
33 // one named region and one anonymous region is handled.
34 // Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32`
35 // Here, we would return the hir::Param for y, we return the type &'a
36 // i32, which is the type of y but with the anonymous region replaced
37 // with 'a, the corresponding bound region and is_first which is true if
38 // the hir::Param is the first parameter in the function declaration.
39 pub(super) fn find_param_with_region(
41 anon_region: Region<'tcx>,
42 replace_region: Region<'tcx>,
43 ) -> Option<AnonymousParamInfo<'_>> {
44 let (id, bound_region) = match *anon_region {
45 ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
46 ty::ReEarlyBound(ebr) => (
47 self.tcx().parent(ebr.def_id).unwrap(),
48 ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
50 _ => return None, // not a free region
53 let hir = &self.tcx().hir();
54 let hir_id = hir.as_local_hir_id(id)?;
55 let body_id = hir.maybe_body_owned_by(hir_id)?;
56 let body = hir.body(body_id);
57 let owner_id = hir.body_owner(body_id);
58 let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
59 let poly_fn_sig = self.tcx().fn_sig(id);
60 let fn_sig = self.tcx().liberate_late_bound_regions(id, &poly_fn_sig);
64 .filter_map(|(index, param)| {
65 // May return None; sometimes the tables are not yet populated.
66 let ty = fn_sig.inputs()[index];
67 let mut found_anon_region = false;
68 let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
69 if *r == *anon_region {
70 found_anon_region = true;
76 if found_anon_region {
77 let ty_hir_id = fn_decl.inputs[index].hir_id;
78 let param_ty_span = hir.span(ty_hir_id);
79 let is_first = index == 0;
80 Some(AnonymousParamInfo {
82 param_ty: new_param_ty,
94 // Here, we check for the case where the anonymous region
95 // is in the return type.
96 // FIXME(#42703) - Need to handle certain cases here.
97 pub(super) fn is_return_type_anon(
101 decl: &hir::FnDecl<'_>,
103 let ret_ty = self.tcx().type_of(scope_def_id);
104 if let ty::FnDef(_, _) = ret_ty.kind {
105 let sig = ret_ty.fn_sig(self.tcx());
106 let late_bound_regions =
107 self.tcx().collect_referenced_late_bound_regions(&sig.output());
108 if late_bound_regions.iter().any(|r| *r == br) {
109 return Some(decl.output.span());
115 // Here we check for the case where anonymous region
116 // corresponds to self and if yes, we display E0312.
117 // FIXME(#42700) - Need to format self properly to
118 // enable E0621 for it.
119 pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: DefId) -> bool {
121 && self.tcx().opt_associated_item(scope_def_id).map(|i| i.method_has_self_argument)