Skip to content

Commit 9121454

Browse files
committed
consistency check for self-contained linking components CLI options
emit an error if components are both enabled and disabled on the CLI
1 parent 9ea118f commit 9121454

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,17 @@ impl LinkSelfContained {
306306
pub fn is_linker_disabled(&self) -> bool {
307307
self.disabled_components.contains(LinkSelfContainedComponents::LINKER)
308308
}
309+
310+
/// Returns CLI inconsistencies to emit errors: individual components were both enabled and
311+
/// disabled.
312+
fn check_consistency(&self) -> Option<LinkSelfContainedComponents> {
313+
if self.explicitly_set.is_some() {
314+
None
315+
} else {
316+
let common = self.enabled_components.intersection(self.disabled_components);
317+
if common.is_empty() { None } else { Some(common) }
318+
}
319+
}
309320
}
310321

311322
/// Used with `-Z assert-incr-state`.
@@ -2753,6 +2764,19 @@ pub fn build_session_options(
27532764
}
27542765
}
27552766

2767+
// Check `-C link-self-contained` for consistency: individual components cannot be both enabled
2768+
// and disabled at the same time.
2769+
if let Some(erroneous_components) = cg.link_self_contained.check_consistency() {
2770+
let names: String = erroneous_components
2771+
.into_iter()
2772+
.map(|c| c.as_str().unwrap())
2773+
.intersperse(", ")
2774+
.collect();
2775+
handler.early_error(format!(
2776+
"some `-C link-self-contained` components were both enabled and disabled: {names}"
2777+
));
2778+
}
2779+
27562780
let prints = collect_print_requests(handler, &mut cg, &mut unstable_opts, matches);
27572781

27582782
let cg = cg;

compiler/rustc_session/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![feature(option_get_or_insert_default)]
77
#![feature(rustc_attrs)]
88
#![feature(map_many_mut)]
9+
#![feature(iter_intersperse)]
910
#![recursion_limit = "256"]
1011
#![allow(rustc::potential_query_instability)]
1112
#![deny(rustc::untranslatable_diagnostic)]

compiler/rustc_target/src/spec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ impl LinkSelfContainedComponents {
668668
/// Return the component's name.
669669
///
670670
/// Returns `None` if the bitflags aren't a singular component (but a mix of multiple flags).
671-
fn as_str(self) -> Option<&'static str> {
671+
pub fn as_str(self) -> Option<&'static str> {
672672
Some(match self {
673673
LinkSelfContainedComponents::CRT_OBJECTS => "crto",
674674
LinkSelfContainedComponents::LIBC => "libc",

0 commit comments

Comments
 (0)