34
34
macro_rules! cfg_if {
35
35
// match if/else chains with a final `else`
36
36
( $(
37
- if #[ cfg( $( $meta: meta) ,* ) ] { $( $it : item ) * }
37
+ if #[ cfg( $( $meta: meta) ,* ) ] { $( $tokens : tt ) * }
38
38
) else * else {
39
- $( $it2 : item ) *
39
+ $( $tokens2 : tt ) *
40
40
} ) => {
41
41
$crate:: cfg_if! {
42
42
@__items
43
43
( ) ;
44
- $( ( ( $( $meta) ,* ) ( $( $it ) * ) ) , ) *
45
- ( ( ) ( $( $it2 ) * ) ) ,
44
+ $( ( ( $( $meta) ,* ) ( $( $tokens ) * ) ) , ) *
45
+ ( ( ) ( $( $tokens2 ) * ) ) ,
46
46
}
47
47
} ;
48
48
49
49
// match if/else chains lacking a final `else`
50
50
(
51
- if #[ cfg( $( $i_met: meta) ,* ) ] { $( $i_it : item ) * }
51
+ if #[ cfg( $( $i_met: meta) ,* ) ] { $( $i_tokens : tt ) * }
52
52
$(
53
- else if #[ cfg( $( $e_met: meta) ,* ) ] { $( $e_it : item ) * }
53
+ else if #[ cfg( $( $e_met: meta) ,* ) ] { $( $e_tokens : tt ) * }
54
54
) *
55
55
) => {
56
56
$crate:: cfg_if! {
57
57
@__items
58
58
( ) ;
59
- ( ( $( $i_met) ,* ) ( $( $i_it ) * ) ) ,
60
- $( ( ( $( $e_met) ,* ) ( $( $e_it ) * ) ) , ) *
59
+ ( ( $( $i_met) ,* ) ( $( $i_tokens ) * ) ) ,
60
+ $( ( ( $( $e_met) ,* ) ( $( $e_tokens ) * ) ) , ) *
61
61
( ( ) ( ) ) ,
62
62
}
63
63
} ;
@@ -67,21 +67,22 @@ macro_rules! cfg_if {
67
67
// Collects all the negated cfgs in a list at the beginning and after the
68
68
// semicolon is all the remaining items
69
69
( @__items ( $( $not: meta, ) * ) ; ) => { } ;
70
- ( @__items ( $( $not: meta, ) * ) ; ( ( $( $m: meta) ,* ) ( $( $it : item ) * ) ) , $( $rest: tt) * ) => {
71
- // Emit all items within one block, applying an approprate #[cfg]. The
70
+ ( @__items ( $( $not: meta, ) * ) ; ( ( $( $m: meta) ,* ) ( $( $tokens : tt ) * ) ) , $( $rest: tt) * ) => {
71
+ // Emit all items within one block, applying an appropriate #[cfg]. The
72
72
// #[cfg] will require all `$m` matchers specified and must also negate
73
73
// all previous matchers.
74
- $crate :: cfg_if! { @__apply cfg( all( $( $m, ) * not( any( $( $not) ,* ) ) ) ) , $ ( $it ) * }
74
+ # [ cfg( all( $( $m, ) * not( any( $( $not) ,* ) ) ) ) ] $crate :: cfg_if! { @__identity $ ( $tokens ) * }
75
75
76
76
// Recurse to emit all other items in `$rest`, and when we do so add all
77
77
// our `$m` matchers to the list of `$not` matchers as future emissions
78
78
// will have to negate everything we just matched as well.
79
79
$crate:: cfg_if! { @__items ( $( $not, ) * $( $m, ) * ) ; $( $rest) * }
80
80
} ;
81
81
82
- // Internal macro to Apply a cfg attribute to a list of items
83
- ( @__apply $m: meta, $( $it: item) * ) => {
84
- $( #[ $m] $it) *
82
+ // Internal macro to make __apply work out right for different match types,
83
+ // because of how macros matching/expand stuff.
84
+ ( @__identity $( $tokens: tt) * ) => {
85
+ $( $tokens) *
85
86
} ;
86
87
}
87
88
@@ -137,4 +138,18 @@ mod tests {
137
138
assert ! ( works4( ) . is_some( ) ) ;
138
139
assert ! ( works5( ) ) ;
139
140
}
141
+
142
+ #[ test]
143
+ #[ allow( clippy:: assertions_on_constants) ]
144
+ fn test_usage_within_a_function ( ) {
145
+ cfg_if ! { if #[ cfg( debug_assertions) ] {
146
+ // we want to put more than one thing here to make sure that they
147
+ // all get configured properly.
148
+ assert!( cfg!( debug_assertions) ) ;
149
+ assert_eq!( 4 , 2 +2 ) ;
150
+ } else {
151
+ assert!( works1( ) . is_some( ) ) ;
152
+ assert_eq!( 10 , 5 +5 ) ;
153
+ } }
154
+ }
140
155
}
0 commit comments