Skip to content

Commit

Permalink
Use lifetime name if available
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Jun 20, 2018
1 parent aaf78a5 commit 612657d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use infer::error_reporting::nice_region_error::NiceRegionError;
use infer::lexical_region_resolve::RegionResolutionError;
use ty::RegionKind;
use ty::{BoundRegion, FreeRegion, RegionKind};
use util::common::ErrorReported;

impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
Expand All @@ -29,7 +29,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
) => {
let anon_reg_sup = self.is_suitable_region(sup_r)?;
if sub_r == &RegionKind::ReStatic &&
self._is_return_type_impl_trait(anon_reg_sup.def_id)
self.is_return_type_impl_trait(anon_reg_sup.def_id)
{
let sp = var_origin.span();
let return_sp = sub_origin.span();
Expand All @@ -54,6 +54,12 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
);
}

let lifetime_name = match sup_r {
RegionKind::ReFree(FreeRegion {
bound_region: BoundRegion::BrNamed(_, ref name), ..
}) => format!("{}", name),
_ => "'_".to_owned(),
};
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(return_sp) {
err.span_suggestion(
return_sp,
Expand All @@ -62,7 +68,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
less than `'static` and match {}",
lifetime,
),
format!("{} + '_", snippet),
format!("{} + {}", snippet, lifetime_name),
);
}
err.emit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
None
}

pub(super) fn _is_return_type_impl_trait(
pub(super) fn is_return_type_impl_trait(
&self,
scope_def_id: DefId,
) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: you can add a constraint to the return type to make it last less than `'static` and match the lifetime 'a as defined on the method body at 20:5
|
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + '_ {
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
Expand Down

0 comments on commit 612657d

Please sign in to comment.