rust-lang / rust-lang/rust

rustc crashes with SIGSEGV when compiling a 1000 nested inline const blocks.

Open
#138,547 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-parser C-bug I-crash P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

I have generated input file containing a 1000 nested inline const blocks with following python script:

def main():
    with open("silly.rs", "w") as file:
        n = 1000
        file.write("const _: () = ")
        for _ in range(0, n):
            file.write("const { ")

        for _ in range(0, n):
            file.write("} ")

        file.write(";\n")


if __name__ == "__main__":
    main()

Then called rustc:

rustc --crate-type=lib silly.rs

Which crashes with a SIGSEGV (backtrace below).

I expected to see this happen:

I don't know really. I was just being silly and wanted to see what would happen. I know that this is an unreasonable rust code, and I was prepared for some compiler error. However seeing not even an ICE, but a full compiler crash surprised me. Setting $RUST_MIN_STACK to suggested value helps, but still this seems to be some form of a compiler bug.

I quickly searched for relevant issues, but I have not found anything similar. I have now idea if and how this should be fixed, but I thought that at least I can report it.

Meta

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7
Backtrace

$ rustc --crate-type=lib silly.rs
error: rustc interrupted by SIGSEGV, printing backtrace

/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-77ea5bfe5f9d9ec5.so(+0x36705c3) [0x7221174705c3]
/usr/lib/libc.so.6(+0x3dcd0) [0x722113c4bcd0]
/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc(+0x1475a) [0x56e2f7f6875a]
/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-77ea5bfe5f9d9ec5.so(_RNvMNtNtCs2a1sXFqEM9A_11rustc_parse6parser4stmtNtB4_6Parser27parse_stmt_without_recovery+0xf4) [0x7221193a157a]

### cycle encountered after 4 frames with period 7
/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-77ea5bfe5f9d9ec5.so(_RNvMNtNtCs2a1sXFqEM9A_11rustc_parse6parser4stmtNtB4_6Parser15parse_full_stmt+0x41) [0x72211939f301]
/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-77ea5bfe5f9d9ec5.so(+0x559bb5b) [0x72211939bb5b]
/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-77ea5bfe5f9d9ec5.so(+0x5e73f81) [0x722119c73f81]
/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-77ea5bfe5f9d9ec5.so(+0x5560ad2) [0x722119360ad2]
/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-77ea5bfe5f9d9ec5.so(+0x55516b1) [0x7221193516b1]
/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-77ea5bfe5f9d9ec5.so(_RNvMNtNtCs2a1sXFqEM9A_11rustc_parse6parser4exprNtB4_6Parser21parse_expr_assoc_with+0x53) [0x72211935521f]
/home/username/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-77ea5bfe5f9d9ec5.so(_RNvMNtNtCs2a1sXFqEM9A_11rustc_parse6parser4stmtNtB4_6Parser27parse_stmt_without_recovery+0x2b66) [0x7221193a3fec]
### recursed 36 times


note: rustc unexpectedly overflowed its stack! this is a bug
note: maximum backtrace depth reached, frames may have been lost
note: we would appreciate a report at https://github.com/rust-lang/rust
help: you can increase rustc's stack size by setting RUST_MIN_STACK=16777216
zsh: segmentation fault (core dumped)  rustc --crate-type=lib silly.rs

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash by generating silly.rs with the provided Python script, then run rustc --crate-type=lib silly.rs and inspect the rustc_parse parser frames in the backtrace, especially parse_stmt_without_recovery, parse_full_stmt, and parse_expr_assoc_with. Done means the same deeply nested inline-const input no longer causes a SIGSEGV or stack overflow, while the compiler reports an appropriate result.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.