Skip to content

Commit 837c4a8

Browse files
authored
Merge pull request #1879 from ehuss/cfg_attr
Update `cfg_attr` to use the attribute template
2 parents 9015826 + f7705a0 commit 837c4a8

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

src/conditional-compilation.md

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -391,52 +391,58 @@ When a crate-level `cfg` has a false predicate, the crate itself still exists. A
391391
r[cfg.cfg_attr]
392392
### The `cfg_attr` attribute
393393
394+
r[cfg.cfg_attr.intro]
395+
The *`cfg_attr` [attribute]* conditionally includes attributes based on a configuration predicate.
396+
397+
> [!EXAMPLE]
398+
> The following module will either be found at `linux.rs` or `windows.rs` based on the target.
399+
>
400+
> <!-- ignore: `mod` needs multiple files -->
401+
> ```rust,ignore
402+
> #[cfg_attr(target_os = "linux", path = "linux.rs")]
403+
> #[cfg_attr(windows, path = "windows.rs")]
404+
> mod os;
405+
> ```
406+
394407
r[cfg.cfg_attr.syntax]
408+
The syntax for the `cfg_attr` attribute is:
409+
395410
```grammar,configuration
396411
@root CfgAttrAttribute -> `cfg_attr` `(` ConfigurationPredicate `,` CfgAttrs? `)`
397412
398413
CfgAttrs -> Attr (`,` Attr)* `,`?
399414
```
400415
401-
r[cfg.cfg_attr.general]
402-
The `cfg_attr` [attribute] conditionally includes [attributes] based on a
403-
configuration predicate.
416+
r[cfg.cfg_attr.allowed-positions]
417+
The `cfg_attr` attribute is allowed anywhere attributes are allowed.
404418

405-
r[cfg.cfg_attr.behaviour]
406-
When the configuration predicate is true, this attribute expands out to the
407-
attributes listed after the predicate. For example, the following module will
408-
either be found at `linux.rs` or `windows.rs` based on the target.
419+
r[cfg.cfg_attr.duplicates]
420+
Multiple `cfg_attr` attributes may be specified.
409421

410-
<!-- ignore: `mod` needs multiple files -->
411-
```rust,ignore
412-
#[cfg_attr(target_os = "linux", path = "linux.rs")]
413-
#[cfg_attr(windows, path = "windows.rs")]
414-
mod os;
415-
```
422+
r[cfg.cfg_attr.attr-restriction]
423+
The [`crate_type`] and [`crate_name`] attributes cannot be used with `cfg_attr`.
424+
425+
r[cfg.cfg_attr.behavior]
426+
When the configuration predicate is true, `cfg_attr` expands out to the attributes listed after the predicate.
416427

417428
r[cfg.cfg_attr.attribute-list]
418-
Zero, one, or more attributes may be listed. Multiple attributes will each be
419-
expanded into separate attributes. For example:
420-
421-
<!-- ignore: fake attributes -->
422-
```rust,ignore
423-
#[cfg_attr(feature = "magic", sparkles, crackles)]
424-
fn bewitched() {}
425-
426-
// When the `magic` feature flag is enabled, the above will expand to:
427-
#[sparkles]
428-
#[crackles]
429-
fn bewitched() {}
430-
```
429+
Zero, one, or more attributes may be listed. Multiple attributes will each be expanded into separate attributes.
430+
431+
> [!EXAMPLE]
432+
> <!-- ignore: fake attributes -->
433+
> ```rust,ignore
434+
> #[cfg_attr(feature = "magic", sparkles, crackles)]
435+
> fn bewitched() {}
436+
>
437+
> // When the `magic` feature flag is enabled, the above will expand to:
438+
> #[sparkles]
439+
> #[crackles]
440+
> fn bewitched() {}
441+
> ```
431442
432443
> [!NOTE]
433444
> The `cfg_attr` can expand to another `cfg_attr`. For example, `#[cfg_attr(target_os = "linux", cfg_attr(feature = "multithreaded", some_other_attribute))]` is valid. This example would be equivalent to `#[cfg_attr(all(target_os = "linux", feature ="multithreaded"), some_other_attribute)]`.
434445
435-
r[cfg.cfg_attr.restriction]
436-
The `cfg_attr` attribute is allowed anywhere attributes are allowed.
437-
438-
The [`crate_type`] and [`crate_name`] attributes cannot be used with `cfg_attr`.
439-
440446
r[cfg.macro]
441447
### The `cfg` macro
442448

0 commit comments

Comments
 (0)