Skip to content

fn(u64) -> u64 can be cast as i64, resulting in gibberish #50796

Closed
@jonathanstrong

Description

@jonathanstrong

minimal example:

fn f(x: u64) -> u64 { x * 2 }

fn main() {
    let g = f as i64;
    println!("{}", g); // 94295280847184
}

playground

Ordinarily rust is very strict about casts so I was surprised by this behavior when, because of a typo, I was trying to cast a fn as an i64. It compiled fine and resulted in garbage numbers that were difficult to track down. I assume this is a bug and not intended behavior.

First encountered the problem on nightly 1.27 2018-05-13 but it is also present in stable 1.26, per the playground example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.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