Skip to content

Commit 4d166cc

Browse files
authored
Rollup merge of #134639 - compiler-errors:negative-ambiguity-causes, r=oli-obk
Make sure we note ambiguity causes on positive/negative impl conflicts Fixes #134632 by explaining why the error must be
2 parents a8edf08 + 62d1f4f commit 4d166cc

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -483,15 +483,19 @@ fn report_negative_positive_conflict<'tcx>(
483483
negative_impl_def_id: DefId,
484484
positive_impl_def_id: DefId,
485485
) -> ErrorGuaranteed {
486-
tcx.dcx()
487-
.create_err(NegativePositiveConflict {
488-
impl_span: tcx.def_span(local_impl_def_id),
489-
trait_desc: overlap.trait_ref,
490-
self_ty: overlap.self_ty,
491-
negative_impl_span: tcx.span_of_impl(negative_impl_def_id),
492-
positive_impl_span: tcx.span_of_impl(positive_impl_def_id),
493-
})
494-
.emit()
486+
let mut diag = tcx.dcx().create_err(NegativePositiveConflict {
487+
impl_span: tcx.def_span(local_impl_def_id),
488+
trait_desc: overlap.trait_ref,
489+
self_ty: overlap.self_ty,
490+
negative_impl_span: tcx.span_of_impl(negative_impl_def_id),
491+
positive_impl_span: tcx.span_of_impl(positive_impl_def_id),
492+
});
493+
494+
for cause in &overlap.intercrate_ambiguity_causes {
495+
cause.add_intercrate_ambiguity_hint(&mut diag);
496+
}
497+
498+
diag.emit()
495499
}
496500

497501
fn report_conflicting_impls<'tcx>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0119]: conflicting implementations of trait `MyTrait` for type `String`
2+
--> $DIR/ambiguity-cause.rs:10:1
3+
|
4+
LL | impl<T: Copy> MyTrait for T { }
5+
| --------------------------- first implementation here
6+
LL |
7+
LL | impl MyTrait for String { }
8+
| ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `String`
9+
|
10+
= note: upstream crates may add a new impl of trait `std::marker::Copy` for type `std::string::String` in future versions
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0119`.

Diff for: tests/ui/traits/negative-impls/ambiguity-cause.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ revisions: simple negative_coherence
2+
3+
#![feature(negative_impls)]
4+
#![cfg_attr(negative_coherence, feature(with_negative_coherence))]
5+
6+
trait MyTrait {}
7+
8+
impl<T: Copy> MyTrait for T { }
9+
10+
impl MyTrait for String { }
11+
//~^ ERROR conflicting implementations of trait `MyTrait` for type `String`
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0119]: conflicting implementations of trait `MyTrait` for type `String`
2+
--> $DIR/ambiguity-cause.rs:10:1
3+
|
4+
LL | impl<T: Copy> MyTrait for T { }
5+
| --------------------------- first implementation here
6+
LL |
7+
LL | impl MyTrait for String { }
8+
| ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `String`
9+
|
10+
= note: upstream crates may add a new impl of trait `std::marker::Copy` for type `std::string::String` in future versions
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0119`.

0 commit comments

Comments
 (0)