Skip to content

Commit 62c741c

Browse files
c410-f3rgitbot
authored and
gitbot
committed
Adjust syntax
1 parent 7840e9a commit 62c741c

File tree

4 files changed

+280
-21
lines changed

4 files changed

+280
-21
lines changed

core/src/macros/mod.rs

+52
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ pub macro assert_matches {
224224
/// }
225225
/// }
226226
/// ```
227+
#[cfg(bootstrap)]
227228
#[unstable(feature = "cfg_match", issue = "115585")]
228229
#[rustc_diagnostic_item = "cfg_match"]
229230
pub macro cfg_match {
@@ -284,6 +285,57 @@ pub macro cfg_match {
284285
}
285286
}
286287

288+
/// A macro for defining `#[cfg]` match-like statements.
289+
///
290+
/// It is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade of
291+
/// `#[cfg]` cases, emitting the implementation which matches first.
292+
///
293+
/// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
294+
/// without having to rewrite each clause multiple times.
295+
///
296+
/// Trailing `_` wildcard match arms are **optional** and they indicate a fallback branch when
297+
/// all previous declarations do not evaluate to true.
298+
///
299+
/// # Example
300+
///
301+
/// ```
302+
/// #![feature(cfg_match)]
303+
///
304+
/// cfg_match! {
305+
/// unix => {
306+
/// fn foo() { /* unix specific functionality */ }
307+
/// }
308+
/// target_pointer_width = "32" => {
309+
/// fn foo() { /* non-unix, 32-bit functionality */ }
310+
/// }
311+
/// _ => {
312+
/// fn foo() { /* fallback implementation */ }
313+
/// }
314+
/// }
315+
/// ```
316+
#[cfg(not(bootstrap))]
317+
#[unstable(feature = "cfg_match", issue = "115585")]
318+
#[rustc_diagnostic_item = "cfg_match"]
319+
pub macro cfg_match {
320+
({ $($tt:tt)* }) => {{
321+
cfg_match! { $($tt)* }
322+
}},
323+
(_ => { $($output:tt)* }) => {
324+
$($output)*
325+
},
326+
(
327+
$cfg:meta => $output:tt
328+
$($( $rest:tt )+)?
329+
) => {
330+
#[cfg($cfg)]
331+
cfg_match! { _ => $output }
332+
$(
333+
#[cfg(not($cfg))]
334+
cfg_match! { $($rest)+ }
335+
)?
336+
},
337+
}
338+
287339
/// Asserts that a boolean expression is `true` at runtime.
288340
///
289341
/// This will invoke the [`panic!`] macro if the provided expression cannot be

core/tests/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ mod intrinsics;
153153
mod io;
154154
mod iter;
155155
mod lazy;
156+
#[cfg(not(bootstrap))]
156157
mod macros;
158+
#[cfg(bootstrap)]
159+
mod macros_bootstrap;
157160
mod manually_drop;
158161
mod mem;
159162
mod net;

core/tests/macros.rs

+32-21
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct Struct;
1010

1111
impl Trait for Struct {
1212
cfg_match! {
13-
cfg(feature = "blah") => {
13+
feature = "blah" => {
1414
fn blah(&self) {
1515
unimplemented!();
1616
}
@@ -47,21 +47,21 @@ fn matches_leading_pipe() {
4747
#[test]
4848
fn cfg_match_basic() {
4949
cfg_match! {
50-
cfg(target_pointer_width = "64") => { fn f0_() -> bool { true }}
50+
target_pointer_width = "64" => { fn f0_() -> bool { true }}
5151
}
5252

5353
cfg_match! {
54-
cfg(unix) => { fn f1_() -> bool { true }}
55-
cfg(any(target_os = "macos", target_os = "linux")) => { fn f1_() -> bool { false }}
54+
unix => { fn f1_() -> bool { true } }
55+
any(target_os = "macos", target_os = "linux") => { fn f1_() -> bool { false }}
5656
}
5757

5858
cfg_match! {
59-
cfg(target_pointer_width = "32") => { fn f2_() -> bool { false }}
60-
cfg(target_pointer_width = "64") => { fn f2_() -> bool { true }}
59+
target_pointer_width = "32" => { fn f2_() -> bool { false } }
60+
target_pointer_width = "64" => { fn f2_() -> bool { true } }
6161
}
6262

6363
cfg_match! {
64-
cfg(target_pointer_width = "16") => { fn f3_() -> i32 { 1 }}
64+
target_pointer_width = "16" => { fn f3_() -> i32 { 1 } }
6565
_ => { fn f3_() -> i32 { 2 }}
6666
}
6767

@@ -83,7 +83,7 @@ fn cfg_match_basic() {
8383
#[test]
8484
fn cfg_match_debug_assertions() {
8585
cfg_match! {
86-
cfg(debug_assertions) => {
86+
debug_assertions => {
8787
assert!(cfg!(debug_assertions));
8888
assert_eq!(4, 2+2);
8989
}
@@ -98,13 +98,13 @@ fn cfg_match_debug_assertions() {
9898
#[test]
9999
fn cfg_match_no_duplication_on_64() {
100100
cfg_match! {
101-
cfg(windows) => {
101+
windows => {
102102
fn foo() {}
103103
}
104-
cfg(unix) => {
104+
unix => {
105105
fn foo() {}
106106
}
107-
cfg(target_pointer_width = "64") => {
107+
target_pointer_width = "64" => {
108108
fn foo() {}
109109
}
110110
}
@@ -114,34 +114,34 @@ fn cfg_match_no_duplication_on_64() {
114114
#[test]
115115
fn cfg_match_options() {
116116
cfg_match! {
117-
cfg(test) => {
117+
test => {
118118
use core::option::Option as Option2;
119119
fn works1() -> Option2<u32> { Some(1) }
120120
}
121121
_ => { fn works1() -> Option<u32> { None } }
122122
}
123123

124124
cfg_match! {
125-
cfg(feature = "foo") => { fn works2() -> bool { false } }
126-
cfg(test) => { fn works2() -> bool { true } }
125+
feature = "foo" => { fn works2() -> bool { false } }
126+
test => { fn works2() -> bool { true } }
127127
_ => { fn works2() -> bool { false } }
128128
}
129129

130130
cfg_match! {
131-
cfg(feature = "foo") => { fn works3() -> bool { false } }
131+
feature = "foo" => { fn works3() -> bool { false } }
132132
_ => { fn works3() -> bool { true } }
133133
}
134134

135135
cfg_match! {
136-
cfg(test) => {
136+
test => {
137137
use core::option::Option as Option3;
138138
fn works4() -> Option3<u32> { Some(1) }
139139
}
140140
}
141141

142142
cfg_match! {
143-
cfg(feature = "foo") => { fn works5() -> bool { false } }
144-
cfg(test) => { fn works5() -> bool { true } }
143+
feature = "foo" => { fn works5() -> bool { false } }
144+
test => { fn works5() -> bool { true } }
145145
}
146146

147147
assert!(works1().is_some());
@@ -154,7 +154,7 @@ fn cfg_match_options() {
154154
#[test]
155155
fn cfg_match_two_functions() {
156156
cfg_match! {
157-
cfg(target_pointer_width = "64") => {
157+
target_pointer_width = "64" => {
158158
fn foo1() {}
159159
fn bar1() {}
160160
}
@@ -178,7 +178,7 @@ fn cfg_match_two_functions() {
178178

179179
fn _accepts_expressions() -> i32 {
180180
cfg_match! {
181-
cfg(unix) => { 1 }
181+
unix => { 1 }
182182
_ => { 2 }
183183
}
184184
}
@@ -189,7 +189,18 @@ fn _allows_stmt_expr_attributes() {
189189
let one = 1;
190190
let two = 2;
191191
cfg_match! {
192-
cfg(unix) => { one * two; }
192+
unix => { one * two; }
193193
_ => { one + two; }
194194
}
195195
}
196+
197+
fn _expression() {
198+
let _ = cfg_match!({
199+
windows => {
200+
" XP"
201+
}
202+
_ => {
203+
""
204+
}
205+
});
206+
}

0 commit comments

Comments
 (0)