]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/error_reporting/util.rs
Rollup merge of #44562 - eddyb:ugh-rustdoc, r=nikomatsakis
[rust.git] / src / librustc / infer / error_reporting / util.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Helper functions corresponding to lifetime errors due to
12 //! anonymous regions.
13 use hir;
14 use infer::InferCtxt;
15 use ty::{self, Region, Ty};
16 use hir::def_id::DefId;
17 use hir::map as hir_map;
18
19 macro_rules! or_false {
20      ($v:expr) => {
21          match $v {
22              Some(v) => v,
23              None => {
24                  debug!("or_false failed: {}", stringify!($v));
25                  return false;
26              }
27          }
28      }
29 }
30
31 // The struct contains the information about the anonymous region
32 // we are searching for.
33 #[derive(Debug)]
34 pub struct AnonymousArgInfo<'tcx> {
35     // the argument corresponding to the anonymous region
36     pub arg: &'tcx hir::Arg,
37     // the type corresponding to the anonymopus region argument
38     pub arg_ty: Ty<'tcx>,
39     // the ty::BoundRegion corresponding to the anonymous region
40     pub bound_region: ty::BoundRegion,
41     // corresponds to id the argument is the first parameter
42     // in the declaration
43     pub is_first: bool,
44 }
45
46 // This struct contains information regarding the
47 // Refree((FreeRegion) corresponding to lifetime conflict
48 #[derive(Debug)]
49 pub struct FreeRegionInfo {
50     // def id corresponding to FreeRegion
51     pub def_id: DefId,
52     // the bound region corresponding to FreeRegion
53     pub boundregion: ty::BoundRegion,
54     // checks if bound region is in Impl Item
55     pub is_impl_item: bool,
56 }
57
58 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
59     // This method walks the Type of the function body arguments using
60     // `fold_regions()` function and returns the
61     // &hir::Arg of the function argument corresponding to the anonymous
62     // region and the Ty corresponding to the named region.
63     // Currently only the case where the function declaration consists of
64     // one named region and one anonymous region is handled.
65     // Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32`
66     // Here, we would return the hir::Arg for y, we return the type &'a
67     // i32, which is the type of y but with the anonymous region replaced
68     // with 'a, the corresponding bound region and is_first which is true if
69     // the hir::Arg is the first argument in the function declaration.
70     pub fn find_arg_with_region(&self,
71                                 anon_region: Region<'tcx>,
72                                 replace_region: Region<'tcx>)
73                                 -> Option<AnonymousArgInfo> {
74
75         let (id, bound_region) = match *anon_region {
76             ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
77             ty::ReEarlyBound(ref ebr) => {
78                 (self.tcx.parent_def_id(ebr.def_id).unwrap(),
79                  ty::BoundRegion::BrNamed(ebr.def_id, ebr.name))
80             }
81             _ => return None, // not a free region
82         };
83
84         let hir = &self.tcx.hir;
85         if let Some(node_id) = hir.as_local_node_id(id) {
86             if let Some(body_id) = hir.maybe_body_owned_by(node_id) {
87                 let body = hir.body(body_id);
88                 if let Some(tables) = self.in_progress_tables {
89                     body.arguments
90                         .iter()
91                         .enumerate()
92                         .filter_map(|(index, arg)| {
93                             let ty = match tables.borrow().node_id_to_type_opt(arg.hir_id) {
94                                 Some(v) => v,
95                                 None => return None, // sometimes the tables are not yet populated
96                             };
97                             let mut found_anon_region = false;
98                             let new_arg_ty = self.tcx
99                                 .fold_regions(&ty, &mut false, |r, _| if *r == *anon_region {
100                                     found_anon_region = true;
101                                     replace_region
102                                 } else {
103                                     r
104                                 });
105                             if found_anon_region {
106                                 let is_first = index == 0;
107                                 Some(AnonymousArgInfo {
108                                          arg: arg,
109                                          arg_ty: new_arg_ty,
110                                          bound_region: bound_region,
111                                          is_first: is_first,
112                                      })
113                             } else {
114                                 None
115                             }
116                         })
117                         .next()
118                 } else {
119                     None
120                 }
121             } else {
122                 None
123             }
124         } else {
125             None
126         }
127     }
128
129     // This method returns the DefId and the BoundRegion corresponding to the given region.
130     pub fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
131
132         let (suitable_region_binding_scope, bound_region) = match *region {
133             ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
134             ty::ReEarlyBound(ref ebr) => {
135                 (self.tcx.parent_def_id(ebr.def_id).unwrap(),
136                  ty::BoundRegion::BrNamed(ebr.def_id, ebr.name))
137             }
138             _ => return None, // not a free region
139         };
140
141         let node_id = self.tcx
142             .hir
143             .as_local_node_id(suitable_region_binding_scope)
144             .unwrap();
145         let is_impl_item = match self.tcx.hir.find(node_id) {
146
147             Some(hir_map::NodeItem(..)) |
148             Some(hir_map::NodeTraitItem(..)) => false,
149             Some(hir_map::NodeImplItem(..)) => {
150                 self.is_bound_region_in_impl_item(suitable_region_binding_scope)
151             }
152             _ => return None,
153         };
154
155         return Some(FreeRegionInfo {
156                         def_id: suitable_region_binding_scope,
157                         boundregion: bound_region,
158                         is_impl_item: is_impl_item,
159                     });
160
161     }
162
163     // Here, we check for the case where the anonymous region
164     // is in the return type.
165     // FIXME(#42703) - Need to handle certain cases here.
166     pub fn is_return_type_anon(&self, scope_def_id: DefId, br: ty::BoundRegion) -> bool {
167         let ret_ty = self.tcx.type_of(scope_def_id);
168         match ret_ty.sty {
169             ty::TyFnDef(_, _) => {
170                 let sig = ret_ty.fn_sig(self.tcx);
171                 let late_bound_regions = self.tcx
172                     .collect_referenced_late_bound_regions(&sig.output());
173                 if late_bound_regions.iter().any(|r| *r == br) {
174                     return true;
175                 }
176             }
177             _ => {}
178         }
179         false
180     }
181     // Here we check for the case where anonymous region
182     // corresponds to self and if yes, we display E0312.
183     // FIXME(#42700) - Need to format self properly to
184     // enable E0621 for it.
185     pub fn is_self_anon(&self, is_first: bool, scope_def_id: DefId) -> bool {
186         is_first &&
187         self.tcx
188             .opt_associated_item(scope_def_id)
189             .map(|i| i.method_has_self_argument) == Some(true)
190     }
191
192     // Here we check if the bound region is in Impl Item.
193     pub fn is_bound_region_in_impl_item(&self, suitable_region_binding_scope: DefId) -> bool {
194         let container_id = self.tcx
195             .associated_item(suitable_region_binding_scope)
196             .container
197             .id();
198         if self.tcx.impl_trait_ref(container_id).is_some() {
199             // For now, we do not try to target impls of traits. This is
200             // because this message is going to suggest that the user
201             // change the fn signature, but they may not be free to do so,
202             // since the signature must match the trait.
203             //
204             // FIXME(#42706) -- in some cases, we could do better here.
205             return true;
206         }
207         false
208     }
209
210     // This method returns whether the given Region is Named
211     pub fn is_named_region(&self, region: Region<'tcx>) -> bool {
212         match *region {
213             ty::ReFree(ref free_region) => {
214                 match free_region.bound_region {
215                     ty::BrNamed(..) => true,
216                     _ => false,
217                 }
218             }
219             _ => false,
220         }
221     }
222 }