Skip to content

When a proc_macro has a stack overflow, rustc crashes and errors confusingly #118701

Open
@PizzasBear

Description

@PizzasBear

Code

// --- macro_crate/src/lib.rs ---
use proc_macro::TokenStream;

#[proc_macro_derive(Trait)]
pub fn derive_trait(tokens: TokenStream) -> TokenStream {
    if true {
        derive_trait(tokens)
    } else {
        TokenStream::new()
    }
}


// --- my-crate/src/main.rs ---
#[derive(Trait)]
struct S;

fn main() {}

Current output

error: rustc interrupted by SIGSEGV, printing backtrace

~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-fd05879751a9d790.so(+0x3104966)[0x7f5e85f04966]
/lib64/libc.so.6(+0x3e9a0)[0x7f5e82ac49a0]
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21420)[0x7f5e79482420]

### cycle encountered after 3 frames with period 4
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]
.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]
### recursed 63 times

.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so(+0x21425)[0x7f5e79482425]

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
error: could not compile `test-issue` (bin "test-issue")

Caused by:
  process didn't exit successfully: `~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name test_issue --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=212 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=10df6c618bb41553 -C extra-filename=-10df6c618bb41553 --out-dir .../test-issue/target/debug/deps -C incremental=.../test-issue/target/debug/incremental -L dependency=.../test-issue/target/debug/deps --extern askama=.../test-issue/target/debug/deps/libaskama-a71517d66164d0f1.rlib --extern macro_crate=.../test-issue/target/debug/deps/libmacro_crate-a1b04264bf77fcce.so --extern serde=.../test-issue/target/debug/deps/libserde-916cd61b435466f2.rlib -C link-arg=-fuse-ld=lld` (signal: 11, SIGSEGV: invalid memory reference)

Desired output

Macro at `#[derive(Trait)]` has overflowed.

Or something that specifies the place where the macro overflowed, its crate, and that doesn't imply the issue is with rustc.

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-error-handlingArea: Error handlingA-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions