Skip to content

Commit abdb087

Browse files
rperierRomain Perier
authored andcommitted
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 919c409 commit abdb087

File tree

7 files changed

+85
-0
lines changed

7 files changed

+85
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
487487
);
488488
}
489489

490+
self.suggest_impl_similarly_named_trait(&mut err, &obligation, leaf_trait_predicate);
491+
490492
self.try_to_add_help_message(
491493
&root_obligation,
492494
&obligation,

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5108,6 +5108,44 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
51085108
_ => {}
51095109
}
51105110
}
5111+
pub(crate) fn suggest_impl_similarly_named_trait(
5112+
&self,
5113+
err: &mut Diag<'_>,
5114+
obligation: &PredicateObligation<'tcx>,
5115+
trait_predicate: ty::PolyTraitPredicate<'tcx>,
5116+
) {
5117+
let trait_def_id = trait_predicate.def_id();
5118+
let trait_name = self.tcx.item_name(trait_def_id);
5119+
if let Some(other_trait_def_id) = self.tcx.all_traits_including_private().find(|def_id| {
5120+
trait_def_id != *def_id
5121+
&& trait_name == self.tcx.item_name(def_id)
5122+
&& self
5123+
.tcx
5124+
.all_impls(*def_id)
5125+
.find(|impl_def_id| {
5126+
let Some(trait_ref) = self.tcx.impl_trait_ref(impl_def_id) else {
5127+
return false;
5128+
};
5129+
self.predicate_must_hold_modulo_regions(&Obligation::new(
5130+
self.tcx,
5131+
obligation.cause.clone(),
5132+
obligation.param_env,
5133+
trait_ref
5134+
.skip_binder()
5135+
.with_self_ty(self.tcx, trait_predicate.skip_binder().self_ty()),
5136+
))
5137+
})
5138+
.is_some()
5139+
}) {
5140+
err.note(format!(
5141+
"`{}` implements similarly named `{}`, but not `{}`",
5142+
trait_predicate.self_ty(),
5143+
self.tcx.def_path_str(other_trait_def_id),
5144+
trait_predicate.print_modifiers_and_trait_path()
5145+
));
5146+
}
5147+
()
5148+
}
51115149
}
51125150

51135151
/// Add a hint to add a missing borrow or remove an unnecessary one.

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
@@ -12,6 +12,7 @@ help: trait impl with same name found
1212
LL | impl Bar for Foo {}
1313
| ^^^^^^^^^^^^^^^^
1414
= note: perhaps two different versions of crate `crate_a2` are being used?
15+
= note: `Foo` implements similarly named `main::a::Bar`, but not `main::a::Bar`
1516
= help: the trait `main::a::Bar` is implemented for `ImplementsTraitForUsize<usize>`
1617
note: required by a bound in `try_foo`
1718
--> $DIR/auxiliary/crate_a1.rs:3:24
@@ -48,6 +49,7 @@ help: trait impl with same name found
4849
LL | impl Bar for ImplementsWrongTraitConditionally<isize> {}
4950
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5051
= note: perhaps two different versions of crate `crate_a2` are being used?
52+
= note: `ImplementsWrongTraitConditionally<isize>` implements similarly named `main::a::Bar`, but not `main::a::Bar`
5153
= help: the trait `main::a::Bar` is implemented for `ImplementsTraitForUsize<usize>`
5254
note: required by a bound in `try_foo`
5355
--> $DIR/auxiliary/crate_a1.rs:3:24
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait Trait {} //~ HELP this trait has no implementations, consider adding one
2+
3+
mod m {
4+
pub trait Trait {}
5+
pub struct St;
6+
impl Trait for St {}
7+
}
8+
9+
fn func<T: Trait>(_: T) {} //~ NOTE required by a bound in `func`
10+
//~^ NOTE required by this bound in `func`
11+
12+
fn main() {
13+
func(m::St); //~ ERROR the trait bound `St: Trait` is not satisfied
14+
//~^ NOTE the trait `Trait` is not implemented for `St`
15+
//~| NOTE required by a bound introduced by this call
16+
//~| NOTE `St` implements similarly named `m::Trait`, but not `Trait`
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0277]: the trait bound `St: Trait` is not satisfied
2+
--> $DIR/similarly_named_trait.rs:13: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:9:12
17+
|
18+
LL | fn func<T: Trait>(_: T) {}
19+
| ^^^^^ required by this bound in `func`
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)