@@ -23,7 +23,6 @@ use errors::{Applicability, FatalError};
2323use smallvec:: { smallvec, SmallVec } ;
2424use syntax_pos:: { Span , DUMMY_SP , FileName } ;
2525
26- use rustc_data_structures:: fx:: FxHashMap ;
2726use rustc_data_structures:: sync:: Lrc ;
2827use std:: io:: ErrorKind ;
2928use std:: { iter, mem, slice} ;
@@ -72,6 +71,22 @@ macro_rules! ast_fragments {
7271 }
7372
7473 impl AstFragment {
74+ pub fn add_placeholders( & mut self , placeholders: & [ NodeId ] ) {
75+ if placeholders. is_empty( ) {
76+ return ;
77+ }
78+ match self {
79+ $( $( AstFragment :: $Kind( ast) => ast. extend( placeholders. iter( ) . flat_map( |id| {
80+ // We are repeating through arguments with `many`, to do that we have to
81+ // mention some macro variable from those arguments even if it's not used.
82+ #[ cfg_attr( bootstrap, allow( unused_macros) ) ]
83+ macro _repeating( $flat_map_ast_elt) { }
84+ placeholder( AstFragmentKind :: $Kind, * id) . $make_ast( )
85+ } ) ) , ) ?) *
86+ _ => panic!( "unexpected AST fragment kind" )
87+ }
88+ }
89+
7590 pub fn make_opt_expr( self ) -> Option <P <ast:: Expr >> {
7691 match self {
7792 AstFragment :: OptExpr ( expr) => expr,
@@ -339,7 +354,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
339354 // Unresolved macros produce dummy outputs as a recovery measure.
340355 invocations. reverse ( ) ;
341356 let mut expanded_fragments = Vec :: new ( ) ;
342- let mut all_derive_placeholders: FxHashMap < ExpnId , Vec < _ > > = FxHashMap :: default ( ) ;
343357 let mut undetermined_invocations = Vec :: new ( ) ;
344358 let ( mut progress, mut force) = ( false , !self . monotonic ) ;
345359 loop {
@@ -416,9 +430,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
416430 self . cx . resolver . add_derives ( invoc. expansion_data . id , SpecialDerives :: COPY ) ;
417431 }
418432
419- let derive_placeholders =
420- all_derive_placeholders. entry ( invoc. expansion_data . id ) . or_default ( ) ;
421- derive_placeholders. reserve ( derives. len ( ) ) ;
433+ let mut derive_placeholders = Vec :: with_capacity ( derives. len ( ) ) ;
422434 invocations. reserve ( derives. len ( ) ) ;
423435 for path in derives {
424436 let expn_id = ExpnId :: fresh ( None ) ;
@@ -434,7 +446,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
434446 }
435447 let fragment = invoc. fragment_kind
436448 . expect_from_annotatables ( :: std:: iter:: once ( item) ) ;
437- self . collect_invocations ( fragment, derive_placeholders)
449+ self . collect_invocations ( fragment, & derive_placeholders)
438450 }
439451 } ;
440452
@@ -453,10 +465,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
453465 let mut placeholder_expander = PlaceholderExpander :: new ( self . cx , self . monotonic ) ;
454466 while let Some ( expanded_fragments) = expanded_fragments. pop ( ) {
455467 for ( expn_id, expanded_fragment) in expanded_fragments. into_iter ( ) . rev ( ) {
456- let derive_placeholders =
457- all_derive_placeholders. remove ( & expn_id) . unwrap_or_else ( Vec :: new) ;
458468 placeholder_expander. add ( NodeId :: placeholder_from_expn_id ( expn_id) ,
459- expanded_fragment, derive_placeholders ) ;
469+ expanded_fragment) ;
460470 }
461471 }
462472 fragment_with_placeholders. mut_visit_with ( & mut placeholder_expander) ;
@@ -489,13 +499,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
489499 monotonic : self . monotonic ,
490500 } ;
491501 fragment. mut_visit_with ( & mut collector) ;
502+ fragment. add_placeholders ( extra_placeholders) ;
492503 collector. invocations
493504 } ;
494505
495- // FIXME: Merge `extra_placeholders` into the `fragment` as regular placeholders.
496506 if self . monotonic {
497507 self . cx . resolver . visit_ast_fragment_with_placeholders (
498- self . cx . current_expansion . id , & fragment, extra_placeholders) ;
508+ self . cx . current_expansion . id , & fragment
509+ ) ;
499510 }
500511
501512 ( fragment, invocations)
0 commit comments