Skip to content

Commit 7591eb6

Browse files
committed
Fix compiler panic with a large number of threads
1 parent 16422db commit 7591eb6

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

compiler/rustc_session/src/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2464,6 +2464,10 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
24642464
early_dcx.early_fatal("value for threads must be a positive non-zero integer");
24652465
}
24662466

2467+
if unstable_opts.threads == parse::MAX_THREADS_CAP {
2468+
early_dcx.early_warn(format!("number of threads was capped at {}", parse::MAX_THREADS_CAP));
2469+
}
2470+
24672471
let fuel = unstable_opts.fuel.is_some() || unstable_opts.print_fuel.is_some();
24682472
if fuel && unstable_opts.threads > 1 {
24692473
early_dcx.early_fatal("optimization fuel is incompatible with multiple threads");

compiler/rustc_session/src/options.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,11 @@ mod desc {
457457
"either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)";
458458
}
459459

460-
mod parse {
460+
pub mod parse {
461461
use std::str::FromStr;
462462

463463
pub(crate) use super::*;
464+
pub(crate) const MAX_THREADS_CAP: usize = 256;
464465

465466
/// This is for boolean options that don't take a value and start with
466467
/// `no-`. This style of option is deprecated.
@@ -657,7 +658,7 @@ mod parse {
657658
}
658659

659660
pub(crate) fn parse_threads(slot: &mut usize, v: Option<&str>) -> bool {
660-
match v.and_then(|s| s.parse().ok()) {
661+
let ret = match v.and_then(|s| s.parse().ok()) {
661662
Some(0) => {
662663
*slot = std::thread::available_parallelism().map_or(1, NonZero::<usize>::get);
663664
true
@@ -667,7 +668,11 @@ mod parse {
667668
true
668669
}
669670
None => false,
670-
}
671+
};
672+
// We want to cap the number of threads here to avoid large numbers like 999999 and compiler panics.
673+
// This solution was suggested here https://github.com/rust-lang/rust/issues/117638#issuecomment-1800925067
674+
*slot = slot.clone().min(MAX_THREADS_CAP);
675+
ret
671676
}
672677

673678
/// Use this for any numeric option that has a static default.

0 commit comments

Comments
 (0)