Skip to content

Commit a6853be

Browse files
committed
Add a note when a type implements a trait with the same name as the required one
This is useful when you have two dependencies that use different trait for the same thing and with the same name. The user can accidentally implement the bad one which might be confusing.
1 parent 1c9952f commit a6853be

File tree

7 files changed

+121
-1
lines changed

7 files changed

+121
-1
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
449449
span,
450450
leaf_trait_predicate,
451451
);
452-
self.note_version_mismatch(&mut err, leaf_trait_predicate);
452+
self.note_version_mismatch(&mut err, &obligation, leaf_trait_predicate);
453453
self.suggest_remove_await(&obligation, &mut err);
454454
self.suggest_derive(&obligation, &mut err, leaf_trait_predicate);
455455

@@ -2346,6 +2346,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
23462346
fn note_version_mismatch(
23472347
&self,
23482348
err: &mut Diag<'_>,
2349+
obligation: &PredicateObligation<'tcx>,
23492350
trait_pred: ty::PolyTraitPredicate<'tcx>,
23502351
) -> bool {
23512352
let get_trait_impls = |trait_def_id| {
@@ -2372,6 +2373,49 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
23722373
let traits_with_same_path =
23732374
traits_with_same_path.into_items().into_sorted_stable_ord_by_key(|(p, _)| p);
23742375
let mut suggested = false;
2376+
let trait_def_id = trait_pred.def_id();
2377+
let trait_has_same_params = |other_trait_def_id: DefId| -> bool {
2378+
let trait_generics = self.tcx.generics_of(trait_def_id);
2379+
let other_trait_generics = self.tcx.generics_of(other_trait_def_id);
2380+
2381+
if trait_generics.count() != other_trait_generics.count() {
2382+
return false;
2383+
}
2384+
trait_generics.own_params.iter().zip(other_trait_generics.own_params.iter()).all(
2385+
|(a, b)| {
2386+
(matches!(a.kind, ty::GenericParamDefKind::Type { .. })
2387+
&& matches!(b.kind, ty::GenericParamDefKind::Type { .. }))
2388+
|| (matches!(a.kind, ty::GenericParamDefKind::Lifetime,)
2389+
&& matches!(b.kind, ty::GenericParamDefKind::Lifetime))
2390+
|| (matches!(a.kind, ty::GenericParamDefKind::Const { .. })
2391+
&& matches!(b.kind, ty::GenericParamDefKind::Const { .. }))
2392+
},
2393+
)
2394+
};
2395+
2396+
let trait_name = self.tcx.item_name(trait_def_id);
2397+
if let Some(other_trait_def_id) = self.tcx.all_traits_including_private().find(|def_id| {
2398+
trait_def_id != *def_id
2399+
&& trait_name == self.tcx.item_name(def_id)
2400+
&& trait_has_same_params(*def_id)
2401+
&& self.predicate_must_hold_modulo_regions(&Obligation::new(
2402+
self.tcx,
2403+
obligation.cause.clone(),
2404+
obligation.param_env,
2405+
trait_pred.map_bound(|tr| ty::TraitPredicate {
2406+
trait_ref: ty::TraitRef::new(self.tcx, *def_id, tr.trait_ref.args),
2407+
..tr
2408+
}),
2409+
))
2410+
}) {
2411+
err.note(format!(
2412+
"`{}` implements similarly named `{}`, but not `{}`",
2413+
trait_pred.self_ty(),
2414+
self.tcx.def_path_str(other_trait_def_id),
2415+
trait_pred.print_modifiers_and_trait_path()
2416+
));
2417+
suggested = true;
2418+
}
23752419
for (_, trait_with_same_path) in traits_with_same_path {
23762420
let trait_impls = get_trait_impls(trait_with_same_path);
23772421
if trait_impls.is_empty() {

tests/run-make/crate-loading-crate-depends-on-itself/foo.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0277]: the trait bound `foo::Struct: Trait` is not satisfied
44
13 | check_trait::<foo::Struct>();
55
| ^^^^^^^^^^^ the trait `Trait` is not implemented for `foo::Struct`
66
|
7+
= note: `foo::Struct` implements similarly named `foo::Trait`, but not `Trait`
78
note: there are multiple different versions of crate `foo` in the dependency graph
89
--> foo-current.rs:7:1
910
|

tests/run-make/crate-loading/multiple-dep-versions.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | do_something(Type);
66
| |
77
| required by a bound introduced by this call
88
|
9+
= note: `dep_2_reexport::Type` implements similarly named `dependency::Trait`, but not `Trait`
910
note: there are multiple different versions of crate `dependency` in the dependency graph
1011
--> replaced
1112
|
@@ -92,6 +93,7 @@ LL | do_something(OtherType);
9293
| |
9394
| required by a bound introduced by this call
9495
|
96+
= note: `OtherType` implements similarly named `dependency::Trait`, but not `Trait`
9597
note: there are multiple different versions of crate `dependency` in the dependency graph
9698
--> replaced
9799
|

tests/ui/traits/bound/same-crate-name.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | a::try_foo(foo);
66
| |
77
| required by a bound introduced by this call
88
|
9+
= note: `Foo` implements similarly named `main::a::Bar`, but not `main::a::Bar`
910
help: trait impl with same name found
1011
--> $DIR/auxiliary/crate_a2.rs:5:1
1112
|
@@ -42,6 +43,7 @@ LL | a::try_foo(other_variant_implements_mismatched_trait);
4243
| |
4344
| required by a bound introduced by this call
4445
|
46+
= note: `ImplementsWrongTraitConditionally<isize>` implements similarly named `main::a::Bar`, but not `main::a::Bar`
4547
help: trait impl with same name found
4648
--> $DIR/auxiliary/crate_a2.rs:13:1
4749
|
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
trait Trait {} //~ HELP this trait has no implementations, consider adding one
2+
trait TraitWithParam<T> {} //~ HELP this trait has no implementations, consider adding one
3+
4+
mod m {
5+
pub trait Trait {}
6+
pub trait TraitWithParam<T> {}
7+
pub struct St;
8+
impl Trait for St {}
9+
impl<T> TraitWithParam<T> for St {}
10+
}
11+
12+
fn func<T: Trait>(_: T) {} //~ NOTE required by a bound in `func`
13+
//~^ NOTE required by this bound in `func`
14+
15+
fn func2<T: TraitWithParam<T>> (_: T) {} //~ NOTE required by a bound in `func2`
16+
//~^ NOTE required by this bound in `func2`
17+
18+
fn main() {
19+
func(m::St); //~ ERROR the trait bound `St: Trait` is not satisfied
20+
//~^ NOTE the trait `Trait` is not implemented for `St`
21+
//~| NOTE required by a bound introduced by this call
22+
//~| NOTE `St` implements similarly named `m::Trait`, but not `Trait`
23+
func2(m::St); //~ ERROR the trait bound `St: TraitWithParam<St>` is not satisfied
24+
//~^ NOTE the trait `TraitWithParam<St>` is not implemented for `St`
25+
//~| NOTE required by a bound introduced by this call
26+
//~| NOTE `St` implements similarly named `m::TraitWithParam`, but not `TraitWithParam<St>`
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0277]: the trait bound `St: Trait` is not satisfied
2+
--> $DIR/similarly_named_trait.rs:19:10
3+
|
4+
LL | func(m::St);
5+
| ---- ^^^^^ the trait `Trait` is not implemented for `St`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= note: `St` implements similarly named `m::Trait`, but not `Trait`
10+
help: this trait has no implementations, consider adding one
11+
--> $DIR/similarly_named_trait.rs:1:1
12+
|
13+
LL | trait Trait {}
14+
| ^^^^^^^^^^^
15+
note: required by a bound in `func`
16+
--> $DIR/similarly_named_trait.rs:12:12
17+
|
18+
LL | fn func<T: Trait>(_: T) {}
19+
| ^^^^^ required by this bound in `func`
20+
21+
error[E0277]: the trait bound `St: TraitWithParam<St>` is not satisfied
22+
--> $DIR/similarly_named_trait.rs:23:11
23+
|
24+
LL | func2(m::St);
25+
| ----- ^^^^^ the trait `TraitWithParam<St>` is not implemented for `St`
26+
| |
27+
| required by a bound introduced by this call
28+
|
29+
= note: `St` implements similarly named `m::TraitWithParam`, but not `TraitWithParam<St>`
30+
help: this trait has no implementations, consider adding one
31+
--> $DIR/similarly_named_trait.rs:2:1
32+
|
33+
LL | trait TraitWithParam<T> {}
34+
| ^^^^^^^^^^^^^^^^^^^^^^^
35+
note: required by a bound in `func2`
36+
--> $DIR/similarly_named_trait.rs:15:13
37+
|
38+
LL | fn func2<T: TraitWithParam<T>> (_: T) {}
39+
| ^^^^^^^^^^^^^^^^^ required by this bound in `func2`
40+
41+
error: aborting due to 2 previous errors
42+
43+
For more information about this error, try `rustc --explain E0277`.

tests/ui/union/issue-81199.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ error[E0277]: the trait bound `T: Pointee` is not satisfied
1616
LL | components: PtrComponents<T>,
1717
| ^^^^^^^^^^^^^^^^ the trait `Pointee` is not implemented for `T`
1818
|
19+
= note: `T` implements similarly named `std::ptr::Pointee`, but not `Pointee`
1920
note: required by a bound in `PtrComponents`
2021
--> $DIR/issue-81199.rs:11:25
2122
|

0 commit comments

Comments
 (0)