Skip to content

Commit ff869dc

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 919c409 commit ff869dc

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-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, 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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5108,6 +5108,38 @@ 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+
trait_predicate: ty::PolyTraitPredicate<'tcx>,
5115+
) {
5116+
let trait_def_id = trait_predicate.def_id();
5117+
let trait_name = self.tcx.item_name(trait_def_id);
5118+
if let Some(other_trait_def_id) = self.tcx.all_traits_including_private().find(|def_id| {
5119+
trait_def_id != *def_id
5120+
&& trait_name == self.tcx.item_name(def_id)
5121+
&& self
5122+
.tcx
5123+
.all_impls(*def_id)
5124+
.find(|impl_def_id| {
5125+
let imp = self.tcx.impl_trait_header(impl_def_id).unwrap();
5126+
if imp.polarity != ty::ImplPolarity::Positive {
5127+
return false;
5128+
}
5129+
let imp = imp.trait_ref.skip_binder();
5130+
trait_predicate.skip_binder().self_ty() == imp.self_ty()
5131+
})
5132+
.is_some()
5133+
}) {
5134+
err.note(format!(
5135+
"`{}` implements similarly named `{}`, but not `{}`",
5136+
trait_predicate.self_ty(),
5137+
self.tcx.def_path_str(other_trait_def_id),
5138+
trait_predicate.print_modifiers_and_trait_path()
5139+
));
5140+
}
5141+
()
5142+
}
51115143
}
51125144

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

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)