From 7f97dfe700be69d359eaebf97275285f8a85faf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 29 Feb 2024 16:30:01 +0000 Subject: [PATCH] Account for unmet `T: !Copy` in E0277 message --- .../src/traits/error_reporting/suggestions.rs | 17 ++++++++++++----- tests/ui/traits/negative-bounds/simple.stderr | 8 ++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index e82e94993d249..06f908f01a97e 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -4773,11 +4773,18 @@ pub(super) fn get_explanation_based_on_obligation<'tcx>( Some(desc) => format!(" {desc}"), None => String::new(), }; - format!( - "{pre_message}the trait `{}` is not implemented for{desc} `{}`{post}", - trait_predicate.print_modifiers_and_trait_path(), - tcx.short_ty_string(trait_ref.skip_binder().self_ty(), &mut None), - ) + if let ty::ImplPolarity::Positive = trait_predicate.polarity() { + format!( + "{pre_message}the trait `{}` is not implemented for{desc} `{}`{post}", + trait_predicate.print_modifiers_and_trait_path(), + tcx.short_ty_string(trait_ref.skip_binder().self_ty(), &mut None), + ) + } else { + // "the trait bound `T: !Send` is not satisfied" reads better than "`!Send` is + // not implemented for `T`". + // FIXME: add note explaining explicit negative trait bounds. + format!("{pre_message}the trait bound `{trait_predicate}` is not satisfied{post}") + } } } diff --git a/tests/ui/traits/negative-bounds/simple.stderr b/tests/ui/traits/negative-bounds/simple.stderr index 6d750739e197c..b8d12138794bf 100644 --- a/tests/ui/traits/negative-bounds/simple.stderr +++ b/tests/ui/traits/negative-bounds/simple.stderr @@ -2,7 +2,7 @@ error[E0277]: the trait bound `T: !Copy` is not satisfied --> $DIR/simple.rs:10:16 | LL | not_copy::(); - | ^ the trait `!Copy` is not implemented for `T` + | ^ the trait bound `T: !Copy` is not satisfied | note: required by a bound in `not_copy` --> $DIR/simple.rs:3:16 @@ -14,7 +14,7 @@ error[E0277]: the trait bound `T: !Copy` is not satisfied --> $DIR/simple.rs:15:16 | LL | not_copy::(); - | ^ the trait `!Copy` is not implemented for `T` + | ^ the trait bound `T: !Copy` is not satisfied | note: required by a bound in `not_copy` --> $DIR/simple.rs:3:16 @@ -26,7 +26,7 @@ error[E0277]: the trait bound `Copyable: !Copy` is not satisfied --> $DIR/simple.rs:30:16 | LL | not_copy::(); - | ^^^^^^^^ the trait `!Copy` is not implemented for `Copyable` + | ^^^^^^^^ the trait bound `Copyable: !Copy` is not satisfied | = help: the trait `Copy` is implemented for `Copyable` note: required by a bound in `not_copy` @@ -44,7 +44,7 @@ error[E0277]: the trait bound `NotNecessarilyCopyable: !Copy` is not satisfied --> $DIR/simple.rs:37:16 | LL | not_copy::(); - | ^^^^^^^^^^^^^^^^^^^^^^ the trait `!Copy` is not implemented for `NotNecessarilyCopyable` + | ^^^^^^^^^^^^^^^^^^^^^^ the trait bound `NotNecessarilyCopyable: !Copy` is not satisfied | note: required by a bound in `not_copy` --> $DIR/simple.rs:3:16