Skip to content

Commit 2f8480f

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

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
@@ -370,17 +370,40 @@ 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 {
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> {
376376
if self.explicitly_set.is_some() {
377-
return false;
377+
return Ok(());
378378
}
379379

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

386409
/// Returns whether the self-contained linker component was enabled on the CLI, using the
@@ -2689,17 +2712,13 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
26892712
)
26902713
}
26912714

2692-
// For testing purposes, until we have more feedback about these options: ensure `-Z
2693-
// unstable-options` is required when using the unstable `-C link-self-contained` and `-C
2694-
// linker-flavor` options.
2715+
let target_triple = parse_target_triple(early_dcx, matches);
2716+
2717+
// Ensure `-Z unstable-options` is required when using the unstable `-C link-self-contained` and
2718+
// `-C linker-flavor` options.
26952719
if !unstable_options_enabled {
2696-
let uses_unstable_self_contained_option =
2697-
cg.link_self_contained.are_unstable_variants_set();
2698-
if uses_unstable_self_contained_option {
2699-
early_dcx.early_fatal(
2700-
"only `-C link-self-contained` values `y`/`yes`/`on`/`n`/`no`/`off`/`-linker`/`+linker` are stable, \
2701-
the `-Z unstable-options` flag must also be passed to use the unstable values",
2702-
);
2720+
if let Err(error) = cg.link_self_contained.check_unstable_variants(&target_triple) {
2721+
early_dcx.early_fatal(error);
27032722
}
27042723

27052724
if let Some(flavor) = cg.linker_flavor {
@@ -2739,7 +2758,6 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27392758

27402759
let cg = cg;
27412760

2742-
let target_triple = parse_target_triple(early_dcx, matches);
27432761
let opt_level = parse_opt_level(early_dcx, matches, &cg);
27442762
// The `-g` and `-C debuginfo` flags specify the same setting, so we want to be able
27452763
// 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)