Skip to content

Compiler incorrectly (and inconsistently) finds Iterator methods on i32 #84495

Closed
@aDotInTheVoid

Description

@aDotInTheVoid

Given the following code: playground

fn main() {
    let x: i32 = 1;
    println!("{:?}", x.count())
}

The current output is:

error[E0599]: the method `count` exists for type `i32`, but its trait bounds were not satisfied
 --> src/main.rs:3:24
  |
3 |     println!("{:?}", x.count())
  |                        ^^^^^ method cannot be called on `i32` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `i32: Iterator`
          which is required by `&mut i32: Iterator`

error: aborting due to previous error

Ideally the output should look like:

error[E0599]: the method `count` does not exist for type `i32`

For some reason, this doesn't happen for all traits in std. Eg

fn main() {
    let x: i32 = 1;
    println!("{:?}", x.write(&[1, 2, 3]))
}

Just gives

error[E0599]: no method named `write` found for type `i32` in the current scope
 --> src/main.rs:3:24
  |
3 |     println!("{:?}", x.write(&[1, 2, 3]))
  |                        ^^^^^ method not found in `i32`

error: aborting due to previous error

playground

Version: 1.53.0-nightly (2021-04-22 7f4afdf)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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