|
| 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 | +//! Error Reporting for Anonymous Region Lifetime Errors. |
| 12 | +use hir; |
| 13 | +use infer::InferCtxt; |
| 14 | +use ty::{self, Region}; |
| 15 | +use infer::region_inference::RegionResolutionError::*; |
| 16 | +use infer::region_inference::RegionResolutionError; |
| 17 | +use hir::map as hir_map; |
| 18 | +use hir::def_id::DefId; |
| 19 | + |
| 20 | +impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { |
| 21 | + // This method walks the Type of the function body arguments using |
| 22 | + // `fold_regions()` function and returns the |
| 23 | + // &hir::Arg of the function argument corresponding to the anonymous |
| 24 | + // region and the Ty corresponding to the named region. |
| 25 | + // Currently only the case where the function declaration consists of |
| 26 | + // one named region and one anonymous region is handled. |
| 27 | + // Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32` |
| 28 | + // Here, we would return the hir::Arg for y, we return the type &'a |
| 29 | + // i32, which is the type of y but with the anonymous region replaced |
| 30 | + // with 'a, the corresponding bound region and is_first which is true if |
| 31 | + // the hir::Arg is the first argument in the function declaration. |
| 32 | + fn find_arg_with_anonymous_region |
| 33 | + (&self, |
| 34 | + anon_region: Region<'tcx>, |
| 35 | + named_region: Region<'tcx>) |
| 36 | + -> Option<(&hir::Arg, ty::Ty<'tcx>, ty::BoundRegion, bool)> { |
| 37 | + |
| 38 | + match *anon_region { |
| 39 | + ty::ReFree(ref free_region) => { |
| 40 | + |
| 41 | + let id = free_region.scope; |
| 42 | + let node_id = self.tcx.hir.as_local_node_id(id).unwrap(); |
| 43 | + let body_id = self.tcx.hir.maybe_body_owned_by(node_id).unwrap(); |
| 44 | + let body = self.tcx.hir.body(body_id); |
| 45 | + if let Some(tables) = self.in_progress_tables { |
| 46 | + body.arguments |
| 47 | + .iter() |
| 48 | + .enumerate() |
| 49 | + .filter_map(|(index, arg)| { |
| 50 | + let ty = tables.borrow().node_id_to_type(arg.id); |
| 51 | + let mut found_anon_region = false; |
| 52 | + let new_arg_ty = self.tcx |
| 53 | + .fold_regions(&ty, &mut false, |r, _| if *r == *anon_region { |
| 54 | + found_anon_region = true; |
| 55 | + named_region |
| 56 | + } else { |
| 57 | + r |
| 58 | + }); |
| 59 | + if found_anon_region { |
| 60 | + let is_first = index == 0; |
| 61 | + Some((arg, new_arg_ty, free_region.bound_region, is_first)) |
| 62 | + } else { |
| 63 | + None |
| 64 | + } |
| 65 | + }) |
| 66 | + .next() |
| 67 | + } else { |
| 68 | + None |
| 69 | + } |
| 70 | + } |
| 71 | + _ => None, |
| 72 | + |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // This method generates the error message for the case when |
| 77 | + // the function arguments consist of a named region and an anonymous |
| 78 | + // region and corresponds to `ConcreteFailure(..)` |
| 79 | + pub fn try_report_named_anon_conflict(&self, error: &RegionResolutionError<'tcx>) -> bool { |
| 80 | + |
| 81 | + let (span, sub, sup) = match *error { |
| 82 | + ConcreteFailure(ref origin, sub, sup) => (origin.span(), sub, sup), |
| 83 | + _ => return false, // inapplicable |
| 84 | + }; |
| 85 | + |
| 86 | + // Determine whether the sub and sup consist of one named region ('a) |
| 87 | + // and one anonymous (elided) region. If so, find the parameter arg |
| 88 | + // where the anonymous region appears (there must always be one; we |
| 89 | + // only introduced anonymous regions in parameters) as well as a |
| 90 | + // version new_ty of its type where the anonymous region is replaced |
| 91 | + // with the named one. |
| 92 | + let (named, (arg, new_ty, br, is_first), scope_def_id) = |
| 93 | + if sub.is_named_region() && self.is_suitable_anonymous_region(sup).is_some() { |
| 94 | + (sub, |
| 95 | + self.find_arg_with_anonymous_region(sup, sub).unwrap(), |
| 96 | + self.is_suitable_anonymous_region(sup).unwrap()) |
| 97 | + } else if sup.is_named_region() && self.is_suitable_anonymous_region(sub).is_some() { |
| 98 | + (sup, |
| 99 | + self.find_arg_with_anonymous_region(sub, sup).unwrap(), |
| 100 | + self.is_suitable_anonymous_region(sub).unwrap()) |
| 101 | + } else { |
| 102 | + return false; // inapplicable |
| 103 | + }; |
| 104 | + |
| 105 | + // Here, we check for the case where the anonymous region |
| 106 | + // is in the return type. |
| 107 | + // FIXME(#42703) - Need to handle certain cases here. |
| 108 | + let ret_ty = self.tcx.type_of(scope_def_id); |
| 109 | + match ret_ty.sty { |
| 110 | + ty::TyFnDef(_, _) => { |
| 111 | + let sig = ret_ty.fn_sig(self.tcx); |
| 112 | + let late_bound_regions = self.tcx |
| 113 | + .collect_referenced_late_bound_regions(&sig.output()); |
| 114 | + if late_bound_regions.iter().any(|r| *r == br) { |
| 115 | + return false; |
| 116 | + } else { |
| 117 | + } |
| 118 | + } |
| 119 | + _ => {} |
| 120 | + } |
| 121 | + |
| 122 | + // Here we check for the case where anonymous region |
| 123 | + // corresponds to self and if yes, we display E0312. |
| 124 | + // FIXME(#42700) - Need to format self properly to |
| 125 | + // enable E0621 for it. |
| 126 | + if is_first && |
| 127 | + self.tcx |
| 128 | + .opt_associated_item(scope_def_id) |
| 129 | + .map(|i| i.method_has_self_argument) |
| 130 | + .unwrap_or(false) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + let (error_var, span_label_var) = if let Some(simple_name) = arg.pat.simple_name() { |
| 135 | + (format!("the type of `{}`", simple_name), format!("the type of `{}`", simple_name)) |
| 136 | + } else { |
| 137 | + (format!("parameter type"), format!("type")) |
| 138 | + }; |
| 139 | + |
| 140 | + |
| 141 | + struct_span_err!(self.tcx.sess, |
| 142 | + span, |
| 143 | + E0621, |
| 144 | + "explicit lifetime required in {}", |
| 145 | + error_var) |
| 146 | + .span_label(arg.pat.span, |
| 147 | + format!("consider changing {} to `{}`", span_label_var, new_ty)) |
| 148 | + .span_label(span, format!("lifetime `{}` required", named)) |
| 149 | + .emit(); |
| 150 | + |
| 151 | + return true; |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + // This method returns whether the given Region is Anonymous |
| 156 | + // and returns the DefId corresponding to the region. |
| 157 | + pub fn is_suitable_anonymous_region(&self, region: Region<'tcx>) -> Option<DefId> { |
| 158 | + |
| 159 | + match *region { |
| 160 | + ty::ReFree(ref free_region) => { |
| 161 | + match free_region.bound_region { |
| 162 | + ty::BrAnon(..) => { |
| 163 | + let anonymous_region_binding_scope = free_region.scope; |
| 164 | + let node_id = self.tcx |
| 165 | + .hir |
| 166 | + .as_local_node_id(anonymous_region_binding_scope) |
| 167 | + .unwrap(); |
| 168 | + match self.tcx.hir.find(node_id) { |
| 169 | + Some(hir_map::NodeItem(..)) | |
| 170 | + Some(hir_map::NodeTraitItem(..)) => { |
| 171 | + // proceed ahead // |
| 172 | + } |
| 173 | + Some(hir_map::NodeImplItem(..)) => { |
| 174 | + let container_id = self.tcx |
| 175 | + .associated_item(anonymous_region_binding_scope) |
| 176 | + .container |
| 177 | + .id(); |
| 178 | + if self.tcx.impl_trait_ref(container_id).is_some() { |
| 179 | + // For now, we do not try to target impls of traits. This is |
| 180 | + // because this message is going to suggest that the user |
| 181 | + // change the fn signature, but they may not be free to do so, |
| 182 | + // since the signature must match the trait. |
| 183 | + // |
| 184 | + // FIXME(#42706) -- in some cases, we could do better here. |
| 185 | + return None; |
| 186 | + } |
| 187 | + } |
| 188 | + _ => return None, // inapplicable |
| 189 | + // we target only top-level functions |
| 190 | + } |
| 191 | + return Some(anonymous_region_binding_scope); |
| 192 | + } |
| 193 | + _ => None, |
| 194 | + } |
| 195 | + } |
| 196 | + _ => None, |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments