Skip to content

Commit 50db59e

Browse files
committed
Use stricter -Z flag parsing.
1 parent 04efd9c commit 50db59e

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

src/cargo/core/features.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use std::env;
5050
use std::fmt;
5151
use std::str::FromStr;
5252

53-
use failure::Error;
53+
use failure::{bail, Error};
5454
use serde::{Deserialize, Serialize};
5555

5656
use crate::util::errors::CargoResult;
@@ -82,7 +82,7 @@ impl FromStr for Edition {
8282
match s {
8383
"2015" => Ok(Edition::Edition2015),
8484
"2018" => Ok(Edition::Edition2018),
85-
s => failure::bail!(
85+
s => bail!(
8686
"supported edition values are `2015` or `2018`, but `{}` \
8787
is unknown",
8888
s
@@ -227,11 +227,11 @@ impl Features {
227227
fn add(&mut self, feature: &str, warnings: &mut Vec<String>) -> CargoResult<()> {
228228
let (slot, status) = match self.status(feature) {
229229
Some(p) => p,
230-
None => failure::bail!("unknown cargo feature `{}`", feature),
230+
None => bail!("unknown cargo feature `{}`", feature),
231231
};
232232

233233
if *slot {
234-
failure::bail!("the cargo feature `{}` has already been activated", feature);
234+
bail!("the cargo feature `{}` has already been activated", feature);
235235
}
236236

237237
match status {
@@ -244,7 +244,7 @@ impl Features {
244244
);
245245
warnings.push(warning);
246246
}
247-
Status::Unstable if !nightly_features_allowed() => failure::bail!(
247+
Status::Unstable if !nightly_features_allowed() => bail!(
248248
"the cargo feature `{}` requires a nightly version of \
249249
Cargo, but this is the `{}` channel\n\
250250
{}",
@@ -288,7 +288,7 @@ impl Features {
288288
);
289289
msg.push_str(&s);
290290
}
291-
failure::bail!("{}", msg);
291+
bail!("{}", msg);
292292
}
293293
}
294294

@@ -346,7 +346,7 @@ pub struct CliUnstable {
346346
impl CliUnstable {
347347
pub fn parse(&mut self, flags: &[String]) -> CargoResult<()> {
348348
if !flags.is_empty() && !nightly_features_allowed() {
349-
failure::bail!(
349+
bail!(
350350
"the `-Z` flag is only accepted on the nightly channel of Cargo, \
351351
but this is the `{}` channel\n\
352352
{}",
@@ -365,11 +365,11 @@ impl CliUnstable {
365365
let k = parts.next().unwrap();
366366
let v = parts.next();
367367

368-
fn parse_bool(value: Option<&str>) -> CargoResult<bool> {
368+
fn parse_bool(key: &str, value: Option<&str>) -> CargoResult<bool> {
369369
match value {
370370
None | Some("yes") => Ok(true),
371371
Some("no") => Ok(false),
372-
Some(s) => failure::bail!("expected `no` or `yes`, found: {}", s),
372+
Some(s) => bail!("flag -Z{} expected `no` or `yes`, found: `{}`", key, s),
373373
}
374374
}
375375

@@ -380,28 +380,36 @@ impl CliUnstable {
380380
}
381381
}
382382

383+
// Asserts that there is no argument to the flag.
384+
fn parse_empty(key: &str, value: Option<&str>) -> CargoResult<bool> {
385+
if let Some(v) = value {
386+
bail!("flag -Z{} does not take a value, found: `{}`", key, v);
387+
}
388+
Ok(true)
389+
};
390+
383391
match k {
384-
"print-im-a-teapot" => self.print_im_a_teapot = parse_bool(v)?,
385-
"unstable-options" => self.unstable_options = true,
386-
"no-index-update" => self.no_index_update = true,
387-
"avoid-dev-deps" => self.avoid_dev_deps = true,
388-
"minimal-versions" => self.minimal_versions = true,
389-
"package-features" => self.package_features = true,
390-
"advanced-env" => self.advanced_env = true,
391-
"config-profile" => self.config_profile = true,
392-
"dual-proc-macros" => self.dual_proc_macros = true,
392+
"print-im-a-teapot" => self.print_im_a_teapot = parse_bool(k, v)?,
393+
"unstable-options" => self.unstable_options = parse_empty(k, v)?,
394+
"no-index-update" => self.no_index_update = parse_empty(k, v)?,
395+
"avoid-dev-deps" => self.avoid_dev_deps = parse_empty(k, v)?,
396+
"minimal-versions" => self.minimal_versions = parse_empty(k, v)?,
397+
"package-features" => self.package_features = parse_empty(k, v)?,
398+
"advanced-env" => self.advanced_env = parse_empty(k, v)?,
399+
"config-profile" => self.config_profile = parse_empty(k, v)?,
400+
"dual-proc-macros" => self.dual_proc_macros = parse_empty(k, v)?,
393401
// can also be set in .cargo/config or with and ENV
394-
"mtime-on-use" => self.mtime_on_use = true,
395-
"install-upgrade" => self.install_upgrade = true,
396-
"named-profiles" => self.named_profiles = true,
397-
"binary-dep-depinfo" => self.binary_dep_depinfo = true,
402+
"mtime-on-use" => self.mtime_on_use = parse_empty(k, v)?,
403+
"install-upgrade" => self.install_upgrade = parse_empty(k, v)?,
404+
"named-profiles" => self.named_profiles = parse_empty(k, v)?,
405+
"binary-dep-depinfo" => self.binary_dep_depinfo = parse_empty(k, v)?,
398406
"build-std" => {
399407
self.build_std = Some(crate::core::compiler::standard_lib::parse_unstable_flag(v))
400408
}
401409
"timings" => self.timings = Some(parse_timings(v)),
402-
"doctest-xcompile" => self.doctest_xcompile = true,
403-
"panic-abort-tests" => self.panic_abort_tests = true,
404-
_ => failure::bail!("unknown `-Z` flag specified: {}", k),
410+
"doctest-xcompile" => self.doctest_xcompile = parse_empty(k, v)?,
411+
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
412+
_ => bail!("unknown `-Z` flag specified: {}", k),
405413
}
406414

407415
Ok(())
@@ -418,14 +426,14 @@ impl CliUnstable {
418426
issue, flag
419427
);
420428
if nightly_features_allowed() {
421-
failure::bail!(
429+
bail!(
422430
"the `{}` flag is unstable, pass `-Z unstable-options` to enable it\n\
423431
{}",
424432
flag,
425433
see
426434
);
427435
} else {
428-
failure::bail!(
436+
bail!(
429437
"the `{}` flag is unstable, and only available on the nightly channel \
430438
of Cargo, but this is the `{}` channel\n\
431439
{}\n\

0 commit comments

Comments
 (0)