From 612657d9f0b158f0b78e85e3f0a694ddcbf83268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 20 Jun 2018 15:38:07 -0700 Subject: [PATCH] Use lifetime name if available --- .../nice_region_error/static_impl_trait.rs | 12 +++++++++--- .../infer/error_reporting/nice_region_error/util.rs | 2 +- .../impl-trait/static-return-lifetime-infered.stderr | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs b/src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs index bc6b7453c96e6..f9ec5fa13c960 100644 --- a/src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs +++ b/src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs @@ -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> { @@ -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(); @@ -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, @@ -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(); diff --git a/src/librustc/infer/error_reporting/nice_region_error/util.rs b/src/librustc/infer/error_reporting/nice_region_error/util.rs index 63e036166f8d7..1cc2b9d50b99b 100644 --- a/src/librustc/infer/error_reporting/nice_region_error/util.rs +++ b/src/librustc/infer/error_reporting/nice_region_error/util.rs @@ -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 { diff --git a/src/test/ui/impl-trait/static-return-lifetime-infered.stderr b/src/test/ui/impl-trait/static-return-lifetime-infered.stderr index 06459ef66195b..2795bb92ed56f 100644 --- a/src/test/ui/impl-trait/static-return-lifetime-infered.stderr +++ b/src/test/ui/impl-trait/static-return-lifetime-infered.stderr @@ -37,7 +37,7 @@ LL | fn iter_values<'a>(&'a self) -> impl Iterator { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 + '_ { +LL | fn iter_values<'a>(&'a self) -> impl Iterator + 'a { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 2 previous errors