Skip to content

panic in program that shouldn't panic when compiling at optimization level 1 or 2 #37747

Closed
@mrwright

Description

@mrwright

It looks like if a function called from main contains just a loop {}, and it's compiled at optimization level 1 or 2, the program will panic when run instead of looping as it's supposed to.

Contents of main.rs:

fn x() { loop {} }

fn main() { x(); }

I expect this to loop forever when run. Instead, when compiled with rustc -C opt-level=1 main.rs and run, it panics; with RUST_BACKTRACE=1 set, the output is

thread 'main' panicked at 'assertion failed: c.borrow().is_none()', ../src/libstd/sys/common/thread_info.rs:51
stack backtrace:
   1:     0x56456e2de7ff - std::sys::backtrace::tracing::imp::write::h6f1d53a70916b90d
   2:     0x56456e2e12ad - std::panicking::default_hook::{{closure}}::h137e876f7d3b5850
   3:     0x56456e2e080a - std::panicking::default_hook::h0ac3811ec7cee78c
   4:     0x56456e2e0d58 - std::panicking::rust_panic_with_hook::hc303199e04562edf
   5:     0x56456e2e0b9f - std::panicking::begin_panic::h341b039f84d0b176
   6:     0x56456e2de3ef - std::sys_common::thread_info::set::h6f96b62c207d3b7d
   7:     0x56456e2dfdd9 - std::rt::lang_start::h538f8960e7644c80
   8:     0x56456e2e8d76 - __rust_maybe_catch_panic
   9:     0x56456e2e0081 - std::rt::lang_start::h538f8960e7644c80
  10:     0x7fb381dce2b0 - __libc_start_main
  11:     0x56456e2da349 - _start
  12:                0x0 - <unknown>

Tested on rustc 1.13.0 (2c6933acc 2016-11-07), rustc 1.13.0-beta.3 (106d18793 2016-11-04), and rustc 1.14.0-nightly (cae6ab1c4 2016-11-05); the issue occurs on all three.

It also (unsurprisingly) happens with opt-level=2. It does not occur at optimization level 0, and doesn't occur if the loop is in main itself rather than a function that main calls.

Output of rustc --version --verbose for the stable rustc:

rustc 1.13.0 (2c6933acc 2016-11-07)               
binary: rustc
commit-hash: 2c6933acc05c61e041be764cb1331f6281993f3f
commit-date: 2016-11-07
host: x86_64-unknown-linux-gnu
release: 1.13.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions