@@ -12,10 +12,9 @@ This RFC introduces a way to name configuration predicates for easy reuse
12
12
throughout a crate.
13
13
14
14
``` 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
+ ))]
19
18
20
19
#[cfg(x86_linux)]
21
20
fn foo () { /* ... */ }
@@ -44,14 +43,30 @@ various Cargo environment variables and potentially doing string manipulation
44
43
worth doing. Allowing aliases to be defined within the crate and with the same
45
44
syntax as the ` cfg ` itself makes this much easier.
46
45
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
+
47
62
# Guide-level explanation
48
63
49
64
[ guide-level-explanation ] : #guide-level-explanation
50
65
51
66
There is a new crate-level attribute that takes a name and a ` cfg ` predicate:
52
67
53
68
``` rust
54
- #![cfg_alias(some_alias, predicate)]
69
+ #![cfg_alias(some_alias = predicate)]
55
70
```
56
71
57
72
` predicate ` can be anything that usually works within ` #[cfg(...)] ` , including
@@ -79,11 +94,11 @@ The new crate-level attribute is introduced:
79
94
80
95
``` text
81
96
CfgAliasAttribute:
82
- cfg_alias(IDENTIFIER, ConfigurationPredicate)
97
+ cfg_alias(IDENTIFIER `=` ConfigurationPredicate)
83
98
```
84
99
85
100
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 ]
87
102
88
103
Once defined, the alias can be used as a regular predicate.
89
104
@@ -97,7 +112,7 @@ will emit an unknown configuration lint:
97
112
// The lint could mention that `some_alias` was found in the
98
113
// crate but is not available here.
99
114
100
- #![cfg_alias(some_alias, true)]
115
+ #![cfg_alias(some_alias = true)]
101
116
```
102
117
103
118
RFC Question:
@@ -110,6 +125,9 @@ other?
110
125
If we go with the first option, we should limit to a single expansion to avoid
111
126
recursing (as is done for ` #define ` in C).
112
127
128
+ [ ^ check-cfg ] : ` --check-cfg ` is included here because it indicates there may be a
129
+ corresponding ` --cfg ` .
130
+
113
131
# Drawbacks
114
132
115
133
[ drawbacks ] : #drawbacks
@@ -124,9 +142,10 @@ recursing (as is done for `#define` in C).
124
142
125
143
[ rationale-and-alternatives ] : #rationale-and-alternatives
126
144
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) ` .
130
149
- It may be possible to have ` #[cfg_alias(...)] ` work as an outer macro and only
131
150
apply to a specific scope. This likely is not worth the complexity.
132
151
0 commit comments