Skip to content

Commit 5968d86

Browse files
committed
check that -Clinker-features=[-+]lld can only be used on x64 linux without -Zunstable-options
1 parent 475c70d commit 5968d86

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

445474
/// Used with `-Z assert-incr-state`.
@@ -2601,9 +2630,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26012630
}
26022631
}
26032632

2604-
if !nightly_options::is_unstable_enabled(matches)
2605-
&& cg.force_frame_pointers == FramePointer::NonLeaf
2606-
{
2633+
let unstable_options_enabled = nightly_options::is_unstable_enabled(matches);
2634+
if !unstable_options_enabled && cg.force_frame_pointers == FramePointer::NonLeaf {
26072635
early_dcx.early_fatal(
26082636
"`-Cforce-frame-pointers=non-leaf` or `always` also requires `-Zunstable-options` \
26092637
and a nightly compiler",
@@ -2613,7 +2641,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26132641
// For testing purposes, until we have more feedback about these options: ensure `-Z
26142642
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
26152643
// linker-flavor` options.
2616-
if !nightly_options::is_unstable_enabled(matches) {
2644+
if !unstable_options_enabled {
26172645
let uses_unstable_self_contained_option =
26182646
cg.link_self_contained.are_unstable_variants_set();
26192647
if uses_unstable_self_contained_option {
@@ -2661,6 +2689,12 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26612689
let debuginfo = select_debuginfo(matches, &cg);
26622690
let debuginfo_compression = unstable_opts.debuginfo_compression;
26632691

2692+
if !unstable_options_enabled {
2693+
if let Err(error) = cg.linker_features.check_unstable_variants(&target_triple) {
2694+
early_dcx.early_fatal(error);
2695+
}
2696+
}
2697+
26642698
let crate_name = matches.opt_str("crate-name");
26652699
let unstable_features = UnstableFeatures::from_environment(crate_name.as_deref());
26662700
// 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)