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 //! Error Reporting for static impl Traits.
13 use infer::error_reporting::nice_region_error::NiceRegionError;
14 use infer::lexical_region_resolve::RegionResolutionError;
15 use ty::{BoundRegion, FreeRegion, RegionKind};
16 use util::common::ErrorReported;
18 impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
19 /// Print the error message for lifetime errors when the return type is a static impl Trait.
20 pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorReported> {
21 if let Some(ref error) = self.error {
23 RegionResolutionError::SubSupConflict(
30 let anon_reg_sup = self.is_suitable_region(sup_r)?;
31 if sub_r == &RegionKind::ReStatic &&
32 self.is_return_type_impl_trait(anon_reg_sup.def_id)
34 let sp = var_origin.span();
35 let return_sp = sub_origin.span();
36 let mut err = self.tcx.sess.struct_span_err(
38 "cannot infer an appropriate lifetime",
42 "this return type evaluates to the `'static` lifetime...",
46 "...but this borrow...",
49 let (lifetime, lt_sp_opt) = self.tcx.msg_span_from_free_region(sup_r);
50 if let Some(lifetime_sp) = lt_sp_opt {
53 &format!("...can't outlive {}", lifetime),
57 let lifetime_name = match sup_r {
58 RegionKind::ReFree(FreeRegion {
59 bound_region: BoundRegion::BrNamed(_, ref name), ..
60 }) => format!("{}", name),
63 if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(return_sp) {
67 "you can add a constraint to the return type to make it last \
68 less than `'static` and match {}",
71 format!("{} + {}", snippet, lifetime_name),
75 return Some(ErrorReported);