diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 7c3934e525f..7f66b7895d4 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -11,7 +11,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey}; use rustc_target::abi::Align; -use rustc_target::spec::{LinkerFlavorCli, PanicStrategy, SanitizerSet, SplitDebuginfo}; +use rustc_target::spec::{PanicStrategy, SanitizerSet, SplitDebuginfo}; use rustc_target::spec::{Target, TargetTriple, TargetWarnings, TARGETS}; use crate::parse::{CrateCheckConfig, CrateConfig}; @@ -308,6 +308,14 @@ pub fn on() -> Self { on } + /// To help checking CLI usage while some of the values are unstable: returns whether one of the + /// components was set individually. This would also require the `-Zunstable-options` flag, to + /// be allowed. + fn are_unstable_variants_set(&self) -> bool { + let any_component_set = !self.components.is_empty(); + self.explicitly_set.is_none() && any_component_set + } + /// Returns whether the self-contained linker component is enabled. pub fn linker(&self) -> bool { self.components.contains(LinkSelfContainedComponents::LINKER) @@ -2648,16 +2656,28 @@ pub fn build_session_options( } } - if let Some(flavor) = cg.linker_flavor { - if matches!(flavor, LinkerFlavorCli::BpfLinker | LinkerFlavorCli::PtxLinker) - && !nightly_options::is_unstable_enabled(matches) - { - let msg = format!( - "linker flavor `{}` is unstable, `-Z unstable-options` \ - flag must also be passed to explicitly use it", - flavor.desc() + // For testing purposes, until we have more feedback about these options: ensure `-Z + // unstable-options` is required when using the unstable `-C link-self-contained` options, like + // `-C link-self-contained=+linker`, and when using the unstable `-C linker-flavor` options, like + // `-C linker-flavor=gnu-lld-cc`. + if !nightly_options::is_unstable_enabled(matches) { + let uses_unstable_self_contained_option = + cg.link_self_contained.are_unstable_variants_set(); + if uses_unstable_self_contained_option { + handler.early_error( + "only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off` are stable, \ + the `-Z unstable-options` flag must also be passed to use the unstable values", ); - handler.early_error(msg); + } + + if let Some(flavor) = cg.linker_flavor { + if flavor.is_unstable() { + handler.early_error(format!( + "the linker flavor `{}` is unstable, the `-Z unstable-options` \ + flag must also be passed to use the unstable values", + flavor.desc() + )); + } } } diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index c3eed5955e9..2e5bb3db886 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -181,6 +181,29 @@ pub enum LinkerFlavorCli { PtxLinker, } +impl LinkerFlavorCli { + /// Returns whether this `-C linker-flavor` option is one of the unstable values. + pub fn is_unstable(&self) -> bool { + match self { + LinkerFlavorCli::Gnu(..) + | LinkerFlavorCli::Darwin(..) + | LinkerFlavorCli::WasmLld(..) + | LinkerFlavorCli::Unix(..) + | LinkerFlavorCli::Msvc(Lld::Yes) + | LinkerFlavorCli::EmCc + | LinkerFlavorCli::Bpf + | LinkerFlavorCli::Ptx + | LinkerFlavorCli::BpfLinker + | LinkerFlavorCli::PtxLinker => true, + LinkerFlavorCli::Gcc + | LinkerFlavorCli::Ld + | LinkerFlavorCli::Lld(..) + | LinkerFlavorCli::Msvc(Lld::No) + | LinkerFlavorCli::Em => false, + } + } +} + #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] pub enum LldFlavor { Wasm,