Skip to content

Commit 87b4cb2

Browse files
committed
Auto merge of #13274 - weihanglo:unstable-features-doc, r=epage
docs(unstable): doc comments for items and fields
2 parents 616bae2 + 4105a15 commit 87b4cb2

File tree

3 files changed

+85
-55
lines changed

3 files changed

+85
-55
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: 81 additions & 51 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.
@@ -357,20 +358,43 @@ enum Status {
357358
Removed,
358359
}
359360

361+
/// A listing of stable and unstable new syntax in Cargo.toml.
362+
///
363+
/// This generates definitions and impls for [`Features`] and [`Feature`]
364+
/// for each new syntax.
365+
///
366+
/// Note that all feature names in the macro invocation are valid Rust
367+
/// identifiers, but the `_` character is translated to `-` when specified in
368+
/// the `cargo-features` manifest entry in `Cargo.toml`.
369+
///
370+
/// See the [module-level documentation](self#new-cargotoml-syntax)
371+
/// for the process of adding a new syntax.
360372
macro_rules! features {
361373
(
362-
$(($stab:ident, $feature:ident, $version:expr, $docs:expr),)*
374+
$(
375+
$(#[$attr:meta])*
376+
($stab:ident, $feature:ident, $version:expr, $docs:expr),
377+
)*
363378
) => (
379+
/// Unstable feature context for querying if a new Cargo.toml syntax
380+
/// is allowed to use.
381+
///
382+
/// See the [module-level documentation](self#new-cargotoml-syntax) for the usage.
364383
#[derive(Default, Clone, Debug)]
365384
pub struct Features {
366385
$($feature: bool,)*
386+
/// The current activated features.
367387
activated: Vec<String>,
388+
/// Whether is allowed to use any unstable features.
368389
nightly_features_allowed: bool,
390+
/// Whether the source mainfest is from a local package.
369391
is_local: bool,
370392
}
371393

372394
impl Feature {
373395
$(
396+
$(#[$attr])*
397+
#[doc = concat!("\n\n\nSee <https://doc.rust-lang.org/nightly/cargo/", $docs, ">.")]
374398
pub fn $feature() -> &'static Feature {
375399
fn get(features: &Features) -> bool {
376400
stab!($stab) == Status::Stable || features.$feature
@@ -386,6 +410,7 @@ macro_rules! features {
386410
}
387411
)*
388412

413+
/// Whether this feature is allowed to use in the given [`Features`] context.
389414
fn is_enabled(&self, features: &Features) -> bool {
390415
(self.get)(features)
391416
}
@@ -420,96 +445,91 @@ macro_rules! stab {
420445
};
421446
}
422447

423-
// A listing of all features in Cargo.
424-
//
425448
// "look here"
426-
//
427-
// This is the macro that lists all stable and unstable features in Cargo.
428-
// You'll want to add to this macro whenever you add a feature to Cargo, also
429-
// following the directions above.
430-
//
431-
// Note that all feature names here are valid Rust identifiers, but the `_`
432-
// character is translated to `-` when specified in the `cargo-features`
433-
// manifest entry in `Cargo.toml`.
434449
features! {
435-
// A dummy feature that doesn't actually gate anything, but it's used in
436-
// testing to ensure that we can enable stable features.
450+
/// A dummy feature that doesn't actually gate anything, but it's used in
451+
/// testing to ensure that we can enable stable features.
437452
(stable, test_dummy_stable, "1.0", ""),
438453

439-
// A dummy feature that gates the usage of the `im-a-teapot` manifest
440-
// entry. This is basically just intended for tests.
454+
/// A dummy feature that gates the usage of the `im-a-teapot` manifest
455+
/// entry. This is basically just intended for tests.
441456
(unstable, test_dummy_unstable, "", "reference/unstable.html"),
442457

443-
// Downloading packages from alternative registry indexes.
458+
/// Downloading packages from alternative registry indexes.
444459
(stable, alternative_registries, "1.34", "reference/registries.html"),
445460

446-
// Using editions
461+
/// Using editions
447462
(stable, edition, "1.31", "reference/manifest.html#the-edition-field"),
448463

449-
// Renaming a package in the manifest via the `package` key
464+
/// Renaming a package in the manifest via the `package` key.
450465
(stable, rename_dependency, "1.31", "reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml"),
451466

452-
// Whether a lock file is published with this crate
467+
/// Whether a lock file is published with this crate.
453468
(removed, publish_lockfile, "1.37", "reference/unstable.html#publish-lockfile"),
454469

455-
// Overriding profiles for dependencies.
470+
/// Overriding profiles for dependencies.
456471
(stable, profile_overrides, "1.41", "reference/profiles.html#overrides"),
457472

458-
// "default-run" manifest option,
473+
/// "default-run" manifest option.
459474
(stable, default_run, "1.37", "reference/manifest.html#the-default-run-field"),
460475

461-
// Declarative build scripts.
476+
/// Declarative build scripts.
462477
(unstable, metabuild, "", "reference/unstable.html#metabuild"),
463478

464-
// Specifying the 'public' attribute on dependencies
479+
/// Specifying the 'public' attribute on dependencies.
465480
(unstable, public_dependency, "", "reference/unstable.html#public-dependency"),
466481

467-
// Allow to specify profiles other than 'dev', 'release', 'test', etc.
482+
/// Allow to specify profiles other than 'dev', 'release', 'test', etc.
468483
(stable, named_profiles, "1.57", "reference/profiles.html#custom-profiles"),
469484

470-
// Opt-in new-resolver behavior.
485+
/// Opt-in new-resolver behavior.
471486
(stable, resolver, "1.51", "reference/resolver.html#resolver-versions"),
472487

473-
// Allow to specify whether binaries should be stripped.
488+
/// Allow to specify whether binaries should be stripped.
474489
(stable, strip, "1.58", "reference/profiles.html#strip-option"),
475490

476-
// Specifying a minimal 'rust-version' attribute for crates
491+
/// Specifying a minimal 'rust-version' attribute for crates.
477492
(stable, rust_version, "1.56", "reference/manifest.html#the-rust-version-field"),
478493

479-
// Support for 2021 edition.
494+
/// Support for 2021 edition.
480495
(stable, edition2021, "1.56", "reference/manifest.html#the-edition-field"),
481496

482-
// Allow to specify per-package targets (compile kinds)
497+
/// Allow to specify per-package targets (compile kinds).
483498
(unstable, per_package_target, "", "reference/unstable.html#per-package-target"),
484499

485-
// Allow to specify which codegen backend should be used.
500+
/// Allow to specify which codegen backend should be used.
486501
(unstable, codegen_backend, "", "reference/unstable.html#codegen-backend"),
487502

488-
// Allow specifying different binary name apart from the crate name
503+
/// Allow specifying different binary name apart from the crate name.
489504
(unstable, different_binary_name, "", "reference/unstable.html#different-binary-name"),
490505

491-
// Allow specifying rustflags directly in a profile
506+
/// Allow specifying rustflags directly in a profile.
492507
(unstable, profile_rustflags, "", "reference/unstable.html#profile-rustflags-option"),
493508

494-
// Allow specifying rustflags directly in a profile
509+
/// Allow workspace members to inherit fields and dependencies from a workspace.
495510
(stable, workspace_inheritance, "1.64", "reference/unstable.html#workspace-inheritance"),
496511

497-
// Support for 2024 edition.
512+
/// Support for 2024 edition.
498513
(unstable, edition2024, "", "reference/unstable.html#edition-2024"),
499514

500-
// Allow setting trim-paths in a profile to control the sanitisation of file paths in build outputs.
515+
/// Allow setting trim-paths in a profile to control the sanitisation of file paths in build outputs.
501516
(unstable, trim_paths, "", "reference/unstable.html#profile-trim-paths-option"),
502517
}
503518

519+
/// Status and metadata for a single unstable feature.
504520
pub struct Feature {
521+
/// Feature name. This is valid Rust identifer so no dash only underscore.
505522
name: &'static str,
506523
stability: Status,
524+
/// Version that this feature was stabilized or removed.
507525
version: &'static str,
526+
/// Link to the unstable documentation.
508527
docs: &'static str,
509528
get: fn(&Features) -> bool,
510529
}
511530

512531
impl Features {
532+
/// Creates a new unstable features context.
513533
pub fn new(
514534
features: &[String],
515535
config: &Config,
@@ -616,10 +636,12 @@ impl Features {
616636
Ok(())
617637
}
618638

639+
/// Gets the current activated features.
619640
pub fn activated(&self) -> &[String] {
620641
&self.activated
621642
}
622643

644+
/// Checks if the given feature is enabled.
623645
pub fn require(&self, feature: &Feature) -> CargoResult<()> {
624646
if feature.is_enabled(self) {
625647
return Ok(());
@@ -665,16 +687,20 @@ impl Features {
665687
bail!("{}", msg);
666688
}
667689

690+
/// Whether the given feature is allowed to use in this context.
668691
pub fn is_enabled(&self, feature: &Feature) -> bool {
669692
feature.is_enabled(self)
670693
}
671694
}
672695

696+
/// Generates `-Z` flags as fields of [`CliUnstable`].
697+
///
698+
/// See the [module-level documentation](self#-z-options) for details.
673699
macro_rules! unstable_cli_options {
674700
(
675701
$(
676702
$(#[$meta:meta])?
677-
$element: ident: $ty: ty = ($help: expr ),
703+
$element: ident: $ty: ty$( = ($help:literal))?,
678704
)*
679705
) => {
680706
/// A parsed representation of all unstable flags that Cargo accepts.
@@ -686,13 +712,15 @@ macro_rules! unstable_cli_options {
686712
#[serde(default, rename_all = "kebab-case")]
687713
pub struct CliUnstable {
688714
$(
715+
$(#[doc = $help])?
689716
$(#[$meta])?
690717
pub $element: $ty
691718
),*
692719
}
693720
impl CliUnstable {
694-
pub fn help() -> Vec<(&'static str, &'static str)> {
695-
let fields = vec![$((stringify!($element), $help)),*];
721+
/// Returns a list of `(<option-name>, <help-text>)`.
722+
pub fn help() -> Vec<(&'static str, Option<&'static str>)> {
723+
let fields = vec![$((stringify!($element), None$(.or(Some($help)))?)),*];
696724
fields
697725
}
698726
}
@@ -721,12 +749,12 @@ macro_rules! unstable_cli_options {
721749

722750
unstable_cli_options!(
723751
// Permanently unstable features:
724-
allow_features: Option<BTreeSet<String>> = ("Allow *only* the listed unstable features"),
725-
print_im_a_teapot: bool = (HIDDEN),
752+
allow_features: Option<AllowFeatures> = ("Allow *only* the listed unstable features"),
753+
print_im_a_teapot: bool,
726754

727755
// All other unstable features.
728756
// Please keep this list lexicographically ordered.
729-
advanced_env: bool = (HIDDEN),
757+
advanced_env: bool,
730758
asymmetric_token: bool = ("Allows authenticating with asymmetric tokens"),
731759
avoid_dev_deps: bool = ("Avoid installing dev-dependencies if possible"),
732760
binary_dep_depinfo: bool = ("Track changes to dependency artifacts"),
@@ -740,24 +768,24 @@ unstable_cli_options!(
740768
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
741769
doctest_xcompile: bool = ("Compile and run doctests for non-host target using runner config"),
742770
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
743-
features: Option<Vec<String>> = (HIDDEN),
771+
features: Option<Vec<String>>,
744772
gc: bool = ("Track cache usage and \"garbage collect\" unused files"),
745773
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"),
774+
host_config: bool = ("Enable the `[host]` section in the .cargo/config.toml file"),
747775
lints: bool = ("Pass `[lints]` to the linting tools"),
748776
minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum"),
749777
msrv_policy: bool = ("Enable rust-version aware policy within cargo"),
750778
mtime_on_use: bool = ("Configure Cargo to update the mtime of used files"),
751-
next_lockfile_bump: bool = (HIDDEN),
779+
next_lockfile_bump: bool,
752780
no_index_update: bool = ("Do not update the registry index even if the cache is outdated"),
753781
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
754782
profile_rustflags: bool = ("Enable the `rustflags` option in profiles in .cargo/config.toml file"),
755783
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
756784
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
757785
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
758786
script: bool = ("Enable support for single-file, `.rs` packages"),
759-
separate_nightlies: bool = (HIDDEN),
760-
skip_rustdoc_fingerprint: bool = (HIDDEN),
787+
separate_nightlies: bool,
788+
skip_rustdoc_fingerprint: bool,
761789
target_applies_to_host: bool = ("Enable the `target-applies-to-host` key in the .cargo/config.toml file"),
762790
trim_paths: bool = ("Enable the `trim-paths` option in profiles"),
763791
unstable_options: bool = ("Allow the usage of unstable options"),
@@ -915,6 +943,8 @@ fn parse_gitoxide(
915943
}
916944

917945
impl CliUnstable {
946+
/// Parses `-Z` flags from the command line, and returns messages that warn
947+
/// if any flag has alreardy been stabilized.
918948
pub fn parse(
919949
&mut self,
920950
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)