Skip to content

Commit 3a16ffa

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

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
@@ -373,17 +373,40 @@ impl LinkSelfContained {
373373
}
374374

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

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

389412
/// Returns whether the self-contained linker component was enabled on the CLI, using the
@@ -2657,17 +2680,13 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26572680
)
26582681
}
26592682

2660-
// For testing purposes, until we have more feedback about these options: ensure `-Z
2661-
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
2662-
// linker-flavor` options.
2683+
let target_triple = parse_target_triple(early_dcx, matches);
2684+
2685+
// Ensure `-Z unstable-options` is required when using the unstable `-C link-self-contained` and
2686+
// `-C linker-flavor` options.
26632687
if !unstable_options_enabled {
2664-
let uses_unstable_self_contained_option =
2665-
cg.link_self_contained.are_unstable_variants_set();
2666-
if uses_unstable_self_contained_option {
2667-
early_dcx.early_fatal(
2668-
"only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off`/`-linker`/`+linker` are stable, \
2669-
the `-Z unstable-options` flag must also be passed to use the unstable values",
2670-
);
2688+
if let Err(error) = cg.link_self_contained.check_unstable_variants(&target_triple) {
2689+
early_dcx.early_fatal(error);
26712690
}
26722691

26732692
if let Some(flavor) = cg.linker_flavor {
@@ -2699,7 +2718,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26992718
let cg = cg;
27002719

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