Skip to content

Commit ce652db

Browse files
committed
Auto merge of rust-lang#124209 - Urgau:check-cfg-more-friendly-docs, r=ehuss
Make check-cfg docs more user-friendly This PR improves the `--check-cfg` to make them more user-friendly by: - explaining the purpose of the feature - removing the "form" jargon - making it (bit) less formal and more "friendly" - making the doc less cluttered - and by fixing (the width and flags) of the examples `@rustbot` label +F-check-cfg
2 parents 3111015 + a7a9793 commit ce652db

File tree

2 files changed

+97
-62
lines changed

2 files changed

+97
-62
lines changed

src/doc/rustc/src/check-cfg.md

+89-60
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
11
# Checking conditional configurations
22

3-
`rustc` accepts the `--check-cfg` option, which specifies whether to check conditions and how to
4-
check them. The `--check-cfg` option takes a value, called the _check cfg specification_.
5-
This specification has one form:
3+
`rustc` supports checking that every _reachable_[^reachable] `#[cfg]` matches a list of the
4+
expected config names and values.
65

7-
1. `--check-cfg cfg(...)` mark a configuration and it's expected values as expected.
6+
This can help with verifying that the crate is correctly handling conditional compilation for
7+
different target platforms or features. It ensures that the cfg settings are consistent between
8+
what is intended and what is used, helping to catch potential bugs or errors early in the
9+
development process.
810

9-
*No implicit expectation is added when using `--cfg`. Users are expected to
10-
pass all expected names and values using the _check cfg specification_.*
11+
In order to accomplish that goal, `rustc` accepts the `--check-cfg` flag, which specifies
12+
whether to check conditions and how to check them.
1113

12-
## The `cfg(...)` form
14+
> **Note:** No implicit expectation is added when using `--cfg`. Users are expected to
15+
pass all expected names and values using the _check cfg specification_.
1316

14-
The `cfg(...)` form enables checking the values within list-valued conditions. It has this
15-
basic form:
17+
[^reachable]: `rustc` promises to at least check reachable `#[cfg]`, and while non-reachable
18+
`#[cfg]` are not currently checked, they may well be checked in the future without it being a
19+
breaking change.
20+
21+
## Specifying expected names and values
22+
23+
To specify expected names and values, the _check cfg specification_ provides the `cfg(...)`
24+
option which enables specifying for an expected config name and it's expected values.
25+
26+
It has this basic form:
1627

1728
```bash
1829
rustc --check-cfg 'cfg(name, values("value1", "value2", ... "valueN"))'
1930
```
2031

2132
where `name` is a bare identifier (has no quotes) and each `"value"` term is a quoted literal
2233
string. `name` specifies the name of the condition, such as `feature` or `my_cfg`.
34+
`"value"` specify one of the value of that condition name.
35+
36+
When the `cfg(...)` option is specified, `rustc` will check every[^reachable]:
37+
- `#[cfg(name = "value")]` attribute
38+
- `#[cfg_attr(name = "value")]` attribute
39+
- `#[link(name = "a", cfg(name = "value"))]` attribute
40+
- `cfg!(name = "value")` macro call
2341

24-
When the `cfg(...)` option is specified, `rustc` will check every `#[cfg(name = "value")]`
25-
attribute, `#[cfg_attr(name = "value")]` attribute, `#[link(name = "a", cfg(name = "value"))]`
26-
attribute and `cfg!(name = "value")` macro call. It will check that the `"value"` specified is
27-
present in the list of expected values. If `"value"` is not in it, then `rustc` will report an
28-
`unexpected_cfgs` lint diagnostic. The default diagnostic level for this lint is `Warn`.
42+
> *The command line `--cfg` arguments are currently NOT checked but may very well be checked
43+
in the future.*
2944

30-
*The command line `--cfg` arguments are currently *NOT* checked but may very well be checked in
31-
the future.*
45+
`rustc` will check that the `"value"` specified is present in the list of expected values.
46+
If `"value"` is not in it, then `rustc` will report an `unexpected_cfgs` lint diagnostic.
47+
The default diagnostic level for this lint is `Warn`.
3248

3349
To check for the _none_ value (ie `#[cfg(foo)]`) one can use the `none()` predicate inside
3450
`values()`: `values(none())`. It can be followed or preceded by any number of `"value"`.
@@ -43,12 +59,12 @@ rustc --check-cfg 'cfg(name, values(none()))'
4359

4460
To enable checking of name but not values, use one of these forms:
4561

46-
- No expected values (_will lint on every value_):
62+
- No expected values (_will lint on every value of `name`_):
4763
```bash
4864
rustc --check-cfg 'cfg(name, values())'
4965
```
5066

