15
15
//! not yet been designed, or for more complex features that affect multiple
16
16
//! parts of Cargo should use a new `-Z` flag.
17
17
//!
18
- //! See below for more details.
19
- //!
20
18
//! When adding new tests for your feature, usually the tests should go into a
21
19
//! new module of the testsuite. See
22
20
//! <https://doc.crates.io/contrib/tests/writing.html> for more information on
63
61
//!
64
62
//! The steps to add a new `-Z` option are:
65
63
//!
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.
68
66
//! 2. Update the [`CliUnstable::add`] function to parse the flag.
69
67
//! 3. Wherever the new functionality is implemented, call
70
68
//! [`Config::cli_unstable`] to get an instance of [`CliUnstable`]
136
134
//! [`Config::cli_unstable`]: crate::util::config::Config::cli_unstable
137
135
//! [`fail_if_stable_opt`]: CliUnstable::fail_if_stable_opt
138
136
//! [`features!`]: macro.features.html
137
+ //! [`unstable_cli_options!`]: macro.unstable_cli_options.html
139
138
140
139
use std:: collections:: BTreeSet ;
141
140
use std:: env;
@@ -151,11 +150,13 @@ use crate::util::errors::CargoResult;
151
150
use crate :: util:: { indented_lines, iter_join} ;
152
151
use crate :: Config ;
153
152
154
- pub const HIDDEN : & str = "" ;
155
153
pub const SEE_CHANNELS : & str =
156
154
"See https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information \
157
155
about Rust release channels.";
158
156
157
+ /// Value of [`allow-features`](CliUnstable::allow_features]
158
+ pub type AllowFeatures = BTreeSet < String > ;
159
+
159
160
/// The edition of the compiler ([RFC 2052])
160
161
///
161
162
/// The following sections will guide you how to add and stabilize an edition.
@@ -357,20 +358,43 @@ enum Status {
357
358
Removed ,
358
359
}
359
360
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.
360
372
macro_rules! features {
361
373
(
362
- $( ( $stab: ident, $feature: ident, $version: expr, $docs: expr) , ) *
374
+ $(
375
+ $( #[ $attr: meta] ) *
376
+ ( $stab: ident, $feature: ident, $version: expr, $docs: expr) ,
377
+ ) *
363
378
) => (
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.
364
383
#[ derive( Default , Clone , Debug ) ]
365
384
pub struct Features {
366
385
$( $feature: bool , ) *
386
+ /// The current activated features.
367
387
activated: Vec <String >,
388
+ /// Whether is allowed to use any unstable features.
368
389
nightly_features_allowed: bool ,
390
+ /// Whether the source mainfest is from a local package.
369
391
is_local: bool ,
370
392
}
371
393
372
394
impl Feature {
373
395
$(
396
+ $( #[ $attr] ) *
397
+ #[ doc = concat!( "\n \n \n See <https://doc.rust-lang.org/nightly/cargo/" , $docs, ">." ) ]
374
398
pub fn $feature( ) -> & ' static Feature {
375
399
fn get( features: & Features ) -> bool {
376
400
stab!( $stab) == Status :: Stable || features. $feature
@@ -386,6 +410,7 @@ macro_rules! features {
386
410
}
387
411
) *
388
412
413
+ /// Whether this feature is allowed to use in the given [`Features`] context.
389
414
fn is_enabled( & self , features: & Features ) -> bool {
390
415
( self . get) ( features)
391
416
}
@@ -420,96 +445,91 @@ macro_rules! stab {
420
445
} ;
421
446
}
422
447
423
- // A listing of all features in Cargo.
424
- //
425
448
// "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`.
434
449
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.
437
452
( stable, test_dummy_stable, "1.0" , "" ) ,
438
453
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.
441
456
( unstable, test_dummy_unstable, "" , "reference/unstable.html" ) ,
442
457
443
- // Downloading packages from alternative registry indexes.
458
+ /// Downloading packages from alternative registry indexes.
444
459
( stable, alternative_registries, "1.34" , "reference/registries.html" ) ,
445
460
446
- // Using editions
461
+ /// Using editions
447
462
( stable, edition, "1.31" , "reference/manifest.html#the-edition-field" ) ,
448
463
449
- // Renaming a package in the manifest via the `package` key
464
+ /// Renaming a package in the manifest via the `package` key.
450
465
( stable, rename_dependency, "1.31" , "reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml" ) ,
451
466
452
- // Whether a lock file is published with this crate
467
+ /// Whether a lock file is published with this crate.
453
468
( removed, publish_lockfile, "1.37" , "reference/unstable.html#publish-lockfile" ) ,
454
469
455
- // Overriding profiles for dependencies.
470
+ /// Overriding profiles for dependencies.
456
471
( stable, profile_overrides, "1.41" , "reference/profiles.html#overrides" ) ,
457
472
458
- // "default-run" manifest option,
473
+ /// "default-run" manifest option.
459
474
( stable, default_run, "1.37" , "reference/manifest.html#the-default-run-field" ) ,
460
475
461
- // Declarative build scripts.
476
+ /// Declarative build scripts.
462
477
( unstable, metabuild, "" , "reference/unstable.html#metabuild" ) ,
463
478
464
- // Specifying the 'public' attribute on dependencies
479
+ /// Specifying the 'public' attribute on dependencies.
465
480
( unstable, public_dependency, "" , "reference/unstable.html#public-dependency" ) ,
466
481
467
- // Allow to specify profiles other than 'dev', 'release', 'test', etc.
482
+ /// Allow to specify profiles other than 'dev', 'release', 'test', etc.
468
483
( stable, named_profiles, "1.57" , "reference/profiles.html#custom-profiles" ) ,
469
484
470
- // Opt-in new-resolver behavior.
485
+ /// Opt-in new-resolver behavior.
471
486
( stable, resolver, "1.51" , "reference/resolver.html#resolver-versions" ) ,
472
487
473
- // Allow to specify whether binaries should be stripped.
488
+ /// Allow to specify whether binaries should be stripped.
474
489
( stable, strip, "1.58" , "reference/profiles.html#strip-option" ) ,
475
490
476
- // Specifying a minimal 'rust-version' attribute for crates
491
+ /// Specifying a minimal 'rust-version' attribute for crates.
477
492
( stable, rust_version, "1.56" , "reference/manifest.html#the-rust-version-field" ) ,
478
493
479
- // Support for 2021 edition.
494
+ /// Support for 2021 edition.
480
495
( stable, edition2021, "1.56" , "reference/manifest.html#the-edition-field" ) ,
481
496
482
- // Allow to specify per-package targets (compile kinds)
497
+ /// Allow to specify per-package targets (compile kinds).
483
498
( unstable, per_package_target, "" , "reference/unstable.html#per-package-target" ) ,
484
499
485
- // Allow to specify which codegen backend should be used.
500
+ /// Allow to specify which codegen backend should be used.
486
501
( unstable, codegen_backend, "" , "reference/unstable.html#codegen-backend" ) ,
487
502
488
- // Allow specifying different binary name apart from the crate name
503
+ /// Allow specifying different binary name apart from the crate name.
489
504
( unstable, different_binary_name, "" , "reference/unstable.html#different-binary-name" ) ,
490
505
491
- // Allow specifying rustflags directly in a profile
506
+ /// Allow specifying rustflags directly in a profile.
492
507
( unstable, profile_rustflags, "" , "reference/unstable.html#profile-rustflags-option" ) ,
493
508
494
- // Allow specifying rustflags directly in a profile
509
+ /// Allow workspace members to inherit fields and dependencies from a workspace.
495
510
( stable, workspace_inheritance, "1.64" , "reference/unstable.html#workspace-inheritance" ) ,
496
511
497
- // Support for 2024 edition.
512
+ / // Support for 2024 edition.
498
513
( unstable, edition2024, "" , "reference/unstable.html#edition-2024" ) ,
499
514
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.
501
516
( unstable, trim_paths, "" , "reference/unstable.html#profile-trim-paths-option" ) ,
502
517
}
503
518
519
+ /// Status and metadata for a single unstable feature.
504
520
pub struct Feature {
521
+ /// Feature name. This is valid Rust identifer so no dash only underscore.
505
522
name : & ' static str ,
506
523
stability : Status ,
524
+ /// Version that this feature was stabilized or removed.
507
525
version : & ' static str ,
526
+ /// Link to the unstable documentation.
508
527
docs : & ' static str ,
509
528
get : fn ( & Features ) -> bool ,
510
529
}
511
530
512
531
impl Features {
532
+ /// Creates a new unstable features context.
513
533
pub fn new (
514
534
features : & [ String ] ,
515
535
config : & Config ,
@@ -616,10 +636,12 @@ impl Features {
616
636
Ok ( ( ) )
617
637
}
618
638
639
+ /// Gets the current activated features.
619
640
pub fn activated ( & self ) -> & [ String ] {
620
641
& self . activated
621
642
}
622
643
644
+ /// Checks if the given feature is enabled.
623
645
pub fn require ( & self , feature : & Feature ) -> CargoResult < ( ) > {
624
646
if feature. is_enabled ( self ) {
625
647
return Ok ( ( ) ) ;
@@ -665,16 +687,20 @@ impl Features {
665
687
bail ! ( "{}" , msg) ;
666
688
}
667
689
690
+ /// Whether the given feature is allowed to use in this context.
668
691
pub fn is_enabled ( & self , feature : & Feature ) -> bool {
669
692
feature. is_enabled ( self )
670
693
}
671
694
}
672
695
696
+ /// Generates `-Z` flags as fields of [`CliUnstable`].
697
+ ///
698
+ /// See the [module-level documentation](self#-z-options) for details.
673
699
macro_rules! unstable_cli_options {
674
700
(
675
701
$(
676
702
$( #[ $meta: meta] ) ?
677
- $element: ident: $ty: ty = ( $help: expr ) ,
703
+ $element: ident: $ty: ty$ ( = ( $help: literal ) ) ? ,
678
704
) *
679
705
) => {
680
706
/// A parsed representation of all unstable flags that Cargo accepts.
@@ -686,13 +712,15 @@ macro_rules! unstable_cli_options {
686
712
#[ serde( default , rename_all = "kebab-case" ) ]
687
713
pub struct CliUnstable {
688
714
$(
715
+ $( #[ doc = $help] ) ?
689
716
$( #[ $meta] ) ?
690
717
pub $element: $ty
691
718
) ,*
692
719
}
693
720
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) ) ) ?) ) ,* ] ;
696
724
fields
697
725
}
698
726
}
@@ -721,12 +749,12 @@ macro_rules! unstable_cli_options {
721
749
722
750
unstable_cli_options ! (
723
751
// 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 ,
726
754
727
755
// All other unstable features.
728
756
// Please keep this list lexicographically ordered.
729
- advanced_env: bool = ( HIDDEN ) ,
757
+ advanced_env: bool ,
730
758
asymmetric_token: bool = ( "Allows authenticating with asymmetric tokens" ) ,
731
759
avoid_dev_deps: bool = ( "Avoid installing dev-dependencies if possible" ) ,
732
760
binary_dep_depinfo: bool = ( "Track changes to dependency artifacts" ) ,
@@ -740,24 +768,24 @@ unstable_cli_options!(
740
768
direct_minimal_versions: bool = ( "Resolve minimal dependency versions instead of maximum (direct dependencies only)" ) ,
741
769
doctest_xcompile: bool = ( "Compile and run doctests for non-host target using runner config" ) ,
742
770
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 >>,
744
772
gc: bool = ( "Track cache usage and \" garbage collect\" unused files" ) ,
745
773
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" ) ,
747
775
lints: bool = ( "Pass `[lints]` to the linting tools" ) ,
748
776
minimal_versions: bool = ( "Resolve minimal dependency versions instead of maximum" ) ,
749
777
msrv_policy: bool = ( "Enable rust-version aware policy within cargo" ) ,
750
778
mtime_on_use: bool = ( "Configure Cargo to update the mtime of used files" ) ,
751
- next_lockfile_bump: bool = ( HIDDEN ) ,
779
+ next_lockfile_bump: bool ,
752
780
no_index_update: bool = ( "Do not update the registry index even if the cache is outdated" ) ,
753
781
panic_abort_tests: bool = ( "Enable support to run tests with -Cpanic=abort" ) ,
754
782
profile_rustflags: bool = ( "Enable the `rustflags` option in profiles in .cargo/config.toml file" ) ,
755
783
publish_timeout: bool = ( "Enable the `publish.timeout` key in .cargo/config.toml file" ) ,
756
784
rustdoc_map: bool = ( "Allow passing external documentation mappings to rustdoc" ) ,
757
785
rustdoc_scrape_examples: bool = ( "Allows Rustdoc to scrape code examples from reverse-dependencies" ) ,
758
786
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 ,
761
789
target_applies_to_host: bool = ( "Enable the `target-applies-to-host` key in the .cargo/config.toml file" ) ,
762
790
trim_paths: bool = ( "Enable the `trim-paths` option in profiles" ) ,
763
791
unstable_options: bool = ( "Allow the usage of unstable options" ) ,
@@ -915,6 +943,8 @@ fn parse_gitoxide(
915
943
}
916
944
917
945
impl CliUnstable {
946
+ /// Parses `-Z` flags from the command line, and returns messages that warn
947
+ /// if any flag has alreardy been stabilized.
918
948
pub fn parse (
919
949
& mut self ,
920
950
flags : & [ String ] ,
0 commit comments