Skip to content

"the method rev() exists on usize" is misleading #90315

Closed
@rcoh

Description

@rcoh

Given the following code:

  let arr = &[0,1,2,3];
  for i in 0..arr.len().rev() {
     // ...   
  }

The current output is:

error[E0599]: the method `rev` exists for type `usize`, but its trait bounds were not satisfied
 --> src/main.rs:5:25
  |
5 |   for i in 0..arr.len().rev() {
  |                         ^^^ method cannot be called on `usize` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `usize: Iterator`
          which is required by `&mut usize: Iterator

Ideally the output should look like:

// Minimal fix: remove misleading message:
Omit "the method rev exists for type usize." 

// Amazing fix:
  |
5 |   for i in 0..arr.len().rev() {
  |                         ^^^ method cannot be called on `usize` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `usize: Iterator`
          which is required by `&mut usize: Iterator
  = hint: (0..arr.len()) implements `Iterator`, perhaps you are missing parentheses? 

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions