Skip to content

Commit 31ad4e4

Browse files
authored
Rollup merge of #132355 - practicalrs:fix_117638, r=SparrowLii
Fix compiler panic with a large number of threads Hi, This PR is an attempt to fix the problem described here #117638 using the solution suggested in this comment #117638 (comment) Best regards, Michal
2 parents 432972c + 7591eb6 commit 31ad4e4

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Diff for: compiler/rustc_session/src/config.rs

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

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

Diff for: 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)