@@ -23,7 +23,6 @@ use errors::{Applicability, FatalError};
23
23
use smallvec:: { smallvec, SmallVec } ;
24
24
use syntax_pos:: { Span , DUMMY_SP , FileName } ;
25
25
26
- use rustc_data_structures:: fx:: FxHashMap ;
27
26
use rustc_data_structures:: sync:: Lrc ;
28
27
use std:: io:: ErrorKind ;
29
28
use std:: { iter, mem, slice} ;
@@ -72,6 +71,22 @@ macro_rules! ast_fragments {
72
71
}
73
72
74
73
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
+
75
90
pub fn make_opt_expr( self ) -> Option <P <ast:: Expr >> {
76
91
match self {
77
92
AstFragment :: OptExpr ( expr) => expr,
@@ -339,7 +354,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
339
354
// Unresolved macros produce dummy outputs as a recovery measure.
340
355
invocations. reverse ( ) ;
341
356
let mut expanded_fragments = Vec :: new ( ) ;
342
- let mut all_derive_placeholders: FxHashMap < ExpnId , Vec < _ > > = FxHashMap :: default ( ) ;
343
357
let mut undetermined_invocations = Vec :: new ( ) ;
344
358
let ( mut progress, mut force) = ( false , !self . monotonic ) ;
345
359
loop {
@@ -416,9 +430,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
416
430
self . cx . resolver . add_derives ( invoc. expansion_data . id , SpecialDerives :: COPY ) ;
417
431
}
418
432
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 ( ) ) ;
422
434
invocations. reserve ( derives. len ( ) ) ;
423
435
for path in derives {
424
436
let expn_id = ExpnId :: fresh ( None ) ;
@@ -434,7 +446,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
434
446
}
435
447
let fragment = invoc. fragment_kind
436
448
. expect_from_annotatables ( :: std:: iter:: once ( item) ) ;
437
- self . collect_invocations ( fragment, derive_placeholders)
449
+ self . collect_invocations ( fragment, & derive_placeholders)
438
450
}
439
451
} ;
440
452
@@ -453,10 +465,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
453
465
let mut placeholder_expander = PlaceholderExpander :: new ( self . cx , self . monotonic ) ;
454
466
while let Some ( expanded_fragments) = expanded_fragments. pop ( ) {
455
467
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) ;
458
468
placeholder_expander. add ( NodeId :: placeholder_from_expn_id ( expn_id) ,
459
- expanded_fragment, derive_placeholders ) ;
469
+ expanded_fragment) ;
460
470
}
461
471
}
462
472
fragment_with_placeholders. mut_visit_with ( & mut placeholder_expander) ;
@@ -489,13 +499,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
489
499
monotonic : self . monotonic ,
490
500
} ;
491
501
fragment. mut_visit_with ( & mut collector) ;
502
+ fragment. add_placeholders ( extra_placeholders) ;
492
503
collector. invocations
493
504
} ;
494
505
495
- // FIXME: Merge `extra_placeholders` into the `fragment` as regular placeholders.
496
506
if self . monotonic {
497
507
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
+ ) ;
499
510
}
500
511
501
512
( fragment, invocations)
0 commit comments