@@ -4,189 +4,208 @@ The tracking issue for this feature is: [#82450](https://github.com/rust-lang/ru
44
55------------------------
66
7- This feature allows you to enable complete or partial checking of configuration.
7+ This feature enables checking of conditional configuration.
88
99` rustc ` accepts the ` --check-cfg ` option, which specifies whether to check conditions and how to
10- check them. The ` --check-cfg ` option takes a value, called the _ check cfg specification_ . The
11- check cfg specification is parsed using the Rust metadata syntax, just as the ` --cfg ` option is.
10+ check them. The ` --check-cfg ` option takes a value, called the _ check cfg specification_ .
11+ This specification has one form:
1212
13- ` --check-cfg ` option take one form:
13+ 1 . ` --check-cfg cfg(...) ` mark a configuration and it's expected values as expected.
1414
15- 1 . ` --check-cfg cfg(...) ` enables checking the values within list-valued conditions.
16-
17- NOTE: No implicit expectation is added when using ` --cfg ` for both forms. Users are expected to
18- pass all expected names and values using ` cfg(...) ` .
15+ * No implicit expectation is added when using ` --cfg ` . Users are expected to
16+ pass all expected names and values using the _ check cfg specification_ .*
1917
2018## The ` cfg(...) ` form
2119
2220The ` cfg(...) ` form enables checking the values within list-valued conditions. It has this
2321basic form:
2422
2523``` bash
26- rustc --check-cfg ' cfg(name1, ..., nameN , values("value1", "value2", ... "valueN"))'
24+ rustc --check-cfg ' cfg(name , values("value1", "value2", ... "valueN"))'
2725```
2826
2927where ` name ` is a bare identifier (has no quotes) and each ` "value" ` term is a quoted literal
3028string. ` name ` specifies the name of the condition, such as ` feature ` or ` my_cfg ` .
3129
3230When the ` cfg(...) ` option is specified, ` rustc ` will check every ` #[cfg(name = "value")] `
3331attribute, ` #[cfg_attr(name = "value")] ` attribute, ` #[link(name = "a", cfg(name = "value"))] `
34- and ` cfg!(name = "value") ` call. It will check that the ` "value" ` specified is present in the
35- list of expected values. If ` "value" ` is not in it, then ` rustc ` will report an ` unexpected_cfgs `
36- lint diagnostic. The default diagnostic level for this lint is ` Warn ` .
32+ attribute and ` cfg!(name = "value") ` macro call. It will check that the ` "value" ` specified is
33+ present in the list of expected values. If ` "value" ` is not in it, then ` rustc ` will report an
34+ ` unexpected_cfgs ` lint diagnostic. The default diagnostic level for this lint is ` Warn ` .
3735
38- The command line ` --cfg ` arguments are currently * NOT* checked but may very well be checked in
39- the future.
36+ * The command line ` --cfg ` arguments are currently * NOT* checked but may very well be checked in
37+ the future.*
4038
41- To enable checking of values, but to provide an empty set of expected values, use these forms:
39+ To enable checking of values, but to provide an * none* /empty set of expected values
40+ (ie. expect ` #[cfg(name)] ` ), use these forms:
4241
4342``` bash
44- rustc --check-cfg ' cfg(name1, ..., nameN )'
45- rustc --check-cfg ' cfg(name1, ..., nameN , values())'
43+ rustc --check-cfg ' cfg(name )'
44+ rustc --check-cfg ' cfg(name , values())'
4645```
4746
4847To enable checking of name but not values (i.e. unknown expected values), use this form:
4948
5049``` bash
51- rustc --check-cfg ' cfg(name1, ..., nameN, values(any()))'
50+ rustc --check-cfg ' cfg(name, values(any()))'
51+ ```
52+
53+ To avoid repeating the same set of values, use this form:
54+
55+ ``` bash
56+ rustc --check-cfg ' cfg(name1, ..., nameN, values("value1", "value2", ... "valueN"))'
5257```
5358
5459The ` --check-cfg cfg(...) ` option can be repeated, both for the same condition name and for
5560different names. If it is repeated for the same condition name, then the sets of values for that
56- condition are merged together (presedence is given to ` any() ` ).
61+ condition are merged together (precedence is given to ` values( any() )` ).
5762
5863## Well known names and values
5964
6065` rustc ` has a internal list of well known names and their corresponding values.
6166Those well known names and values follows the same stability as what they refer to.
6267
63- Well known values checking is always enabled as long as a ` --check-cfg ` argument is present.
68+ Well known names and values checking is always enabled as long as at least one
69+ ` --check-cfg ` argument is present.
70+
71+ As of the ` 2023-12-22T ` the list of well known names is:
72+ - ` debug_assertions `
73+ - ` doc `
74+ - ` doctest `
75+ - ` miri `
76+ - ` overflow_checks `
77+ - ` panic `
78+ - ` proc_macro `
79+ - ` relocation_model `
80+ - ` sanitize `
81+ - ` sanitizer_cfi_generalize_pointers `
82+ - ` sanitizer_cfi_normalize_integers `
83+ - ` target_abi `
84+ - ` target_arch `
85+ - ` target_endian `
86+ - ` target_env `
87+ - ` target_family `
88+ - ` target_feature `
89+ - ` target_has_atomic `
90+ - ` target_has_atomic_equal_alignment `
91+ - ` target_has_atomic_load_store `
92+ - ` target_os `
93+ - ` target_pointer_width `
94+ - ` target_thread_local `
95+ - ` target_vendor `
96+ - ` test `
97+ - ` unix `
98+ - ` windows `
99+
100+ Like with ` values(any()) ` , well known names checking can be disabled by passing ` cfg(any()) `
101+ as argument to ` --check-cfg ` .
64102
65- Well known names checking is always enable as long as a ` --check-cfg ` argument is present
66- ** unless** any ` cfg(any()) ` argument is passed.
103+ ## Examples
67104
68- To disable checking of well known names, use this form:
105+ ### Equivalence table
69106
70- ``` bash
71- rustc --check-cfg ' cfg(any())'
72- ```
107+ This table describe the equivalence of a ` --cfg ` argument to a ` --check-cfg ` argument.
73108
74- NOTE: If one want to enable values and names checking without having any cfg to declare, one
75- can use an empty ` cfg() ` argument.
109+ | ` --cfg ` | ` --check-cfg ` |
110+ | -----------------------------| ----------------------------------------------------------|
111+ | * nothing* | * nothing* or ` --check-cfg=cfg() ` (to enable the checking) |
112+ | ` --cfg foo ` | ` --check-cfg=cfg(foo) or --check-cfg=cfg(foo, values()) ` |
113+ | ` --cfg foo="" ` | ` --check-cfg=cfg(foo, values("")) ` |
114+ | ` --cfg foo="bar" ` | ` --check-cfg=cfg(foo, values("bar")) ` |
115+ | ` --cfg foo="1" --cfg foo="2" ` | ` --check-cfg=cfg(foo, values("1", "2")) ` |
116+ | ` --cfg foo="1" --cfg bar="2" ` | ` --check-cfg=cfg(foo, values("1")) --check-cfg=cfg(bar, values("2")) ` |
117+ | ` --cfg foo --cfg foo="bar" ` | ` --check-cfg=cfg(foo) --check-cfg=cfg(foo, values("bar")) ` |
76118
77- ## Examples
119+ NOTE: There is (currently) no way to express that a condition name is expected but no (!= none)
120+ values are expected. Passing an empty ` values() ` means * (none)* in the sense of ` #[cfg(foo)] `
121+ with no value. Users are expected to NOT pass a ` --check-cfg ` with that condition name.
122+
123+ ### Example: Cargo-like ` feature ` example
78124
79125Consider this command line:
80126
81127``` bash
82128rustc --check-cfg ' cfg(feature, values("lion", "zebra"))' \
83- --cfg ' feature="lion"' -Z unstable-options \
84- example.rs
129+ --cfg ' feature="lion"' -Z unstable-options example.rs
85130```
86131
87132This command line indicates that this crate has two features: ` lion ` and ` zebra ` . The ` lion `
88- feature is enabled, while the ` zebra ` feature is disabled. Exhaustive checking of names and
89- values are enabled by default. Consider compiling this code:
133+ feature is enabled, while the ` zebra ` feature is disabled.
134+ Given the ` --check-cfg ` arguments, exhaustive checking of names and
135+ values are enabled.
90136
137+ ` example.rs ` :
91138``` rust
92- // This is expected, and tame_lion() will be compiled
93- #[cfg(feature = " lion" )]
139+ #[cfg(feature = " lion" )] // This condition is expected, as "lion" is an expected value of `feature`
94140fn tame_lion (lion : Lion ) {}
95141
96- // This is expected, and ride_zebra() will NOT be compiled.
97- #[cfg(feature = " zebra" )]
98- fn ride_zebra (zebra : Zebra ) {}
142+ #[cfg(feature = " zebra" )] // This condition is expected, as "zebra" is an expected value of `feature`
143+ // but the condition will still evaluate to false
144+ // since only --cfg feature="lion" was passed
145+ fn ride_zebra (z : Zebra ) {}
99146
100- // This is UNEXPECTED, and will cause a compiler warning (by default).
101- #[cfg( feature = " platypus " )]
147+ #[cfg(feature = " platypus " )] // This condition is UNEXPECTED, as "platypus" is NOT an expected value of
148+ // ` feature` and will cause a compiler warning (by default).
102149fn poke_platypus () {}
103150
104- // This is UNEXPECTED, because 'feechure' is not a known condition name,
105- // and will cause a compiler warning (by default).
106- #[cfg(feechure = " lion" )]
151+ #[cfg(feechure = " lion" )] // This condition is UNEXPECTED, as 'feechure' is NOT a expected condition
152+ // name, no `cfg(feechure, ...)` was passed in `--check-cfg`
107153fn tame_lion () {}
108154
109- // This is UNEXPECTED, because 'windows' is a well known condition name,
110- // and because 'windows' doesn't take any values,
111- // and will cause a compiler warning (by default).
112- #[cfg(windows = " unix" )]
155+ #[cfg(windows = " unix" )] // This condition is UNEXPECTED, as while 'windows' is a well known
156+ // condition name, it doens't expect any values
113157fn tame_windows () {}
114158```
115159
116- ### Example: Checking condition names, but not values
160+ ### Example: Multiple names and values
117161
118162``` bash
119- # This turns on checking for condition names, but not values, such as 'feature' values.
120- rustc --check-cfg ' cfg(is_embedded, has_feathers, values(any() ))' \
121- --cfg has_feathers -Z unstable-options
163+ rustc --check-cfg ' cfg(is_embedded, has_feathers) ' \
164+ --check-cfg ' cfg(feature, values("zapping", "lasers" ))' \
165+ --cfg has_feathers --cfg ' feature="zapping" ' - Z unstable-options
122166```
123167
124168``` rust
125- #[cfg(is_embedded)] // This is expected as "is_embedded" was provided in cfg()
126- fn do_embedded () {} // and because names exhaustiveness was not disabled
127-
128- #[cfg(has_feathers)] // This is expected as "has_feathers" was provided in cfg()
129- fn do_features () {} // and because names exhaustiveness was not disabled
169+ #[cfg(is_embedded)] // This condition is expected, as 'is_embedded' was provided in --check-cfg
170+ fn do_embedded () {} // and doesn't take any value
130171
131- #[cfg(has_feathers = " zapping" )] // This is expected as "has_feathers" was provided in cfg()
132- // and because no value checking was enable for "has_feathers"
133- // no warning is emitted for the value "zapping"
134- fn do_zapping () {}
172+ #[cfg(has_feathers)] // This condition is expected, as 'has_feathers' was provided in --check-cfg
173+ fn do_features () {} // and doesn't take any value
135174
136- #[cfg(has_mumble_frotz)] // This is UNEXPECTED because names checking is enable and
137- // "has_mumble_frotz" was not provided in cfg()
175+ #[cfg(has_mumble_frotz)] // This condition is UNEXPECTED, as 'has_mumble_frotz' was NEVER provided
176+ // in any --check- cfg arguments
138177fn do_mumble_frotz () {}
139- ```
140-
141- ### Example: Checking feature values, but not condition names
142178
143- ``` bash
144- # This turns on checking for feature values, but not for condition names.
145- rustc --check-cfg ' cfg(feature, values("zapping", "lasers"))' \
146- --check-cfg ' cfg(any())' \
147- --cfg ' feature="zapping"' -Z unstable-options
148- ```
149-
150- ``` rust
151- #[cfg(is_embedded)] // This is doesn't raise a warning, because names checking was
152- // disabled by 'cfg(any())'
153- fn do_embedded () {}
154-
155- #[cfg(has_feathers)] // Same as above, 'cfg(any())' was provided so no name
156- // checking is performed
157- fn do_features () {}
158-
159- #[cfg(feature = " lasers" )] // This is expected, "lasers" is in the cfg(feature) list
179+ #[cfg(feature = " lasers" )] // This condition is expected, as "lasers" is an expected value of `feature`
160180fn shoot_lasers () {}
161181
162- #[cfg(feature = " monkeys" )] // This is UNEXPECTED, because "monkeys" is not in the
163- // cfg( feature) list
182+ #[cfg(feature = " monkeys" )] // This condition is UNEXPECTED, as "monkeys" is NOT an expected value of
183+ // ` feature`
164184fn write_shakespeare () {}
165185```
166186
167- ### Example: Checking both condition names and feature values
187+ ### Example: Condition names without values
168188
169189``` bash
170- # This turns on checking for feature values and for condition names.
171- rustc --check-cfg ' cfg(is_embedded, has_feathers)' \
172- --check-cfg ' cfg(feature, values("zapping", "lasers"))' \
173- --cfg has_feathers --cfg ' feature="zapping"' -Z unstable-options
190+ rustc --check-cfg ' cfg(is_embedded, has_feathers, values(any()))' \
191+ --cfg has_feathers -Z unstable-options
174192```
175193
176194``` rust
177- #[cfg(is_embedded)] // This is expected because "is_embedded" was provided in cfg()
178- fn do_embedded () {} // and doesn't take any value
179-
180- #[cfg(has_feathers)] // This is expected because "has_feathers" was provided in cfg()
181- fn do_features () {} // and deosn't take any value
195+ #[cfg(is_embedded)] // This condition is expected, as 'is_embedded' was provided in --check-cfg
196+ // as condition name
197+ fn do_embedded () {}
182198
183- #[cfg(has_mumble_frotz)] // This is UNEXPECTED, because "has_mumble_frotz" was never provided
184- fn do_mumble_frotz () {}
199+ #[cfg(has_feathers)] // This condition is expected, as "has_feathers" was provided in --check-cfg
200+ // as condition name
201+ fn do_features () {}
185202
186- #[cfg(feature = " lasers" )] // This is expected, "lasers" is in the cfg(feature) list
187- fn shoot_lasers () {}
203+ #[cfg(has_feathers = " zapping" )] // This condition is expected, as "has_feathers" was provided in
204+ // and because *any* values is expected for 'has_feathers' no
205+ // warning is emitted for the value "zapping"
206+ fn do_zapping () {}
188207
189- #[cfg(feature = " monkeys " )] // This is UNEXPECTED, because "monkeys" is not in
190- // the cfg(feature) list
191- fn write_shakespeare () {}
208+ #[cfg(has_mumble_frotz )] // This condition is UNEXPECTED, as 'has_mumble_frotz' was not provided
209+ // in any --check- cfg arguments
210+ fn do_mumble_frotz () {}
192211```
0 commit comments