Skip to content

Commit 8bcfbb6

Browse files
lqdKobzol
authored andcommitted
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 e731c4e commit 8bcfbb6

6 files changed

+55
-28
lines changed

compiler/rustc_session/src/config.rs

+30-21
Original file line numberDiff line numberDiff line change
@@ -445,32 +445,41 @@ impl LinkerFeaturesCli {
445445
}
446446
}
447447

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

463-
for feature in LinkerFeatures::all() {
464-
// Check that no other features were enabled without -Zunstable-options
465-
// Note that this should currently be unreachable, because the `-Clinker-features` parser
466-
// currently only accepts lld.
467-
if feature != LinkerFeatures::LLD && mentioned_features.contains(feature) {
468-
return Err("`-C linker-features` is stable only for the lld feature, \
469-
the`-Z unstable-options` flag must also be passed to use it with other features"
470-
.to_string());
471-
}
472-
}
473-
474483
Ok(())
475484
}
476485
}

compiler/rustc_target/src/spec/mod.rs

+11
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)
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+
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+
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

-2
This file was deleted.

0 commit comments

Comments
 (0)