51-
- Unknown expected values (_will never lint_):
67+
- Unknown expected values (_will never lint on value of `name`_):
5268
```bash
5369
rustc --check-cfg 'cfg(name, values(any()))'
5470
```
@@ -59,17 +75,26 @@ To avoid repeating the same set of values, use this form:
5975
rustc --check-cfg 'cfg(name1, ..., nameN, values("value1", "value2", ... "valueN"))'
6076
```
6177

78+
To enable checking without specifying any names or values, use this form:
79+
80+
```bash
81+
rustc --check-cfg 'cfg()'
82+
```
83+
6284
The `--check-cfg cfg(...)` option can be repeated, both for the same condition name and for
6385
different names. If it is repeated for the same condition name, then the sets of values for that
6486
condition are merged together (precedence is given to `values(any())`).
6587
88+
> To help out an equivalence table between `--cfg` arguments and `--check-cfg` is available
89+
[down below](#equivalence-table-with---cfg).
90+
6691
## Well known names and values
6792
68-
`rustc` has a internal list of well known names and their corresponding values.
69-
Those well known names and values follows the same stability as what they refer to.
93+
`rustc` maintains a list of well-known names and their corresponding values in order to avoid
94+
the need to specify them manually.
7095
71-
Well known names and values checking is always enabled as long as at least one
72-
`--check-cfg` argument is present.
96+
Well known names and values are implicitly added as long as at least one `--check-cfg` argument
97+
is present.
7398
7499
As of `2024-05-06T`, the list of known names is as follows:
75100
@@ -109,11 +134,9 @@ As of `2024-05-06T`, the list of known names is as follows:
109134
Like with `values(any())`, well known names checking can be disabled by passing `cfg(any())`
110135
as argument to `--check-cfg`.
111136
112-
## Examples
113-
114-
### Equivalence table
137+
## Equivalence table with `--cfg`
115138
116-
This table describe the equivalence of a `--cfg` argument to a `--check-cfg` argument.
139+
This table describe the equivalence between a `--cfg` argument to a `--check-cfg` argument.
117140
118141
| `--cfg` | `--check-cfg` |
119142
|-------------------------------|------------------------------------------------------------|
@@ -125,40 +148,42 @@ This table describe the equivalence of a `--cfg` argument to a `--check-cfg` arg
125148
| `--cfg foo="1" --cfg bar="2"` | `--check-cfg=cfg(foo, values("1")) --check-cfg=cfg(bar, values("2"))` |
126149
| `--cfg foo --cfg foo="bar"` | `--check-cfg=cfg(foo, values(none(), "bar"))` |
127150
151+
## Examples
152+
128153
### Example: Cargo-like `feature` example
129154
130155
Consider this command line:
131156
132157
```bash
133158
rustc --check-cfg 'cfg(feature, values("lion", "zebra"))' \
134-
--cfg 'feature="lion"' -Z unstable-options example.rs
159+
--cfg 'feature="lion"' example.rs
135160
```
136161
137-
This command line indicates that this crate has two features: `lion` and `zebra`. The `lion`
162+
> This command line indicates that this crate has two features: `lion` and `zebra`. The `lion`
138163
feature is enabled, while the `zebra` feature is disabled.
139-
Given the `--check-cfg` arguments, exhaustive checking of names and
140-
values are enabled.
141164
142-
`example.rs`:
143165
```rust
144-
#[cfg(feature = "lion")] // This condition is expected, as "lion" is an expected value of `feature`
166+
#[cfg(feature = "lion")] // This condition is expected, as "lion" is an
167+
// expected value of `feature`
145168
fn tame_lion(lion: Lion) {}
146169
147-
#[cfg(feature = "zebra")] // This condition is expected, as "zebra" is an expected value of `feature`
148-
// but the condition will still evaluate to false
149-
// since only --cfg feature="lion" was passed
170+
#[cfg(feature = "zebra")] // This condition is expected, as "zebra" is an expected
171+
// value of `feature` but the condition will evaluate
172+
// to false since only --cfg feature="lion" was passed
150173
fn ride_zebra(z: Zebra) {}
151174
152-
#[cfg(feature = "platypus")] // This condition is UNEXPECTED, as "platypus" is NOT an expected value of
153-
// `feature` and will cause a compiler warning (by default).
175+
#[cfg(feature = "platypus")] // This condition is UNEXPECTED, as "platypus" is NOT
176+
// an expected value of `feature` and will cause a
177+
// the compiler to emit the `unexpected_cfgs` lint
154178
fn poke_platypus() {}
155179
156-
#[cfg(feechure = "lion")] // This condition is UNEXPECTED, as 'feechure' is NOT a expected condition
157-
// name, no `cfg(feechure, ...)` was passed in `--check-cfg`
180+
#[cfg(feechure = "lion")] // This condition is UNEXPECTED, as 'feechure' is NOT
181+
// a expected condition name, no `cfg(feechure, ...)`
182+
// was passed in `--check-cfg`
158183
fn tame_lion() {}
159184
160-
#[cfg(windows = "unix")] // This condition is UNEXPECTED, as while 'windows' is a well known
161-
// condition name, it doesn't expect any values
185+
#[cfg(windows = "unix")] // This condition is UNEXPECTED, as the well known
186+
// 'windows' cfg doesn't expect any values
162187
fn tame_windows() {}
163188
```
164189
@@ -167,50 +192,54 @@ fn tame_windows() {}
167192
```bash
168193
rustc --check-cfg 'cfg(is_embedded, has_feathers)' \
169194
--check-cfg 'cfg(feature, values("zapping", "lasers"))' \
170-
--cfg has_feathers --cfg 'feature="zapping"' -Z unstable-options
195+
--cfg has_feathers --cfg 'feature="zapping"'
171196
```
172197
173198
```rust
174-
#[cfg(is_embedded)] // This condition is expected, as 'is_embedded' was provided in --check-cfg
175-
fn do_embedded() {} // and doesn't take any value
199+
#[cfg(is_embedded)] // This condition is expected, as 'is_embedded' was
200+
// provided in --check-cfg and doesn't take any value
201+
fn do_embedded() {}
176202
177-
#[cfg(has_feathers)] // This condition is expected, as 'has_feathers' was provided in --check-cfg
178-
fn do_features() {} // and doesn't take any value
203+
#[cfg(has_feathers)] // This condition is expected, as 'has_feathers' was
204+
// provided in --check-cfg and doesn't take any value
205+
fn do_features() {}
179206
180-
#[cfg(has_mumble_frotz)] // This condition is UNEXPECTED, as 'has_mumble_frotz' was NEVER provided
181-
// in any --check-cfg arguments
207+
#[cfg(has_mumble_frotz)] // This condition is UNEXPECTED, as 'has_mumble_frotz'
208+
// was NEVER provided in any --check-cfg arguments
182209
fn do_mumble_frotz() {}
183210
184-
#[cfg(feature = "lasers")] // This condition is expected, as "lasers" is an expected value of `feature`
211+
#[cfg(feature = "lasers")] // This condition is expected, as "lasers" is an
212+
// expected value of `feature`
185213
fn shoot_lasers() {}
186214
187-
#[cfg(feature = "monkeys")] // This condition is UNEXPECTED, as "monkeys" is NOT an expected value of
188-
// `feature`
215+
#[cfg(feature = "monkeys")] // This condition is UNEXPECTED, as "monkeys" is NOT
216+
// an expected value of `feature`
189217
fn write_shakespeare() {}
190218
```
191219
192220
### Example: Condition names without values
193221
194222
```bash
195223
rustc --check-cfg 'cfg(is_embedded, has_feathers, values(any()))' \
196-
--cfg has_feathers -Z unstable-options
224+
--cfg has_feathers
197225
```
198226
199227
```rust
200-
#[cfg(is_embedded)] // This condition is expected, as 'is_embedded' was provided in --check-cfg
201-
// as condition name
228+
#[cfg(is_embedded)] // This condition is expected, as 'is_embedded' was
229+
// provided in --check-cfg as condition name
202230
fn do_embedded() {}
203231
204-
#[cfg(has_feathers)] // This condition is expected, as "has_feathers" was provided in --check-cfg
205-
// as condition name
232+
#[cfg(has_feathers)] // This condition is expected, as "has_feathers" was
233+
// provided in --check-cfg as condition name
206234
fn do_features() {}
207235
208-
#[cfg(has_feathers = "zapping")] // This condition is expected, as "has_feathers" was provided in
209-
// and because *any* values is expected for 'has_feathers' no
236+
#[cfg(has_feathers = "zapping")] // This condition is expected, as "has_feathers"
237+
// was provided and because *any* values is
238+
// expected for 'has_feathers' no
210239
// warning is emitted for the value "zapping"
211240
fn do_zapping() {}
212241
213-
#[cfg(has_mumble_frotz)] // This condition is UNEXPECTED, as 'has_mumble_frotz' was not provided
214-
// in any --check-cfg arguments
242+
#[cfg(has_mumble_frotz)] // This condition is UNEXPECTED, as 'has_mumble_frotz'
243+
// was not provided in any --check-cfg arguments
215244
fn do_mumble_frotz() {}
216245
```

src/doc/rustc/src/command-line-arguments.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ For examples, `--cfg 'verbose'` or `--cfg 'feature="serde"'`. These correspond
1919
to `#[cfg(verbose)]` and `#[cfg(feature = "serde")]` respectively.
2020

2121
<a id="option-check-cfg"></a>
22-
## `--check-cfg`: enables checking conditional configurations
22+
## `--check-cfg`: configure compile-time checking of conditional compilation
23+
24+
This flag enables checking conditional configurations of the crate at compile-time,
25+
specifically it helps configure the set of expected cfg names and values, in order
26+
to check that every _reachable_ `#[cfg]` matches the expected config names and values.
27+
28+
This is different from the `--cfg` flag above which activates some config but do
29+
not expect them. This is useful to prevent stalled conditions, typos, ...
2330

24-
This flag will enable checking conditional configurations.
2531
Refer to the [Checking conditional configurations](check-cfg.md) of this book
2632
for further details and explanation.
2733

0 commit comments

Comments
 (0)