about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-12-22 02:03:11 +0000
committerMichael Goulet <michael@errs.io>2024-12-22 02:04:14 +0000
commit62d1f4faa172b409ea09a24acaf9dfccfd0c2710 (patch)
tree7833370527b4d2846a33fc4aad7d1f33806798ae
parent54dcff104b0acf01716a94429717d364fe43f90d (diff)
downloadrust-62d1f4faa172b409ea09a24acaf9dfccfd0c2710.tar.gz
rust-62d1f4faa172b409ea09a24acaf9dfccfd0c2710.zip
Make sure we note ambiguity causes on positive/negative impl conflicts
-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`.