Skip to content

Commit 9d16539

Browse files
committed
Auto merge of rust-lang#143461 - folkertdev:cfg-select-builtin-macro, r=petrochenkov
make `cfg_select` a builtin macro tracking issue: rust-lang#115585 This parses mostly the same as the `macro cfg_select` version, except: 1. wrapping in double brackets is no longer supported (or needed): `cfg_select {{ /* ... */ }}` is now rejected. 2. in an expression context, the rhs is no longer wrapped in a block, so that this now works: ```rust fn main() { println!(cfg_select! { unix => { "foo" } _ => { "bar" } }); } ``` 3. a single wildcard rule is now supported: `cfg_select { _ => 1 }` now works I've also added an error if none of the rules evaluate to true, and warnings for any arms that follow the `_` wildcard rule. cc `@traviscross` if I'm missing any feature that should/should not be included r? `@petrochenkov` for the macro logic details
2 parents f07edf7 + 775e504 commit 9d16539

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

core/src/macros/mod.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -230,32 +230,16 @@ pub macro assert_matches {
230230
/// ```
231231
/// #![feature(cfg_select)]
232232
///
233-
/// let _some_string = cfg_select! {{
233+
/// let _some_string = cfg_select! {
234234
/// unix => { "With great power comes great electricity bills" }
235235
/// _ => { "Behind every successful diet is an unwatched pizza" }
236-
/// }};
236+
/// };
237237
/// ```
238238
#[unstable(feature = "cfg_select", issue = "115585")]
239239
#[rustc_diagnostic_item = "cfg_select"]
240-
#[rustc_macro_transparency = "semitransparent"]
241-
pub macro cfg_select {
242-
({ $($tt:tt)* }) => {{
243-
$crate::cfg_select! { $($tt)* }
244-
}},
245-
(_ => { $($output:tt)* }) => {
246-
$($output)*
247-
},
248-
(
249-
$cfg:meta => $output:tt
250-
$($( $rest:tt )+)?
251-
) => {
252-
#[cfg($cfg)]
253-
$crate::cfg_select! { _ => $output }
254-
$(
255-
#[cfg(not($cfg))]
256-
$crate::cfg_select! { $($rest)+ }
257-
)?
258-
},
240+
#[rustc_builtin_macro]
241+
pub macro cfg_select($($tt:tt)*) {
242+
/* compiler built-in */
259243
}
260244

261245
/// Asserts that a boolean expression is `true` at runtime.

coretests/tests/macros.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ fn matches_leading_pipe() {
4848
fn cfg_select_basic() {
4949
cfg_select! {
5050
target_pointer_width = "64" => { fn f0_() -> bool { true }}
51+
_ => {}
5152
}
5253

5354
cfg_select! {
5455
unix => { fn f1_() -> bool { true } }
55-
any(target_os = "macos", target_os = "linux") => { fn f1_() -> bool { false }}
56+
_ => { fn f1_() -> bool { false }}
5657
}
5758

5859
cfg_select! {
@@ -70,6 +71,8 @@ fn cfg_select_basic() {
7071

7172
#[cfg(unix)]
7273
assert!(f1_());
74+
#[cfg(not(unix))]
75+
assert!(!f1_());
7376

7477
#[cfg(target_pointer_width = "32")]
7578
assert!(!f2_());
@@ -183,6 +186,12 @@ fn _accepts_expressions() -> i32 {
183186
}
184187
}
185188

189+
fn _accepts_only_wildcard() -> i32 {
190+
cfg_select! {
191+
_ => { 1 }
192+
}
193+
}
194+
186195
// The current implementation expands to a macro call, which allows the use of expression
187196
// statements.
188197
fn _allows_stmt_expr_attributes() {
@@ -195,12 +204,12 @@ fn _allows_stmt_expr_attributes() {
195204
}
196205

197206
fn _expression() {
198-
let _ = cfg_select!({
207+
let _ = cfg_select!(
199208
windows => {
200209
" XP"
201210
}
202211
_ => {
203212
""
204213
}
205-
});
214+
);
206215
}

0 commit comments

Comments
 (0)