@@ -19,6 +19,7 @@ use attr;
1919use attr:: { AttrMetaMethods , WithAttrs , ThinAttributesExt } ;
2020use codemap;
2121use codemap:: { Span , Spanned , ExpnInfo , ExpnId , NameAndSpan , MacroBang , MacroAttribute } ;
22+ use config:: StripUnconfigured ;
2223use ext:: base:: * ;
2324use feature_gate:: { self , Features } ;
2425use fold;
@@ -76,6 +77,17 @@ impl_macro_generable! {
7677 "statement" , . make_stmts, lift . fold_stmt, |_span| SmallVector :: zero( ) ;
7778}
7879
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+
7991pub fn expand_expr ( e : P < ast:: Expr > , fld : & mut MacroExpander ) -> P < ast:: Expr > {
8092 return e. and_then ( |ast:: Expr { id, node, span, attrs} | match node {
8193
@@ -322,7 +334,8 @@ fn expand_mac_invoc<T>(mac: ast::Mac, ident: Option<Ident>, attrs: Vec<ast::Attr
322334 } ;
323335
324336 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) ;
326339 fld. cx . bt_pop ( ) ;
327340 fully_expanded
328341}
@@ -987,6 +1000,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
9871000 pub fn new ( cx : & ' a mut ExtCtxt < ' b > ) -> MacroExpander < ' a , ' b > {
9881001 MacroExpander { cx : cx }
9891002 }
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+ }
9901009}
9911010
9921011impl < ' a , ' b > Folder for MacroExpander < ' a , ' b > {
@@ -999,6 +1018,19 @@ impl<'a, 'b> Folder for MacroExpander<'a, 'b> {
9991018 expand_expr ( expr, self )
10001019 }
10011020
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+
10021034 fn fold_pat ( & mut self , pat : P < ast:: Pat > ) -> P < ast:: Pat > {
10031035 expand_pat ( pat, self )
10041036 }
0 commit comments