Skip to content

Commit dbc02dc

Browse files
committed
Address PR feedback
1 parent 1788dd4 commit dbc02dc

File tree

1 file changed

+57
-6
lines changed

1 file changed

+57
-6
lines changed

text/0000-conditional-compilation-checking.md

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ Each `--check-cfg` option can take one of two forms:
273273

274274
### The `names(...)` form
275275

276-
277276
This form uses a named metadata list:
278277

279278
```bash
@@ -302,9 +301,32 @@ form. The parentheses are required.
302301
rustc --check-cfg 'names()'
303302
```
304303

304+
Note that `--check-cfg 'names()'` is _not_ equivalent to omitting the option entirely.
305+
The first form enables checking condition names, while specifying that there are no valid
306+
condition names (outside of the set of well-known names defined by `rustc`). Omitting the
307+
`--check-cfg 'names(...)'` option does not enable checking condition names.
308+
305309
Conditions that are enabled are implicitly valid; it is unnecessary (but legal) to specify a
306310
condition name as both enabled and valid. For example, the following invocations are equivalent:
307311

312+
```bash
313+
# condition names will be checked, and 'has_time_travel' is valid
314+
rustc --cfg 'has_time_travel' --check-cfg 'names()'
315+
316+
# condition names will be checked, and 'has_time_travel' is valid
317+
rustc --cfg 'has_time_travel' --check-cfg 'names(has_time_travel)'
318+
```
319+
320+
In contrast, the following two invocations are _not_ equivalent:
321+
322+
```bash
323+
# condition names will not be checked (because there is no --check-cfg names(...))
324+
rustc --cfg 'has_time_travel'
325+
326+
# condition names will be checked, and 'has_time_travel' is both valid and enabled.
327+
rustc --cfg 'has_time_travel' --check-cfg 'names(has_time_travel)'
328+
```
329+
308330
### The `values(...)` form
309331

310332
The `values(...)` form enables checking the values within list-valued conditions. It has this
@@ -320,7 +342,7 @@ string. `name` specifies the name of the condition, such as `feature` or `target
320342
When the `values(...)` option is specified, `rustc` will check every `#[cfg(name = "value")]`
321343
attribute, `#[cfg_attr(name = "value")]` attribute, and `cfg!(name = "value")` call. It will
322344
check that the `"value"` specified is present in the list of valid values. If `"value"` is not
323-
valid, then `rustc` will report an `invalid_cfg_name` lint diagnostic. The default diagnostic
345+
valid, then `rustc` will report an `invalid_cfg_value` lint diagnostic. The default diagnostic
324346
level for this lint is `Warn`.
325347
326348
The form `values()` is an error, because it does not specify a condition name.
@@ -354,19 +376,21 @@ rustc --check-cfg 'values(animals, "lion", "zebra")'
354376
355377
This is intended to give tool developers more flexibility when generating Rustc command lines.
356378
357-
### Enabled conditions are implicitly valid
379+
### Enabled condition names are implicitly valid
358380
359381
Specifying an enabled condition name implicitly makes it valid. For example, the following
360382
invocations are equivalent:
361383
362384
```bash
363385
# legal but redundant:
364-
rustc --check-cfg 'values(animals, "lion", "zebra")' --cfg 'animals = "lion"'
386+
rustc --check-cfg 'names(animals)' --cfg 'animals = "lion"'
365387
366388
# equivalent:
367-
rustc --check-cfg 'values(animals, "zebra")' --cfg 'animals = "lion"'
389+
rustc --check-cfg 'names()' --cfg 'animals = "lion"'
368390
```
369391
392+
### Enabled condition values are implicitly valid
393+
370394
Specifying an enabled condition _value_ implicitly makes that _value_ valid. For example, the
371395
following invocations are equivalent:
372396
@@ -389,6 +413,32 @@ rustc --check-cfg 'names(other, animals)' --check-cfg 'values(animals, "lion")'
389413
rustc --check-cfg 'names(other)' --check-cfg 'values(animals, "lion")'
390414
```
391415
416+
### Checking condition names and values is independent
417+
418+
Checking condition names may be enabled independently of checking condition values.
419+
If checking of condition values is enabled, then it is enabled separately for each condition name.
420+
421+
Examples:
422+
423+
```bash
424+
425+
# no checking is performed
426+
rustc
427+
428+
# names are checked, but values are not checked
429+
rustc --check-cfg 'names(has_time_travel)'
430+
431+
# names are not checked, but 'feature' values are checked.
432+
# note that #[cfg(market = "...")] values are not checked.
433+
rustc --check-cfg 'values(feature, "lighting", "bump_maps")'
434+
435+
# names are not checked, but 'feature' values _and_ 'market' values are checked.
436+
rustc --check-cfg 'values(feature, "lighting", "bump_maps")' --check-cfg 'markets(feature = "europe", "asia")'
437+
438+
# names _and_ feature values are checked.
439+
rustc --check-cfg 'names(has_time_travel)' --check-cfg 'values(feature, "lighting", "bump_maps")'
440+
```
441+
392442
## Stabilizing
393443
394444
Until this feature is stabilized, it can only be used with a `nightly` compiler, and only when
@@ -458,7 +508,8 @@ fn tame_lion() { ... }
458508
459509
## Drawbacks
460510
461-
* Adds complexity, in the form of additional command-line options.
511+
* Adds complexity, in the form of additional command-line options. Fortunately, this is
512+
complexity that will be mainly be exposed to build systems, such as Cargo.
462513
* As with all lints, correct code may be trigger lints. Developers will need to take time to
463514
examine them and see whether they are legitimate or not.
464515
* To take full advantage of this, build systems (including but not limited to Cargo) must be

0 commit comments

Comments
 (0)