@@ -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
@@ -32,30 +38,6 @@ pub struct StripUnconfigured<'a> {
32
38
}
33
39
34
40
impl < ' a > StripUnconfigured < ' a > {
35
- // Determine if an item should be translated in the current crate
36
- // configuration based on the item's attributes
37
- fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
38
- attrs. iter ( ) . all ( |attr| {
39
- let mis = match attr. node . value . node {
40
- ast:: MetaItemKind :: List ( _, ref mis) if is_cfg ( & attr) => mis,
41
- _ => return true
42
- } ;
43
-
44
- if mis. len ( ) != 1 {
45
- self . diag . emit_error ( |diagnostic| {
46
- diagnostic. span_err ( attr. span , "expected 1 cfg-pattern" ) ;
47
- } ) ;
48
- return true ;
49
- }
50
-
51
- attr:: cfg_matches ( self . config , & mis[ 0 ] , & mut self . diag )
52
- } )
53
- }
54
-
55
- fn process_cfg_attrs ( & mut self , attrs : Vec < ast:: Attribute > ) -> Vec < ast:: Attribute > {
56
- attrs. into_iter ( ) . filter_map ( |attr| self . process_cfg_attr ( attr) ) . collect ( )
57
- }
58
-
59
41
fn process_cfg_attr ( & mut self , attr : ast:: Attribute ) -> Option < ast:: Attribute > {
60
42
if !attr. check_name ( "cfg_attr" ) {
61
43
return Some ( attr) ;
@@ -92,9 +74,30 @@ impl<'a> StripUnconfigured<'a> {
92
74
}
93
75
94
76
impl < ' a > CfgFolder for StripUnconfigured < ' a > {
95
- fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > {
96
- let node = node. map_attrs ( |attrs| self . process_cfg_attrs ( attrs) ) ;
97
- if self . in_cfg ( node. attrs ( ) ) { Some ( node) } else { None }
77
+ // Determine if an item should be translated in the current crate
78
+ // configuration based on the item's attributes
79
+ fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
80
+ attrs. iter ( ) . all ( |attr| {
81
+ let mis = match attr. node . value . node {
82
+ ast:: MetaItemKind :: List ( _, ref mis) if is_cfg ( & attr) => mis,
83
+ _ => return true
84
+ } ;
85
+
86
+ if mis. len ( ) != 1 {
87
+ self . diag . emit_error ( |diagnostic| {
88
+ diagnostic. span_err ( attr. span , "expected 1 cfg-pattern" ) ;
89
+ } ) ;
90
+ return true ;
91
+ }
92
+
93
+ attr:: cfg_matches ( self . config , & mis[ 0 ] , & mut self . diag )
94
+ } )
95
+ }
96
+
97
+ fn process_attrs < T : HasAttrs > ( & mut self , node : T ) -> T {
98
+ node. map_attrs ( |attrs| {
99
+ attrs. into_iter ( ) . filter_map ( |attr| self . process_cfg_attr ( attr) ) . collect ( )
100
+ } )
98
101
}
99
102
100
103
fn visit_stmt_or_expr_attrs ( & mut self , attrs : & [ ast:: Attribute ] ) {
@@ -104,7 +107,7 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
104
107
}
105
108
}
106
109
107
- fn visit_unconfigurable_expr ( & mut self , expr : & ast:: Expr ) {
110
+ fn visit_unremovable_expr ( & mut self , expr : & ast:: Expr ) {
108
111
if let Some ( attr) = expr. attrs ( ) . iter ( ) . find ( |a| is_cfg ( a) ) {
109
112
let msg = "removing an expression is not supported in this position" ;
110
113
self . diag . diag . span_err ( attr. span , msg) ;
@@ -195,7 +198,8 @@ impl<T: CfgFolder> fold::Folder for T {
195
198
//
196
199
// NB: This is intentionally not part of the fold_expr() function
197
200
// in order for fold_opt_expr() to be able to avoid this check
198
- self . visit_unconfigurable_expr ( & expr) ;
201
+ self . visit_unremovable_expr ( & expr) ;
202
+ let expr = self . process_attrs ( expr) ;
199
203
fold_expr ( self , expr)
200
204
}
201
205
0 commit comments