Skip to content

Commit 5c9a8d7

Browse files
Rollup merge of #119282 - Urgau:check-cfg-rework-unstable-doc, r=JohnTitor
Rework and improve the unstable documentation of check-cfg This PR rework and improve the unstable documentation of the check-cfg feature. The goal is to have a simpler to understand documentation with examples that are more practical and less theoretical. As well as making the documentation more explicit about the whereabouts of the feature. `@rustbot` label +T-compiler +F-check-cfg
2 parents df2f4ae + 3611014 commit 5c9a8d7

File tree

1 file changed

+123
-101
lines changed

1 file changed

+123
-101
lines changed

src/doc/unstable-book/src/compiler-flags/check-cfg.md

+123-101
Original file line numberDiff line numberDiff line change
@@ -4,189 +4,211 @@ 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

2220
The `cfg(...)` form enables checking the values within list-valued conditions. It has this
2321
basic 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

2927
where `name` is a bare identifier (has no quotes) and each `"value"` term is a quoted literal
3028
string. `name` specifies the name of the condition, such as `feature` or `my_cfg`.
3129

3230
When the `cfg(...)` option is specified, `rustc` will check every `#[cfg(name = "value")]`
3331
attribute, `#[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

4847
To 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

5459
The `--check-cfg cfg(...)` option can be repeated, both for the same condition name and for
5560
different 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.
6166
Those 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 `2024-01-09T`, the list of known names is as follows:
72+
73+
<!--- See CheckCfg::fill_well_known in compiler/rustc_session/src/config.rs -->
74+
75+
- `debug_assertions`
76+
- `doc`
77+
- `doctest`
78+
- `miri`
79+
- `overflow_checks`
80+
- `panic`
81+
- `proc_macro`
82+
- `relocation_model`
83+
- `sanitize`
84+
- `sanitizer_cfi_generalize_pointers`
85+
- `sanitizer_cfi_normalize_integers`
86+
- `target_abi`
87+
- `target_arch`
88+
- `target_endian`
89+
- `target_env`
90+
- `target_family`
91+
- `target_feature`
92+
- `target_has_atomic`
93+
- `target_has_atomic_equal_alignment`
94+
- `target_has_atomic_load_store`
95+
- `target_os`
96+
- `target_pointer_width`
97+
- `target_thread_local`
98+
- `target_vendor`
99+
- `test`
100+
- `unix`
101+
- `windows`
102+
103+
Like with `values(any())`, well known names checking can be disabled by passing `cfg(any())`
104+
as argument to `--check-cfg`.
64105

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.
106+
## Examples
67107

68-
To disable checking of well known names, use this form:
108+
### Equivalence table
69109

70-
```bash
71-
rustc --check-cfg 'cfg(any())'
72-
```
110+
This table describe the equivalence of a `--cfg` argument to a `--check-cfg` argument.
73111

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.
112+
| `--cfg` | `--check-cfg` |
113+
|-----------------------------|----------------------------------------------------------|
114+
| *nothing* | *nothing* or `--check-cfg=cfg()` (to enable the checking) |
115+
| `--cfg foo` | `--check-cfg=cfg(foo) or --check-cfg=cfg(foo, values())` |
116+
| `--cfg foo=""` | `--check-cfg=cfg(foo, values(""))` |
117+
| `--cfg foo="bar"` | `--check-cfg=cfg(foo, values("bar"))` |
118+
| `--cfg foo="1" --cfg foo="2"` | `--check-cfg=cfg(foo, values("1", "2"))` |
119+
| `--cfg foo="1" --cfg bar="2"` | `--check-cfg=cfg(foo, values("1")) --check-cfg=cfg(bar, values("2"))` |
120+
| `--cfg foo --cfg foo="bar"` | `--check-cfg=cfg(foo) --check-cfg=cfg(foo, values("bar"))` |
76121

77-
## Examples
122+
NOTE: There is (currently) no way to express that a condition name is expected but no (!= none)
123+
values are expected. Passing an empty `values()` means *(none)* in the sense of `#[cfg(foo)]`
124+
with no value. Users are expected to NOT pass a `--check-cfg` with that condition name.
125+
126+
### Example: Cargo-like `feature` example
78127

79128
Consider this command line:
80129

81130
```bash
82131
rustc --check-cfg 'cfg(feature, values("lion", "zebra"))' \
83-
--cfg 'feature="lion"' -Z unstable-options \
84-
example.rs
132+
--cfg 'feature="lion"' -Z unstable-options example.rs
85133
```
86134

87135
This 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:
136+
feature is enabled, while the `zebra` feature is disabled.
137+
Given the `--check-cfg` arguments, exhaustive checking of names and
138+
values are enabled.
90139

