@@ -273,7 +273,6 @@ Each `--check-cfg` option can take one of two forms:
273
273
274
274
### The ` names(...) ` form
275
275
276
-
277
276
This form uses a named metadata list:
278
277
279
278
``` bash
@@ -302,9 +301,32 @@ form. The parentheses are required.
302
301
rustc --check-cfg ' names()'
303
302
```
304
303
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
+
305
309
Conditions that are enabled are implicitly valid; it is unnecessary (but legal) to specify a
306
310
condition name as both enabled and valid. For example, the following invocations are equivalent:
307
311
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
+
308
330
### The ` values(...) ` form
309
331
310
332
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
320
342
When the `values(...)` option is specified, `rustc` will check every `#[cfg(name = "value")]`
321
343
attribute, `#[cfg_attr(name = "value")]` attribute, and `cfg!(name = "value")` call. It will
322
344
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
324
346
level for this lint is `Warn`.
325
347
326
348
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")'
354
376
355
377
This is intended to give tool developers more flexibility when generating Rustc command lines.
356
378
357
- ### Enabled conditions are implicitly valid
379
+ ### Enabled condition names are implicitly valid
358
380
359
381
Specifying an enabled condition name implicitly makes it valid. For example, the following
360
382
invocations are equivalent:
361
383
362
384
```bash
363
385
# legal but redundant:
364
- rustc --check-cfg ' values (animals, " lion " , " zebra " )' --cfg ' animals = " lion" '
386
+ rustc --check-cfg ' names (animals)' --cfg ' animals = " lion" '
365
387
366
388
# equivalent:
367
- rustc --check-cfg ' values(animals, " zebra " )' --cfg ' animals = " lion" '
389
+ rustc --check-cfg ' names( )' --cfg ' animals = " lion" '
368
390
```
369
391
392
+ ### Enabled condition values are implicitly valid
393
+
370
394
Specifying an enabled condition _value_ implicitly makes that _value_ valid. For example, the
371
395
following invocations are equivalent:
372
396
@@ -389,6 +413,32 @@ rustc --check-cfg 'names(other, animals)' --check-cfg 'values(animals, "lion")'
389
413
rustc --check-cfg ' names(other)' --check-cfg ' values(animals, " lion" )'
390
414
```
391
415
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
+
392
442
## Stabilizing
393
443
394
444
Until this feature is stabilized, it can only be used with a `nightly` compiler, and only when
@@ -458,7 +508,8 @@ fn tame_lion() { ... }
458
508
459
509
## Drawbacks
460
510
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.
462
513
* As with all lints, correct code may be trigger lints. Developers will need to take time to
463
514
examine them and see whether they are legitimate or not.
464
515
* To take full advantage of this, build systems (including but not limited to Cargo) must be
0 commit comments