From 731ea85f215f03fc33a92147d6cc51a01dee589f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 26 May 2020 17:20:08 -0700 Subject: [PATCH] review comment: tweak wording and account for span overlap --- .../infer/error_reporting/mod.rs | 3 +-- .../nice_region_error/static_impl_trait.rs | 19 +++++++++++++++++-- .../ui/async-await/issues/issue-62097.stderr | 2 +- .../must_outlive_least_region_or_bound.stderr | 6 +++--- .../static-return-lifetime-infered.stderr | 4 ++-- src/test/ui/issues/issue-16922.stderr | 2 +- ...ect-lifetime-default-from-box-error.stderr | 5 +---- .../region-object-lifetime-in-coercion.stderr | 2 +- ...types_pin_lifetime_impl_trait-async.stderr | 2 +- ..._self_types_pin_lifetime_impl_trait.stderr | 2 +- .../missing-lifetimes-in-signature.stderr | 6 +++--- .../dyn-trait-underscore.stderr | 10 ++++++---- 12 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/librustc_infer/infer/error_reporting/mod.rs b/src/librustc_infer/infer/error_reporting/mod.rs index a7b40d39215e..d00c90d06e4c 100644 --- a/src/librustc_infer/infer/error_reporting/mod.rs +++ b/src/librustc_infer/infer/error_reporting/mod.rs @@ -1806,8 +1806,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { |err: &mut DiagnosticBuilder<'tcx>, type_param_span: Option<(Span, bool, bool)>, bound_kind: GenericKind<'tcx>| { - let msg = "consider introducing an explicit lifetime bound to unify the type \ - parameter and the output"; + let msg = "consider introducing an explicit lifetime bound"; if let Some((sp, has_lifetimes, is_impl_trait)) = type_param_span { let suggestion = if is_impl_trait { (sp.shrink_to_hi(), format!(" + {}", new_lt)) diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs index 1a8b7fda179b..5c5c86a4fb11 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs @@ -27,8 +27,23 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { let return_sp = sub_origin.span(); let mut err = self.tcx().sess.struct_span_err(sp, "cannot infer an appropriate lifetime"); - err.span_label(return_sp, "this evaluates to the `'static` lifetime..."); - err.span_label(sup_origin.span(), "...but this borrow..."); + if sp == sup_origin.span() && return_sp == sp { + // Example: `ui/object-lifetime/object-lifetime-default-from-box-error.rs` + err.span_label( + sup_origin.span(), + "this needs to be `'static` but the borrow...", + ); + } else { + err.span_label(return_sp, "this is `'static`..."); + // We try to make the output have fewer overlapping spans if possible. + if sp == sup_origin.span() || !return_sp.overlaps(sup_origin.span()) { + // When `sp == sup_origin` we already have overlapping spans in the + // main diagnostic output, so we don't split this into its own note. + err.span_label(sup_origin.span(), "...but this borrow..."); + } else { + err.span_note(sup_origin.span(), "...but this borrow..."); + } + } let (lifetime, lt_sp_opt) = msg_span_from_free_region(self.tcx(), sup_r); if let Some(lifetime_sp) = lt_sp_opt { diff --git a/src/test/ui/async-await/issues/issue-62097.stderr b/src/test/ui/async-await/issues/issue-62097.stderr index cd141b82e412..161b2565c3df 100644 --- a/src/test/ui/async-await/issues/issue-62097.stderr +++ b/src/test/ui/async-await/issues/issue-62097.stderr @@ -4,7 +4,7 @@ error: cannot infer an appropriate lifetime LL | pub async fn run_dummy_fn(&self) { | ^^^^^ ...but this borrow... LL | foo(|| self.bar()).await; - | --- this evaluates to the `'static` lifetime... + | --- this is `'static`... | note: ...can't outlive the lifetime `'_` as defined on the method body at 12:31 --> $DIR/issue-62097.rs:12:31 diff --git a/src/test/ui/impl-trait/must_outlive_least_region_or_bound.stderr b/src/test/ui/impl-trait/must_outlive_least_region_or_bound.stderr index 42667d6ca1a5..9d068d70bd48 100644 --- a/src/test/ui/impl-trait/must_outlive_least_region_or_bound.stderr +++ b/src/test/ui/impl-trait/must_outlive_least_region_or_bound.stderr @@ -4,7 +4,7 @@ error: cannot infer an appropriate lifetime LL | fn elided(x: &i32) -> impl Copy { x } | --------- ^ ...but this borrow... | | - | this evaluates to the `'static` lifetime... + | this is `'static`... | note: ...can't outlive the anonymous lifetime #1 defined on the function body at 3:1 --> $DIR/must_outlive_least_region_or_bound.rs:3:1 @@ -22,7 +22,7 @@ error: cannot infer an appropriate lifetime LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x } | --------- ^ ...but this borrow... | | - | this evaluates to the `'static` lifetime... + | this is `'static`... | note: ...can't outlive the lifetime `'a` as defined on the function body at 6:13 --> $DIR/must_outlive_least_region_or_bound.rs:6:13 @@ -40,7 +40,7 @@ error: cannot infer an appropriate lifetime LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x } | -------------------------------- ^ ...but this borrow... | | - | this evaluates to the `'static` lifetime... + | this is `'static`... | note: ...can't outlive the lifetime `'a` as defined on the function body at 12:15 --> $DIR/must_outlive_least_region_or_bound.rs:12:15 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 963de2d448d7..645c7e1e1954 100644 --- a/src/test/ui/impl-trait/static-return-lifetime-infered.stderr +++ b/src/test/ui/impl-trait/static-return-lifetime-infered.stderr @@ -2,7 +2,7 @@ error: cannot infer an appropriate lifetime --> $DIR/static-return-lifetime-infered.rs:7:16 | LL | fn iter_values_anon(&self) -> impl Iterator { - | ----------------------- this evaluates to the `'static` lifetime... + | ----------------------- this is `'static`... LL | self.x.iter().map(|a| a.0) | ------ ^^^^ | | @@ -24,7 +24,7 @@ error: cannot infer an appropriate lifetime --> $DIR/static-return-lifetime-infered.rs:11:16 | LL | fn iter_values<'a>(&'a self) -> impl Iterator { - | ----------------------- this evaluates to the `'static` lifetime... + | ----------------------- this is `'static`... LL | self.x.iter().map(|a| a.0) | ------ ^^^^ | | diff --git a/src/test/ui/issues/issue-16922.stderr b/src/test/ui/issues/issue-16922.stderr index 8bcfe979ce14..20a6b287429f 100644 --- a/src/test/ui/issues/issue-16922.stderr +++ b/src/test/ui/issues/issue-16922.stderr @@ -5,7 +5,7 @@ LL | Box::new(value) as Box | ---------^^^^^- | | | | | ...but this borrow... - | this evaluates to the `'static` lifetime... + | this is `'static`... | note: ...can't outlive the anonymous lifetime #1 defined on the function body at 3:1 --> $DIR/issue-16922.rs:3:1 diff --git a/src/test/ui/object-lifetime/object-lifetime-default-from-box-error.stderr b/src/test/ui/object-lifetime/object-lifetime-default-from-box-error.stderr index 7981d082c800..465409c63989 100644 --- a/src/test/ui/object-lifetime/object-lifetime-default-from-box-error.stderr +++ b/src/test/ui/object-lifetime/object-lifetime-default-from-box-error.stderr @@ -2,10 +2,7 @@ error: cannot infer an appropriate lifetime --> $DIR/object-lifetime-default-from-box-error.rs:18:5 | LL | ss.r - | ^^^^ - | | - | this evaluates to the `'static` lifetime... - | ...but this borrow... + | ^^^^ this needs to be `'static` but the borrow... | note: ...can't outlive the anonymous lifetime #2 defined on the function body at 14:1 --> $DIR/object-lifetime-default-from-box-error.rs:14:1 diff --git a/src/test/ui/regions/region-object-lifetime-in-coercion.stderr b/src/test/ui/regions/region-object-lifetime-in-coercion.stderr index 8048b79b015d..5a414c477a69 100644 --- a/src/test/ui/regions/region-object-lifetime-in-coercion.stderr +++ b/src/test/ui/regions/region-object-lifetime-in-coercion.stderr @@ -21,7 +21,7 @@ LL | Box::new(v) | ---------^- | | | | | ...but this borrow... - | this evaluates to the `'static` lifetime... + | this is `'static`... | note: ...can't outlive the anonymous lifetime #1 defined on the function body at 17:1 --> $DIR/region-object-lifetime-in-coercion.rs:17:1 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 fcde3bb5ca65..21775539cea7 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,7 +2,7 @@ error: cannot infer an appropriate lifetime --> $DIR/arbitrary_self_types_pin_lifetime_impl_trait-async.rs:8:16 | LL | async fn f(self: Pin<&Self>) -> impl Clone { self } - | ^^^^ ---------- this evaluates to the `'static` lifetime... + | ^^^^ ---------- this is `'static`... | | | ...but this borrow... | diff --git a/src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.stderr b/src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.stderr index c89ee27aa8cd..e931cec8a2a8 100644 --- a/src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.stderr +++ b/src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.stderr @@ -4,7 +4,7 @@ error: cannot infer an appropriate lifetime LL | fn f(self: Pin<&Self>) -> impl Clone { self } | ---------- ^^^^ ...but this borrow... | | - | this evaluates to the `'static` lifetime... + | this is `'static`... | note: ...can't outlive the anonymous lifetime #1 defined on the method body at 6:5 --> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:6: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 5a2530bdcbb2..d6d2a0c4d2b6 100644 --- a/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr +++ b/src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr @@ -10,7 +10,7 @@ error: cannot infer an appropriate lifetime --> $DIR/missing-lifetimes-in-signature.rs:19:5 | LL | fn foo(g: G, dest: &mut T) -> impl FnOnce() - | ------------- this evaluates to the `'static` lifetime... + | ------------- this is `'static`... ... LL | / move || { LL | | *dest = g.get(); @@ -55,7 +55,7 @@ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:30:5: | LL | fn bar(g: G, dest: &mut T) -> impl FnOnce() + '_ | ^^^^^^^^^^^^^^^^^^ -help: consider introducing an explicit lifetime bound to unify the type parameter and the output +help: consider introducing an explicit lifetime bound | LL | fn bar<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a | ^^^^^ ^^^^ @@ -82,7 +82,7 @@ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:52:5: | LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ | ^^^^^^^^^^^^^^^^^^ -help: consider introducing an explicit lifetime bound to unify the type parameter and the output +help: consider introducing an explicit lifetime bound | LL | fn qux<'b, 'a, G: 'b + 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'b | ^^^ ^^^^^^^ ^^^^ diff --git a/src/test/ui/underscore-lifetime/dyn-trait-underscore.stderr b/src/test/ui/underscore-lifetime/dyn-trait-underscore.stderr index 65714d16a81d..bcbdec4f306d 100644 --- a/src/test/ui/underscore-lifetime/dyn-trait-underscore.stderr +++ b/src/test/ui/underscore-lifetime/dyn-trait-underscore.stderr @@ -2,11 +2,13 @@ error: cannot infer an appropriate lifetime --> $DIR/dyn-trait-underscore.rs:8:20 | LL | Box::new(items.iter()) - | ---------------^^^^--- - | | | - | | ...but this borrow... - | this evaluates to the `'static` lifetime... + | ---------------^^^^--- this is `'static`... | +note: ...but this borrow... + --> $DIR/dyn-trait-underscore.rs:8:14 + | +LL | Box::new(items.iter()) + | ^^^^^ note: ...can't outlive the anonymous lifetime #1 defined on the function body at 6:1 --> $DIR/dyn-trait-underscore.rs:6:1 |