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.
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.
11 //! Helper functions corresponding to lifetime errors due to
12 //! anonymous regions.
15 use ty::{self, Region, Ty};
16 use hir::def_id::DefId;
17 use hir::map as hir_map;
19 macro_rules! or_false {
24 debug!("or_false failed: {}", stringify!($v));
31 // The struct contains the information about the anonymous region
32 // we are searching for.
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
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
46 // This struct contains information regarding the
47 // Refree((FreeRegion) corresponding to lifetime conflict
49 pub struct FreeRegionInfo {
50 // def id corresponding to FreeRegion
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,
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> {
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))
81 _ => return None, // not a free region
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 {
92 .filter_map(|(index, arg)| {
93 let ty = match tables.borrow().node_id_to_type_opt(arg.hir_id) {
95 None => return None, // sometimes the tables are not yet populated
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;
105 if found_anon_region {
106 let is_first = index == 0;
107 Some(AnonymousArgInfo {
110 bound_region: bound_region,
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> {
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))
138 _ => return None, // not a free region
141 let node_id = self.tcx
143 .as_local_node_id(suitable_region_binding_scope)
145 let is_impl_item = match self.tcx.hir.find(node_id) {
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)
155 return Some(FreeRegionInfo {
156 def_id: suitable_region_binding_scope,
157 boundregion: bound_region,
158 is_impl_item: is_impl_item,
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);
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) {
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 {
188 .opt_associated_item(scope_def_id)
189 .map(|i| i.method_has_self_argument) == Some(true)
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)
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.
204 // FIXME(#42706) -- in some cases, we could do better here.
210 // This method returns whether the given Region is Named
211 pub fn is_named_region(&self, region: Region<'tcx>) -> bool {
213 ty::ReFree(ref free_region) => {
214 match free_region.bound_region {
215 ty::BrNamed(..) => true,