Skip to content

Commit eb34617

Browse files
committed
Simplify Strip option
- Accept a string option that's passed to rustc to decide how to handle it. - Remove early validation. Signed-off-by: David Calavera <[email protected]>
1 parent 933cbc4 commit eb34617

File tree

3 files changed

+20
-57
lines changed

3 files changed

+20
-57
lines changed

src/cargo/core/profiles.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,7 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
552552
}
553553
match toml.lto {
554554
Some(StringOrBool::Bool(b)) => profile.lto = Lto::Bool(b),
555-
Some(StringOrBool::String(ref n)) if matches!(n.as_str(), "off" | "n" | "no") => {
556-
profile.lto = Lto::Off
557-
}
555+
Some(StringOrBool::String(ref n)) if is_off(n.as_str()) => profile.lto = Lto::Off,
558556
Some(StringOrBool::String(ref n)) => profile.lto = Lto::Named(InternedString::new(n)),
559557
None => {}
560558
}
@@ -591,19 +589,10 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) {
591589
profile.incremental = incremental;
592590
}
593591
profile.strip = match toml.strip {
594-
Some(StringOrBool::Bool(enabled)) if enabled => Strip::Symbols,
595-
Some(StringOrBool::Bool(enabled)) if !enabled => Strip::None,
596-
Some(StringOrBool::String(ref n)) if matches!(n.as_str(), "off" | "n" | "no" | "none") => {
597-
Strip::None
598-
}
599-
Some(StringOrBool::String(ref n)) if matches!(n.as_str(), "debuginfo") => Strip::DebugInfo,
600-
Some(StringOrBool::String(ref n)) if matches!(n.as_str(), "symbols") => Strip::Symbols,
601-
None => Strip::None,
602-
Some(ref strip) => panic!(
603-
"unknown variant `{}`, expected one of `debuginfo`, `none`, `symbols` for key `strip`
604-
",
605-
strip
606-
),
592+
Some(StringOrBool::Bool(true)) => Strip::Named(InternedString::new("symbols")),
593+
None | Some(StringOrBool::Bool(false)) => Strip::None,
594+
Some(StringOrBool::String(ref n)) if is_off(n.as_str()) => Strip::None,
595+
Some(StringOrBool::String(ref n)) => Strip::Named(InternedString::new(n)),
607596
};
608597
}
609598

@@ -821,24 +810,22 @@ impl fmt::Display for PanicStrategy {
821810
)]
822811
#[serde(rename_all = "lowercase")]
823812
pub enum Strip {
824-
/// Only strip debugging symbols
825-
DebugInfo,
826813
/// Don't remove any symbols
827814
None,
828-
/// Strip all non-exported symbols from the final binary
829-
Symbols,
815+
/// Named Strip settings
816+
Named(InternedString),
830817
}
831818

832819
impl fmt::Display for Strip {
833820
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
834821
match *self {
835-
Strip::DebugInfo => "debuginfo",
836822
Strip::None => "none",
837-
Strip::Symbols => "symbols",
823+
Strip::Named(s) => s.as_str(),
838824
}
839825
.fmt(f)
840826
}
841827
}
828+
842829
/// Flags used in creating `Unit`s to indicate the purpose for the target, and
843830
/// to ensure the target's dependencies have the correct settings.
844831
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
@@ -1261,3 +1248,8 @@ fn validate_packages_unmatched(
12611248
}
12621249
Ok(())
12631250
}
1251+
1252+
/// Returns `true` if a string is a toggle that turns an option off.
1253+
fn is_off(s: &str) -> bool {
1254+
matches!(s, "off" | "n" | "no" | "none")
1255+
}

src/cargo/util/toml/mod.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -560,18 +560,6 @@ impl TomlProfile {
560560

561561
if self.strip.is_some() {
562562
features.require(Feature::strip())?;
563-
match self.strip {
564-
Some(StringOrBool::Bool(_)) => {}
565-
Some(StringOrBool::String(ref n)) => match n.as_str() {
566-
"off" | "n" | "none" | "no" | "debuginfo" | "symbols" => {}
567-
_ => bail!(
568-
"`strip` setting of `{}` is not a valid setting,\
569-
must be `symbols`, `debuginfo`, `none`, `true`, or `false`",
570-
n
571-
),
572-
},
573-
None => {}
574-
}
575563
}
576564
Ok(())
577565
}
@@ -780,15 +768,6 @@ impl<'de> de::Deserialize<'de> for StringOrBool {
780768
}
781769
}
782770

783-
impl fmt::Display for StringOrBool {
784-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
785-
match self {
786-
Self::String(s) => write!(f, "{}", &s),
787-
Self::Bool(b) => write!(f, "{}", b),
788-
}
789-
}
790-
}
791-
792771
#[derive(PartialEq, Clone, Debug, Serialize)]
793772
#[serde(untagged)]
794773
pub enum VecStringOrBool {

tests/testsuite/profiles.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ Caused by:
546546
}
547547

548548
#[cargo_test]
549-
fn strip_rejects_invalid_option() {
549+
fn strip_passes_unknown_option_to_rustc() {
550550
if !is_nightly() {
551551
// -Zstrip is unstable
552552
return;
@@ -563,7 +563,7 @@ fn strip_rejects_invalid_option() {
563563
version = "0.1.0"
564564
565565
[profile.release]
566-
strip = 'wrong'
566+
strip = 'unknown'
567567
"#,
568568
)
569569
.file("src/main.rs", "fn main() {}")
@@ -574,10 +574,9 @@ fn strip_rejects_invalid_option() {
574574
.with_status(101)
575575
.with_stderr(
576576
"\
577-
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
578-
579-
Caused by:
580-
unknown variant `wrong`, expected one of `debuginfo`, `none`, `symbols` for key `strip`
577+
[COMPILING] foo [..]
578+
[RUNNING] `rustc [..] -Z strip=unknown [..]`
579+
[FINISHED] [..]
581580
",
582581
)
583582
.run();
@@ -644,13 +643,6 @@ fn strip_accepts_false_to_disable_strip() {
644643

645644
p.cargo("build --release -v")
646645
.masquerade_as_nightly_cargo()
647-
.with_stderr(
648-
"\
649-
[COMPILING] foo [..]
650-
[RUNNING] `rustc [..] -Z strip=none
651-
[..]`
652-
[FINISHED] [..]
653-
",
654-
)
646+
.with_stderr_does_not_contain("-Z strip")
655647
.run();
656648
}

0 commit comments

Comments
 (0)