@@ -492,6 +492,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
492
492
self . cx . force_mode = force;
493
493
494
494
// FIXME(jseyfried): Refactor out the following logic
495
+ let fragment_kind = invoc. fragment_kind ;
495
496
let ( expanded_fragment, new_invocations) = match res {
496
497
InvocationRes :: Single ( ext) => match self . expand_invoc ( invoc, & ext. kind ) {
497
498
ExpandResult :: Ready ( fragment) => self . collect_invocations ( fragment, & [ ] ) ,
@@ -512,36 +513,45 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
512
513
InvocationRes :: DeriveContainer ( _exts) => {
513
514
// FIXME: Consider using the derive resolutions (`_exts`) immediately,
514
515
// instead of enqueuing the derives to be resolved again later.
515
- let ( derives, item) = match invoc. kind {
516
+ let ( derives, mut item) = match invoc. kind {
516
517
InvocationKind :: DeriveContainer { derives, item } => ( derives, item) ,
517
518
_ => unreachable ! ( ) ,
518
519
} ;
519
- if !item. derive_allowed ( ) {
520
+ let ( item , derive_placeholders ) = if !item. derive_allowed ( ) {
520
521
self . error_derive_forbidden_on_non_adt ( & derives, & item) ;
521
- }
522
+ item. visit_attrs ( |attrs| attrs. retain ( |a| !a. has_name ( sym:: derive) ) ) ;
523
+ ( item, Vec :: new ( ) )
524
+ } else {
525
+ let mut item = StripUnconfigured {
526
+ sess : self . cx . sess ,
527
+ features : self . cx . ecfg . features ,
528
+ }
529
+ . fully_configure ( item) ;
530
+ item. visit_attrs ( |attrs| attrs. retain ( |a| !a. has_name ( sym:: derive) ) ) ;
531
+
532
+ invocations. reserve ( derives. len ( ) ) ;
533
+ let derive_placeholders = derives
534
+ . into_iter ( )
535
+ . map ( |path| {
536
+ let expn_id = ExpnId :: fresh ( None ) ;
537
+ invocations. push ( (
538
+ Invocation {
539
+ kind : InvocationKind :: Derive { path, item : item. clone ( ) } ,
540
+ fragment_kind,
541
+ expansion_data : ExpansionData {
542
+ id : expn_id,
543
+ ..self . cx . current_expansion . clone ( )
544
+ } ,
545
+ } ,
546
+ None ,
547
+ ) ) ;
548
+ NodeId :: placeholder_from_expn_id ( expn_id)
549
+ } )
550
+ . collect :: < Vec < _ > > ( ) ;
551
+ ( item, derive_placeholders)
552
+ } ;
522
553
523
- let mut item = self . fully_configure ( item) ;
524
- item. visit_attrs ( |attrs| attrs. retain ( |a| !a. has_name ( sym:: derive) ) ) ;
525
-
526
- let mut derive_placeholders = Vec :: with_capacity ( derives. len ( ) ) ;
527
- invocations. reserve ( derives. len ( ) ) ;
528
- for path in derives {
529
- let expn_id = ExpnId :: fresh ( None ) ;
530
- derive_placeholders. push ( NodeId :: placeholder_from_expn_id ( expn_id) ) ;
531
- invocations. push ( (
532
- Invocation {
533
- kind : InvocationKind :: Derive { path, item : item. clone ( ) } ,
534
- fragment_kind : invoc. fragment_kind ,
535
- expansion_data : ExpansionData {
536
- id : expn_id,
537
- ..invoc. expansion_data . clone ( )
538
- } ,
539
- } ,
540
- None ,
541
- ) ) ;
542
- }
543
- let fragment =
544
- invoc. fragment_kind . expect_from_annotatables ( :: std:: iter:: once ( item) ) ;
554
+ let fragment = fragment_kind. expect_from_annotatables ( :: std:: iter:: once ( item) ) ;
545
555
self . collect_invocations ( fragment, & derive_placeholders)
546
556
}
547
557
} ;
0 commit comments