Skip to content

Commit 3f194a8

Browse files
committed
stabilize -Clink-self-contained=-linker on x64 linux
This stabilizes a subset of the `-Clink-self-contained` components on x64 linux: the rust-lld opt-out. The opt-in is not stabilized, as interactions with other stable flags require more internal work, but are not needed for stabilizing using rust-lld by default. Similarly, since we only switch to rust-lld on x64 linux, the opt-out is only stabilized there. Other targets still require `-Zunstable-options` to use it.
1 parent 2e6d82c commit 3f194a8

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,34 @@ impl LinkSelfContained {
370370
}
371371

372372
/// To help checking CLI usage while some of the values are unstable: returns whether one of the
373-
/// components was set individually. This would also require the `-Zunstable-options` flag, to
374-
/// be allowed.
375-
fn are_unstable_variants_set(&self) -> bool {
376-
let any_component_set =
377-
!self.enabled_components.is_empty() || !self.disabled_components.is_empty();
378-
self.explicitly_set.is_none() && any_component_set
373+
/// unstable components was set individually, for the given `TargetTuple`. This would also
374+
/// require the `-Zunstable-options` flag, to be allowed.
375+
fn check_unstable_variants(&self, target_tuple: &TargetTuple) -> Result<(), String> {
376+
if self.explicitly_set.is_some() {
377+
return Ok(());
378+
}
379+
380+
// `-C link-self-contained=-linker` is only stable on x64 linux.
381+
let has_minus_linker = self.disabled_components.is_linker_enabled();
382+
if has_minus_linker && target_tuple.tuple() != "x86_64-unknown-linux-gnu" {
383+
return Err(format!(
384+
"`-C link-self-contained=-linker` is unstable on the `{target_tuple}` \
385+
target. The `-Z unstable-options` flag must also be passed to use it on this target",
386+
));
387+
}
388+
389+
// Any `+linker` or other component used is unstable, and that's an error.
390+
let unstable_enabled = self.enabled_components;
391+
let unstable_disabled = self.disabled_components - LinkSelfContainedComponents::LINKER;
392+
if !unstable_enabled.union(unstable_disabled).is_empty() {
393+
return Err(String::from(
394+
"only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off`/`-linker` \
395+
are stable, the `-Z unstable-options` flag must also be passed to use \
396+
the unstable values",
397+
));
398+
}
399+
400+
Ok(())
379401
}
380402

381403
/// Returns whether the self-contained linker component was enabled on the CLI, using the
@@ -2679,17 +2701,13 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26792701
)
26802702
}
26812703

2682-
// For testing purposes, until we have more feedback about these options: ensure `-Z
2683-
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
2684-
// linker-flavor` options.
2704+
let target_triple = parse_target_triple(early_dcx, matches);
2705+
2706+
// Ensure `-Z unstable-options` is required when using the unstable `-C link-self-contained` and
2707+
// `-C linker-flavor` options.
26852708
if !unstable_options_enabled {
2686-
let uses_unstable_self_contained_option =
2687-
cg.link_self_contained.are_unstable_variants_set();
2688-
if uses_unstable_self_contained_option {
2689-
early_dcx.early_fatal(
2690-
"only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off` are stable, \
2691-
the `-Z unstable-options` flag must also be passed to use the unstable values",
2692-
);
2709+
if let Err(error) = cg.link_self_contained.check_unstable_variants(&target_triple) {
2710+
early_dcx.early_fatal(error);
26932711
}
26942712

26952713
if let Some(flavor) = cg.linker_flavor {
@@ -2729,7 +2747,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27292747

27302748
let cg = cg;
27312749

2732-
let target_triple = parse_target_triple(early_dcx, matches);
27332750
let opt_level = parse_opt_level(early_dcx, matches, &cg);
27342751
// The `-g` and `-C debuginfo` flags specify the same setting, so we want to be able
27352752
// to use them interchangeably. See the note above (regarding `-O` and `-C opt-level`)

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Test linking using `cc` with `rust-lld`, using the unstable CLI described in MCP 510
2-
// see https://github.com/rust-lang/compiler-team/issues/510 for more info
1+
// Test linking using `cc` with `rust-lld`, using the `-Clinker-features` and
2+
// `-Clink-self-contained` CLI flags.
33

44
//@ needs-rust-lld
55
//@ ignore-s390x lld does not yet support s390x as target
@@ -14,12 +14,14 @@ fn main() {
1414
rustc()
1515
.arg("-Clinker-features=+lld")
1616
.arg("-Clink-self-contained=+linker")
17-
.arg("-Zunstable-options")
17+
.arg("-Zunstable-options") // the opt-ins are unstable
1818
.input("main.rs"),
1919
);
2020

2121
// It should not be used when we explicitly opt out of lld.
22-
assert_rustc_doesnt_use_lld(rustc().arg("-Clinker-features=-lld").input("main.rs"));
22+
assert_rustc_doesnt_use_lld(
23+
rustc().arg("-Clinker-features=-lld").arg("-Zunstable-options").input("main.rs"),
24+
);
2325

2426
// While we're here, also check that the last linker feature flag "wins" when passed multiple
2527
// times to rustc.

0 commit comments

Comments
 (0)