about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-12-22 09:12:14 +0100
committerGitHub <noreply@github.com>2024-12-22 09:12:14 +0100
commit4d166cc369c898984a251d36c48cb741ecb3b6c1 (patch)
treee2d04ef7a8dba007e6773676ec432aa4a8aa6ee4
parenta8edf082e10f954d2786d9aa0d10d51e085755d1 (diff)
parent62d1f4faa172b409ea09a24acaf9dfccfd0c2710 (diff)
downloadrust-4d166cc369c898984a251d36c48cb741ecb3b6c1.tar.gz
rust-4d166cc369c898984a251d36c48cb741ecb3b6c1.zip
Rollup merge of #134639 - compiler-errors:negative-ambiguity-causes, r=oli-obk
Make sure we note ambiguity causes on positive/negative impl conflicts

Fixes https://github.com/rust-lang/rust/issues/134632 by explaining why the error must be
-rw-r--r--compiler/rustc_trait_selection/src/traits/specialize/mod.rs22
-rw-r--r--tests/ui/traits/negative-impls/ambiguity-cause.negative_coherence.stderr14
-rw-r--r--tests/ui/traits/negative-impls/ambiguity-cause.rs13
-rw-r--r--tests/ui/traits/negative-impls/ambiguity-cause.simple.stderr14
4 files changed, 54 insertions, 9 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs
index 1430cfae51f..401b41c796d 100644
--- a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs
+++ b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs
@@ -483,15 +483,19 @@ fn report_negative_positive_conflict<'tcx>(
     negative_impl_def_id: DefId,
     positive_impl_def_id: DefId,
 ) -> ErrorGuaranteed {
-    tcx.dcx()
-        .create_err(NegativePositiveConflict {
-            impl_span: tcx.def_span(local_impl_def_id),
-            trait_desc: overlap.trait_ref,
-            self_ty: overlap.self_ty,
-            negative_impl_span: tcx.span_of_impl(negative_impl_def_id),
-            positive_impl_span: tcx.span_of_impl(positive_impl_def_id),
-        })
-        .emit()
+    let mut diag = tcx.dcx().create_err(NegativePositiveConflict {
+        impl_span: tcx.def_span(local_impl_def_id),
+        trait_desc: overlap.trait_ref,
+        self_ty: overlap.self_ty,
+        negative_impl_span: tcx.span_of_impl(negative_impl_def_id),
+        positive_impl_span: tcx.span_of_impl(positive_impl_def_id),
+    });
+
+    for cause in &overlap.intercrate_ambiguity_causes {
+        cause.add_intercrate_ambiguity_hint(&mut diag);
+    }
+
+    diag.emit()
 }
 
 fn report_conflicting_impls<'tcx>(
diff --git a/tests/ui/traits/negative-impls/ambiguity-cause.negative_coherence.stderr b/tests/ui/traits/negative-impls/ambiguity-cause.negative_coherence.stderr
new file mode 100644
index 00000000000..4ec3414a57b
--- /dev/null
+++ b/tests/ui/traits/negative-impls/ambiguity-cause.negative_coherence.stderr
@@ -0,0 +1,14 @@
+error[E0119]: conflicting implementations of trait `MyTrait` for type `String`
+  --> $DIR/ambiguity-cause.rs:10:1
+   |
+LL | impl<T: Copy> MyTrait for T { }
+   | --------------------------- first implementation here
+LL |
+LL | impl MyTrait for String { }
+   | ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `String`
+   |
+   = note: upstream crates may add a new impl of trait `std::marker::Copy` for type `std::string::String` in future versions
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0119`.
diff --git a/tests/ui/traits/negative-impls/ambiguity-cause.rs b/tests/ui/traits/negative-impls/ambiguity-cause.rs
new file mode 100644
index 00000000000..30a528c535d
--- /dev/null
+++ b/tests/ui/traits/negative-impls/ambiguity-cause.rs
@@ -0,0 +1,13 @@
+//@ revisions: simple negative_coherence
+
+#![feature(negative_impls)]
+#![cfg_attr(negative_coherence, feature(with_negative_coherence))]
+
+trait MyTrait {}
+
+impl<T: Copy> MyTrait for T { }
+
+impl MyTrait for String { }
+//~^ ERROR conflicting implementations of trait `MyTrait` for type `String`
+
+fn main() {}
diff --git a/tests/ui/traits/negative-impls/ambiguity-cause.simple.stderr b/tests/ui/traits/negative-impls/ambiguity-cause.simple.stderr
new file mode 100644
index 00000000000..4ec3414a57b
--- /dev/null
+++ b/tests/ui/traits/negative-impls/ambiguity-cause.simple.stderr
@@ -0,0 +1,14 @@
+error[E0119]: conflicting implementations of trait `MyTrait` for type `String`
+  --> $DIR/ambiguity-cause.rs:10:1
+   |
+LL | impl<T: Copy> MyTrait for T { }
+   | --------------------------- first implementation here
+LL |
+LL | impl MyTrait for String { }
+   | ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `String`
+   |
+   = note: upstream crates may add a new impl of trait `std::marker::Copy` for type `std::string::String` in future versions
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0119`.