Skip to content

Commit be8e2d1

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

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -368,17 +368,40 @@ impl LinkSelfContained {
368368
}
369369

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

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

384407
/// Returns whether the self-contained linker component was enabled on the CLI, using the
@@ -2652,17 +2675,13 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26522675
)
26532676
}
26542677

2655-
// For testing purposes, until we have more feedback about these options: ensure `-Z
2656-
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
2657-
// linker-flavor` options.
2678+
let target_triple = parse_target_triple(early_dcx, matches);
2679+
2680+
// Ensure `-Z unstable-options` is required when using the unstable `-C link-self-contained` and
2681+
// `-C linker-flavor` options.
26582682
if !unstable_options_enabled {
2659-
let uses_unstable_self_contained_option =
2660-
cg.link_self_contained.are_unstable_variants_set();
2661-
if uses_unstable_self_contained_option {
2662-
early_dcx.early_fatal(
2663-
"only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off`/`-linker`/`+linker` are stable, \
2664-
the `-Z unstable-options` flag must also be passed to use the unstable values",
2665-
);
2683+
if let Err(error) = cg.link_self_contained.check_unstable_variants(&target_triple) {
2684+
early_dcx.early_fatal(error);
26662685
}
26672686

26682687
if let Some(flavor) = cg.linker_flavor {
@@ -2694,7 +2713,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26942713
let cg = cg;
26952714

26962715
let sysroot_opt = matches.opt_str("sysroot").map(|m| PathBuf::from(&m));
2697-
let target_triple = parse_target_triple(early_dcx, matches);
26982716
let opt_level = parse_opt_level(early_dcx, matches, &cg);
26992717
// The `-g` and `-C debuginfo` flags specify the same setting, so we want to be able
27002718
// to use them interchangeably. See the note above (regarding `-O` and `-C opt-level`)
Lines changed: 2 additions & 0 deletions
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+
Lines changed: 2 additions & 0 deletions
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+
Lines changed: 16 additions & 0 deletions
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)