Skip to content

Mis-compilation of infinite recursions #85742

Closed
@ksqsf

Description

@ksqsf

Consider this code

use std::time::Instant;

fn collatz(i: usize) -> bool {
    if i == 1 {
        true
    } else if i % 2 == 0 {
        collatz(i / 2)
    } else {
        collatz(3 * i + 1)
    }
}

fn main() {
    static TIMES: usize = 500000;
    let start = Instant::now();
    for i in 0..TIMES {
        if ! collatz(i) {
            println!("{}", i);
            return;
        }
    }
    println!("Done, {} millis elapsed!", start.elapsed().as_millis());
}

It contains infinite recursion (collatz(0)), but the whole loop gets optimized away with Nightly + Release.

Related: #28728 (Perhaps also related to this commit).

Not sure if this is an LLVM bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-criticalCritical priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions