From 2ce46f8e8c2efd76973bfa3a068cf67b7b525d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Wed, 20 Sep 2023 20:09:06 +0000 Subject: [PATCH] move single component parsing to dedicated function this will prevent parsing when expecting more than a single component to be parsed, and prepare for the symetric variant-to-name function to be added --- compiler/rustc_session/src/config.rs | 12 ++++++------ compiler/rustc_session/src/options.rs | 2 +- compiler/rustc_target/src/spec/mod.rs | 11 +++++------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 5e46a86f1ff..e3e29669ec5 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -240,21 +240,21 @@ pub struct LinkSelfContained { impl LinkSelfContained { /// Incorporates an enabled or disabled component as specified on the CLI, if possible. /// For example: `+linker`, and `-crto`. - pub(crate) fn handle_cli_component(&mut self, component: &str) -> Result<(), ()> { + pub(crate) fn handle_cli_component(&mut self, component: &str) -> Option<()> { // Note that for example `-Cself-contained=y -Cself-contained=-linker` is not an explicit // set of all values like `y` or `n` used to be. Therefore, if this flag had previously been // set in bulk with its historical values, then manually setting a component clears that // `explicitly_set` state. if let Some(component_to_enable) = component.strip_prefix('+') { self.explicitly_set = None; - self.components.insert(component_to_enable.parse()?); - Ok(()) + self.components.insert(LinkSelfContainedComponents::from_str(component_to_enable)?); + Some(()) } else if let Some(component_to_disable) = component.strip_prefix('-') { self.explicitly_set = None; - self.components.remove(component_to_disable.parse()?); - Ok(()) + self.components.remove(LinkSelfContainedComponents::from_str(component_to_disable)?); + Some(()) } else { - Err(()) + None } } diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index f7c000c8bd6..e8f3123b99e 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1165,7 +1165,7 @@ mod parse { // 2. Parse a list of enabled and disabled components. for comp in s.split(',') { - if slot.handle_cli_component(comp).is_err() { + if slot.handle_cli_component(comp).is_none() { return false; } } diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 40d92bd5387..5c31c856be4 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -539,18 +539,17 @@ bitflags::bitflags! { } } -impl FromStr for LinkSelfContainedComponents { - type Err = (); - - fn from_str(s: &str) -> Result { - Ok(match s { +impl LinkSelfContainedComponents { + /// Parses a single `-Clink-self-contained` well-known component, not a set of flags. + pub fn from_str(s: &str) -> Option { + Some(match s { "crto" => LinkSelfContainedComponents::CRT_OBJECTS, "libc" => LinkSelfContainedComponents::LIBC, "unwind" => LinkSelfContainedComponents::UNWIND, "linker" => LinkSelfContainedComponents::LINKER, "sanitizers" => LinkSelfContainedComponents::SANITIZERS, "mingw" => LinkSelfContainedComponents::MINGW, - _ => return Err(()), + _ => return None, }) } }