diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 9e55f7e558999..f4eb861d61dae 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -50,6 +50,7 @@ use super::region_constraints::GenericKind; use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePairs}; use crate::infer; +use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type; use crate::traits::error_reporting::report_object_safety_error; use crate::traits::{ IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode, @@ -179,7 +180,14 @@ fn msg_span_from_early_bound_and_free_regions( } ty::ReFree(ref fr) => match fr.bound_region { ty::BrAnon(idx) => { - (format!("the anonymous lifetime #{} defined on", idx + 1), tcx.hir().span(node)) + if let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) { + ("the anonymous lifetime defined on".to_string(), ty.span) + } else { + ( + format!("the anonymous lifetime #{} defined on", idx + 1), + tcx.hir().span(node), + ) + } } _ => ( format!("the lifetime `{}` as defined on", region), diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.nll.stderr b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.nll.stderr index b359826cb4ae4..7e07a5775bb12 100644 --- a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.nll.stderr +++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.nll.stderr @@ -7,11 +7,11 @@ LL | | t.test(); LL | | }); | |______^ | -note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 19:1... - --> $DIR/missing-lifetimes-in-signature-2.rs:19:1 +note: the parameter type `T` must be valid for the anonymous lifetime defined on the function body at 19:24... + --> $DIR/missing-lifetimes-in-signature-2.rs:19:24 | LL | fn func(foo: &Foo, t: T) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^ error: aborting due to previous error diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr index c7def9b668d9c..4e7d52978400f 100644 --- a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr +++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr @@ -6,11 +6,11 @@ LL | fn func(foo: &Foo, t: T) { LL | foo.bar(move |_| { | ^^^ | -note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 19:1... - --> $DIR/missing-lifetimes-in-signature-2.rs:19:1 +note: the parameter type `T` must be valid for the anonymous lifetime defined on the function body at 19:24... + --> $DIR/missing-lifetimes-in-signature-2.rs:19:24 | LL | fn func(foo: &Foo, t: T) { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature-2.rs:20:13: 23:6]` will meet its required lifetime bounds --> $DIR/missing-lifetimes-in-signature-2.rs:20:9 | diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.nll.stderr b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.nll.stderr index 1bfcdab5d860d..b509610b89e26 100644 --- a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.nll.stderr +++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.nll.stderr @@ -25,14 +25,11 @@ error[E0311]: the parameter type `G` may not live long enough LL | fn bar(g: G, dest: &mut T) -> impl FnOnce() + '_ | ^^^^^^^^^^^^^^^^^^ | -note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 25:1... - --> $DIR/missing-lifetimes-in-signature.rs:25:1 - | -LL | / fn bar(g: G, dest: &mut T) -> impl FnOnce() + '_ -LL | | -LL | | where -LL | | G: Get - | |_____________^ +note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 25:26... + --> $DIR/missing-lifetimes-in-signature.rs:25:26 + | +LL | fn bar(g: G, dest: &mut T) -> impl FnOnce() + '_ + | ^^^^^^ error[E0311]: the parameter type `G` may not live long enough --> $DIR/missing-lifetimes-in-signature.rs:47:45 @@ -40,14 +37,11 @@ error[E0311]: the parameter type `G` may not live long enough LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ | ^^^^^^^^^^^^^^^^^^ | -note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 47:1... - --> $DIR/missing-lifetimes-in-signature.rs:47:1 +note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 47:34... + --> $DIR/missing-lifetimes-in-signature.rs:47:34 | -LL | / fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ -LL | | -LL | | where -LL | | G: Get - | |_____________^ +LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + | ^^^^^^ error[E0311]: the parameter type `G` may not live long enough --> $DIR/missing-lifetimes-in-signature.rs:59:58 @@ -55,11 +49,11 @@ error[E0311]: the parameter type `G` may not live long enough LL | fn qux<'b, G: Get + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ { | ^^^^^^^^^^^^^^^^^^ | -note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the method body at 59:5... - --> $DIR/missing-lifetimes-in-signature.rs:59:5 +note: the parameter type `G` must be valid for the anonymous lifetime defined on the method body at 59:47... + --> $DIR/missing-lifetimes-in-signature.rs:59:47 | LL | fn qux<'b, G: Get + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^ error[E0311]: the parameter type `G` may not live long enough --> $DIR/missing-lifetimes-in-signature.rs:68:45 @@ -67,14 +61,11 @@ error[E0311]: the parameter type `G` may not live long enough LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a | ^^^^^^^^^^^^^^^^^^^^^^^ | -note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 68:1... - --> $DIR/missing-lifetimes-in-signature.rs:68:1 +note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 68:34... + --> $DIR/missing-lifetimes-in-signature.rs:68:34 | -LL | / fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a -LL | | -LL | | where -LL | | G: Get - | |_____________^ +LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a + | ^^^^^^ error[E0621]: explicit lifetime required in the type of `dest` --> $DIR/missing-lifetimes-in-signature.rs:73:5 diff --git a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr index 69e95efa72d50..789fff7acc29b 100644 --- a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr +++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr @@ -33,14 +33,11 @@ error[E0311]: the parameter type `G` may not live long enough LL | fn bar(g: G, dest: &mut T) -> impl FnOnce() + '_ | ^^^^^^^^^^^^^^^^^^ | -note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 25:1... - --> $DIR/missing-lifetimes-in-signature.rs:25:1 - | -LL | / fn bar(g: G, dest: &mut T) -> impl FnOnce() + '_ -LL | | -LL | | where -LL | | G: Get - | |_____________^ +note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 25:26... + --> $DIR/missing-lifetimes-in-signature.rs:25:26 + | +LL | fn bar(g: G, dest: &mut T) -> impl FnOnce() + '_ + | ^^^^^^ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:30:5: 32:6]` will meet its required lifetime bounds --> $DIR/missing-lifetimes-in-signature.rs:25:37 | @@ -57,14 +54,11 @@ error[E0311]: the parameter type `G` may not live long enough LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ | ^^^^^^^^^^^^^^^^^^ | -note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 47:1... - --> $DIR/missing-lifetimes-in-signature.rs:47:1 +note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 47:34... + --> $DIR/missing-lifetimes-in-signature.rs:47:34 | -LL | / fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ -LL | | -LL | | where -LL | | G: Get - | |_____________^ +LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + | ^^^^^^ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:52:5: 54:6]` will meet its required lifetime bounds --> $DIR/missing-lifetimes-in-signature.rs:47:45 | @@ -81,11 +75,11 @@ error[E0311]: the parameter type `G` may not live long enough LL | fn qux<'b, G: Get + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ { | ^^^^^^^^^^^^^^^^^^ | -note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the method body at 59:5... - --> $DIR/missing-lifetimes-in-signature.rs:59:5 +note: the parameter type `G` must be valid for the anonymous lifetime defined on the method body at 59:47... + --> $DIR/missing-lifetimes-in-signature.rs:59:47 | LL | fn qux<'b, G: Get + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:61:9: 63:10]` will meet its required lifetime bounds --> $DIR/missing-lifetimes-in-signature.rs:59:58 |