Skip to content

Commit a164189

Browse files
committed
Discuss optionally allowing cfg_alias at module scope
1 parent 47d8ed1 commit a164189

File tree

1 file changed

+52
-11
lines changed

1 file changed

+52
-11
lines changed

text/3804-cfg-alias.md

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,13 @@ CfgAliasAttribute:
9797
cfg_alias(IDENTIFIER `=` ConfigurationPredicate)
9898
```
9999

100-
The identifier is added to the `cfg` namespace. It must not conflict with any
101-
builtin configuration names, or with those passed via `--cfg` or `--check-cfg`. [^check-cfg]
100+
The identifier is added to the `cfg` namespace. It must not conflict with:
101+
102+
- Any builtin configuration names
103+
- Any configuration passed via `--cfg`
104+
- Any configuration passed with `--check-cfg`, since this indicates a possible
105+
but omitted `--cfg` option
106+
- Other aliases that are in scope
102107

103108
Once defined, the alias can be used as a regular predicate.
104109

@@ -115,18 +120,54 @@ will emit an unknown configuration lint:
115120
#![cfg_alias(some_alias = true)]
116121
```
117122

118-
RFC Question:
123+
_RFC question: "usable only after definition" is mentioned here to retain the
124+
ability to parse attributes in order, rather than going back and updating
125+
earlier attributes that may use the alias. Is this a reasonable limitation to
126+
keep?_
127+
128+
_RFC question: two ways to implement this are with (1) near-literal
129+
substitution, or (2) checking whether the alias should be set or not at the time
130+
it is defined. Is there any user-visible behavior that would make us need to
131+
specify one or the other?_
132+
133+
_If we go with the first option, we should limit to a single expansion to avoid
134+
recursing (as is done for `#define` in C)._
135+
136+
## `cfg_alias` in non-crate attributes
137+
138+
`cfg_alias` may also be used as a module-level attribute rather than
139+
crate-level:
140+
141+
```rust
142+
#[cfg_alias(foo = bar)]
143+
mod uses_bar {
144+
// Enabled/disabled based on `cfg(bar)`
145+
#[cfg(foo)]
146+
fn qux() { /* ... */ }
147+
}
148+
149+
#[cfg_alias(foo = baz)]
150+
mod uses_baz {
151+
// Enabled/disabled based on `cfg(baz)`
152+
#[cfg(foo)]
153+
fn qux() { /* ... */ }
154+
}
155+
```
119156

120-
Two ways to implement this are with (1) near-literal substitution, or (2)
121-
checking whether the alias should be set or not at the time it is defined. Is
122-
there any user-visible behavior that would make us need to specify one or the
123-
other?
157+
This has the advantage of keeping aliases in closer proximity to where they are
158+
used; if a configuration pattern is only used within a specific module, an alias
159+
can be added at the top of the file rather than making it crate-global.
124160

125-
If we go with the first option, we should limit to a single expansion to avoid
126-
recursing (as is done for `#define` in C).
161+
When defined at a module level, aliases are added to the configuration namespace
162+
for everything within that module including later module-level configuration.
163+
There is no conflict with aliases that use the same name in other modules.
127164

128-
[^check-cfg]: `--check-cfg` is included here because it indicates there may be a
129-
corresponding `--cfg`.
165+
This RFC proposes that the use of `cfg_alias` on modules _should_ be included if
166+
possible. However, this may bring implementation complexity since, to the RFC
167+
author's knowledge, the rustc configuration system is not designed to allow
168+
scoped configuration. If implementation of module-level aliases turns out to be
169+
nontrivial, this portion of the feature may be deferred or dropped before
170+
stabilization.
130171

131172
# Drawbacks
132173

0 commit comments

Comments
 (0)