@@ -19,6 +19,7 @@ use attr;
19
19
use attr:: { AttrMetaMethods , WithAttrs } ;
20
20
use codemap;
21
21
use codemap:: { Span , Spanned , ExpnInfo , ExpnId , NameAndSpan , MacroBang , MacroAttribute } ;
22
+ use config:: StripUnconfigured ;
22
23
use ext:: base:: * ;
23
24
use feature_gate:: { self , Features } ;
24
25
use fold;
@@ -44,9 +45,10 @@ fn check_attributes(attrs: &[ast::Attribute], fld: &MacroExpander) {
44
45
}
45
46
}
46
47
47
- pub fn expand_expr ( e : P < ast:: Expr > , fld : & mut MacroExpander ) -> P < ast:: Expr > {
48
+ pub fn expand_expr ( e : P < ast:: Expr > , fld : & mut MacroExpander , is_optional : bool )
49
+ -> Option < P < ast:: Expr > > {
48
50
let expr_span = e. span ;
49
- return e. and_then ( |ast:: Expr { id, node, span, attrs} | match node {
51
+ return e. and_then ( |ast:: Expr { id, node, span, attrs} | Some ( match node {
50
52
51
53
// expr_mac should really be expr_ext or something; it's the
52
54
// entry-point for all syntax extensions.
@@ -61,17 +63,20 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
61
63
let expanded_expr = match expand_mac_invoc ( mac, span,
62
64
|r| r. make_expr ( ) ,
63
65
mark_expr, fld) {
64
- Some ( expr) => expr,
66
+ Some ( expr) => match is_optional {
67
+ true => fld. strip_unconfigured ( ) . fold_opt_expr ( expr) ,
68
+ false => Some ( fld. strip_unconfigured ( ) . fold_expr ( expr) ) ,
69
+ } ,
65
70
None => {
66
- return DummyResult :: raw_expr ( span) ;
71
+ return Some ( DummyResult :: raw_expr ( span) ) ;
67
72
}
68
73
} ;
69
74
70
75
// Keep going, outside-in.
71
- let fully_expanded = fld. fold_expr ( expanded_expr ) ;
76
+ let fully_expanded = expanded_expr . map ( |expr| fld. fold_expr ( expr ) ) ;
72
77
fld. cx . bt_pop ( ) ;
73
78
74
- fully_expanded
79
+ return fully_expanded;
75
80
}
76
81
77
82
ast:: ExprKind :: InPlace ( placer, value_expr) => {
@@ -178,7 +183,7 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
178
183
attrs : attrs
179
184
} , fld) )
180
185
}
181
- } ) ;
186
+ } ) ) ;
182
187
}
183
188
184
189
/// Expand a (not-ident-style) macro invocation. Returns the result
@@ -487,6 +492,9 @@ pub fn expand_item_mac(it: P<ast::Item>,
487
492
let expn_id = fld. cx . backtrace ( ) ;
488
493
items. into_iter ( )
489
494
. map ( |i| mark_item ( i, fm, expn_id) )
495
+ . flat_map ( |i| fld. strip_unconfigured ( ) . fold_item ( i) )
496
+ . collect :: < SmallVector < _ > > ( )
497
+ . into_iter ( )
490
498
. flat_map ( |i| fld. fold_item ( i) . into_iter ( ) )
491
499
. collect ( )
492
500
}
@@ -533,6 +541,8 @@ fn expand_stmt(stmt: Stmt, fld: &mut MacroExpander) -> SmallVector<Stmt> {
533
541
Some ( stmts) => {
534
542
// Keep going, outside-in.
535
543
let new_items = stmts. into_iter ( ) . flat_map ( |s| {
544
+ fld. strip_unconfigured ( ) . fold_stmt ( s) . into_iter ( )
545
+ } ) . collect :: < SmallVector < _ > > ( ) . into_iter ( ) . flat_map ( |s| {
536
546
fld. fold_stmt ( s) . into_iter ( )
537
547
} ) . collect ( ) ;
538
548
fld. cx . bt_pop ( ) ;
@@ -791,7 +801,7 @@ fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
791
801
mac_span,
792
802
& marked_before[ ..] ) . make_pat ( ) ;
793
803
let expanded = match pat {
794
- Some ( e) => e ,
804
+ Some ( e) => fld . strip_unconfigured ( ) . fold_pat ( e ) ,
795
805
None => {
796
806
fld. cx . span_err (
797
807
pth. span ,
@@ -1089,6 +1099,8 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
1089
1099
Some ( impl_items) => {
1090
1100
// expand again if necessary
1091
1101
let new_items = impl_items. into_iter ( ) . flat_map ( |ii| {
1102
+ fld. strip_unconfigured ( ) . fold_impl_item ( ii)
1103
+ } ) . collect :: < SmallVector < _ > > ( ) . into_iter ( ) . flat_map ( |ii| {
1092
1104
expand_impl_item ( ii, fld) . into_iter ( )
1093
1105
} ) . collect ( ) ;
1094
1106
fld. cx . bt_pop ( ) ;
@@ -1143,7 +1155,7 @@ pub fn expand_type(t: P<ast::Ty>, fld: &mut MacroExpander) -> P<ast::Ty> {
1143
1155
|r| r. make_ty ( ) ,
1144
1156
mark_ty,
1145
1157
fld) {
1146
- Some ( ty) => ty ,
1158
+ Some ( ty) => fld . strip_unconfigured ( ) . fold_ty ( ty ) ,
1147
1159
None => {
1148
1160
return DummyResult :: raw_ty ( t. span ) ;
1149
1161
}
@@ -1184,6 +1196,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
1184
1196
pub fn new ( cx : & ' a mut ExtCtxt < ' b > ) -> MacroExpander < ' a , ' b > {
1185
1197
MacroExpander { cx : cx }
1186
1198
}
1199
+
1200
+ fn strip_unconfigured ( & mut self ) -> StripUnconfigured {
1201
+ StripUnconfigured :: new ( & self . cx . cfg ,
1202
+ & self . cx . parse_sess . span_diagnostic ,
1203
+ self . cx . feature_gated_cfgs )
1204
+ }
1187
1205
}
1188
1206
1189
1207
impl < ' a , ' b > Folder for MacroExpander < ' a , ' b > {
@@ -1193,7 +1211,11 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
1193
1211
}
1194
1212
1195
1213
fn fold_expr ( & mut self , expr : P < ast:: Expr > ) -> P < ast:: Expr > {
1196
- expand_expr ( expr, self )
1214
+ expand_expr ( expr, self , false ) . unwrap ( )
1215
+ }
1216
+
1217
+ fn fold_opt_expr ( & mut self , expr : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
1218
+ expand_expr ( expr, self , true )
1197
1219
}
1198
1220
1199
1221
fn fold_pat ( & mut self , pat : P < ast:: Pat > ) -> P < ast:: Pat > {
0 commit comments