140+
`example.rs`:
91141
```rust
92-
// This is expected, and tame_lion() will be compiled
93-
#[cfg(feature = "lion")]
142+
#[cfg(feature = "lion")] // This condition is expected, as "lion" is an expected value of `feature`
94143
fn tame_lion(lion: Lion) {}
95144

96-
// This is expected, and ride_zebra() will NOT be compiled.
97-
#[cfg(feature = "zebra")]
98-
fn ride_zebra(zebra: Zebra) {}
145+
#[cfg(feature = "zebra")] // This condition is expected, as "zebra" is an expected value of `feature`
146+
// but the condition will still evaluate to false
147+
// since only --cfg feature="lion" was passed
148+
fn ride_zebra(z: Zebra) {}
99149

100-
// This is UNEXPECTED, and will cause a compiler warning (by default).
101-
#[cfg(feature = "platypus")]
150+
#[cfg(feature = "platypus")] // This condition is UNEXPECTED, as "platypus" is NOT an expected value of
151+
// `feature` and will cause a compiler warning (by default).
102152
fn poke_platypus() {}
103153

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")]
154+
#[cfg(feechure = "lion")] // This condition is UNEXPECTED, as 'feechure' is NOT a expected condition
155+
// name, no `cfg(feechure, ...)` was passed in `--check-cfg`
107156
fn tame_lion() {}
108157

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")]
158+
#[cfg(windows = "unix")] // This condition is UNEXPECTED, as while 'windows' is a well known
159+
// condition name, it doens't expect any values
113160
fn tame_windows() {}
114161
```
115162

116-
### Example: Checking condition names, but not values
163+
### Example: Multiple names and values
117164

118165
```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
166+
rustc --check-cfg 'cfg(is_embedded, has_feathers)' \
167+
--check-cfg 'cfg(feature, values("zapping", "lasers"))' \
168+
--cfg has_feathers --cfg 'feature="zapping"' -Z unstable-options
122169
```
123170

124171
```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
172+
#[cfg(is_embedded)] // This condition is expected, as 'is_embedded' was provided in --check-cfg
173+
fn do_embedded() {} // and doesn't take any value
130174

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() {}
175+
#[cfg(has_feathers)] // This condition is expected, as 'has_feathers' was provided in --check-cfg
176+
fn do_features() {} // and doesn't take any value
135177

136-
#[cfg(has_mumble_frotz)] // This is UNEXPECTED because names checking is enable and
137-
// "has_mumble_frotz" was not provided in cfg()
178+
#[cfg(has_mumble_frotz)] // This condition is UNEXPECTED, as 'has_mumble_frotz' was NEVER provided
179+
// in any --check-cfg arguments
138180
fn do_mumble_frotz() {}
139-
```
140-
141-
### Example: Checking feature values, but not condition names
142181

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
182+
#[cfg(feature = "lasers")] // This condition is expected, as "lasers" is an expected value of `feature`
160183
fn shoot_lasers() {}
161184

162-
#[cfg(feature = "monkeys")] // This is UNEXPECTED, because "monkeys" is not in the
163-
// cfg(feature) list
185+
#[cfg(feature = "monkeys")] // This condition is UNEXPECTED, as "monkeys" is NOT an expected value of
186+
// `feature`
164187
fn write_shakespeare() {}
165188
```
166189

167-
### Example: Checking both condition names and feature values
190+
### Example: Condition names without values
168191

169192
```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
193+
rustc --check-cfg 'cfg(is_embedded, has_feathers, values(any()))' \
194+
--cfg has_feathers -Z unstable-options
174195
```
175196

176197
```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
198+
#[cfg(is_embedded)] // This condition is expected, as 'is_embedded' was provided in --check-cfg
199+
// as condition name
200+
fn do_embedded() {}
182201

183-
#[cfg(has_mumble_frotz)] // This is UNEXPECTED, because "has_mumble_frotz" was never provided
184-
fn do_mumble_frotz() {}
202+
#[cfg(has_feathers)] // This condition is expected, as "has_feathers" was provided in --check-cfg
203+
// as condition name
204+
fn do_features() {}
185205

186-
#[cfg(feature = "lasers")] // This is expected, "lasers" is in the cfg(feature) list
187-
fn shoot_lasers() {}
206+
#[cfg(has_feathers = "zapping")] // This condition is expected, as "has_feathers" was provided in
207+
// and because *any* values is expected for 'has_feathers' no
208+
// warning is emitted for the value "zapping"
209+
fn do_zapping() {}
188210

189-
#[cfg(feature = "monkeys")] // This is UNEXPECTED, because "monkeys" is not in
190-
// the cfg(feature) list
191-
fn write_shakespeare() {}
211+
#[cfg(has_mumble_frotz)] // This condition is UNEXPECTED, as 'has_mumble_frotz' was not provided
212+
// in any --check-cfg arguments
213+
fn do_mumble_frotz() {}
192214
```

0 commit comments

Comments
 (0)