]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/error_reporting/nice_region_error/util.rs
more LocalDefId cleanup
[rust.git] / src / librustc_infer / 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 // The struct contains the information about the anonymous region
11 // we are searching for.
12 #[derive(Debug)]
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
23     // in the declaration
24     pub is_first: bool,
25 }
26
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(
40         &self,
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),
49             ),
50             _ => return None, // not a free region
51         };
52
53         let hir = &self.tcx().hir();
54         let hir_id = hir.as_local_hir_id(id.as_local()?);
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);
61         body.params.iter().enumerate().find_map(|(index, param)| {
62             // May return None; sometimes the tables are not yet populated.
63             let ty = fn_sig.inputs()[index];
64             let mut found_anon_region = false;
65             let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
66                 if *r == *anon_region {
67                     found_anon_region = true;
68                     replace_region
69                 } else {
70                     r
71                 }
72             });
73             if found_anon_region {
74                 let ty_hir_id = fn_decl.inputs[index].hir_id;
75                 let param_ty_span = hir.span(ty_hir_id);
76                 let is_first = index == 0;
77                 Some(AnonymousParamInfo {
78                     param,
79                     param_ty: new_param_ty,
80                     param_ty_span,
81                     bound_region,
82                     is_first,
83                 })
84             } else {
85                 None
86             }
87         })
88     }
89
90     // Here, we check for the case where the anonymous region
91     // is in the return type.
92     // FIXME(#42703) - Need to handle certain cases here.
93     pub(super) fn is_return_type_anon(
94         &self,
95         scope_def_id: LocalDefId,
96         br: ty::BoundRegion,
97         decl: &hir::FnDecl<'_>,
98     ) -> Option<Span> {
99         let ret_ty = self.tcx().type_of(scope_def_id);
100         if let ty::FnDef(_, _) = ret_ty.kind {
101             let sig = ret_ty.fn_sig(self.tcx());
102             let late_bound_regions =
103                 self.tcx().collect_referenced_late_bound_regions(&sig.output());
104             if late_bound_regions.iter().any(|r| *r == br) {
105                 return Some(decl.output.span());
106             }
107         }
108         None
109     }
110
111     // Here we check for the case where anonymous region
112     // corresponds to self and if yes, we display E0312.
113     // FIXME(#42700) - Need to format self properly to
114     // enable E0621 for it.
115     pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: LocalDefId) -> bool {
116         is_first
117             && self
118                 .tcx()
119                 .opt_associated_item(scope_def_id.to_def_id())
120                 .map(|i| i.fn_has_self_parameter)
121                 == Some(true)
122     }
123 }