about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStuart Cook <Zalathar@users.noreply.github.com>2024-09-14 20:22:41 +1000
committerGitHub <noreply@github.com>2024-09-14 20:22:41 +1000
commit517e7ce37f3d5acae0eaa7e50bc76c291fb91e59 (patch)
treec13e02da3d3e0bab9fa9b6a500f94277e0d77fff
parente2f17e66ed9b34e5391ac3a813e7453664b2525c (diff)
parentb0db3a7bedc03d054834b675c417b988d7dc676f (diff)
downloadrust-517e7ce37f3d5acae0eaa7e50bc76c291fb91e59.tar.gz
rust-517e7ce37f3d5acae0eaa7e50bc76c291fb91e59.zip
Rollup merge of #130311 - heiseish:issue-70849-fix, r=fmease
(fix) conflicting negative impl marker

## Context

This MR fixes the error message for conflicting negative trait impls by adding the corresponding the polarity marker to the trait name.

## Issues

- closes #70849

r​? `@fmease`
-rw-r--r--compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs3
-rw-r--r--compiler/rustc_type_ir/src/predicate.rs11
-rw-r--r--tests/ui/coherence/coherence-conflicting-repeated-negative-trait-impl-70849.rs10
-rw-r--r--tests/ui/coherence/coherence-conflicting-repeated-negative-trait-impl-70849.stderr13
4 files changed, 36 insertions, 1 deletions
diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs
index 9aa6d1f3d46..752ef729113 100644
--- a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs
+++ b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs
@@ -344,7 +344,8 @@ pub(crate) fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Opti
 
     write!(
         w,
-        " {} for {}",
+        " {}{} for {}",
+        tcx.impl_polarity(impl_def_id).as_str(),
         trait_ref.print_only_trait_path(),
         tcx.type_of(impl_def_id).instantiate_identity()
     )
diff --git a/compiler/rustc_type_ir/src/predicate.rs b/compiler/rustc_type_ir/src/predicate.rs
index b30346ffc53..e4bf1e1379c 100644
--- a/compiler/rustc_type_ir/src/predicate.rs
+++ b/compiler/rustc_type_ir/src/predicate.rs
@@ -196,6 +196,17 @@ impl fmt::Display for ImplPolarity {
     }
 }
 
+impl ImplPolarity {
+    /// The polarity marker in front of the impl trait ref if applicable.
+    pub fn as_str(self) -> &'static str {
+        match self {
+            Self::Positive => "",
+            Self::Negative => "!",
+            Self::Reservation => "",
+        }
+    }
+}
+
 /// Polarity for a trait predicate. May either be negative or positive.
 /// Distinguished from [`ImplPolarity`] since we never compute goals with
 /// "reservation" level.
diff --git a/tests/ui/coherence/coherence-conflicting-repeated-negative-trait-impl-70849.rs b/tests/ui/coherence/coherence-conflicting-repeated-negative-trait-impl-70849.rs
new file mode 100644
index 00000000000..88b9d9e62bf
--- /dev/null
+++ b/tests/ui/coherence/coherence-conflicting-repeated-negative-trait-impl-70849.rs
@@ -0,0 +1,10 @@
+#![feature(negative_impls)]
+//@ edition: 2021
+// Test to ensure we are printing the polarity of the impl trait ref
+// when printing out conflicting trait impls
+
+struct MyType;
+
+impl !Clone for &mut MyType {}
+//~^ ERROR conflicting implementations of trait `Clone` for type `&mut MyType`
+fn main() {}
diff --git a/tests/ui/coherence/coherence-conflicting-repeated-negative-trait-impl-70849.stderr b/tests/ui/coherence/coherence-conflicting-repeated-negative-trait-impl-70849.stderr
new file mode 100644
index 00000000000..b317197eb40
--- /dev/null
+++ b/tests/ui/coherence/coherence-conflicting-repeated-negative-trait-impl-70849.stderr
@@ -0,0 +1,13 @@
+error[E0119]: conflicting implementations of trait `Clone` for type `&mut MyType`
+  --> $DIR/coherence-conflicting-repeated-negative-trait-impl-70849.rs:8:1
+   |
+LL | impl !Clone for &mut MyType {}
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: conflicting implementation in crate `core`:
+           - impl<T> !Clone for &mut T
+             where T: ?Sized;
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0119`.