about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-03-02 10:09:38 +0100
committerGitHub <noreply@github.com>2024-03-02 10:09:38 +0100
commit07bd4590fb0ed1effc8eb42ae3f4cd47bc7f2e3e (patch)
treedf9bc2a176318b29b8fc694f25d4d75777fe3764
parentc95f485744b4fa80a2fea606825dbbb787f8a36b (diff)
parent7f97dfe700be69d359eaebf97275285f8a85faf4 (diff)
downloadrust-07bd4590fb0ed1effc8eb42ae3f4cd47bc7f2e3e.tar.gz
rust-07bd4590fb0ed1effc8eb42ae3f4cd47bc7f2e3e.zip
Rollup merge of #121875 - estebank:e0277-drive-by, r=compiler-errors
 Account for unmet T: !Copy in E0277 message

```
error[E0277]: the trait bound `T: !Copy` is not satisfied
  --> $DIR/simple.rs:10:16
   |
LL |     not_copy::<T>();
   |                ^ the trait bound `T: !Copy` is not satisfied
```
instead of the current

```
error[E0277]: the trait bound `T: !Copy` is not satisfied
  --> $DIR/simple.rs:10:16
   |
LL |     not_copy::<T>();
   |                ^ the trait `!Copy` is not implemented for `T`
```
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs25
-rw-r--r--tests/ui/traits/negative-bounds/simple.stderr8
2 files changed, 17 insertions, 16 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 4ffe587f63d..16290c8dbca 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
@@ -4752,20 +4752,21 @@ pub(super) fn get_explanation_based_on_obligation<'tcx>(
         } else {
             String::new()
         };
-        match ty_desc {
-            Some(desc) => format!(
-                "{}the trait `{}` is not implemented for {} `{}`{post}",
-                pre_message,
-                trait_predicate.print_modifiers_and_trait_path(),
-                desc,
-                tcx.short_ty_string(trait_ref.skip_binder().self_ty(), &mut None),
-            ),
-            None => format!(
-                "{}the trait `{}` is not implemented for `{}`{post}",
-                pre_message,
+        let desc = match ty_desc {
+            Some(desc) => format!(" {desc}"),
+            None => String::new(),
+        };
+        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 6d750739e19..b8d12138794 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::<T>();
-   |                ^ 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::<T>();
-   |                ^ 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::<Copyable>();
-   |                ^^^^^^^^ 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::<NotNecessarilyCopyable>();
-   |                ^^^^^^^^^^^^^^^^^^^^^^ 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