]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/error_reporting/nice_region_error/util.rs
Rollup merge of #69766 - skade:make-point-copy-in-add-documentation, r=shepmaster
[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::ty::{self, DefIdTree, Region, Ty};
6 use rustc_hir as hir;
7 use rustc_hir::def_id::DefId;
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         if let Some(hir_id) = hir.as_local_hir_id(id) {
55             if let Some(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                 if let Some(tables) = self.tables {
60                     body.params
61                         .iter()
62                         .enumerate()
63                         .filter_map(|(index, param)| {
64                             // May return None; sometimes the tables are not yet populated.
65                             let ty_hir_id = fn_decl.inputs[index].hir_id;
66                             let param_ty_span = hir.span(ty_hir_id);
67                             let ty = tables.node_type_opt(param.hir_id)?;
68                             let mut found_anon_region = false;
69                             let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
70                                 if *r == *anon_region {
71                                     found_anon_region = true;
72                                     replace_region
73                                 } else {
74                                     r
75                                 }
76                             });
77                             if found_anon_region {
78                                 let is_first = index == 0;
79                                 Some(AnonymousParamInfo {
80                                     param,
81                                     param_ty: new_param_ty,
82                                     param_ty_span,
83                                     bound_region,
84                                     is_first,
85                                 })
86                             } else {
87                                 None
88                             }
89                         })
90                         .next()
91                 } else {
92                     None
93                 }
94             } else {
95                 None
96             }
97         } else {
98             None
99         }
100     }
101
102     // Here, we check for the case where the anonymous region
103     // is in the return type.
104     // FIXME(#42703) - Need to handle certain cases here.
105     pub(super) fn is_return_type_anon(
106         &self,
107         scope_def_id: DefId,
108         br: ty::BoundRegion,
109         decl: &hir::FnDecl<'_>,
110     ) -> Option<Span> {
111         let ret_ty = self.tcx().type_of(scope_def_id);
112         if let ty::FnDef(_, _) = ret_ty.kind {
113             let sig = ret_ty.fn_sig(self.tcx());
114             let late_bound_regions =
115                 self.tcx().collect_referenced_late_bound_regions(&sig.output());
116             if late_bound_regions.iter().any(|r| *r == br) {
117                 return Some(decl.output.span());
118             }
119         }
120         None
121     }
122
123     // Here we check for the case where anonymous region
124     // corresponds to self and if yes, we display E0312.
125     // FIXME(#42700) - Need to format self properly to
126     // enable E0621 for it.
127     pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: DefId) -> bool {
128         is_first
129             && self.tcx().opt_associated_item(scope_def_id).map(|i| i.method_has_self_argument)
130                 == Some(true)
131     }
132 }