Skip to content

undefined behavior with bare fn pointer #20626

Closed
@pfalabella

Description

@pfalabella

compile with opt-level >= 2 (works fine with opt-level=0)

fn identity(a: &u32) -> &u32 { a }

fn print_foo(f: &fn(&u32) -> &u32, x: &u32) {
    println!("{}", (*f)(x));
}

fn main() {
    let x = &4;
    let f: fn(&u32) -> &u32 = identity;

    print_foo(&f, x); // prints 32597 for me
}
rustc 0.13.0-nightly (ad9e75938 2015-01-05 00:26:28 +0000)
binary: rustc
commit-hash: ad9e759382ad7daed26f86732f41f5f83cd673e2
commit-date: 2015-01-05 00:26:28 +0000
host: x86_64-unknown-linux-gnu
release: 0.13.0-nightly

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions