From d4351015378886fd7204cad5b0c74d176bf9151c Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 5 Oct 2021 12:32:08 +0000 Subject: [PATCH] Use a label instead of a note for member constraint errors --- .../src/infer/error_reporting/mod.rs | 22 ++++++++++++++++--- .../ui/impl-trait/hidden-lifetimes.stderr | 20 +++++------------ .../error-handling-2.stderr | 10 +++------ .../impl-trait/region-escape-via-bound.stderr | 7 ++---- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index f94a86af04b..a48e01b1da2 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -116,7 +116,7 @@ pub(super) fn note_and_explain_region( emit_msg_span(err, prefix, description, span, suffix); } -pub(super) fn note_and_explain_free_region( +fn explain_free_region( tcx: TyCtxt<'tcx>, err: &mut DiagnosticBuilder<'_>, prefix: &str, @@ -125,7 +125,7 @@ pub(super) fn note_and_explain_free_region( ) { let (description, span) = msg_span_from_free_region(tcx, region, None); - emit_msg_span(err, prefix, description, span, suffix); + label_msg_span(err, prefix, description, span, suffix); } fn msg_span_from_free_region( @@ -210,6 +210,22 @@ fn emit_msg_span( } } +fn label_msg_span( + err: &mut DiagnosticBuilder<'_>, + prefix: &str, + description: String, + span: Option, + suffix: &str, +) { + let message = format!("{}{}{}", prefix, description, suffix); + + if let Some(span) = span { + err.span_label(span, &message); + } else { + err.note(&message); + } +} + pub fn unexpected_hidden_region_diagnostic( tcx: TyCtxt<'tcx>, span: Span, @@ -244,7 +260,7 @@ pub fn unexpected_hidden_region_diagnostic( // // (*) if not, the `tainted_by_errors` field would be set to // `Some(ErrorReported)` in any case, so we wouldn't be here at all. - note_and_explain_free_region( + explain_free_region( tcx, &mut err, &format!("hidden type `{}` captures ", hidden_ty), diff --git a/src/test/ui/impl-trait/hidden-lifetimes.stderr b/src/test/ui/impl-trait/hidden-lifetimes.stderr index c8ea98f147f..ba192aa4ab2 100644 --- a/src/test/ui/impl-trait/hidden-lifetimes.stderr +++ b/src/test/ui/impl-trait/hidden-lifetimes.stderr @@ -2,25 +2,17 @@ error[E0700]: hidden type for `impl Trait` captures lifetime that does not appea --> $DIR/hidden-lifetimes.rs:28:54 | LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a { - | ^^^^^^^^^^^^^^ - | -note: hidden type `&'a mut &'b T` captures the lifetime `'b` as defined here - --> $DIR/hidden-lifetimes.rs:28:17 - | -LL | fn hide_ref<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a { - | ^^ + | -- ^^^^^^^^^^^^^^ + | | + | hidden type `&'a mut &'b T` captures the lifetime `'b` as defined here error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds --> $DIR/hidden-lifetimes.rs:45:70 | LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc>) -> impl Swap + 'a { - | ^^^^^^^^^^^^^^ - | -note: hidden type `Rc>` captures the lifetime `'b` as defined here - --> $DIR/hidden-lifetimes.rs:45:24 - | -LL | fn hide_rc_refcell<'a, 'b: 'a, T: 'static>(x: Rc>) -> impl Swap + 'a { - | ^^ + | -- ^^^^^^^^^^^^^^ + | | + | hidden type `Rc>` captures the lifetime `'b` as defined here error: aborting due to 2 previous errors diff --git a/src/test/ui/impl-trait/multiple-lifetimes/error-handling-2.stderr b/src/test/ui/impl-trait/multiple-lifetimes/error-handling-2.stderr index 64f89812fff..cee71400094 100644 --- a/src/test/ui/impl-trait/multiple-lifetimes/error-handling-2.stderr +++ b/src/test/ui/impl-trait/multiple-lifetimes/error-handling-2.stderr @@ -2,13 +2,9 @@ error[E0700]: hidden type for `impl Trait` captures lifetime that does not appea --> $DIR/error-handling-2.rs:13:60 | LL | fn foo<'a: 'b, 'b, 'c>(x: &'static i32, mut y: &'a i32) -> E<'b, 'c> { - | ^^^^^^^^^ - | -note: hidden type `*mut &'a i32` captures the lifetime `'a` as defined here - --> $DIR/error-handling-2.rs:13:8 - | -LL | fn foo<'a: 'b, 'b, 'c>(x: &'static i32, mut y: &'a i32) -> E<'b, 'c> { - | ^^ + | -- ^^^^^^^^^ + | | + | hidden type `*mut &'a i32` captures the lifetime `'a` as defined here error: aborting due to previous error diff --git a/src/test/ui/impl-trait/region-escape-via-bound.stderr b/src/test/ui/impl-trait/region-escape-via-bound.stderr index ddf8939d21d..b9359455278 100644 --- a/src/test/ui/impl-trait/region-escape-via-bound.stderr +++ b/src/test/ui/impl-trait/region-escape-via-bound.stderr @@ -3,12 +3,9 @@ error[E0700]: hidden type for `impl Trait` captures lifetime that does not appea | LL | fn foo(x: Cell<&'x u32>) -> impl Trait<'y> | ^^^^^^^^^^^^^^ - | -note: hidden type `Cell<&'x u32>` captures the lifetime `'x` as defined here - --> $DIR/region-escape-via-bound.rs:17:7 - | +LL | LL | where 'x: 'y - | ^^ + | -- hidden type `Cell<&'x u32>` captures the lifetime `'x` as defined here error: aborting due to previous error