Skip to content

Commit 2de96a0

Browse files
committed
Update cfg.attr.crate-level-attrs
This moves the rule to follow the template order, rewords it a bit to simplify, and adds an example to illustrate. Closes #103
1 parent d92431b commit 2de96a0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/conditional-compilation.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,21 @@ r[cfg.attr.effect]
374374
If the predicate is true, the thing is rewritten to not have the `cfg` attribute on it. If the predicate is false, the thing is removed from the source code.
375375

376376
r[cfg.attr.crate-level-attrs]
377-
When a crate-level `cfg` has a false predicate, the behavior is slightly different: any crate attributes preceding the `cfg` are kept, and any crate attributes following the `cfg` are removed. This allows `#![no_std]` and `#![no_core]` crates to avoid linking `std`/`core` even if a `#![cfg(...)]` has removed the entire crate.
377+
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.
378+
379+
> [!EXAMPLE]
380+
> 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 removed the entire crate. For example:
381+
>
382+
> <!-- ignore: test infrastructure can't handle no_std -->
383+
> ```rust,ignore
384+
> // This `no_std` attribute is kept even though the crate-level `cfg`
385+
> // attribute is false.
386+
> #![no_std]
387+
> #![cfg(false)]
388+
>
389+
> // This function is not included.
390+
> pub fn example() {}
391+
> ```
378392
379393
r[cfg.cfg_attr]
380394
### The `cfg_attr` attribute

0 commit comments

Comments
 (0)