When a `proc_macro` has a stack overflow, rustc crashes and errors confusingly
Open
Nobody has claimed this yet.
A-error-handling
A-proc-macros
C-bug
I-crash
T-compiler
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the crash using macro_crate/src/lib.rs and my-crate/src/main.rs, where the derive proc macro recursively calls itself. Compare the current SIGSEGV and stack-overflow output with the requested diagnostic identifying #[derive(Trait)] and its location; the report does not name a rustc implementation file or test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100