@@ -24,7 +24,6 @@ use errors::{Applicability, FatalError};
2424use smallvec:: { smallvec, SmallVec } ;
2525use syntax_pos:: { Span , DUMMY_SP , FileName } ;
2626
27- use rustc_data_structures:: fx:: FxHashMap ;
2827use rustc_data_structures:: sync:: Lrc ;
2928use std:: io:: ErrorKind ;
3029use std:: { iter, mem, slice} ;
@@ -73,6 +72,22 @@ macro_rules! ast_fragments {
7372 }
7473
7574 impl AstFragment {
75+ pub fn add_placeholders( & mut self , placeholders: & [ NodeId ] ) {
76+ if placeholders. is_empty( ) {
77+ return ;
78+ }
79+ match self {
80+ $( $( AstFragment :: $Kind( ast) => ast. extend( placeholders. iter( ) . flat_map( |id| {
81+ // We are repeating through arguments with `many`, to do that we have to
82+ // mention some macro variable from those arguments even if it's not used.
83+ #[ cfg_attr( bootstrap, allow( unused_macros) ) ]
84+ macro _repeating( $flat_map_ast_elt) { }
85+ placeholder( AstFragmentKind :: $Kind, * id) . $make_ast( )
86+ } ) ) , ) ?) *
87+ _ => panic!( "unexpected AST fragment kind" )
88+ }
89+ }
90+
7691 pub fn make_opt_expr( self ) -> Option <P <ast:: Expr >> {
7792 match self {
7893 AstFragment :: OptExpr ( expr) => expr,
@@ -340,7 +355,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
340355 // Unresolved macros produce dummy outputs as a recovery measure.
341356 invocations. reverse ( ) ;
342357 let mut expanded_fragments = Vec :: new ( ) ;
343- let mut all_derive_placeholders: FxHashMap < ExpnId , Vec < _ > > = FxHashMap :: default ( ) ;
344358 let mut undetermined_invocations = Vec :: new ( ) ;
345359 let ( mut progress, mut force) = ( false , !self . monotonic ) ;
346360 loop {
@@ -418,9 +432,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
418432 self . cx . resolver . add_derives ( invoc. expansion_data . id , SpecialDerives :: COPY ) ;
419433 }
420434
421- let derive_placeholders =
422- all_derive_placeholders. entry ( invoc. expansion_data . id ) . or_default ( ) ;
423- derive_placeholders. reserve ( derives. len ( ) ) ;
435+ let mut derive_placeholders = Vec :: with_capacity ( derives. len ( ) ) ;
424436 invocations. reserve ( derives. len ( ) ) ;
425437 for path in derives {
426438 let expn_id = ExpnId :: fresh ( None ) ;
@@ -436,7 +448,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
436448 }
437449 let fragment = invoc. fragment_kind
438450 . expect_from_annotatables ( :: std:: iter:: once ( item) ) ;
439- self . collect_invocations ( fragment, derive_placeholders)
451+ self . collect_invocations ( fragment, & derive_placeholders)
440452 }
441453 } ;
442454
@@ -455,10 +467,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
455467 let mut placeholder_expander = PlaceholderExpander :: new ( self . cx , self . monotonic ) ;
456468 while let Some ( expanded_fragments) = expanded_fragments. pop ( ) {
457469 for ( expn_id, expanded_fragment) in expanded_fragments. into_iter ( ) . rev ( ) {
458- let derive_placeholders =
459- all_derive_placeholders. remove ( & expn_id) . unwrap_or_else ( Vec :: new) ;
460470 placeholder_expander. add ( NodeId :: placeholder_from_expn_id ( expn_id) ,
461- expanded_fragment, derive_placeholders ) ;
471+ expanded_fragment) ;
462472 }
463473 }
464474 fragment_with_placeholders. mut_visit_with ( & mut placeholder_expander) ;
@@ -491,13 +501,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
491501 monotonic : self . monotonic ,
492502 } ;
493503 fragment. mut_visit_with ( & mut collector) ;
504+ fragment. add_placeholders ( extra_placeholders) ;
494505 collector. invocations
495506 } ;
496507
497- // FIXME: Merge `extra_placeholders` into the `fragment` as regular placeholders.
498508 if self . monotonic {
499509 self . cx . resolver . visit_ast_fragment_with_placeholders (
500- self . cx . current_expansion . id , & fragment, extra_placeholders) ;
510+ self . cx . current_expansion . id , & fragment
511+ ) ;
501512 }
502513
503514 ( fragment, invocations)
0 commit comments