Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vita-dounai opened this issue Dec 28, 2020 · 2 comments
Closed

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

vita-dounai opened this issue Dec 28, 2020 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@vita-dounai
Copy link

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>

@vita-dounai vita-dounai added the C-bug Category: This is a bug. label Dec 28, 2020
@ehuss
Copy link
Contributor

ehuss commented Dec 28, 2020

I believe that infinite loops are undefined behavior in LLVM, which can result in anything happening. See issue #28728.

@jonas-schievink
Copy link
Contributor

Yes, closing as a duplicate of that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants