Skip to content

Commit da48704

Browse files
committed
Add the cfg_match! macro
1 parent c4f2577 commit da48704

File tree

4 files changed

+239
-0
lines changed

4 files changed

+239
-0
lines changed

library/core/src/macros/mod.rs

+89
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,95 @@ pub macro debug_assert_matches($($arg:tt)*) {
321321
}
322322
}
323323

324+
/// A macro for defining `#[cfg]` match-like statements.
325+
///
326+
/// It is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade of
327+
/// `#[cfg]` cases, emitting the implementation which matches first.
328+
///
329+
/// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
330+
/// without having to rewrite each clause multiple times.
331+
///
332+
/// Trailing `_` wildcard match arms are **optional** and they indicate a fallback branch when
333+
/// all previous declarations do not evaluate to true.
334+
///
335+
/// # Example
336+
///
337+
/// ```
338+
/// #![feature(cfg_match)]
339+
///
340+
/// cfg_match! {
341+
/// cfg(unix) => {
342+
/// fn foo() { /* unix specific functionality */ }
343+
/// }
344+
/// cfg(target_pointer_width = "32") => {
345+
/// fn foo() { /* non-unix, 32-bit functionality */ }
346+
/// }
347+
/// _ => {
348+
/// fn foo() { /* fallback implementation */ }
349+
/// }
350+
/// }
351+
/// ```
352+
#[macro_export]
353+
#[unstable(feature = "cfg_match", issue = "115585")]
354+
#[rustc_diagnostic_item = "cfg_match"]
355+
macro_rules! cfg_match {
356+
// with a final wildcard
357+
(
358+
$(cfg($initial_meta:meta) => { $($initial_tokens:item)* })+
359+
_ => { $($extra_tokens:item)* }
360+
) => {
361+
cfg_match! {
362+
@__items ();
363+
$((($initial_meta) ($($initial_tokens)*)),)+
364+
(() ($($extra_tokens)*)),
365+
}
366+
};
367+
368+
// without a final wildcard
369+
(
370+
$(cfg($extra_meta:meta) => { $($extra_tokens:item)* })*
371+
) => {
372+
cfg_match! {
373+
@__items ();
374+
$((($extra_meta) ($($extra_tokens)*)),)*
375+
}
376+
};
377+
378+
// Internal and recursive macro to emit all the items
379+
//
380+
// Collects all the previous cfgs in a list at the beginning, so they can be
381+
// negated. After the semicolon is all the remaining items.
382+
(@__items ($($_:meta,)*);) => {};
383+
(
384+
@__items ($($no:meta,)*);
385+
(($($yes:meta)?) ($($tokens:item)*)),
386+
$($rest:tt,)*
387+
) => {
388+
// Emit all items within one block, applying an appropriate #[cfg]. The
389+
// #[cfg] will require all `$yes` matchers specified and must also negate
390+
// all previous matchers.
391+
#[cfg(all(
392+
$($yes,)?
393+
not(any($($no),*))
394+
))]
395+
cfg_match! { @__identity $($tokens)* }
396+
397+
// Recurse to emit all other items in `$rest`, and when we do so add all
398+
// our `$yes` matchers to the list of `$no` matchers as future emissions
399+
// will have to negate everything we just matched as well.
400+
cfg_match! {
401+
@__items ($($no,)* $($yes,)?);
402+
$($rest,)*
403+
}
404+
};
405+
406+
// Internal macro to make __apply work out right for different match types,
407+
// because of how macros match/expand stuff.
408+
(@__identity $($tokens:item)*) => {
409+
$($tokens)*
410+
};
411+
}
412+
324413
/// Returns whether the given expression matches any of the given patterns.
325414
///
326415
/// Like in a `match` expression, the pattern can be optionally followed by `if`

library/core/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
#![feature(const_option_ext)]
9797
#![feature(const_result)]
9898
#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
99+
#![cfg_attr(test, feature(cfg_match))]
99100
#![feature(int_roundings)]
100101
#![feature(slice_group_by)]
101102
#![feature(split_array)]
@@ -139,6 +140,7 @@ mod hash;
139140
mod intrinsics;
140141
mod iter;
141142
mod lazy;
143+
#[cfg(test)]
142144
mod macros;
143145
mod manually_drop;
144146
mod mem;

library/core/tests/macros.rs

+145
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
trait Trait {
2+
fn blah(&self);
3+
}
4+
5+
#[allow(dead_code)]
6+
struct Struct;
7+
8+
impl Trait for Struct {
9+
cfg_match! {
10+
cfg(feature = "blah") => {
11+
fn blah(&self) {
12+
unimplemented!();
13+
}
14+
}
15+
_ => {
16+
fn blah(&self) {
17+
unimplemented!();
18+
}
19+
}
20+
}
21+
}
22+
123
#[test]
224
fn assert_eq_trailing_comma() {
325
assert_eq!(1, 1,);
@@ -18,3 +40,126 @@ fn assert_ne_trailing_comma() {
1840
fn matches_leading_pipe() {
1941
matches!(1, | 1 | 2 | 3);
2042
}
43+
#[test]
44+
fn cfg_match_basic() {
45+
cfg_match! {
46+
cfg(target_pointer_width = "64") => { fn f0_() -> bool { true }}
47+
}
48+
cfg_match! {
49+
cfg(unix) => { fn f1_() -> bool { true }}
50+
cfg(any(target_os = "macos", target_os = "linux")) => { fn f1_() -> bool { false }}
51+
}
52+
53+
cfg_match! {
54+
cfg(target_pointer_width = "64") => { fn f2_() -> bool { true }}
55+
cfg(target_pointer_width = "32") => { fn f2_() -> bool { false }}
56+
}
57+
58+
cfg_match! {
59+
cfg(target_pointer_width = "16") => { fn f3_() -> i32 { 1 }}
60+
_ => { fn f3_() -> i32 { 2 }}
61+
}
62+
63+
#[cfg(target_pointer_width = "64")]
64+
assert!(f0_());
65+
#[cfg(unix)]
66+
assert!(f1_());
67+
assert!(f2_());
68+
assert_eq!(f3_(), 2);
69+
}
70+
71+
#[cfg(target_pointer_width = "64")]
72+
#[test]
73+
fn cfg_match_no_duplication_on_64() {
74+
cfg_match! {
75+
cfg(windows) => {
76+
fn foo() {}
77+
}
78+
cfg(unix) => {
79+
fn foo() {}
80+
}
81+
cfg(target_pointer_width = "64") => {
82+
fn foo() {}
83+
}
84+
}
85+
foo();
86+
}
87+
88+
#[test]
89+
fn cfg_match_options() {
90+
cfg_match! {
91+
cfg(test) => {
92+
use core::option::Option as Option2;
93+
fn works1() -> Option2<u32> { Some(1) }
94+
}
95+
_ => { fn works1() -> Option<u32> { None } }
96+
}
97+
98+
cfg_match! {
99+
cfg(feature = "foo") => { fn works2() -> bool { false } }
100+
cfg(test) => { fn works2() -> bool { true } }
101+
_ => { fn works2() -> bool { false } }
102+
}
103+
104+
cfg_match! {
105+
cfg(feature = "foo") => { fn works3() -> bool { false } }
106+
_ => { fn works3() -> bool { true } }
107+
}
108+
109+
cfg_match! {
110+
cfg(test) => {
111+
use core::option::Option as Option3;
112+
fn works4() -> Option3<u32> { Some(1) }
113+
}
114+
}
115+
116+
cfg_match! {
117+
cfg(feature = "foo") => { fn works5() -> bool { false } }
118+
cfg(test) => { fn works5() -> bool { true } }
119+
}
120+
121+
assert!(works1().is_some());
122+
assert!(works2());
123+
assert!(works3());
124+
assert!(works4().is_some());
125+
assert!(works5());
126+
}
127+
128+
#[test]
129+
fn cfg_match_two_functions() {
130+
cfg_match! {
131+
cfg(target_pointer_width = "64") => {
132+
fn foo1() {}
133+
fn bar1() {}
134+
}
135+
_ => {
136+
fn foo2() {}
137+
fn bar2() {}
138+
}
139+
}
140+
141+
#[cfg(target_pointer_width = "64")]
142+
{
143+
foo1();
144+
bar1();
145+
}
146+
#[cfg(not(target_pointer_width = "64"))]
147+
{
148+
foo2();
149+
bar2();
150+
}
151+
}
152+
153+
#[test]
154+
fn cfg_match_usage_within_a_function() {
155+
cfg_match! {
156+
cfg(debug_assertions) => {
157+
assert!(cfg!(debug_assertions));
158+
assert_eq!(4, 2+2);
159+
}
160+
_ => {
161+
assert!(cfg!(not(debug_assertions)));
162+
assert_eq!(10, 5+5);
163+
}
164+
}
165+
}

library/std/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,9 @@ pub use core::{
644644
)]
645645
pub use core::concat_bytes;
646646

647+
#[unstable(feature = "cfg_match", issue = "115585")]
648+
pub use core::cfg_match;
649+
647650
#[stable(feature = "core_primitive", since = "1.43.0")]
648651
pub use core::primitive;
649652

0 commit comments

Comments
 (0)