Skip to content

When multiple traits are applicable for method, do not specialize main message to the first one that applied#153679

Open
estebank wants to merge 1 commit intorust-lang:mainfrom
estebank:issue-113550
Open

When multiple traits are applicable for method, do not specialize main message to the first one that applied#153679
estebank wants to merge 1 commit intorust-lang:mainfrom
estebank:issue-113550

Conversation

@estebank
Copy link
Contributor

@estebank estebank commented Mar 10, 2026

When calling a method that appears on Iterator, we often say "Foo is not an iterator", even if that method exists in other traits, which is quite confusing and many times nonsensical. Now, if we mention multiple traits we revert the main message back:

error[E0599]: no method named `take` found for struct `Foo` in the current scope
  --> $DIR/method-call-err-msg.rs:19:7
   |
LL |   pub struct Foo;
   |   -------------- method `take` not found for this struct because it doesn't satisfy `Foo: Iterator`
...
LL | /     y.zero()
LL | |      .take()
   | |      -^^^^ `Foo` is not an iterator
   | |______|
   |
   |
   = note: the following trait bounds were not satisfied:
           `Foo: Iterator`
           which is required by `&mut Foo: Iterator`
note: the trait `Iterator` must be implemented
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following traits define an item `take`, perhaps you need to implement one of them:
           candidate #1: `Iterator`
           candidate #2: `std::io::Read`

Fix #113550.

…n message to the first one that applied

When calling a method that appears on `Iterator`, we often say "`Foo` is not an iterator", even if that method exists in other traits, which is quite confusing and many times nonsensical. Now, if we mention multiple traits we revert the main message back:

```
error[E0599]: no method named `take` found for struct `Foo` in the current scope
  --> $DIR/method-call-err-msg.rs:19:7
   |
LL |   pub struct Foo;
   |   -------------- method `take` not found for this struct because it doesn't satisfy `Foo: Iterator`
...
LL | /     y.zero()
LL | |      .take()
   | |      -^^^^ `Foo` is not an iterator
   | |______|
   |
   |
   = note: the following trait bounds were not satisfied:
           `Foo: Iterator`
           which is required by `&mut Foo: Iterator`
note: the trait `Iterator` must be implemented
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following traits define an item `take`, perhaps you need to implement one of them:
           candidate rust-lang#1: `Iterator`
           candidate rust-lang#2: `std::io::Read`
```
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 10, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 10, 2026

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 17 candidates

@rustbot
Copy link
Collaborator

rustbot commented Mar 10, 2026

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

name = item_name,
)
};
if candidates_len > 1 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit loose and could cause a regression when it's still unsatisfied but multiple traits are found while a method actually existing. Maybe we can add a check for such a case not to override.

--> $DIR/method-call-err-msg.rs:19:7
|
LL | pub struct Foo;
| -------------- method `take` not found for this struct because it doesn't satisfy `Foo: Iterator`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message is now correct, but the labels here still assume that Iterator is the correct trait

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 11, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmp on non-Ord type says problem is type doesn't implement Iterator

4 participants