Skip to content

Commit a18936b

Browse files
committed
Auto merge of #9369 - PicoJr:fix-9350, r=ehuss
Fix #9350 (cargo build -Z help is missing options) > Do not merge yet, some options are still undocumented. Fix #9350 (cargo build -Z help is missing options) Add a procedural macro to declare `CliUnstable` struct and provide help messages instead of hard-coding help in `src/bin/cargo/cli.rs` > Flags documentation: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html Feedback welcome
2 parents eb5476b + 05d37ae commit a18936b

File tree

6 files changed

+101
-63
lines changed

6 files changed

+101
-63
lines changed

src/bin/cargo/cli.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use cargo::core::features;
1+
use cargo::core::{features, CliUnstable};
22
use cargo::{self, drop_print, drop_println, CliResult, Config};
33
use clap::{AppSettings, Arg, ArgMatches};
44

55
use super::commands;
66
use super::list_commands;
77
use crate::command_prelude::*;
8+
use cargo::core::features::HIDDEN;
89

910
pub fn main(config: &mut Config) -> CliResult {
1011
// CAUTION: Be careful with using `config` until it is configured below.
@@ -30,25 +31,38 @@ pub fn main(config: &mut Config) -> CliResult {
3031
};
3132

3233
if args.value_of("unstable-features") == Some("help") {
34+
let options = CliUnstable::help();
35+
let non_hidden_options: Vec<(String, String)> = options
36+
.iter()
37+
.filter(|(_, help_message)| *help_message != HIDDEN)
38+
.map(|(name, help)| (name.to_string(), help.to_string()))
39+
.collect();
40+
let longest_option = non_hidden_options
41+
.iter()
42+
.map(|(option_name, _)| option_name.len())
43+
.max()
44+
.unwrap_or(0);
45+
let help_lines: Vec<String> = non_hidden_options
46+
.iter()
47+
.map(|(option_name, option_help_message)| {
48+
let option_name_kebab_case = option_name.replace("_", "-");
49+
let padding = " ".repeat(longest_option - option_name.len()); // safe to substract
50+
format!(
51+
" -Z {}{} -- {}",
52+
option_name_kebab_case, padding, option_help_message
53+
)
54+
})
55+
.collect();
56+
let joined = help_lines.join("\n");
3357
drop_println!(
3458
config,
3559
"
3660
Available unstable (nightly-only) flags:
3761
38-
-Z allow-features -- Allow *only* the listed unstable features
39-
-Z avoid-dev-deps -- Avoid installing dev-dependencies if possible
40-
-Z extra-link-arg -- Allow `cargo:rustc-link-arg` in build scripts
41-
-Z minimal-versions -- Install minimal dependency versions instead of maximum
42-
-Z no-index-update -- Do not update the registry, avoids a network request for benchmarking
43-
-Z unstable-options -- Allow the usage of unstable options
44-
-Z timings -- Display concurrency information
45-
-Z doctest-xcompile -- Compile and run doctests for non-host target using runner config
46-
-Z terminal-width -- Provide a terminal width to rustc for error truncation
47-
-Z namespaced-features -- Allow features with `dep:` prefix
48-
-Z weak-dep-features -- Allow `dep_name?/feature` feature syntax
49-
-Z patch-in-config -- Allow `[patch]` sections in .cargo/config.toml files
62+
{}
5063
51-
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'"
64+
Run with 'cargo -Z [FLAG] [SUBCOMMAND]'",
65+
joined
5266
);
5367
if !config.nightly_features_allowed {
5468
drop_println!(

src/cargo/core/compiler/job_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ impl<'cfg> DrainState<'cfg> {
808808
}
809809

810810
fn emit_future_incompat(&mut self, cx: &mut Context<'_, '_>) {
811-
if cx.bcx.config.cli_unstable().enable_future_incompat_feature {
811+
if cx.bcx.config.cli_unstable().future_incompat_report {
812812
if self.per_crate_future_incompat_reports.is_empty() {
813813
drop(
814814
cx.bcx

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ fn build_base_args(
936936
.env("RUSTC_BOOTSTRAP", "1");
937937
}
938938

939-
if bcx.config.cli_unstable().enable_future_incompat_feature {
939+
if bcx.config.cli_unstable().future_incompat_report {
940940
cmd.arg("-Z").arg("emit-future-incompat-report");
941941
}
942942

src/cargo/core/features.rs

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
//! get an instance of `CliUnstable` and check if the option has been
6969
//! enabled on the `CliUnstable` instance. Nightly gating is already
7070
//! handled, so no need to worry about that.
71-
//! 4. Update the `-Z help` documentation in the `main` function.
7271
//!
7372
//! ## Stabilization
7473
//!
@@ -83,9 +82,9 @@
8382
//! 2. `-Z unstable-options`: Find the call to `fail_if_stable_opt` and
8483
//! remove it. Be sure to update the man pages if necessary.
8584
//! 3. `-Z` flag: Change the parsing code in [`CliUnstable::add`] to call
86-
//! `stabilized_warn` or `stabilized_err`. Remove it from the `-Z help`
87-
//! docs in the `main` function. Remove the `(unstable)` note in the
88-
//! clap help text if necessary.
85+
//! `stabilized_warn` or `stabilized_err` and remove the field from
86+
//! `CliUnstable. Remove the `(unstable)` note in the clap help text if
87+
//! necessary.
8988
//! 2. Remove `masquerade_as_nightly_cargo` from any tests, and remove
9089
//! `cargo-features` from `Cargo.toml` test files if any.
9190
//! 3. Remove the docs from unstable.md and update the redirect at the bottom
@@ -105,6 +104,7 @@ use crate::util::errors::CargoResult;
105104
use crate::util::{indented_lines, iter_join};
106105
use crate::Config;
107106

107+
pub const HIDDEN: &str = "";
108108
pub const SEE_CHANNELS: &str =
109109
"See https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information \
110110
about Rust release channels.";
@@ -538,51 +538,73 @@ impl Features {
538538
}
539539
}
540540

541-
/// A parsed representation of all unstable flags that Cargo accepts.
542-
///
543-
/// Cargo, like `rustc`, accepts a suite of `-Z` flags which are intended for
544-
/// gating unstable functionality to Cargo. These flags are only available on
545-
/// the nightly channel of Cargo.
546-
#[derive(Default, Debug, Deserialize)]
547-
#[serde(default, rename_all = "kebab-case")]
548-
pub struct CliUnstable {
541+
macro_rules! unstable_cli_options {
542+
(
543+
$(
544+
$(#[$meta:meta])?
545+
$element: ident: $ty: ty = ($help: expr )
546+
),*
547+
) => {
548+
/// A parsed representation of all unstable flags that Cargo accepts.
549+
///
550+
/// Cargo, like `rustc`, accepts a suite of `-Z` flags which are intended for
551+
/// gating unstable functionality to Cargo. These flags are only available on
552+
/// the nightly channel of Cargo.
553+
#[derive(Default, Debug, Deserialize)]
554+
#[serde(default, rename_all = "kebab-case")]
555+
pub struct CliUnstable {
556+
$(
557+
$(#[$meta])?
558+
pub $element: $ty
559+
),*
560+
}
561+
impl CliUnstable {
562+
pub fn help() -> Vec<(&'static str, &'static str)> {
563+
let fields = vec![$((stringify!($element), $help)),*];
564+
fields
565+
}
566+
}
567+
}
568+
}
569+
570+
unstable_cli_options!(
549571
// Permanently unstable features:
550-
pub allow_features: Option<BTreeSet<String>>,
551-
pub print_im_a_teapot: bool,
572+
allow_features: Option<BTreeSet<String>> = ("Allow *only* the listed unstable features"),
573+
print_im_a_teapot: bool= (HIDDEN),
552574

553575
// All other unstable features.
554576
// Please keep this list lexiographically ordered.
555-
pub advanced_env: bool,
556-
pub avoid_dev_deps: bool,
557-
pub binary_dep_depinfo: bool,
577+
advanced_env: bool = (HIDDEN),
578+
avoid_dev_deps: bool = ("Avoid installing dev-dependencies if possible"),
579+
binary_dep_depinfo: bool = ("Track changes to dependency artifacts"),
558580
#[serde(deserialize_with = "deserialize_build_std")]
559-
pub build_std: Option<Vec<String>>,
560-
pub build_std_features: Option<Vec<String>>,
561-
pub config_include: bool,
562-
pub configurable_env: bool,
563-
pub credential_process: bool,
564-
pub doctest_in_workspace: bool,
565-
pub doctest_xcompile: bool,
566-
pub dual_proc_macros: bool,
567-
pub enable_future_incompat_feature: bool,
568-
pub extra_link_arg: bool,
569-
pub features: Option<Vec<String>>,
570-
pub jobserver_per_rustc: bool,
571-
pub minimal_versions: bool,
572-
pub mtime_on_use: bool,
573-
pub multitarget: bool,
574-
pub named_profiles: bool,
575-
pub namespaced_features: bool,
576-
pub no_index_update: bool,
577-
pub panic_abort_tests: bool,
578-
pub patch_in_config: bool,
579-
pub rustdoc_map: bool,
580-
pub separate_nightlies: bool,
581-
pub terminal_width: Option<Option<usize>>,
582-
pub timings: Option<Vec<String>>,
583-
pub unstable_options: bool,
584-
pub weak_dep_features: bool,
585-
}
581+
build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
582+
build_std_features: Option<Vec<String>> = ("Configure features enabled for the standard library itself when building the standard library"),
583+
config_include: bool = ("Enable the `include` key in config files"),
584+
configurable_env: bool = ("Enable the [env] section in the .cargo/config.toml file"),
585+
credential_process: bool = ("Add a config setting to fetch registry authentication tokens by calling an external process"),
586+
doctest_in_workspace: bool = ("Compile doctests with paths relative to the workspace root"),
587+
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
588+
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
589+
future_incompat_report: bool = ("Enable creation of a future-incompat report for all dependencies"),
590+
extra_link_arg: bool = ("Allow `cargo:rustc-link-arg` in build scripts"),
591+
features: Option<Vec<String>> = (HIDDEN),
592+
jobserver_per_rustc: bool = (HIDDEN),
593+
minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum"),
594+
mtime_on_use: bool = ("Configure Cargo to update the mtime of used files"),
595+
multitarget: bool = ("Allow passing multiple `--target` flags to the cargo subcommand selected"),
596+
named_profiles: bool = ("Allow defining custom profiles"),
597+
namespaced_features: bool = ("Allow features with `dep:` prefix"),
598+
no_index_update: bool = ("Do not update the registry index even if the cache is outdated"),
599+
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
600+
patch_in_config: bool = ("Allow `[patch]` sections in .cargo/config.toml files"),
601+
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
602+
separate_nightlies: bool = (HIDDEN),
603+
terminal_width: Option<Option<usize>> = ("Provide a terminal width to rustc for error truncation"),
604+
timings: Option<Vec<String>> = ("Display concurrency information"),
605+
unstable_options: bool = ("Allow the usage of unstable options"),
606+
weak_dep_features: bool = ("Allow `dep_name?/feature` feature syntax")
607+
);
586608

587609
const STABILIZED_COMPILE_PROGRESS: &str = "The progress bar is now always \
588610
enabled when used on an interactive console.\n\
@@ -798,7 +820,7 @@ impl CliUnstable {
798820
"config-profile" => stabilized_warn(k, "1.43", STABILIZED_CONFIG_PROFILE),
799821
"crate-versions" => stabilized_warn(k, "1.47", STABILIZED_CRATE_VERSIONS),
800822
"package-features" => stabilized_warn(k, "1.51", STABILIZED_PACKAGE_FEATURES),
801-
"future-incompat-report" => self.enable_future_incompat_feature = parse_empty(k, v)?,
823+
"future-incompat-report" => self.future_incompat_report = parse_empty(k, v)?,
802824
_ => bail!("unknown `-Z` flag specified: {}", k),
803825
}
804826

src/cargo/util/command_prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ pub trait ArgMatchesExt {
488488
// TODO: Tracking issue
489489
.fail_if_stable_opt("--future-incompat-report", 9241)?;
490490

491-
if !config.cli_unstable().enable_future_incompat_feature {
491+
if !config.cli_unstable().future_incompat_report {
492492
anyhow::bail!(
493493
"Usage of `--future-incompat-report` requires `-Z future-incompat-report`"
494494
)

tests/testsuite/help.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ fn z_flags_help() {
4848
// Test that the output of `cargo -Z help` shows a different help screen with
4949
// all the `-Z` flags.
5050
cargo_process("-Z help")
51-
.with_stdout_contains(" -Z unstable-options -- Allow the usage of unstable options")
51+
.with_stdout_contains(
52+
" -Z allow-features[..]-- Allow *only* the listed unstable features",
53+
)
5254
.run();
5355
}
5456

0 commit comments

Comments
 (0)