Skip to content

Commit 16d7167

Browse files
committed
check that -Clink-self-contained=[-+]linker can only be used on x64 linux without -Zunstable-options
1 parent 0edfc80 commit 16d7167

4 files changed

+57
-19
lines changed

compiler/rustc_session/src/config.rs

+37-19
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,40 @@ impl LinkSelfContained {
367367
}
368368

369369
/// To help checking CLI usage while some of the values are unstable: returns whether one of the
370-
/// components was set individually. This would also require the `-Zunstable-options` flag, to
371-
/// be allowed.
372-
fn are_unstable_variants_set(&self) -> bool {
370+
/// unstable components was set individually, for the given `TargetTuple`. This would also
371+
/// require the `-Zunstable-options` flag, to be allowed.
372+
fn check_unstable_variants(&self, target_tuple: &TargetTuple) -> Result<(), String> {
373373
if self.explicitly_set.is_some() {
374-
return false;
374+
return Ok(());
375375
}
376376

377-
// Only the linker component is stable, anything else is thus unstable.
378-
let mentioned_components = self.enabled_components.union(self.disabled_components);
379-
let unstable_components = mentioned_components - LinkSelfContainedComponents::LINKER;
380-
!unstable_components.is_empty()
377+
// `-C link-self-contained=[-+]linker` is only stable on x64 linux.
378+
let check_linker = |components: LinkSelfContainedComponents, polarity: &str| {
379+
let has_linker = components.is_linker_enabled();
380+
if has_linker && target_tuple.tuple() != "x86_64-unknown-linux-gnu" {
381+
return Err(format!(
382+
"`-C link-self-contained={polarity}linker` is unstable on the `{target_tuple}` \
383+
target. The `-Z unstable-options` flag must also be passed to use it on this target",
384+
));
385+
}
386+
Ok(())
387+
};
388+
check_linker(self.enabled_components, "+")?;
389+
check_linker(self.disabled_components, "-")?;
390+
391+
// Since only the linker component is stable, any other component used is unstable, and
392+
// that's an error.
393+
let unstable_enabled = self.enabled_components - LinkSelfContainedComponents::LINKER;
394+
let unstable_disabled = self.disabled_components - LinkSelfContainedComponents::LINKER;
395+
if !unstable_enabled.union(unstable_disabled).is_empty() {
396+
return Err(String::from(
397+
"only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off`/`-linker`\
398+
/`+linker` are stable, the `-Z unstable-options` flag must also be passed to use \
399+
the unstable values",
400+
));
401+
}
402+
403+
Ok(())
381404
}
382405

383406
/// Returns whether the self-contained linker component was enabled on the CLI, using the
@@ -2646,17 +2669,13 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26462669
)
26472670
}
26482671

2649-
// For testing purposes, until we have more feedback about these options: ensure `-Z
2650-
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
2651-
// linker-flavor` options.
2672+
let target_triple = parse_target_triple(early_dcx, matches);
2673+
2674+
// Ensure `-Z unstable-options` is required when using the unstable `-C link-self-contained` and
2675+
// `-C linker-flavor` options.
26522676
if !unstable_options_enabled {
2653-
let uses_unstable_self_contained_option =
2654-
cg.link_self_contained.are_unstable_variants_set();
2655-
if uses_unstable_self_contained_option {
2656-
early_dcx.early_fatal(
2657-
"only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off`/`-linker`/`+linker` are stable, \
2658-
the `-Z unstable-options` flag must also be passed to use the unstable values",
2659-
);
2677+
if let Err(error) = cg.link_self_contained.check_unstable_variants(&target_triple) {
2678+
early_dcx.early_fatal(error);
26602679
}
26612680

26622681
if let Some(flavor) = cg.linker_flavor {
@@ -2688,7 +2707,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26882707
let cg = cg;
26892708

26902709
let sysroot_opt = matches.opt_str("sysroot").map(|m| PathBuf::from(&m));
2691-
let target_triple = parse_target_triple(early_dcx, matches);
26922710
let opt_level = parse_opt_level(early_dcx, matches, &cg);
26932711
// The `-g` and `-C debuginfo` flags specify the same setting, so we want to be able
26942712
// to use them interchangeably. See the note above (regarding `-O` and `-C opt-level`)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: `-C link-self-contained=-linker` is unstable on 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,2 @@
1+
error: `-C link-self-contained=+linker` is unstable on 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,16 @@
1+
// Check that `-C link-self-contained=[+-]linker` is only stable on x64 linux, and needs `-Z
2+
// unstable-options` elsewhere.
3+
4+
// ignore-tidy-linelength
5+
6+
//@ revisions: positive negative
7+
//@ [negative] compile-flags: --target=x86_64-unknown-linux-musl -C link-self-contained=-linker --crate-type=rlib
8+
//@ [negative] needs-llvm-components: x86
9+
//@ [positive] compile-flags: --target=x86_64-unknown-linux-musl -C link-self-contained=+linker --crate-type=rlib
10+
//@ [positive] needs-llvm-components: x86
11+
12+
#![feature(no_core)]
13+
#![no_core]
14+
15+
//[negative]~? ERROR `-C link-self-contained=-linker` is unstable on the `x86_64-unknown-linux-musl` target
16+
//[positive]~? ERROR `-C link-self-contained=+linker` is unstable on the `x86_64-unknown-linux-musl` target

0 commit comments

Comments
 (0)