|
| 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 | + |
| 18 | +impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { |
| 19 | + // This method walks the Type of the function body arguments using |
| 20 | + // `fold_regions()` function and returns the |
| 21 | + // &hir::Arg of the function argument corresponding to the anonymous |
| 22 | + // region and the Ty corresponding to the named region. |
| 23 | + // Currently only the case where the function declaration consists of |
| 24 | + // one named region and one anonymous region is handled. |
| 25 | + // Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32` |
| 26 | + // Here, the `y` and the `Ty` of `y` is returned after being substituted |
| 27 | + // by that of the named region. |
| 28 | + pub fn find_arg_with_anonymous_region(&self, |
| 29 | + anon_region: Region<'tcx>, |
| 30 | + named_region: Region<'tcx>) |
| 31 | + -> Option<(&hir::Arg, ty::Ty<'tcx>)> { |
| 32 | + |
| 33 | + match *anon_region { |
| 34 | + ty::ReFree(ref free_region) => { |
| 35 | + |
| 36 | + let id = free_region.scope; |
| 37 | + let node_id = self.tcx.hir.as_local_node_id(id).unwrap(); |
| 38 | + let body_id = self.tcx.hir.maybe_body_owned_by(node_id).unwrap(); |
| 39 | + |
| 40 | + let body = self.tcx.hir.body(body_id); |
| 41 | + body.arguments |
| 42 | + .iter() |
| 43 | + .filter_map(|arg| if let Some(tables) = self.in_progress_tables { |
| 44 | + let ty = tables.borrow().node_id_to_type(arg.id); |
| 45 | + let mut found_anon_region = false; |
| 46 | + let new_arg_ty = self.tcx |
| 47 | + .fold_regions(&ty, |
| 48 | + &mut false, |
| 49 | + |r, _| if *r == *anon_region { |
| 50 | + found_anon_region = true; |
| 51 | + named_region |
| 52 | + } else { |
| 53 | + r |
| 54 | + }); |
| 55 | + if found_anon_region { |
| 56 | + return Some((arg, new_arg_ty)); |
| 57 | + } else { |
| 58 | + None |
| 59 | + } |
| 60 | + } else { |
| 61 | + None |
| 62 | + }) |
| 63 | + .next() |
| 64 | + } |
| 65 | + _ => None, |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + // This method generates the error message for the case when |
| 71 | + // the function arguments consist of a named region and an anonymous |
| 72 | + // region and corresponds to `ConcreteFailure(..)` |
| 73 | + pub fn report_named_anon_conflict(&self, error: &RegionResolutionError<'tcx>) -> bool { |
| 74 | + |
| 75 | + let (span, sub, sup) = match *error { |
| 76 | + ConcreteFailure(ref origin, sub, sup) => (origin.span(), sub, sup), |
| 77 | + _ => return false, // inapplicable |
| 78 | + }; |
| 79 | + |
| 80 | + let (named, (var, new_ty)) = |
| 81 | + if self.is_named_region(sub) && self.is_anonymous_region(sup) { |
| 82 | + (sub, self.find_arg_with_anonymous_region(sup, sub).unwrap()) |
| 83 | + } else if self.is_named_region(sup) && self.is_anonymous_region(sub) { |
| 84 | + (sup, self.find_arg_with_anonymous_region(sub, sup).unwrap()) |
| 85 | + } else { |
| 86 | + return false; // inapplicable |
| 87 | + }; |
| 88 | + |
| 89 | + if let Some(simple_name) = var.pat.simple_name() { |
| 90 | + struct_span_err!(self.tcx.sess, |
| 91 | + var.pat.span, |
| 92 | + E0611, |
| 93 | + "explicit lifetime required in the type of `{}`", |
| 94 | + simple_name) |
| 95 | + .span_label(var.pat.span, |
| 96 | + format!("consider changing the type of `{}` to `{}`", |
| 97 | + simple_name, |
| 98 | + new_ty)) |
| 99 | + .span_label(span, format!("lifetime `{}` required", named)) |
| 100 | + .emit(); |
| 101 | + |
| 102 | + } else { |
| 103 | + struct_span_err!(self.tcx.sess, |
| 104 | + var.pat.span, |
| 105 | + E0611, |
| 106 | + "explicit lifetime required in parameter type") |
| 107 | + .span_label(var.pat.span, |
| 108 | + format!("consider changing type to `{}`", new_ty)) |
| 109 | + .span_label(span, format!("lifetime `{}` required", named)) |
| 110 | + .emit(); |
| 111 | + } |
| 112 | + return true; |
| 113 | + |
| 114 | + } |
| 115 | +} |
0 commit comments