Skip to content

generic const items: polymorphically recursive consts lead to compiler stack overflow #114192

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

Closed
fmease opened this issue Jul 29, 2023 · 1 comment · Fixed by #135167
Closed
Labels
C-bug Category: This is a bug. F-generic_const_items `#![feature(generic_const_items)]` I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. requires-incomplete-features This issue requires the use of incomplete features. S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fmease
Copy link
Member

fmease commented Jul 29, 2023

I expected the following code to lead to an error (overflow, const eval, or cycle):

#![feature(generic_const_items)]

const RECUR<T>: () = RECUR::<(T,)>;

fn main() {}

Instead, the memory & CPU usage of the compiler quickly grew until the OOM killer terminated the process (I presume this to be a stack overflow turned into an OOM situation due to the use of ensure_sufficient_stack somewhere which causes stacks to be allocated on the heap).

Debug builds of the compiler crash immediately with a segmentation fault.
Running rustc with GDB yields (which merely looks like a symptom):

Thread 2 "rustc" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffe8bff6c0 (LWP 433325)]
rustc_middle::ty::print::pretty::PrettyPrinter::pretty_print_type<rustc_middle::ty::print::pretty::FmtPrinter> (self=..., ty=...) at compiler/rustc_middle/src/ty/print/pretty.rs:909
909             ty::Slice(ty) => p!("[", print(ty), "]"),

(14,000 line backtrace not included in this issue ^^)

Corresponding UI test: tests/ui/generic-const-items/recursive.rs (known-bug, ignore-test).

Meta

rustc -Vv
rustc 1.73.0-nightly (04abc370b 2023-07-28)
binary: rustc
commit-hash: 04abc370b9f3855b28172b65a7f7d5a433f41412
commit-date: 2023-07-28
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

@rustbot label C-bug T-compiler I-crash requires-incomplete-features F-generic_const_items
@rustbot claim

@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-bug Category: This is a bug. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. requires-incomplete-features This issue requires the use of incomplete features. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 29, 2023
@rustbot rustbot added F-generic_const_items `#![feature(generic_const_items)]` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 29, 2023
@fmease fmease added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Sep 20, 2023
@theemathas
Copy link
Contributor

Reproduction of the issue on stable:

struct Thing<T>(T);

impl<T> Thing<T> {
    const X: usize = Thing::<Option<T>>::X;
}

fn main() {
    println!("{}", Thing::<i32>::X);
}

bors added a commit to rust-lang-ci/rust that referenced this issue Jan 8, 2025
… r=<try>

Depth limit const eval query

Currently the const-eval query doesn't have a recursion limit or timeout, causing the complier to freeze in an infinite loop, see rust-lang#125718. This PR depth limits the `eval_to_const_value_raw` query (with the [`recursion_limit`](https://doc.rust-lang.org/reference/attributes/limits.html) attribute) and improves the diagnostics for query overflow errors, so spans are reported for other dep kinds than `layout_of` (e.g. `eval_to_const_value_raw`).

fixes rust-lang#125718
fixes rust-lang#114192
mzacho added a commit to mzacho/rust that referenced this issue Jan 8, 2025
mzacho added a commit to mzacho/rust that referenced this issue Jan 8, 2025
@bors bors closed this as completed in 3ff1b64 Jan 13, 2025
@fmease fmease removed their assignment Feb 11, 2025
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. F-generic_const_items `#![feature(generic_const_items)]` I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. requires-incomplete-features This issue requires the use of incomplete features. S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Development

Successfully merging a pull request may close this issue.

3 participants