Skip to content

Simple recursion but get weird result when optimization is enabled #80445

Closed
@vita-dounai

Description

@vita-dounai

I tried this code:

fn main() {
    let ret = fn1(42);
    println!("it returns! {}", ret);
}

fn fn1(i: u32) -> u32 {
    fn2(i)
}

fn fn2(i: u32) -> u32 {
    fn1(i)
}

I expected to see this happen: in the code above, fn1 and fn2 just simply call each other, and there is indeed a infinitely recursion. I expect that when the main is executed, the call to f1 inside main should never return, and the stack will be overflowed at some point.

Instead, this happened: when I enable lto or set opt-level to 1 or higher, the call to f1 suprisingly returns, and the return value is always zero.

image

Meta

rustc --version --verbose:

rustc 1.48.0 (7eac88abb 2020-11-16)
binary: rustc
commit-hash: 7eac88abb2e57e752f3302f02be5f3ce3d7adfb4
commit-date: 2020-11-16
host: x86_64-pc-windows-msvc
release: 1.48.0
LLVM version: 11.0
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions