Fix compiler panic with a large number of threads

This commit is contained in:
Michal Piotrowski 2024-10-30 12:31:26 +01:00
parent 16422dbd89
commit 7591eb60ad
No known key found for this signature in database
GPG Key ID: 2538DC6C8E7C7859
2 changed files with 12 additions and 3 deletions

View File

@ -2464,6 +2464,10 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
early_dcx.early_fatal("value for threads must be a positive non-zero integer"); early_dcx.early_fatal("value for threads must be a positive non-zero integer");
} }
if unstable_opts.threads == parse::MAX_THREADS_CAP {
early_dcx.early_warn(format!("number of threads was capped at {}", parse::MAX_THREADS_CAP));
}
let fuel = unstable_opts.fuel.is_some() || unstable_opts.print_fuel.is_some(); let fuel = unstable_opts.fuel.is_some() || unstable_opts.print_fuel.is_some();
if fuel && unstable_opts.threads > 1 { if fuel && unstable_opts.threads > 1 {
early_dcx.early_fatal("optimization fuel is incompatible with multiple threads"); early_dcx.early_fatal("optimization fuel is incompatible with multiple threads");

View File

@ -457,10 +457,11 @@ mod desc {
"either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)"; "either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)";
} }
mod parse { pub mod parse {
use std::str::FromStr; use std::str::FromStr;
pub(crate) use super::*; pub(crate) use super::*;
pub(crate) const MAX_THREADS_CAP: usize = 256;
/// This is for boolean options that don't take a value and start with /// This is for boolean options that don't take a value and start with
/// `no-`. This style of option is deprecated. /// `no-`. This style of option is deprecated.
@ -657,7 +658,7 @@ mod parse {
} }
pub(crate) fn parse_threads(slot: &mut usize, v: Option<&str>) -> bool { pub(crate) fn parse_threads(slot: &mut usize, v: Option<&str>) -> bool {
match v.and_then(|s| s.parse().ok()) { let ret = match v.and_then(|s| s.parse().ok()) {
Some(0) => { Some(0) => {
*slot = std::thread::available_parallelism().map_or(1, NonZero::<usize>::get); *slot = std::thread::available_parallelism().map_or(1, NonZero::<usize>::get);
true true
@ -667,7 +668,11 @@ mod parse {
true true
} }
None => false, None => false,
} };
// We want to cap the number of threads here to avoid large numbers like 999999 and compiler panics.
// This solution was suggested here https://github.com/rust-lang/rust/issues/117638#issuecomment-1800925067
*slot = slot.clone().min(MAX_THREADS_CAP);
ret
} }
/// Use this for any numeric option that has a static default. /// Use this for any numeric option that has a static default.