Skip to content

Commit 068d157

Browse files
committed
check that -Clinker-features=[-+]lld can only be used on x64 linux without -Zunstable-options
1 parent 7636218 commit 068d157

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

450479
/// Used with `-Z assert-incr-state`.
@@ -2606,9 +2635,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26062635
}
26072636
}
26082637

2609-
if !nightly_options::is_unstable_enabled(matches)
2610-
&& cg.force_frame_pointers == FramePointer::NonLeaf
2611-
{
2638+
let unstable_options_enabled = nightly_options::is_unstable_enabled(matches);
2639+
if !unstable_options_enabled && cg.force_frame_pointers == FramePointer::NonLeaf {
26122640
early_dcx.early_fatal(
26132641
"`-Cforce-frame-pointers=non-leaf` or `always` also requires `-Zunstable-options` \
26142642
and a nightly compiler",
@@ -2618,7 +2646,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26182646
// For testing purposes, until we have more feedback about these options: ensure `-Z
26192647
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
26202648
// linker-flavor` options.
2621-
if !nightly_options::is_unstable_enabled(matches) {
2649+
if !unstable_options_enabled {
26222650
let uses_unstable_self_contained_option =
26232651
cg.link_self_contained.are_unstable_variants_set();
26242652
if uses_unstable_self_contained_option {
@@ -2666,6 +2694,12 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26662694
let debuginfo = select_debuginfo(matches, &cg);
26672695
let debuginfo_compression = unstable_opts.debuginfo_compression;
26682696

2697+
if !unstable_options_enabled {
2698+
if let Err(error) = cg.linker_features.check_unstable_variants(&target_triple) {
2699+
early_dcx.early_fatal(error);
2700+
}
2701+
}
2702+
26692703
let crate_name = matches.opt_str("crate-name");
26702704
let unstable_features = UnstableFeatures::from_environment(crate_name.as_deref());
26712705
// 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)