@@ -26,7 +26,6 @@ use errors::{Applicability, FatalError};
26
26
use smallvec:: { smallvec, SmallVec } ;
27
27
use syntax_pos:: { Span , DUMMY_SP , FileName } ;
28
28
29
- use rustc_data_structures:: fx:: FxHashMap ;
30
29
use rustc_data_structures:: sync:: Lrc ;
31
30
use std:: io:: ErrorKind ;
32
31
use std:: { iter, mem, slice} ;
@@ -75,6 +74,22 @@ macro_rules! ast_fragments {
75
74
}
76
75
77
76
impl AstFragment {
77
+ pub fn add_placeholders( & mut self , placeholders: & [ NodeId ] ) {
78
+ if placeholders. is_empty( ) {
79
+ return ;
80
+ }
81
+ match self {
82
+ $( $( AstFragment :: $Kind( ast) => ast. extend( placeholders. iter( ) . flat_map( |id| {
83
+ // We are repeating through arguments with `many`, to do that we have to
84
+ // mention some macro variable from those arguments even if it's not used.
85
+ #[ cfg_attr( bootstrap, allow( unused_macros) ) ]
86
+ macro _repeating( $flat_map_ast_elt) { }
87
+ placeholder( AstFragmentKind :: $Kind, * id) . $make_ast( )
88
+ } ) ) , ) ?) *
89
+ _ => panic!( "unexpected AST fragment kind" )
90
+ }
91
+ }
92
+
78
93
pub fn make_opt_expr( self ) -> Option <P <ast:: Expr >> {
79
94
match self {
80
95
AstFragment :: OptExpr ( expr) => expr,
@@ -342,7 +357,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
342
357
// Unresolved macros produce dummy outputs as a recovery measure.
343
358
invocations. reverse ( ) ;
344
359
let mut expanded_fragments = Vec :: new ( ) ;
345
- let mut all_derive_placeholders: FxHashMap < ExpnId , Vec < _ > > = FxHashMap :: default ( ) ;
346
360
let mut undetermined_invocations = Vec :: new ( ) ;
347
361
let ( mut progress, mut force) = ( false , !self . monotonic ) ;
348
362
loop {
@@ -420,9 +434,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
420
434
self . cx . resolver . add_derives ( invoc. expansion_data . id , SpecialDerives :: COPY ) ;
421
435
}
422
436
423
- let derive_placeholders =
424
- all_derive_placeholders. entry ( invoc. expansion_data . id ) . or_default ( ) ;
425
- derive_placeholders. reserve ( derives. len ( ) ) ;
437
+ let mut derive_placeholders = Vec :: with_capacity ( derives. len ( ) ) ;
426
438
invocations. reserve ( derives. len ( ) ) ;
427
439
for path in derives {
428
440
let expn_id = ExpnId :: fresh ( None ) ;
@@ -438,7 +450,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
438
450
}
439
451
let fragment = invoc. fragment_kind
440
452
. expect_from_annotatables ( :: std:: iter:: once ( item) ) ;
441
- self . collect_invocations ( fragment, derive_placeholders)
453
+ self . collect_invocations ( fragment, & derive_placeholders)
442
454
}
443
455
} ;
444
456
@@ -457,10 +469,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
457
469
let mut placeholder_expander = PlaceholderExpander :: new ( self . cx , self . monotonic ) ;
458
470
while let Some ( expanded_fragments) = expanded_fragments. pop ( ) {
459
471
for ( expn_id, expanded_fragment) in expanded_fragments. into_iter ( ) . rev ( ) {
460
- let derive_placeholders =
461
- all_derive_placeholders. remove ( & expn_id) . unwrap_or_else ( Vec :: new) ;
462
472
placeholder_expander. add ( NodeId :: placeholder_from_expn_id ( expn_id) ,
463
- expanded_fragment, derive_placeholders ) ;
473
+ expanded_fragment) ;
464
474
}
465
475
}
466
476
fragment_with_placeholders. mut_visit_with ( & mut placeholder_expander) ;
@@ -493,13 +503,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
493
503
monotonic : self . monotonic ,
494
504
} ;
495
505
fragment. mut_visit_with ( & mut collector) ;
506
+ fragment. add_placeholders ( extra_placeholders) ;
496
507
collector. invocations
497
508
} ;
498
509
499
- // FIXME: Merge `extra_placeholders` into the `fragment` as regular placeholders.
500
510
if self . monotonic {
501
511
self . cx . resolver . visit_ast_fragment_with_placeholders (
502
- self . cx . current_expansion . id , & fragment, extra_placeholders) ;
512
+ self . cx . current_expansion . id , & fragment
513
+ ) ;
503
514
}
504
515
505
516
( fragment, invocations)
0 commit comments