Skip to content

Commit d0390c2

Browse files
committed
docs(unstable): doc comments for CliUnstable
1 parent 8859998 commit d0390c2

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

src/bin/cargo/cli.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use super::commands;
1313
use super::list_commands;
1414
use crate::command_prelude::*;
1515
use crate::util::is_rustup;
16-
use cargo::core::features::HIDDEN;
1716
use cargo::util::style;
1817

1918
pub fn main(config: &mut LazyConfig) -> CliResult {
@@ -63,15 +62,16 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
6362
let options = CliUnstable::help();
6463
let max_length = options
6564
.iter()
66-
.filter(|(_, help)| *help != HIDDEN)
65+
.filter(|(_, help)| help.is_some())
6766
.map(|(option_name, _)| option_name.len())
6867
.max()
6968
.unwrap_or(0);
7069
let z_flags = options
7170
.iter()
72-
.filter(|(_, help)| *help != HIDDEN)
71+
.filter(|(_, help)| help.is_some())
7372
.map(|(opt, help)| {
7473
let opt = opt.replace("_", "-");
74+
let help = help.unwrap();
7575
format!(" {literal}-Z {opt:<max_length$}{reset} {help}")
7676
})
7777
.join("\n");

src/cargo/core/features.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
//! not yet been designed, or for more complex features that affect multiple
1616
//! parts of Cargo should use a new `-Z` flag.
1717
//!
18-
//! See below for more details.
19-
//!
2018
//! When adding new tests for your feature, usually the tests should go into a
2119
//! new module of the testsuite. See
2220
//! <https://doc.crates.io/contrib/tests/writing.html> for more information on
@@ -63,8 +61,8 @@
6361
//!
6462
//! The steps to add a new `-Z` option are:
6563
//!
66-
//! 1. Add the option to the [`CliUnstable`] struct below. Flags can take an
67-
//! optional value if you want.
64+
//! 1. Add the option to the [`CliUnstable`] struct in the macro invocation of
65+
//! [`unstable_cli_options!`]. Flags can take an optional value if you want.
6866
//! 2. Update the [`CliUnstable::add`] function to parse the flag.
6967
//! 3. Wherever the new functionality is implemented, call
7068
//! [`Config::cli_unstable`] to get an instance of [`CliUnstable`]
@@ -136,6 +134,7 @@
136134
//! [`Config::cli_unstable`]: crate::util::config::Config::cli_unstable
137135
//! [`fail_if_stable_opt`]: CliUnstable::fail_if_stable_opt
138136
//! [`features!`]: macro.features.html
137+
//! [`unstable_cli_options!`]: macro.unstable_cli_options.html
139138
140139
use std::collections::BTreeSet;
141140
use std::env;
@@ -151,11 +150,13 @@ use crate::util::errors::CargoResult;
151150
use crate::util::{indented_lines, iter_join};
152151
use crate::Config;
153152

154-
pub const HIDDEN: &str = "";
155153
pub const SEE_CHANNELS: &str =
156154
"See https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information \
157155
about Rust release channels.";
158156

157+
/// Value of [`allow-features`](CliUnstable::allow_features]
158+
pub type AllowFeatures = BTreeSet<String>;
159+
159160
/// The edition of the compiler ([RFC 2052])
160161
///
161162
/// The following sections will guide you how to add and stabilize an edition.
@@ -670,11 +671,14 @@ impl Features {
670671
}
671672
}
672673

