Skip to content

Commit c1fa3f2

Browse files
committed
fixup! Give up if ensure_sufficient_stack is called more than 1000 times
1 parent f336213 commit c1fa3f2

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

  • compiler/rustc_data_structures/src

compiler/rustc_data_structures/src/stack.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use std::cell::Cell;
12
use std::hint::{likely, unlikely};
23
use std::io::{self, Write};
3-
use std::sync::atomic::{AtomicU16, Ordering};
44

55
use backtrace::{Backtrace, BacktraceFrame};
66

@@ -18,7 +18,7 @@ const STACK_PER_RECURSION: usize = 1024 * 1024; // 1MB
1818
const STACK_PER_RECURSION: usize = 16 * 1024 * 1024; // 16MB
1919

2020
thread_local! {
21-
static TIMES_GROWN: AtomicU16 = const { AtomicU16::new(0) };
21+
static TIMES_GROWN: Cell<u16> = const { Cell::new(0) };
2222
}
2323

2424
// Give up if we expand the stack this many times and are still trying to recurse deeper.
@@ -39,12 +39,13 @@ pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
3939
if likely(enough_space) {
4040
f()
4141
} else {
42-
let times = TIMES_GROWN.with(|times| times.fetch_add(1, Ordering::Relaxed));
42+
let times = TIMES_GROWN.get();
4343
if unlikely(times > MAX_STACK_GROWTH) {
4444
too_much_stack();
4545
}
46+
TIMES_GROWN.set(times + 1);
4647
let out = stacker::grow(STACK_PER_RECURSION, f);
47-
TIMES_GROWN.with(|times| times.fetch_sub(1, Ordering::Relaxed));
48+
TIMES_GROWN.set(times);
4849
out
4950
}
5051
}

0 commit comments

Comments
 (0)