Skip to content

Commit 47d8ed1

Browse files
committed
Update syntax to =, add another usecase to motivation, mention --check-cfg
1 parent 780ccd7 commit 47d8ed1

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

text/3804-cfg-alias.md

+30-11
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ This RFC introduces a way to name configuration predicates for easy reuse
1212
throughout a crate.
1313

1414
```rust
15-
#![cfg_alias(
16-
x86_linux,
17-
all(any(target_arch = "x86", target_arch = "x86_64"), target_os = "linux")
18-
)]
15+
#![cfg_alias(x86_linux = all(
16+
any(target_arch = "x86", target_arch = "x86_64"), target_os = "linux"
17+
))]
1918

2019
#[cfg(x86_linux)]
2120
fn foo() { /* ... */ }
@@ -44,14 +43,30 @@ various Cargo environment variables and potentially doing string manipulation
4443
worth doing. Allowing aliases to be defined within the crate and with the same
4544
syntax as the `cfg` itself makes this much easier.
4645

46+
Another benefit is the ability to easily adjust configuration to many different
47+
areas of code at once. A simple example is gating unfinished code that can be
48+
toggled together:
49+
50+
```rust
51+
#![cfg_alias(todo = false)] // change `false` to `true` to enable WIP code
52+
53+
#[cfg(todo)]
54+
fn to_be_tested() { /* ... */ }
55+
56+
57+
#[test]
58+
#[cfg(todo)]
59+
fn test_to_be_tested() { /* ... */ }
60+
```
61+
4762
# Guide-level explanation
4863

4964
[guide-level-explanation]: #guide-level-explanation
5065

5166
There is a new crate-level attribute that takes a name and a `cfg` predicate:
5267

5368
```rust
54-
#![cfg_alias(some_alias, predicate)]
69+
#![cfg_alias(some_alias = predicate)]
5570
```
5671

5772
`predicate` can be anything that usually works within `#[cfg(...)]`, including
@@ -79,11 +94,11 @@ The new crate-level attribute is introduced:
7994

8095
```text
8196
CfgAliasAttribute:
82-
cfg_alias(IDENTIFIER, ConfigurationPredicate)
97+
cfg_alias(IDENTIFIER `=` ConfigurationPredicate)
8398
```
8499

85100
The identifier is added to the `cfg` namespace. It must not conflict with any
86-
builtin configuration names, or with those passed via `--cfg`.
101+
builtin configuration names, or with those passed via `--cfg` or `--check-cfg`. [^check-cfg]
87102

88103
Once defined, the alias can be used as a regular predicate.
89104

@@ -97,7 +112,7 @@ will emit an unknown configuration lint:
97112
// The lint could mention that `some_alias` was found in the
98113
// crate but is not available here.
99114

100-
#![cfg_alias(some_alias, true)]
115+
#![cfg_alias(some_alias = true)]
101116
```
102117

103118
RFC Question:
@@ -110,6 +125,9 @@ other?
110125
If we go with the first option, we should limit to a single expansion to avoid
111126
recursing (as is done for `#define` in C).
112127

128+
[^check-cfg]: `--check-cfg` is included here because it indicates there may be a
129+
corresponding `--cfg`.
130+
113131
# Drawbacks
114132

115133
[drawbacks]: #drawbacks
@@ -124,9 +142,10 @@ recursing (as is done for `#define` in C).
124142

125143
[rationale-and-alternatives]: #rationale-and-alternatives
126144

127-
- The syntax `cfg_alias(name, predicate)` was chosen for similarity with
128-
`cfg_attr(predicate, attributes)`. Alternatives include:
129-
- `cfg_alias(name = predicate)`
145+
- The syntax `cfg_alias(name = predicate)` was chosen to mimic assignment in
146+
Rust and key-value mappings in attributes. Alternatives include:
147+
- `cfg_alias(name, predicate)`, which is more similar to
148+
`cfg_attr(predicate, attributes)`.
130149
- It may be possible to have `#[cfg_alias(...)]` work as an outer macro and only
131150
apply to a specific scope. This likely is not worth the complexity.
132151

0 commit comments

Comments
 (0)