diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index 4d2a16aa609..cb4b154d271 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -567,15 +567,17 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { let lifetime = self.try_match_adt_and_generic_args(substs, needle_fr, args, search_stack)?; match lifetime.name { - hir::LifetimeName::Param(_) + hir::LifetimeName::Param(hir::ParamName::Plain(_) | hir::ParamName::Error) | hir::LifetimeName::Error - | hir::LifetimeName::Static - | hir::LifetimeName::Underscore => { + | hir::LifetimeName::Static => { let lifetime_span = lifetime.span; Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime_span)) } - hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Implicit => { + hir::LifetimeName::Param(hir::ParamName::Fresh(_)) + | hir::LifetimeName::ImplicitObjectLifetimeDefault + | hir::LifetimeName::Implicit + | hir::LifetimeName::Underscore => { // In this case, the user left off the lifetime; so // they wrote something like: // diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index a2bd11f8b8e..3a4a7d01823 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -161,45 +161,45 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>( { sp = param.span; } - let text = if br.name == kw::UnderscoreLifetime { - format!("the anonymous lifetime as defined here") - } else { + let text = if br.has_name() { format!("the lifetime `{}` as defined here", br.name) - }; - (text, sp) - } - ty::ReFree(ty::FreeRegion { - bound_region: ty::BoundRegionKind::BrNamed(_, name), .. - }) => { - let mut sp = sm.guess_head_span(tcx.def_span(scope)); - if let Some(param) = - tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name)) - { - sp = param.span; - } - let text = if name == kw::UnderscoreLifetime { - format!("the anonymous lifetime as defined here") } else { - format!("the lifetime `{}` as defined here", name) + format!("the anonymous lifetime as defined here") }; (text, sp) } - ty::ReFree(ref fr) => match fr.bound_region { - ty::BrAnon(idx) => { - if let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) { - ("the anonymous lifetime defined here".to_string(), ty.span) - } else { - ( + ty::ReFree(ref fr) => { + if !fr.bound_region.is_named() + && let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) + { + ("the anonymous lifetime defined here".to_string(), ty.span) + } else { + match fr.bound_region { + ty::BoundRegionKind::BrNamed(_, name) => { + let mut sp = sm.guess_head_span(tcx.def_span(scope)); + if let Some(param) = + tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name)) + { + sp = param.span; + } + let text = if name == kw::UnderscoreLifetime { + format!("the anonymous lifetime as defined here") + } else { + format!("the lifetime `{}` as defined here", name) + }; + (text, sp) + } + ty::BrAnon(idx) => ( format!("the anonymous lifetime #{} defined here", idx + 1), - tcx.def_span(scope), - ) + tcx.def_span(scope) + ), + _ => ( + format!("the lifetime `{}` as defined here", region), + sm.guess_head_span(tcx.def_span(scope)), + ), } } - _ => ( - format!("the lifetime `{}` as defined here", region), - sm.guess_head_span(tcx.def_span(scope)), - ), - }, + } _ => bug!(), } } @@ -2555,7 +2555,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { ty::ReEarlyBound(ty::EarlyBoundRegion { name, .. }) | ty::ReFree(ty::FreeRegion { bound_region: ty::BrNamed(_, name), .. }), _, - ) => { + ) if name != kw::UnderscoreLifetime => { // Does the required lifetime have a nice name we can print? let mut err = struct_span_err!( self.tcx.sess, diff --git a/src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait-async.stderr b/src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait-async.stderr index a0a42e3adbf..5b8b9bb68ad 100644 --- a/src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait-async.stderr +++ b/src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait-async.stderr @@ -2,9 +2,9 @@ error[E0700]: hidden type for `impl Trait` captures lifetime that does not appea --> $DIR/arbitrary_self_types_pin_lifetime_impl_trait-async.rs:8:48 | LL | async fn f(self: Pin<&Self>) -> impl Clone { self } - | - ^^^^^^^^ + | ----- ^^^^^^^^ | | - | hidden type `Pin<&Foo>` captures the anonymous lifetime as defined here + | hidden type `Pin<&Foo>` captures the anonymous lifetime defined here | help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound |