Skip to content

Commit 612657d

Browse files
committed
Use lifetime name if available
1 parent aaf78a5 commit 612657d

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use infer::error_reporting::nice_region_error::NiceRegionError;
1414
use infer::lexical_region_resolve::RegionResolutionError;
15-
use ty::RegionKind;
15+
use ty::{BoundRegion, FreeRegion, RegionKind};
1616
use util::common::ErrorReported;
1717

1818
impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
@@ -29,7 +29,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
2929
) => {
3030
let anon_reg_sup = self.is_suitable_region(sup_r)?;
3131
if sub_r == &RegionKind::ReStatic &&
32-
self._is_return_type_impl_trait(anon_reg_sup.def_id)
32+
self.is_return_type_impl_trait(anon_reg_sup.def_id)
3333
{
3434
let sp = var_origin.span();
3535
let return_sp = sub_origin.span();
@@ -54,6 +54,12 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
5454
);
5555
}
5656

57+
let lifetime_name = match sup_r {
58+
RegionKind::ReFree(FreeRegion {
59+
bound_region: BoundRegion::BrNamed(_, ref name), ..
60+
}) => format!("{}", name),
61+
_ => "'_".to_owned(),
62+
};
5763
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(return_sp) {
5864
err.span_suggestion(
5965
return_sp,
@@ -62,7 +68,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
6268
less than `'static` and match {}",
6369
lifetime,
6470
),
65-
format!("{} + '_", snippet),
71+
format!("{} + {}", snippet, lifetime_name),
6672
);
6773
}
6874
err.emit();

src/librustc/infer/error_reporting/nice_region_error/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
168168
None
169169
}
170170

171-
pub(super) fn _is_return_type_impl_trait(
171+
pub(super) fn is_return_type_impl_trait(
172172
&self,
173173
scope_def_id: DefId,
174174
) -> bool {

src/test/ui/impl-trait/static-return-lifetime-infered.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838
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
3939
|
40-
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + '_ {
40+
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4242

4343
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)