@@ -19,9 +19,15 @@ use ptr::P;
19
19
use util:: small_vector:: SmallVector ;
20
20
21
21
pub trait CfgFolder : fold:: Folder {
22
- fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > ;
22
+ fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool ;
23
+ fn process_attrs < T : HasAttrs > ( & mut self , node : T ) -> T { node }
23
24
fn visit_stmt_or_expr_attrs ( & mut self , _attrs : & [ ast:: Attribute ] ) { }
24
- fn visit_unconfigurable_expr ( & mut self , _expr : & ast:: Expr ) { }
25
+ fn visit_unremovable_expr ( & mut self , _expr : & ast:: Expr ) { }
26
+
27
+ fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > {
28
+ let node = self . process_attrs ( node) ;
29
+ if self . in_cfg ( node. attrs ( ) ) { Some ( node) } else { None }
30
+ }
25
31
}
26
32
27
33
/// A folder that strips out items that do not belong in the current
@@ -42,30 +48,6 @@ impl<'a> StripUnconfigured<'a> {
42
48
}
43
49
}
44
50
45
- // Determine if an item should be translated in the current crate
46
- // configuration based on the item's attributes
47
- fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
48
- attrs. iter ( ) . all ( |attr| {
49
- let mis = match attr. node . value . node {
50
- ast:: MetaItemKind :: List ( _, ref mis) if is_cfg ( & attr) => mis,
51
- _ => return true
52
- } ;
53
-
54
- if mis. len ( ) != 1 {
55
- self . diag . emit_error ( |diagnostic| {
56
- diagnostic. span_err ( attr. span , "expected 1 cfg-pattern" ) ;
57
- } ) ;
58
- return true ;
59
- }
60
-
61
- attr:: cfg_matches ( self . config , & mis[ 0 ] , & mut self . diag )
62
- } )
63
- }
64
-
65
- fn process_cfg_attrs ( & mut self , attrs : Vec < ast:: Attribute > ) -> Vec < ast:: Attribute > {
66
- attrs. into_iter ( ) . filter_map ( |attr| self . process_cfg_attr ( attr) ) . collect ( )
67
- }
68
-
69
51
fn process_cfg_attr ( & mut self , attr : ast:: Attribute ) -> Option < ast:: Attribute > {
70
52
if !attr. check_name ( "cfg_attr" ) {
71
53
return Some ( attr) ;
@@ -102,9 +84,30 @@ impl<'a> StripUnconfigured<'a> {
102
84
}
103
85
104
86
impl < ' a > CfgFolder for StripUnconfigured < ' a > {
105
- fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > {
106
- let node = node. map_attrs ( |attrs| self . process_cfg_attrs ( attrs) ) ;
107
- if self . in_cfg ( node. attrs ( ) ) { Some ( node) } else { None }
87
+ // Determine if an item should be translated in the current crate
88
+ // configuration based on the item's attributes
89
+ fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
90
+ attrs. iter ( ) . all ( |attr| {
91
+ let mis = match attr. node . value . node {
92
+ ast:: MetaItemKind :: List ( _, ref mis) if is_cfg ( & attr) => mis,
93
+ _ => return true
94
+ } ;
95
+
96
+ if mis. len ( ) != 1 {
97
+ self . diag . emit_error ( |diagnostic| {
98
+ diagnostic. span_err ( attr. span , "expected 1 cfg-pattern" ) ;
99
+ } ) ;
100
+ return true ;
101
+ }
102
+
103
+ attr:: cfg_matches ( self . config , & mis[ 0 ] , & mut self . diag )
104
+ } )
105
+ }
106
+
107
+ fn process_attrs < T : HasAttrs > ( & mut self , node : T ) -> T {
108
+ node. map_attrs ( |attrs| {
109
+ attrs. into_iter ( ) . filter_map ( |attr| self . process_cfg_attr ( attr) ) . collect ( )
110
+ } )
108
111
}
109
112
110
113
fn visit_stmt_or_expr_attrs ( & mut self , attrs : & [ ast:: Attribute ] ) {
@@ -114,7 +117,7 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
114
117
}
115
118
}
116
119
117
- fn visit_unconfigurable_expr ( & mut self , expr : & ast:: Expr ) {
120
+ fn visit_unremovable_expr ( & mut self , expr : & ast:: Expr ) {
118
121
if let Some ( attr) = expr. attrs ( ) . iter ( ) . find ( |a| is_cfg ( a) ) {
119
122
let msg = "removing an expression is not supported in this position" ;
120
123
self . diag . diag . span_err ( attr. span , msg) ;
@@ -200,7 +203,8 @@ impl<T: CfgFolder> fold::Folder for T {
200
203
//
201
204
// NB: This is intentionally not part of the fold_expr() function
202
205
// in order for fold_opt_expr() to be able to avoid this check
203
- self . visit_unconfigurable_expr ( & expr) ;
206
+ self . visit_unremovable_expr ( & expr) ;
207
+ let expr = self . process_attrs ( expr) ;
204
208
fold_expr ( self , expr)
205
209
}
206
210
0 commit comments