Skip to content

Commit 9015826

Browse files
authored
Merge pull request #1878 from ehuss/cfg
Update `cfg` to use the attribute template
2 parents 7b99112 + cf4e431 commit 9015826

File tree

1 file changed

+59
-53
lines changed

1 file changed

+59
-53
lines changed

src/conditional-compilation.md

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -318,69 +318,75 @@ Example values:
318318
r[cfg.attr]
319319
### The `cfg` attribute
320320

321+
r[cfg.attr.intro]
322+
The *`cfg` [attribute]* conditionally includes the form to which it is attached based on a configuration predicate.
323+
324+
> [!EXAMPLE]
325+
> ```rust
326+
> // The function is only included in the build when compiling for macOS
327+
> #[cfg(target_os = "macos")]
328+
> fn macos_only() {
329+
> // ...
330+
> }
331+
>
332+
> // This function is only included when either foo or bar is defined
333+
> #[cfg(any(foo, bar))]
334+
> fn needs_foo_or_bar() {
335+
> // ...
336+
> }
337+
>
338+
> // This function is only included when compiling for a unixish OS with a 32-bit
339+
> // architecture
340+
> #[cfg(all(unix, target_pointer_width = "32"))]
341+
> fn on_32bit_unix() {
342+
> // ...
343+
> }
344+
>
345+
> // This function is only included when foo is not defined
346+
> #[cfg(not(foo))]
347+
> fn needs_not_foo() {
348+
> // ...
349+
> }
350+
>
351+
> // This function is only included when the panic strategy is set to unwind
352+
> #[cfg(panic = "unwind")]
353+
> fn when_unwinding() {
354+
> // ...
355+
> }
356+
> ```
357+
321358
r[cfg.attr.syntax]
359+
The syntax for the `cfg` attribute is:
360+
322361
```grammar,configuration
323362
@root CfgAttribute -> `cfg` `(` ConfigurationPredicate `)`
324363
```
325364
326-
<!-- should we say they're active attributes here? -->
327-
328-
r[cfg.attr.general]
329-
The `cfg` [attribute] conditionally includes the thing it is attached to based
330-
on a configuration predicate.
365+
r[cfg.attr.allowed-positions]
366+
The `cfg` attribute is allowed anywhere attributes are allowed.
331367

332-
r[cfg.attr.syntax-explanation]
333-
It is written as `cfg`, `(`, a configuration predicate, and finally `)`.
368+
r[cfg.attr.duplicates]
369+
Multiple `cfg` attributes may be specified. The form to which the attribute is attached will not be included if any of the `cfg` predicates are false except as described in [cfg.attr.crate-level-attrs].
334370

335371
r[cfg.attr.effect]
336-
If the predicate is true, the thing is rewritten to not have the `cfg` attribute
337-
on it. If the predicate is false, the thing is removed from the source code.
372+
If the predicate is true, the form is rewritten to not have the `cfg` attribute on it. If the predicate is false, the form is removed from the source code.
338373

339374
r[cfg.attr.crate-level-attrs]
340-
When a crate-level `cfg` has a false predicate, the behavior is slightly
341-
different: any crate attributes preceding the `cfg` are kept, and any crate
342-
attributes following the `cfg` are removed. This allows `#![no_std]` and
343-
`#![no_core]` crates to avoid linking `std`/`core` even if a `#![cfg(...)]` has
344-
removed the entire crate.
345-
346-
Some examples on functions:
347-
348-
```rust
349-
// The function is only included in the build when compiling for macOS
350-
#[cfg(target_os = "macos")]
351-
fn macos_only() {
352-
// ...
353-
}
354-
355-
// This function is only included when either foo or bar is defined
356-
#[cfg(any(foo, bar))]
357-
fn needs_foo_or_bar() {
358-
// ...
359-
}
360-
361-
// This function is only included when compiling for a unixish OS with a 32-bit
362-
// architecture
363-
#[cfg(all(unix, target_pointer_width = "32"))]
364-
fn on_32bit_unix() {
365-
// ...
366-
}
367-
368-
// This function is only included when foo is not defined
369-
#[cfg(not(foo))]
370-
fn needs_not_foo() {
371-
// ...
372-
}
373-
374-
// This function is only included when the panic strategy is set to unwind
375-
#[cfg(panic = "unwind")]
376-
fn when_unwinding() {
377-
// ...
378-
}
379-
380-
```
381-
382-
r[cfg.attr.restriction]
383-
The `cfg` attribute is allowed anywhere attributes are allowed.
375+
When a crate-level `cfg` has a false predicate, the crate itself still exists. Any crate attributes preceding the `cfg` are kept, and any crate attributes following the `cfg` are removed as well as removing all of the following crate contents.
376+
377+
> [!EXAMPLE]
378+
> The behavior of not removing the preceding attributes allows you to do things such as include `#![no_std]` to avoid linking `std` even if a `#![cfg(...)]` has otherwise removed the contents of the crate. For example:
379+
>
380+
> <!-- ignore: test infrastructure can't handle no_std -->
381+
> ```rust,ignore
382+
> // This `no_std` attribute is kept even though the crate-level `cfg`
383+
> // attribute is false.
384+
> #![no_std]
385+
> #![cfg(false)]
386+
>
387+
> // This function is not included.
388+
> pub fn example() {}
389+
> ```
384390
385391
r[cfg.cfg_attr]
386392
### The `cfg_attr` attribute

0 commit comments

Comments
 (0)