Skip to content

Unary - does not check for overflow #23154

@simias

Description

@simias

The following code prints -2147483648 -2147483648:

fn main() {
    let i = 0x80000000u32 as i32;

    println!("{} {}", i, -i);
}

Both 0 - i and i / -1 generate an overflow error but it seems the unary - gladly wraps around.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions