]>
Commit | Line | Data |
---|---|---|
3b2f2976 XL |
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; | |
ff7c6d11 | 14 | use infer::error_reporting::nice_region_error::NiceRegionError; |
ea8adc8c | 15 | use ty::{self, Region, Ty}; |
3b2f2976 XL |
16 | use hir::def_id::DefId; |
17 | use hir::map as hir_map; | |
ea8adc8c | 18 | use syntax_pos::Span; |
3b2f2976 | 19 | |
3b2f2976 XL |
20 | // The struct contains the information about the anonymous region |
21 | // we are searching for. | |
ea8adc8c | 22 | #[derive(Debug)] |
ff7c6d11 | 23 | pub(super) struct AnonymousArgInfo<'tcx> { |
3b2f2976 XL |
24 | // the argument corresponding to the anonymous region |
25 | pub arg: &'tcx hir::Arg, | |
26 | // the type corresponding to the anonymopus region argument | |
ea8adc8c | 27 | pub arg_ty: Ty<'tcx>, |
3b2f2976 XL |
28 | // the ty::BoundRegion corresponding to the anonymous region |
29 | pub bound_region: ty::BoundRegion, | |
30 | // corresponds to id the argument is the first parameter | |
31 | // in the declaration | |
32 | pub is_first: bool, | |
33 | } | |
34 | ||
35 | // This struct contains information regarding the | |
36 | // Refree((FreeRegion) corresponding to lifetime conflict | |
ea8adc8c | 37 | #[derive(Debug)] |
ff7c6d11 | 38 | pub(super) struct FreeRegionInfo { |
3b2f2976 XL |
39 | // def id corresponding to FreeRegion |
40 | pub def_id: DefId, | |
41 | // the bound region corresponding to FreeRegion | |
42 | pub boundregion: ty::BoundRegion, | |
43 | // checks if bound region is in Impl Item | |
44 | pub is_impl_item: bool, | |
45 | } | |
46 | ||
ff7c6d11 | 47 | impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { |
3b2f2976 XL |
48 | // This method walks the Type of the function body arguments using |
49 | // `fold_regions()` function and returns the | |
50 | // &hir::Arg of the function argument corresponding to the anonymous | |
51 | // region and the Ty corresponding to the named region. | |
52 | // Currently only the case where the function declaration consists of | |
53 | // one named region and one anonymous region is handled. | |
54 | // Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32` | |
55 | // Here, we would return the hir::Arg for y, we return the type &'a | |
56 | // i32, which is the type of y but with the anonymous region replaced | |
57 | // with 'a, the corresponding bound region and is_first which is true if | |
58 | // the hir::Arg is the first argument in the function declaration. | |
ff7c6d11 XL |
59 | pub(super) fn find_arg_with_region( |
60 | &self, | |
61 | anon_region: Region<'tcx>, | |
62 | replace_region: Region<'tcx>, | |
63 | ) -> Option<AnonymousArgInfo> { | |
ea8adc8c XL |
64 | let (id, bound_region) = match *anon_region { |
65 | ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region), | |
ff7c6d11 XL |
66 | ty::ReEarlyBound(ref ebr) => ( |
67 | self.tcx.parent_def_id(ebr.def_id).unwrap(), | |
68 | ty::BoundRegion::BrNamed(ebr.def_id, ebr.name), | |
69 | ), | |
ea8adc8c XL |
70 | _ => return None, // not a free region |
71 | }; | |
72 | ||
73 | let hir = &self.tcx.hir; | |
74 | if let Some(node_id) = hir.as_local_node_id(id) { | |
75 | if let Some(body_id) = hir.maybe_body_owned_by(node_id) { | |
76 | let body = hir.body(body_id); | |
ff7c6d11 | 77 | if let Some(tables) = self.tables { |
ea8adc8c XL |
78 | body.arguments |
79 | .iter() | |
80 | .enumerate() | |
81 | .filter_map(|(index, arg)| { | |
ff7c6d11 XL |
82 | // May return None; sometimes the tables are not yet populated. |
83 | let ty = tables.node_id_to_type_opt(arg.hir_id)?; | |
ea8adc8c | 84 | let mut found_anon_region = false; |
ff7c6d11 XL |
85 | let new_arg_ty = self.tcx.fold_regions(&ty, &mut false, |r, _| { |
86 | if *r == *anon_region { | |
ea8adc8c XL |
87 | found_anon_region = true; |
88 | replace_region | |
3b2f2976 | 89 | } else { |
ea8adc8c | 90 | r |
ff7c6d11 XL |
91 | } |
92 | }); | |
ea8adc8c XL |
93 | if found_anon_region { |
94 | let is_first = index == 0; | |
95 | Some(AnonymousArgInfo { | |
ff7c6d11 XL |
96 | arg: arg, |
97 | arg_ty: new_arg_ty, | |
98 | bound_region: bound_region, | |
99 | is_first: is_first, | |
100 | }) | |
ea8adc8c XL |
101 | } else { |
102 | None | |
103 | } | |
104 | }) | |
105 | .next() | |
3b2f2976 XL |
106 | } else { |
107 | None | |
108 | } | |
109 | } else { | |
110 | None | |
111 | } | |
112 | } else { | |
113 | None | |
114 | } | |
115 | } | |
116 | ||
ea8adc8c | 117 | // This method returns the DefId and the BoundRegion corresponding to the given region. |
ff7c6d11 | 118 | pub(super) fn is_suitable_region(&self, region: Region<'tcx>) -> Option<FreeRegionInfo> { |
ea8adc8c XL |
119 | let (suitable_region_binding_scope, bound_region) = match *region { |
120 | ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region), | |
ff7c6d11 XL |
121 | ty::ReEarlyBound(ref ebr) => ( |
122 | self.tcx.parent_def_id(ebr.def_id).unwrap(), | |
123 | ty::BoundRegion::BrNamed(ebr.def_id, ebr.name), | |
124 | ), | |
ea8adc8c XL |
125 | _ => return None, // not a free region |
126 | }; | |
127 | ||
128 | let node_id = self.tcx | |
129 | .hir | |
130 | .as_local_node_id(suitable_region_binding_scope) | |
131 | .unwrap(); | |
132 | let is_impl_item = match self.tcx.hir.find(node_id) { | |
ff7c6d11 | 133 | Some(hir_map::NodeItem(..)) | Some(hir_map::NodeTraitItem(..)) => false, |
ea8adc8c XL |
134 | Some(hir_map::NodeImplItem(..)) => { |
135 | self.is_bound_region_in_impl_item(suitable_region_binding_scope) | |
136 | } | |
137 | _ => return None, | |
138 | }; | |
139 | ||
140 | return Some(FreeRegionInfo { | |
ff7c6d11 XL |
141 | def_id: suitable_region_binding_scope, |
142 | boundregion: bound_region, | |
143 | is_impl_item: is_impl_item, | |
144 | }); | |
3b2f2976 XL |
145 | } |
146 | ||
147 | // Here, we check for the case where the anonymous region | |
148 | // is in the return type. | |
149 | // FIXME(#42703) - Need to handle certain cases here. | |
ff7c6d11 XL |
150 | pub(super) fn is_return_type_anon( |
151 | &self, | |
152 | scope_def_id: DefId, | |
153 | br: ty::BoundRegion, | |
154 | decl: &hir::FnDecl, | |
155 | ) -> Option<Span> { | |
3b2f2976 XL |
156 | let ret_ty = self.tcx.type_of(scope_def_id); |
157 | match ret_ty.sty { | |
158 | ty::TyFnDef(_, _) => { | |
159 | let sig = ret_ty.fn_sig(self.tcx); | |
160 | let late_bound_regions = self.tcx | |
161 | .collect_referenced_late_bound_regions(&sig.output()); | |
162 | if late_bound_regions.iter().any(|r| *r == br) { | |
ea8adc8c | 163 | return Some(decl.output.span()); |
3b2f2976 XL |
164 | } |
165 | } | |
166 | _ => {} | |
167 | } | |
ea8adc8c | 168 | None |
3b2f2976 XL |
169 | } |
170 | // Here we check for the case where anonymous region | |
171 | // corresponds to self and if yes, we display E0312. | |
172 | // FIXME(#42700) - Need to format self properly to | |
173 | // enable E0621 for it. | |
ff7c6d11 XL |
174 | pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: DefId) -> bool { |
175 | is_first | |
176 | && self.tcx | |
177 | .opt_associated_item(scope_def_id) | |
178 | .map(|i| i.method_has_self_argument) == Some(true) | |
3b2f2976 XL |
179 | } |
180 | ||
181 | // Here we check if the bound region is in Impl Item. | |
ff7c6d11 XL |
182 | pub(super) fn is_bound_region_in_impl_item( |
183 | &self, | |
184 | suitable_region_binding_scope: DefId, | |
185 | ) -> bool { | |
3b2f2976 | 186 | let container_id = self.tcx |
ea8adc8c | 187 | .associated_item(suitable_region_binding_scope) |
3b2f2976 XL |
188 | .container |
189 | .id(); | |
190 | if self.tcx.impl_trait_ref(container_id).is_some() { | |
191 | // For now, we do not try to target impls of traits. This is | |
192 | // because this message is going to suggest that the user | |
193 | // change the fn signature, but they may not be free to do so, | |
194 | // since the signature must match the trait. | |
195 | // | |
196 | // FIXME(#42706) -- in some cases, we could do better here. | |
197 | return true; | |
198 | } | |
199 | false | |
200 | } | |
201 | } |