@@ -19,27 +19,7 @@ use ptr::P;
19
19
20
20
use util:: small_vector:: SmallVector ;
21
21
22
- pub trait CfgFolder : fold:: Folder {
23
- // Check if a node with the given attributes is in this configuration.
24
- fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool ;
25
-
26
- // Update a node before checking if it is in this configuration (used to implement `cfg_attr`).
27
- fn process_attrs < T : HasAttrs > ( & mut self , node : T ) -> T { node }
28
-
29
- // Visit attributes on expression and statements (but not attributes on items in blocks).
30
- fn visit_stmt_or_expr_attrs ( & mut self , _attrs : & [ ast:: Attribute ] ) { }
31
-
32
- // Visit unremovable (non-optional) expressions -- c.f. `fold_expr` vs `fold_opt_expr`.
33
- fn visit_unremovable_expr ( & mut self , _expr : & ast:: Expr ) { }
34
-
35
- fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > {
36
- let node = self . process_attrs ( node) ;
37
- if self . in_cfg ( node. attrs ( ) ) { Some ( node) } else { None }
38
- }
39
- }
40
-
41
- /// A folder that strips out items that do not belong in the current
42
- /// configuration.
22
+ /// A folder that strips out items that do not belong in the current configuration.
43
23
pub struct StripUnconfigured < ' a > {
44
24
diag : CfgDiagReal < ' a , ' a > ,
45
25
should_test : bool ,
@@ -59,6 +39,17 @@ impl<'a> StripUnconfigured<'a> {
59
39
}
60
40
}
61
41
42
+ fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > {
43
+ let node = self . process_cfg_attrs ( node) ;
44
+ if self . in_cfg ( node. attrs ( ) ) { Some ( node) } else { None }
45
+ }
46
+
47
+ fn process_cfg_attrs < T : HasAttrs > ( & mut self , node : T ) -> T {
48
+ node. map_attrs ( |attrs| {
49
+ attrs. into_iter ( ) . filter_map ( |attr| self . process_cfg_attr ( attr) ) . collect ( )
50
+ } )
51
+ }
52
+
62
53
fn process_cfg_attr ( & mut self , attr : ast:: Attribute ) -> Option < ast:: Attribute > {
63
54
if !attr. check_name ( "cfg_attr" ) {
64
55
return Some ( attr) ;
@@ -92,11 +83,8 @@ impl<'a> StripUnconfigured<'a> {
92
83
None
93
84
}
94
85
}
95
- }
96
86
97
- impl < ' a > CfgFolder for StripUnconfigured < ' a > {
98
- // Determine if an item should be translated in the current crate
99
- // configuration based on the item's attributes
87
+ // Determine if a node with the given attributes should be included in this configuation.
100
88
fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
101
89
attrs. iter ( ) . all ( |attr| {
102
90
// When not compiling with --test we should not compile the #[test] functions
@@ -120,19 +108,15 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
120
108
} )
121
109
}
122
110
123
- fn process_attrs < T : HasAttrs > ( & mut self , node : T ) -> T {
124
- node. map_attrs ( |attrs| {
125
- attrs. into_iter ( ) . filter_map ( |attr| self . process_cfg_attr ( attr) ) . collect ( )
126
- } )
127
- }
128
-
111
+ // Visit attributes on expression and statements (but not attributes on items in blocks).
129
112
fn visit_stmt_or_expr_attrs ( & mut self , attrs : & [ ast:: Attribute ] ) {
130
113
// flag the offending attributes
131
114
for attr in attrs. iter ( ) {
132
115
self . diag . feature_gated_cfgs . push ( GatedCfgAttr :: GatedAttr ( attr. span ) ) ;
133
116
}
134
117
}
135
118
119
+ // Visit unremovable (non-optional) expressions -- c.f. `fold_expr` vs `fold_opt_expr`.
136
120
fn visit_unremovable_expr ( & mut self , expr : & ast:: Expr ) {
137
121
if let Some ( attr) = expr. attrs ( ) . iter ( ) . find ( |a| is_cfg ( a) || is_test_or_bench ( a) ) {
138
122
let msg = "removing an expression is not supported in this position" ;
@@ -151,7 +135,7 @@ pub fn strip_unconfigured_items(diagnostic: &Handler, krate: ast::Crate, should_
151
135
StripUnconfigured :: new ( config, should_test, diagnostic, feature_gated_cfgs) . fold_crate ( krate)
152
136
}
153
137
154
- impl < T : CfgFolder > fold:: Folder for T {
138
+ impl < ' a > fold:: Folder for StripUnconfigured < ' a > {
155
139
fn fold_foreign_mod ( & mut self , foreign_mod : ast:: ForeignMod ) -> ast:: ForeignMod {
156
140
ast:: ForeignMod {
157
141
abi : foreign_mod. abi ,
@@ -212,7 +196,7 @@ impl<T: CfgFolder> fold::Folder for T {
212
196
// NB: This is intentionally not part of the fold_expr() function
213
197
// in order for fold_opt_expr() to be able to avoid this check
214
198
self . visit_unremovable_expr ( & expr) ;
215
- let expr = self . process_attrs ( expr) ;
199
+ let expr = self . process_cfg_attrs ( expr) ;
216
200
fold_expr ( self , expr)
217
201
}
218
202
@@ -264,7 +248,7 @@ impl<T: CfgFolder> fold::Folder for T {
264
248
}
265
249
}
266
250
267
- fn fold_expr < F : CfgFolder > ( folder : & mut F , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
251
+ fn fold_expr ( folder : & mut StripUnconfigured , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
268
252
expr. map ( |ast:: Expr { id, span, node, attrs} | {
269
253
fold:: noop_fold_expr ( ast:: Expr {
270
254
id : id,
0 commit comments