Skip to content

Commit b92dfd0

Browse files
committed
Check that -Clink-self-contained=[-+]linker can only be used on x64 linux without -Zunstable-options
1 parent 063b826 commit b92dfd0

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
@@ -2638,17 +2661,13 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26382661
)
26392662
}
26402663

2641-
// For testing purposes, until we have more feedback about these options: ensure `-Z
2642-
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
2643-
// linker-flavor` options.
2664+
let target_triple = parse_target_triple(early_dcx, matches);
2665+
2666+
// Ensure `-Z unstable-options` is required when using the unstable `-C link-self-contained` and
2667+
// `-C linker-flavor` options.
26442668
if !unstable_options_enabled {
2645-
let uses_unstable_self_contained_option =
2646-
cg.link_self_contained.are_unstable_variants_set();
2647-
if uses_unstable_self_contained_option {
2648-
early_dcx.early_fatal(
2649-
"only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off`/`-linker`/`+linker` are stable, \
2650-
the `-Z unstable-options` flag must also be passed to use the unstable values",
2651-
);
2669+
if let Err(error) = cg.link_self_contained.check_unstable_variants(&target_triple) {
2670+
early_dcx.early_fatal(error);
26522671
}
26532672

26542673
if let Some(flavor) = cg.linker_flavor {
@@ -2680,7 +2699,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26802699
let cg = cg;
26812700

26822701
let sysroot_opt = matches.opt_str("sysroot").map(|m| PathBuf::from(&m));
2683-
let target_triple = parse_target_triple(early_dcx, matches);
26842702
let opt_level = parse_opt_level(early_dcx, matches, &cg);
26852703
// The `-g` and `-C debuginfo` flags specify the same setting, so we want to be able
26862704
// 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)