diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs index 1430cfae51f73..401b41c796dad 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 0000000000000..4ec3414a57bcc --- /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 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 0000000000000..30a528c535d44 --- /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 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 0000000000000..4ec3414a57bcc --- /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 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`.