Skip to content

Strange non-primitive cast error near erroneous code #43825

Closed
@0x7CFE

Description

@0x7CFE

tl;dr; If you comment out the let error line, all will compile, obviously.

fn main() {
    let error = error; // Comment me
    for value in (1..10).map(|a| a as f32) {
        
    }
}

However, in presence of it, compiler complains

error[E0605]: non-primitive cast: `{integer}` as `f32`
 --> src/main.rs:3:34
  |
3 |     for value in (1..10).map(|a| a as f32) {
  |                                  ^^^^^^^^
  |
  = note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait

…which is strange and may mislead user in more complex situation. Looks like broken code affects type inference somehow. By the way, error will also appear, if you put erroneous line after the loop.

rustc 1.21.0-nightly (13d94d5fa 2017-08-10)
binary: rustc
commit-hash: 13d94d5fa8129a34f5c77a1bcd76983f5aed2434
commit-date: 2017-08-10
host: x86_64-unknown-linux-gnu
release: 1.21.0-nightly
LLVM version: 4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceA-type-systemArea: Type systemC-bugCategory: This is a bug.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