Skip to content

Commit 6bd34d7

Browse files
committed
update check_unstable_variants's handling of -C linker-features=[-+]lld
- separate enabling and disabling the feature in the error - add both polarities to the dedicated test - update documentation and precondition
1 parent 2ed3d9a commit 6bd34d7

File tree

6 files changed

+55
-28
lines changed

6 files changed

+55
-28
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -451,32 +451,41 @@ impl LinkerFeaturesCli {
451451
}
452452
}
453453

454-
/// Checks usage of unstable variants for linker features for the given `target_tuple`.
455-
/// Returns `Ok` if no unstable variants are used.
454+
/// When *not* using `-Z unstable-options` on the CLI, ensure only stable linker features are
455+
/// used, for the given `TargetTuple`. Returns `Ok` if no unstable variants are used.
456+
/// The caller should ensure that e.g. `nightly_options::is_unstable_enabled()`
457+
/// returns false.
456458
pub(crate) fn check_unstable_variants(&self, target_tuple: &TargetTuple) -> Result<(), String> {
457-
let mentioned_features = self.enabled.union(self.disabled);
458-
let has_lld = mentioned_features.is_lld_enabled();
459-
460-
// Check that -Clinker-features=[-+]lld is not used anywhere else than on x64
461-
// without -Zunstable-options.
462-
if has_lld && target_tuple.tuple() != "x86_64-unknown-linux-gnu" {
459+
// `-C linker-features=[-+]lld` is only stable on x64 linux.
460+
let check_lld = |features: LinkerFeatures, polarity: &str| {
461+
let has_lld = features.is_lld_enabled();
462+
if has_lld && target_tuple.tuple() != "x86_64-unknown-linux-gnu" {
463+
return Err(format!(
464+
"`-C linker-features={polarity}lld` is unstable on the `{target_tuple}` \
465+
target. The `-Z unstable-options` flag must also be passed to use it on this target",
466+
));
467+
}
468+
Ok(())
469+
};
470+
check_lld(self.enabled, "+")?;
471+
check_lld(self.disabled, "-")?;
472+
473+
// Since only lld is stable, any non-lld feature used is unstable, and that's an error.
474+
let unstable_enabled = self.enabled - LinkerFeatures::LLD;
475+
let unstable_disabled = self.disabled - LinkerFeatures::LLD;
476+
if !unstable_enabled.union(unstable_disabled).is_empty() {
477+
let unstable_features: Vec<_> = unstable_enabled
478+
.iter()
479+
.map(|f| format!("+{}", f.as_str().unwrap()))
480+
.chain(unstable_disabled.iter().map(|f| format!("-{}", f.as_str().unwrap())))
481+
.collect();
463482
return Err(format!(
464-
"`-C linker-features` with lld are unstable for the `{target_tuple}` target, \
465-
the `-Z unstable-options` flag must also be passed to use it on this target",
483+
"the requested `-C linker-features={}` are unstable, and also require the \
484+
`-Z unstable-options` flag to be usable",
485+
unstable_features.join(","),
466486
));
467487
}
468488

469-
for feature in LinkerFeatures::all() {
470-
// Check that no other features were enabled without -Zunstable-options
471-
// Note that this should currently be unreachable, because the `-Clinker-features` parser
472-
// currently only accepts lld.
473-
if feature != LinkerFeatures::LLD && mentioned_features.contains(feature) {
474-
return Err("`-C linker-features` is stable only for the lld feature, \
475-
the`-Z unstable-options` flag must also be passed to use it with other features"
476-
.to_string());
477-
}
478-
}
479-
480489
Ok(())
481490
}
482491
}

compiler/rustc_target/src/spec/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,17 @@ impl LinkerFeatures {
761761
})
762762
}
763763

764+
/// Return the linker feature name, as would be passed on the CLI.
765+
///
766+
/// Returns `None` if the bitflags aren't a singular component (but a mix of multiple flags).
767+
pub fn as_str(self) -> Option<&'static str> {
768+
Some(match self {
769+
LinkerFeatures::CC => "cc",
770+
LinkerFeatures::LLD => "lld",
771+
_ => return None,
772+
})
773+
}
774+
764775
/// Returns whether the `lld` linker feature is enabled.
765776
pub fn is_lld_enabled(self) -> bool {
766777
self.contains(LinkerFeatures::LLD)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: `-C linker-features=-lld` is unstable on the `x86_64-unknown-linux-musl` target. The `-Z unstable-options` flag must also be passed to use it on this target
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: `-C linker-features=+lld` is unstable on the `x86_64-unknown-linux-musl` target. The `-Z unstable-options` flag must also be passed to use it on this target
2+
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// Check that `-C linker-features=[+-]lld` is only stable on x64 linux, and needs `-Z
22
// unstable-options` elsewhere.
3-
//
4-
//@ check-fail
5-
//@ compile-flags: --target=x86_64-unknown-linux-musl -C linker-features=-lld --crate-type=rlib
6-
//@ needs-llvm-components: x86
3+
4+
// ignore-tidy-linelength
5+
6+
//@ revisions: positive negative
7+
//@ [negative] compile-flags: --target=x86_64-unknown-linux-musl -C linker-features=-lld --crate-type=rlib
8+
//@ [negative] needs-llvm-components: x86
9+
//@ [positive] compile-flags: --target=x86_64-unknown-linux-musl -C linker-features=+lld --crate-type=rlib
10+
//@ [positive] needs-llvm-components: x86
711

812
#![feature(no_core)]
913
#![no_core]
1014

11-
//~? ERROR `-C linker-features` with lld are unstable for the `x86_64-unknown-linux-musl` target, the `-Z unstable-options` flag must also be passed to use it on this target
15+
//[negative]~? ERROR `-C linker-features=-lld` is unstable on the `x86_64-unknown-linux-musl` target. The `-Z unstable-options` flag must also be passed to use it on this target
16+
//[positive]~? ERROR `-C linker-features=+lld` is unstable on the `x86_64-unknown-linux-musl` target. The `-Z unstable-options` flag must also be passed to use it on this target

tests/ui/linking/linker-features-lld-disallowed-target.stderr

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)