From 92d06b48217746f483f5092364ea60acf75c830f Mon Sep 17 00:00:00 2001 From: Michal Piotrowski Date: Wed, 30 Oct 2024 12:31:26 +0100 Subject: [PATCH] Fix compiler panic with a large number of threads --- compiler/rustc_session/src/config.rs | 4 ++++ compiler/rustc_session/src/options.rs | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index d733e32f209db..20a1affa0087b 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -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"); } + 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(); if fuel && unstable_opts.threads > 1 { early_dcx.early_fatal("optimization fuel is incompatible with multiple threads"); diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 54a4621db2462..d2e3ca754dd95 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -457,10 +457,11 @@ mod desc { "either a boolean (`yes`, `no`, `on`, `off`, etc), or `nll` (default: `nll`)"; } -mod parse { +pub mod parse { use std::str::FromStr; 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 /// `no-`. This style of option is deprecated. @@ -663,7 +664,9 @@ mod parse { true } Some(i) => { - *slot = i; + // 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 = i.min(MAX_THREADS_CAP); true } None => false,