Skip to content

Commit 8bd70c5

Browse files
Kobzollqd
authored andcommitted
Check that -Clinker-features=[-+]lld can be used only on the x64 target without -Zunstable-options
1 parent 200b689 commit 8bd70c5

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

compiler/rustc_session/src/config.rs

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

436465
/// Used with `-Z assert-incr-state`.
@@ -2486,9 +2515,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
24862515
}
24872516
}
24882517

2489-
if !nightly_options::is_unstable_enabled(matches)
2490-
&& cg.force_frame_pointers == FramePointer::NonLeaf
2491-
{
2518+
let unstable_options_enabled = nightly_options::is_unstable_enabled(matches);
2519+
if !unstable_options_enabled && cg.force_frame_pointers == FramePointer::NonLeaf {
24922520
early_dcx.early_fatal(
24932521
"`-Cforce-frame-pointers=non-leaf` or `always` also requires `-Zunstable-options` \
24942522
and a nightly compiler",
@@ -2498,7 +2526,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
24982526
// For testing purposes, until we have more feedback about these options: ensure `-Z
24992527
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
25002528
// linker-flavor` options.
2501-
if !nightly_options::is_unstable_enabled(matches) {
2529+
if !unstable_options_enabled {
25022530
let uses_unstable_self_contained_option =
25032531
cg.link_self_contained.are_unstable_variants_set();
25042532
if uses_unstable_self_contained_option {
@@ -2546,6 +2574,12 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
25462574
let debuginfo = select_debuginfo(matches, &cg);
25472575
let debuginfo_compression = unstable_opts.debuginfo_compression;
25482576

2577+
if !unstable_options_enabled {
2578+
if let Err(error) = cg.linker_features.check_unstable_variants(&target_triple) {
2579+
early_dcx.early_fatal(error);
2580+
}
2581+
}
2582+
25492583
let crate_name = matches.opt_str("crate-name");
25502584
let unstable_features = UnstableFeatures::from_environment(crate_name.as_deref());
25512585
// Parse any `-l` flags, which link to native libraries.

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

+1
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
}
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]
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+
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]
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)