674+
/// Generates `-Z` flags as fields of [`CliUnstable`].
675+
///
676+
/// See the [module-level documentation](self#-z-options) for details.
673677
macro_rules! unstable_cli_options {
674678
(
675679
$(
676680
$(#[$meta:meta])?
677-
$element: ident: $ty: ty = ($help: expr ),
681+
$element: ident: $ty: ty$( = ($help:literal))?,
678682
)*
679683
) => {
680684
/// A parsed representation of all unstable flags that Cargo accepts.
@@ -686,13 +690,15 @@ macro_rules! unstable_cli_options {
686690
#[serde(default, rename_all = "kebab-case")]
687691
pub struct CliUnstable {
688692
$(
693+
$(#[doc = $help])?
689694
$(#[$meta])?
690695
pub $element: $ty
691696
),*
692697
}
693698
impl CliUnstable {
694-
pub fn help() -> Vec<(&'static str, &'static str)> {
695-
let fields = vec![$((stringify!($element), $help)),*];
699+
/// Returns a list of `(<option-name>, <help-text>)`.
700+
pub fn help() -> Vec<(&'static str, Option<&'static str>)> {
701+
let fields = vec![$((stringify!($element), None$(.or(Some($help)))?)),*];
696702
fields
697703
}
698704
}
@@ -721,12 +727,12 @@ macro_rules! unstable_cli_options {
721727

722728
unstable_cli_options!(
723729
// Permanently unstable features:
724-
allow_features: Option<BTreeSet<String>> = ("Allow *only* the listed unstable features"),
725-
print_im_a_teapot: bool = (HIDDEN),
730+
allow_features: Option<AllowFeatures> = ("Allow *only* the listed unstable features"),
731+
print_im_a_teapot: bool,
726732

727733
// All other unstable features.
728734
// Please keep this list lexicographically ordered.
729-
advanced_env: bool = (HIDDEN),
735+
advanced_env: bool,
730736
asymmetric_token: bool = ("Allows authenticating with asymmetric tokens"),
731737
avoid_dev_deps: bool = ("Avoid installing dev-dependencies if possible"),
732738
binary_dep_depinfo: bool = ("Track changes to dependency artifacts"),
@@ -740,24 +746,24 @@ unstable_cli_options!(
740746
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
741747
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
742748
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
743-
features: Option<Vec<String>> = (HIDDEN),
749+
features: Option<Vec<String>>,
744750
gc: bool = ("Track cache usage and \"garbage collect\" unused files"),
745751
gitoxide: Option<GitoxideFeatures> = ("Use gitoxide for the given git interactions, or all of them if no argument is given"),
746-
host_config: bool = ("Enable the [host] section in the .cargo/config.toml file"),
752+
host_config: bool = ("Enable the `[host]` section in the .cargo/config.toml file"),
747753
lints: bool = ("Pass `[lints]` to the linting tools"),
748754
minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum"),
749755
msrv_policy: bool = ("Enable rust-version aware policy within cargo"),
750756
mtime_on_use: bool = ("Configure Cargo to update the mtime of used files"),
751-
next_lockfile_bump: bool = (HIDDEN),
757+
next_lockfile_bump: bool,
752758
no_index_update: bool = ("Do not update the registry index even if the cache is outdated"),
753759
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
754760
profile_rustflags: bool = ("Enable the `rustflags` option in profiles in .cargo/config.toml file"),
755761
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
756762
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
757763
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
758764
script: bool = ("Enable support for single-file, `.rs` packages"),
759-
separate_nightlies: bool = (HIDDEN),
760-
skip_rustdoc_fingerprint: bool = (HIDDEN),
765+
separate_nightlies: bool,
766+
skip_rustdoc_fingerprint: bool,
761767
target_applies_to_host: bool = ("Enable the `target-applies-to-host` key in the .cargo/config.toml file"),
762768
trim_paths: bool = ("Enable the `trim-paths` option in profiles"),
763769
unstable_options: bool = ("Allow the usage of unstable options"),
@@ -915,6 +921,8 @@ fn parse_gitoxide(
915921
}
916922

917923
impl CliUnstable {
924+
/// Parses `-Z` flags from the command line, and returns messages that warn
925+
/// if any flag has alreardy been stabilized.
918926
pub fn parse(
919927
&mut self,
920928
flags: &[String],

tests/testsuite/cargo/z_help/stdout.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Available unstable (nightly-only) flags:
1515
-Z dual-proc-macros Build proc-macros for both the host and the target
1616
-Z gc Track cache usage and "garbage collect" unused files
1717
-Z gitoxide Use gitoxide for the given git interactions, or all of them if no argument is given
18-
-Z host-config Enable the [host] section in the .cargo/config.toml file
18+
-Z host-config Enable the `[host]` section in the .cargo/config.toml file
1919
-Z lints Pass `[lints]` to the linting tools
2020
-Z minimal-versions Resolve minimal dependency versions instead of maximum
2121
-Z msrv-policy Enable rust-version aware policy within cargo

0 commit comments

Comments
 (0)