@@ -19,6 +19,7 @@ use attr;
19
19
use attr:: { AttrMetaMethods , WithAttrs , ThinAttributesExt } ;
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;
@@ -76,6 +77,17 @@ impl_macro_generable! {
76
77
"statement" , . make_stmts, lift . fold_stmt, |_span| SmallVector :: zero( ) ;
77
78
}
78
79
80
+ impl MacroGenerable for Option < P < ast:: Expr > > {
81
+ fn kind_name ( ) -> & ' static str { "expression" }
82
+ fn dummy ( _span : Span ) -> Self { None }
83
+ fn make_with < ' a > ( result : Box < MacResult + ' a > ) -> Option < Self > {
84
+ result. make_expr ( ) . map ( Some )
85
+ }
86
+ fn fold_with < F : Folder > ( self , folder : & mut F ) -> Self {
87
+ self . and_then ( |expr| folder. fold_opt_expr ( expr) )
88
+ }
89
+ }
90
+
79
91
pub fn expand_expr ( e : P < ast:: Expr > , fld : & mut MacroExpander ) -> P < ast:: Expr > {
80
92
return e. and_then ( |ast:: Expr { id, node, span, attrs} | match node {
81
93
@@ -322,7 +334,8 @@ fn expand_mac_invoc<T>(mac: ast::Mac, ident: Option<Ident>, attrs: Vec<ast::Attr
322
334
} ;
323
335
324
336
let marked = expanded. fold_with ( & mut Marker { mark : mark, expn_id : Some ( fld. cx . backtrace ( ) ) } ) ;
325
- let fully_expanded = marked. fold_with ( fld) ;
337
+ let configured = marked. fold_with ( & mut fld. strip_unconfigured ( ) ) ;
338
+ let fully_expanded = configured. fold_with ( fld) ;
326
339
fld. cx . bt_pop ( ) ;
327
340
fully_expanded
328
341
}
@@ -987,6 +1000,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
987
1000
pub fn new ( cx : & ' a mut ExtCtxt < ' b > ) -> MacroExpander < ' a , ' b > {
988
1001
MacroExpander { cx : cx }
989
1002
}
1003
+
1004
+ fn strip_unconfigured ( & mut self ) -> StripUnconfigured {
1005
+ StripUnconfigured :: new ( & self . cx . cfg ,
1006
+ & self . cx . parse_sess . span_diagnostic ,
1007
+ self . cx . feature_gated_cfgs )
1008
+ }
990
1009
}
991
1010
992
1011
impl < ' a , ' b > Folder for MacroExpander < ' a , ' b > {
@@ -999,6 +1018,19 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
999
1018
expand_expr ( expr, self )
1000
1019
}
1001
1020
1021
+ fn fold_opt_expr ( & mut self , expr : P < ast:: Expr > ) -> Option < P < ast:: Expr > > {
1022
+ match expr. node {
1023
+ ast:: ExprKind :: Mac ( _) => { }
1024
+ _ => return Some ( expand_expr ( expr, self ) ) ,
1025
+ }
1026
+
1027
+ expr. and_then ( |ast:: Expr { node, span, attrs, ..} | match node {
1028
+ ast:: ExprKind :: Mac ( mac) =>
1029
+ expand_mac_invoc ( mac, None , attrs. into_attr_vec ( ) , span, self ) ,
1030
+ _ => unreachable ! ( ) ,
1031
+ } )
1032
+ }
1033
+
1002
1034
fn fold_pat ( & mut self , pat : P < ast:: Pat > ) -> P < ast:: Pat > {
1003
1035
expand_pat ( pat, self )
1004
1036
}
0 commit comments