Skip to content

Commit e7f634c

Browse files
committed
Check that -Clinker-features=[-+]lld can only be used on x64 linux without -Zunstable-options
1 parent a6cea08 commit e7f634c

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,35 @@ impl LinkerFeaturesCli {
439439
_ => None,
440440
}
441441
}
442+
443+
/// Checks usage of unstable variants for linker features for the given `target_tuple`.
444+
/// Returns `Ok` if no unstable variants are used.
445+
pub(crate) fn check_unstable_variants(&self, target_tuple: &TargetTuple) -> Result<(), String> {
446+
let mentioned_features = self.enabled.union(self.disabled);
447+
let has_lld = mentioned_features.is_lld_enabled();
448+
449+
// Check that -Clinker-features=[-+]lld is not used anywhere else than on x64
450+
// without -Zunstable-options.
451+
if has_lld && target_tuple.tuple() != "x86_64-unknown-linux-gnu" {
452+
return Err(format!(
453+
"`-C linker-features` with lld are unstable for the `{target_tuple}` target, \
454+
the `-Z unstable-options` flag must also be passed to use it on this target",
455+
));
456+
}
457+
458+
for feature in LinkerFeatures::all() {
459+
// Check that no other features were enabled without -Zunstable-options
460+
// Note that this should currently be unreachable, because the `-Clinker-features` parser
461+
// currently only accepts lld.
462+
if feature != LinkerFeatures::LLD && mentioned_features.contains(feature) {
463+
return Err("`-C linker-features` is stable only for the lld feature, \
464+
the`-Z unstable-options` flag must also be passed to use it with other features"
465+
.to_string());
466+
}
467+
}
468+
469+
Ok(())
470+
}
442471
}
443472

444473
/// Used with `-Z assert-incr-state`.
@@ -2587,9 +2616,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
25872616
}
25882617
}
25892618

2590-
if !nightly_options::is_unstable_enabled(matches)
2591-
&& cg.force_frame_pointers == FramePointer::NonLeaf
2592-
{
2619+
let unstable_options_enabled = nightly_options::is_unstable_enabled(matches);
2620+
if !unstable_options_enabled && cg.force_frame_pointers == FramePointer::NonLeaf {
25932621
early_dcx.early_fatal(
25942622
"`-Cforce-frame-pointers=non-leaf` or `always` also requires `-Zunstable-options` \
25952623
and a nightly compiler",
@@ -2599,7 +2627,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
25992627
// For testing purposes, until we have more feedback about these options: ensure `-Z
26002628
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
26012629
// linker-flavor` options.
2602-
if !nightly_options::is_unstable_enabled(matches) {
2630+
if !unstable_options_enabled {
26032631
let uses_unstable_self_contained_option =
26042632
cg.link_self_contained.are_unstable_variants_set();
26052633
if uses_unstable_self_contained_option {
@@ -2647,6 +2675,12 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26472675
let debuginfo = select_debuginfo(matches, &cg);
26482676
let debuginfo_compression = unstable_opts.debuginfo_compression;
26492677

2678+
if !unstable_options_enabled {
2679+
if let Err(error) = cg.linker_features.check_unstable_variants(&target_triple) {
2680+
early_dcx.early_fatal(error);
2681+
}
2682+
}
2683+
26502684
let crate_name = matches.opt_str("crate-name");
26512685
let unstable_features = UnstableFeatures::from_environment(crate_name.as_deref());
26522686
// Parse any `-l` flags, which link to native libraries.

tests/run-make/rust-lld-custom-target/rmake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn main() {
2424
.crate_type("cdylib")
2525
.target("custom-target.json")
2626
.arg("-Clinker-features=-lld")
27+
.arg("-Zunstable-options")
2728
.input("lib.rs"),
2829
);
2930
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Check that -CLinker-features=[+-]lld can only be used on x64.
2+
//
3+
//@ check-fail
4+
//@ compile-flags: --target=x86_64-unknown-linux-musl -C linker-features=-lld --crate-type=rlib
5+
//@ needs-llvm-components: x86
6+
7+
#![feature(no_core)]
8+
#![no_core]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
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
2+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Check that -CLinker-features with anything else than lld requires -Zunstable-options.
2+
//
3+
//@ check-fail
4+
//@ compile-flags: --target=x86_64-unknown-linux-gnu -C linker-features=+cc --crate-type=rlib
5+
//@ needs-llvm-components: x86
6+
7+
#![feature(no_core)]
8+
#![no_core]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: incorrect value `+cc` for codegen option `linker-features` - a list of enabled (`+` prefix) and disabled (`-` prefix) features: `lld` was expected
2+

0 commit comments

Comments
 (0)