Skip to content

Another mir-opt-level=2 miscompilation #67862

Closed
@leonardo-m

Description

@leonardo-m

On the last Nightly:

rustc 1.42.0-nightly (c5840f9d2 2020-01-03)
binary: rustc
commit-hash: c5840f9d252c2f5cc16698dbf385a29c5de3ca07
commit-date: 2020-01-03
host: x86_64-pc-windows-gnu
release: 1.42.0-nightly
LLVM version: 9.0

rustc -Z mir-opt-level=1 test.rs   => OK
rustc -Z mir-opt-level=2 test.rs   => Error

fn e220() -> String {
    fn get_displacement(mut distance: i64, generation: i32) -> [i64; 4] {
        if generation == 0 {
            return match distance {
                0 => [0, 1, 0, 0],
                1 => [0, 1, 0, 1],
                _ => panic!("Travel past end")
            }
        }

        let half_len = 1i64 << (generation - 1);
        let second_half = distance > half_len;

        if second_half {
            distance = half_len + half_len - distance;
        }

        let mut ret = get_displacement(distance, generation - 1);
        let (ex, ey) = (ret[0], ret[1]);
        ret[0] = ex + ey;
        ret[1] = ey - ex;

        if second_half {
            let (px, py) = (ret[2], ret[3]);
            ret[2] = ex + ey - py;
            ret[3] = ey - ex + px;
        }

        ret
    }

    let res = get_displacement(1_000_000_000_000, 50);
    format!("{},{}", res[2], res[3])
}

fn main() {
    assert_eq!(e220(), "139776,963904");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions