From cd51013b2bbac4172bda6d5e5ef7a5a1eaae0858 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 4 Jun 2025 15:30:28 -0700 Subject: [PATCH 1/9] Unwrap cfg_attr --- src/conditional-compilation.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index 7440fe0c2..51e5d8dcb 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -393,13 +393,10 @@ CfgAttrs -> Attr (`,` Attr)* `,`? ``` r[cfg.cfg_attr.general] -The `cfg_attr` [attribute] conditionally includes [attributes] based on a -configuration predicate. +The `cfg_attr` [attribute] conditionally includes [attributes] based on a configuration predicate. r[cfg.cfg_attr.behaviour] -When the configuration predicate is true, this attribute expands out to the -attributes listed after the predicate. For example, the following module will -either be found at `linux.rs` or `windows.rs` based on the target. +When the configuration predicate is true, this attribute expands out to the attributes listed after the predicate. For example, the following module will either be found at `linux.rs` or `windows.rs` based on the target. ```rust,ignore @@ -409,8 +406,7 @@ mod os; ``` r[cfg.cfg_attr.attribute-list] -Zero, one, or more attributes may be listed. Multiple attributes will each be -expanded into separate attributes. For example: +Zero, one, or more attributes may be listed. Multiple attributes will each be expanded into separate attributes. For example: ```rust,ignore From 62279d170de6ebb59c564bedbbd5313834688902 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 4 Jun 2025 15:40:50 -0700 Subject: [PATCH 2/9] Rename cfg.cfg_attr.general and move to the start --- src/conditional-compilation.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index 51e5d8dcb..377f4c587 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -385,6 +385,9 @@ The `cfg` attribute is allowed anywhere attributes are allowed. r[cfg.cfg_attr] ### The `cfg_attr` attribute +r[cfg.cfg_attr.intro] +The *`cfg_attr` [attribute]* conditionally includes attributes based on a configuration predicate. + r[cfg.cfg_attr.syntax] ```grammar,configuration @root CfgAttrAttribute -> `cfg_attr` `(` ConfigurationPredicate `,` CfgAttrs? `)` @@ -392,8 +395,6 @@ r[cfg.cfg_attr.syntax] CfgAttrs -> Attr (`,` Attr)* `,`? ``` -r[cfg.cfg_attr.general] -The `cfg_attr` [attribute] conditionally includes [attributes] based on a configuration predicate. r[cfg.cfg_attr.behaviour] When the configuration predicate is true, this attribute expands out to the attributes listed after the predicate. For example, the following module will either be found at `linux.rs` or `windows.rs` based on the target. From 39a9675a976166d1c93dd570fdffc3e87d018b33 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 4 Jun 2025 15:41:52 -0700 Subject: [PATCH 3/9] Move example to the intro --- src/conditional-compilation.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index 377f4c587..6cc7e51cd 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -388,6 +388,16 @@ r[cfg.cfg_attr] r[cfg.cfg_attr.intro] The *`cfg_attr` [attribute]* conditionally includes attributes based on a configuration predicate. +> [!EXAMPLE] +> The following module will either be found at `linux.rs` or `windows.rs` based on the target. +> +> +> ```rust,ignore +> #[cfg_attr(target_os = "linux", path = "linux.rs")] +> #[cfg_attr(windows, path = "windows.rs")] +> mod os; +> ``` + r[cfg.cfg_attr.syntax] ```grammar,configuration @root CfgAttrAttribute -> `cfg_attr` `(` ConfigurationPredicate `,` CfgAttrs? `)` @@ -399,12 +409,6 @@ CfgAttrs -> Attr (`,` Attr)* `,`? r[cfg.cfg_attr.behaviour] When the configuration predicate is true, this attribute expands out to the attributes listed after the predicate. For example, the following module will either be found at `linux.rs` or `windows.rs` based on the target. - -```rust,ignore -#[cfg_attr(target_os = "linux", path = "linux.rs")] -#[cfg_attr(windows, path = "windows.rs")] -mod os; -``` r[cfg.cfg_attr.attribute-list] Zero, one, or more attributes may be listed. Multiple attributes will each be expanded into separate attributes. For example: From 478211f25e1d8f586e04273135e96d25dab644c3 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 4 Jun 2025 15:42:10 -0700 Subject: [PATCH 4/9] Add a short intro to the syntax --- src/conditional-compilation.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index 6cc7e51cd..8c7ff3235 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -399,6 +399,8 @@ The *`cfg_attr` [attribute]* conditionally includes attributes based on a config > ``` r[cfg.cfg_attr.syntax] +The syntax for the `cfg_attr` attribute is: + ```grammar,configuration @root CfgAttrAttribute -> `cfg_attr` `(` ConfigurationPredicate `,` CfgAttrs? `)` From ad81b59968637733460c3c7b7b8d2df7c7c8adb9 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 4 Jun 2025 15:42:42 -0700 Subject: [PATCH 5/9] Rename cfg.cfg_attr.restriction This is to follow the template. --- src/conditional-compilation.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index 8c7ff3235..decbadd1e 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -407,6 +407,9 @@ The syntax for the `cfg_attr` attribute is: CfgAttrs -> Attr (`,` Attr)* `,`? ``` +r[cfg.cfg_attr.allowed-positions] +The `cfg_attr` attribute is allowed anywhere attributes are allowed. + r[cfg.cfg_attr.behaviour] When the configuration predicate is true, this attribute expands out to the attributes listed after the predicate. For example, the following module will either be found at `linux.rs` or `windows.rs` based on the target. @@ -429,9 +432,6 @@ fn bewitched() {} > [!NOTE] > 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)]`. -r[cfg.cfg_attr.restriction] -The `cfg_attr` attribute is allowed anywhere attributes are allowed. - The [`crate_type`] and [`crate_name`] attributes cannot be used with `cfg_attr`. r[cfg.macro] From 3089d61e6609825d3da60f5e9687afe76e92a7d1 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 4 Jun 2025 15:43:03 -0700 Subject: [PATCH 6/9] Add cfg.cfg_attr.duplicates --- src/conditional-compilation.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index decbadd1e..2bfa299b8 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -410,6 +410,8 @@ CfgAttrs -> Attr (`,` Attr)* `,`? r[cfg.cfg_attr.allowed-positions] The `cfg_attr` attribute is allowed anywhere attributes are allowed. +r[cfg.cfg_attr.duplicates] +Multiple `cfg_attr` attributes may be specified. r[cfg.cfg_attr.behaviour] When the configuration predicate is true, this attribute expands out to the attributes listed after the predicate. For example, the following module will either be found at `linux.rs` or `windows.rs` based on the target. From b8a3c10ab5aa86a5ce5247e0a0962403bda96f2f Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 4 Jun 2025 15:44:05 -0700 Subject: [PATCH 7/9] Add cfg.cfg_attr.attr-restriction This puts this particular restriction into a dedicated rule since it is unrelated to the previous sentence. --- src/conditional-compilation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index 2bfa299b8..56d6b4989 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -415,6 +415,8 @@ Multiple `cfg_attr` attributes may be specified. r[cfg.cfg_attr.behaviour] When the configuration predicate is true, this attribute expands out to the attributes listed after the predicate. For example, the following module will either be found at `linux.rs` or `windows.rs` based on the target. +r[cfg.cfg_attr.attr-restriction] +The [`crate_type`] and [`crate_name`] attributes cannot be used with `cfg_attr`. r[cfg.cfg_attr.attribute-list] @@ -434,8 +436,6 @@ fn bewitched() {} > [!NOTE] > 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)]`. -The [`crate_type`] and [`crate_name`] attributes cannot be used with `cfg_attr`. - r[cfg.macro] ### The `cfg` macro From 9c3f477a66371888f79274cfb25d176b17b96bc7 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 4 Jun 2025 15:45:14 -0700 Subject: [PATCH 8/9] Update cfg.cfg_attr.behaviour This updates the rule to match the style, moves it to match the template, does a slight rewording. The sentence about the example was moved in a previous commit. --- src/conditional-compilation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index 56d6b4989..f4f970a7c 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -413,11 +413,11 @@ The `cfg_attr` attribute is allowed anywhere attributes are allowed. r[cfg.cfg_attr.duplicates] Multiple `cfg_attr` attributes may be specified. -r[cfg.cfg_attr.behaviour] -When the configuration predicate is true, this attribute expands out to the attributes listed after the predicate. For example, the following module will either be found at `linux.rs` or `windows.rs` based on the target. r[cfg.cfg_attr.attr-restriction] The [`crate_type`] and [`crate_name`] attributes cannot be used with `cfg_attr`. +r[cfg.cfg_attr.behavior] +When the configuration predicate is true, `cfg_attr` expands out to the attributes listed after the predicate. r[cfg.cfg_attr.attribute-list] Zero, one, or more attributes may be listed. Multiple attributes will each be expanded into separate attributes. For example: From f7705a04ae2cc15b5cbac16991a67e702884b727 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 4 Jun 2025 15:45:27 -0700 Subject: [PATCH 9/9] Move example to an example block --- src/conditional-compilation.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index f4f970a7c..f7d6cf3fc 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -420,18 +420,19 @@ r[cfg.cfg_attr.behavior] When the configuration predicate is true, `cfg_attr` expands out to the attributes listed after the predicate. r[cfg.cfg_attr.attribute-list] -Zero, one, or more attributes may be listed. Multiple attributes will each be expanded into separate attributes. For example: +Zero, one, or more attributes may be listed. Multiple attributes will each be expanded into separate attributes. - -```rust,ignore -#[cfg_attr(feature = "magic", sparkles, crackles)] -fn bewitched() {} - -// When the `magic` feature flag is enabled, the above will expand to: -#[sparkles] -#[crackles] -fn bewitched() {} -``` +> [!EXAMPLE] +> +> ```rust,ignore +> #[cfg_attr(feature = "magic", sparkles, crackles)] +> fn bewitched() {} +> +> // When the `magic` feature flag is enabled, the above will expand to: +> #[sparkles] +> #[crackles] +> fn bewitched() {} +> ``` > [!NOTE] > 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)]`.