Skip to content

Commit ecbd110

Browse files
authored
Rollup merge of rust-lang#125302 - workingjubilee:prefer-my-stack-neat, r=compiler-errors
defrost `RUST_MIN_STACK=ice rustc hello.rs` I didn't think too hard about testing my previous PR rust-lang#122847 which makes our stack overflow handler assist people in discovering the `RUST_MIN_STACK` variable (which apparently is surprisingly useful for Really Big codebases). After it was merged, some useful comments left in a drive-by review led me to discover I had added an ICE. This reworks the code a bit to explain the rationale, remove the ICE that I introduced, and properly test one of the diagnostics.
2 parents 199d3bf + b6d0d6d commit ecbd110

File tree

7 files changed

+46
-10
lines changed

7 files changed

+46
-10
lines changed

compiler/rustc_interface/src/interface.rs

+1
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
389389
let hash_kind = config.opts.unstable_opts.src_hash_algorithm(&target);
390390

391391
util::run_in_thread_pool_with_globals(
392+
&early_dcx,
392393
config.opts.edition,
393394
config.opts.unstable_opts.threads,
394395
SourceMapInputs { file_loader, path_mapping, hash_kind },

compiler/rustc_interface/src/util.rs

+32-9
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,38 @@ pub fn add_configuration(cfg: &mut Cfg, sess: &mut Session, codegen_backend: &dy
5151
pub static STACK_SIZE: OnceLock<usize> = OnceLock::new();
5252
pub const DEFAULT_STACK_SIZE: usize = 8 * 1024 * 1024;
5353

54-
fn init_stack_size() -> usize {
54+
fn init_stack_size(early_dcx: &EarlyDiagCtxt) -> usize {
5555
// Obey the environment setting or default
5656
*STACK_SIZE.get_or_init(|| {
5757
env::var_os("RUST_MIN_STACK")
58-
.map(|os_str| os_str.to_string_lossy().into_owned())
59-
// ignore if it is set to nothing
60-
.filter(|s| s.trim() != "")
61-
.map(|s| s.trim().parse::<usize>().unwrap())
58+
.as_ref()
59+
.map(|os_str| os_str.to_string_lossy())
60+
// if someone finds out `export RUST_MIN_STACK=640000` isn't enough stack
61+
// they might try to "unset" it by running `RUST_MIN_STACK= rustc code.rs`
62+
// this is wrong, but std would nonetheless "do what they mean", so let's do likewise
63+
.filter(|s| !s.trim().is_empty())
64+
// rustc is a batch program, so error early on inputs which are unlikely to be intended
65+
// so no one thinks we parsed them setting `RUST_MIN_STACK="64 megabytes"`
66+
// FIXME: we could accept `RUST_MIN_STACK=64MB`, perhaps?
67+
.map(|s| {
68+
let s = s.trim();
69+
// FIXME(workingjubilee): add proper diagnostics when we factor out "pre-run" setup
70+
#[allow(rustc::untranslatable_diagnostic, rustc::diagnostic_outside_of_impl)]
71+
s.parse::<usize>().unwrap_or_else(|_| {
72+
let mut err = early_dcx.early_struct_fatal(format!(
73+
r#"`RUST_MIN_STACK` should be a number of bytes, but was "{s}""#,
74+
));
75+
err.note("you can also unset `RUST_MIN_STACK` to use the default stack size");
76+
err.emit()
77+
})
78+
})
6279
// otherwise pick a consistent default
6380
.unwrap_or(DEFAULT_STACK_SIZE)
6481
})
6582
}
6683

6784
fn run_in_thread_with_globals<F: FnOnce(CurrentGcx) -> R + Send, R: Send>(
85+
thread_stack_size: usize,
6886
edition: Edition,
6987
sm_inputs: SourceMapInputs,
7088
f: F,
@@ -75,7 +93,7 @@ fn run_in_thread_with_globals<F: FnOnce(CurrentGcx) -> R + Send, R: Send>(
7593
// the parallel compiler, in particular to ensure there is no accidental
7694
// sharing of data between the main thread and the compilation thread
7795
// (which might cause problems for the parallel compiler).
78-
let builder = thread::Builder::new().name("rustc".to_string()).stack_size(init_stack_size());
96+
let builder = thread::Builder::new().name("rustc".to_string()).stack_size(thread_stack_size);
7997

8098
// We build the session globals and run `f` on the spawned thread, because
8199
// `SessionGlobals` does not impl `Send` in the non-parallel compiler.
@@ -100,16 +118,19 @@ fn run_in_thread_with_globals<F: FnOnce(CurrentGcx) -> R + Send, R: Send>(
100118

101119
#[cfg(not(parallel_compiler))]
102120
pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send, R: Send>(
121+
thread_builder_diag: &EarlyDiagCtxt,
103122
edition: Edition,
104123
_threads: usize,
105124
sm_inputs: SourceMapInputs,
106125
f: F,
107126
) -> R {
108-
run_in_thread_with_globals(edition, sm_inputs, f)
127+
let thread_stack_size = init_stack_size(thread_builder_diag);
128+
run_in_thread_with_globals(thread_stack_size, edition, sm_inputs, f)
109129
}
110130

111131
#[cfg(parallel_compiler)]
112132
pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send, R: Send>(
133+
thread_builder_diag: &EarlyDiagCtxt,
113134
edition: Edition,
114135
threads: usize,
115136
sm_inputs: SourceMapInputs,
@@ -121,10 +142,12 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
121142
use rustc_query_system::query::{break_query_cycles, QueryContext};
122143
use std::process;
123144

145+
let thread_stack_size = init_stack_size(thread_builder_diag);
146+
124147
let registry = sync::Registry::new(std::num::NonZero::new(threads).unwrap());
125148

126149
if !sync::is_dyn_thread_safe() {
127-
return run_in_thread_with_globals(edition, sm_inputs, |current_gcx| {
150+
return run_in_thread_with_globals(thread_stack_size, edition, sm_inputs, |current_gcx| {
128151
// Register the thread for use with the `WorkerLocal` type.
129152
registry.register();
130153

@@ -167,7 +190,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
167190
})
168191
.unwrap();
169192
})
170-
.stack_size(init_stack_size());
193+
.stack_size(thread_stack_size);
171194

172195
// We create the session globals on the main thread, then create the thread
173196
// pool. Upon creation, each worker thread created gets a copy of the

tests/ui/rustc-env/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Some environment variables affect rustc's behavior not because they are major compiler interfaces
2+
but rather because rustc is, ultimately, a Rust program, with debug logging, stack control, etc.
3+
4+
Prefer to group tests that use environment variables to control something about rustc's core UX,
5+
like "can we parse this number of parens if we raise RUST_MIN_STACK?" with related code for that
6+
compiler feature.
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//@ rustc-env:RUST_MIN_STACK=banana
2+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: `RUST_MIN_STACK` should be a number of bytes, but was "banana"
2+
|
3+
= note: you can also unset `RUST_MIN_STACK` to use the default stack size
4+

tests/ui/rustc-rust-log.rs renamed to tests/ui/rustc-env/rust-log.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//@ dont-check-compiler-stdout
66
//@ dont-check-compiler-stderr
77
//@ compile-flags: --error-format human
8-
//@ aux-build: rustc-rust-log-aux.rs
8+
//@ aux-build: rust-log-aux.rs
99
//@ rustc-env:RUSTC_LOG=debug
1010

1111
fn main() {}

0 commit comments

Comments
 (0)