@@ -14,19 +14,18 @@ use syntax_bridge::{syntax_node_to_token_tree, DocCommentDesugarMode};
14
14
use triomphe:: Arc ;
15
15
16
16
use crate :: {
17
- EagerCallInfo ,
18
- attrs:: { AttrId , collect_attrs} ,
17
+ attrs:: { collect_attrs, AttrId } ,
19
18
builtin:: pseudo_derive_attr_expansion,
20
19
cfg_process,
21
20
declarative:: DeclarativeMacroExpander ,
22
21
fixup:: { self , SyntaxFixupUndoInfo } ,
23
22
hygiene:: {
24
- SyntaxContextExt as _ , span_with_call_site_ctxt, span_with_def_site_ctxt,
25
- span_with_mixed_site_ctxt ,
23
+ span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt ,
24
+ SyntaxContextExt as _ ,
26
25
} ,
27
26
proc_macro:: { CustomProcMacroExpander , ProcMacros } ,
28
27
span_map:: { ExpansionSpanMap , RealSpanMap , SpanMap , SpanMapRef } ,
29
- tt, AstId , BuiltinAttrExpander , BuiltinDeriveExpander , BuiltinFnLikeExpander ,
28
+ tt, AstId , BuiltinAttrExpander , BuiltinDeriveExpander , BuiltinFnLikeExpander , EagerCallInfo ,
30
29
EagerExpander , ExpandError , ExpandResult , ExpandTo , MacroCallKind , MacroCallLoc , MacroDefId ,
31
30
MacroDefKind ,
32
31
} ;
@@ -217,14 +216,8 @@ pub fn expand_speculative(
217
216
) ,
218
217
SyntaxFixupUndoInfo :: NONE ,
219
218
) ,
220
- MacroCallKind :: Derive {
221
- derive_attr_index : index,
222
- ..
223
- }
224
- | MacroCallKind :: Attr {
225
- invoc_attr_index : index,
226
- ..
227
- } => {
219
+ MacroCallKind :: Derive { derive_attr_index : index, .. }
220
+ | MacroCallKind :: Attr { invoc_attr_index : index, .. } => {
228
221
let censor = if let MacroCallKind :: Derive { .. } = loc. kind {
229
222
censor_derive_input ( index, & ast:: Adt :: cast ( speculative_args. clone ( ) ) ?)
230
223
} else {
@@ -264,9 +257,7 @@ pub fn expand_speculative(
264
257
} ;
265
258
266
259
let attr_arg = match loc. kind {
267
- MacroCallKind :: Attr {
268
- invoc_attr_index, ..
269
- } => {
260
+ MacroCallKind :: Attr { invoc_attr_index, .. } => {
270
261
let attr = if loc. def . is_attribute_derive ( ) {
271
262
// for pseudo-derive expansion we actually pass the attribute itself only
272
263
ast:: Attr :: cast ( speculative_args. clone ( ) )
@@ -317,18 +308,17 @@ pub fn expand_speculative(
317
308
pseudo_derive_attr_expansion ( & tt, attr_arg. as_ref ( ) ?, span)
318
309
}
319
310
MacroDefKind :: Declarative ( it) => {
320
- db. decl_macro_expander ( loc. krate , it)
321
- . expand_unhygienic ( tt, span, loc. def . edition )
311
+ db. decl_macro_expander ( loc. krate , it) . expand_unhygienic ( tt, span, loc. def . edition )
312
+ }
313
+ MacroDefKind :: BuiltIn ( _, it) => {
314
+ it. expand ( db, actual_macro_call, & tt, span) . map_err ( Into :: into)
315
+ }
316
+ MacroDefKind :: BuiltInDerive ( _, it) => {
317
+ it. expand ( db, actual_macro_call, & tt, span) . map_err ( Into :: into)
318
+ }
319
+ MacroDefKind :: BuiltInEager ( _, it) => {
320
+ it. expand ( db, actual_macro_call, & tt, span) . map_err ( Into :: into)
322
321
}
323
- MacroDefKind :: BuiltIn ( _, it) => it
324
- . expand ( db, actual_macro_call, & tt, span)
325
- . map_err ( Into :: into) ,
326
- MacroDefKind :: BuiltInDerive ( _, it) => it
327
- . expand ( db, actual_macro_call, & tt, span)
328
- . map_err ( Into :: into) ,
329
- MacroDefKind :: BuiltInEager ( _, it) => it
330
- . expand ( db, actual_macro_call, & tt, span)
331
- . map_err ( Into :: into) ,
332
322
MacroDefKind :: BuiltInAttr ( _, it) => it. expand ( db, actual_macro_call, & tt, span) ,
333
323
} ;
334
324
@@ -341,12 +331,7 @@ pub fn expand_speculative(
341
331
let syntax_node = node. syntax_node ( ) ;
342
332
let token = rev_tmap
343
333
. ranges_with_span ( span_map. span_for_range ( token_to_map. text_range ( ) ) )
344
- . filter_map ( |( range, ctx) | {
345
- syntax_node
346
- . covering_element ( range)
347
- . into_token ( )
348
- . zip ( Some ( ctx) )
349
- } )
334
+ . filter_map ( |( range, ctx) | syntax_node. covering_element ( range) . into_token ( ) . zip ( Some ( ctx) ) )
350
335
. map ( |( t, ctx) | {
351
336
// prefer tokens of the same kind and text, as well as non opaque marked ones
352
337
// Note the inversion of the score here, as we want to prefer the first token in case
@@ -389,10 +374,8 @@ fn parse_macro_expansion(
389
374
let loc = db. lookup_intern_macro_call ( macro_file. macro_call_id ) ;
390
375
let def_edition = loc. def . edition ;
391
376
let expand_to = loc. expand_to ( ) ;
392
- let mbe:: ValueResult {
393
- value : ( tt, matched_arm) ,
394
- err,
395
- } = macro_expand ( db, macro_file. macro_call_id , loc) ;
377
+ let mbe:: ValueResult { value : ( tt, matched_arm) , err } =
378
+ macro_expand ( db, macro_file. macro_call_id , loc) ;
396
379
397
380
let ( parse, mut rev_token_map) = token_tree_to_syntax_node (
398
381
db,
@@ -405,19 +388,15 @@ fn parse_macro_expansion(
405
388
) ;
406
389
rev_token_map. matched_arm = matched_arm;
407
390
408
- ExpandResult {
409
- value : ( parse, Arc :: new ( rev_token_map) ) ,
410
- err,
411
- }
391
+ ExpandResult { value : ( parse, Arc :: new ( rev_token_map) ) , err }
412
392
}
413
393
414
394
fn parse_macro_expansion_error (
415
395
db : & dyn ExpandDatabase ,
416
396
macro_call_id : MacroCallId ,
417
397
) -> Option < Arc < ExpandResult < Arc < [ SyntaxError ] > > > > {
418
- let e: ExpandResult < Arc < [ SyntaxError ] > > = db
419
- . parse_macro_expansion ( MacroFileId { macro_call_id } )
420
- . map ( |it| Arc :: from ( it. 0 . errors ( ) ) ) ;
398
+ let e: ExpandResult < Arc < [ SyntaxError ] > > =
399
+ db. parse_macro_expansion ( MacroFileId { macro_call_id } ) . map ( |it| Arc :: from ( it. 0 . errors ( ) ) ) ;
421
400
if e. value . is_empty ( ) && e. err . is_none ( ) {
422
401
None
423
402
} else {
@@ -457,9 +436,7 @@ fn macro_arg_considering_derives(
457
436
) -> MacroArgResult {
458
437
match kind {
459
438
// Get the macro arg for the derive macro
460
- MacroCallKind :: Derive {
461
- derive_macro_id, ..
462
- } => db. macro_arg ( * derive_macro_id) ,
439
+ MacroCallKind :: Derive { derive_macro_id, .. } => db. macro_arg ( * derive_macro_id) ,
463
440
// Normal macro arg
464
441
_ => db. macro_arg ( id) ,
465
442
}
@@ -469,13 +446,8 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
469
446
let loc = db. lookup_intern_macro_call ( id) ;
470
447
471
448
if let MacroCallLoc {
472
- def : MacroDefId {
473
- kind : MacroDefKind :: BuiltInEager ( ..) ,
474
- ..
475
- } ,
476
- kind : MacroCallKind :: FnLike {
477
- eager : Some ( eager) , ..
478
- } ,
449
+ def : MacroDefId { kind : MacroDefKind :: BuiltInEager ( ..) , .. } ,
450
+ kind : MacroCallKind :: FnLike { eager : Some ( eager) , .. } ,
479
451
..
480
452
} = & loc
481
453
{
@@ -488,20 +460,15 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
488
460
let ( censor, item_node, span) = match loc. kind {
489
461
MacroCallKind :: FnLike { ast_id, .. } => {
490
462
let node = & ast_id. to_ptr ( db) . to_node ( & root) ;
491
- let path_range = node. path ( ) . map_or_else (
492
- || node. syntax ( ) . text_range ( ) ,
493
- |path| path. syntax ( ) . text_range ( ) ,
494
- ) ;
463
+ let path_range = node
464
+ . path ( )
465
+ . map_or_else ( || node. syntax ( ) . text_range ( ) , |path| path. syntax ( ) . text_range ( ) ) ;
495
466
let span = map. span_for_range ( path_range) ;
496
467
497
468
let dummy_tt = |kind| {
498
469
(
499
470
Arc :: new ( tt:: TopSubtree :: from_token_trees (
500
- tt:: Delimiter {
501
- open : span,
502
- close : span,
503
- kind,
504
- } ,
471
+ tt:: Delimiter { open : span, close : span, kind } ,
505
472
tt:: TokenTreesView :: new ( & [ ] ) ,
506
473
) ) ,
507
474
SyntaxFixupUndoInfo :: default ( ) ,
@@ -512,14 +479,8 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
512
479
let Some ( tt) = node. token_tree ( ) else {
513
480
return dummy_tt ( tt:: DelimiterKind :: Invisible ) ;
514
481
} ;
515
- let first = tt
516
- . left_delimiter_token ( )
517
- . map ( |it| it. kind ( ) )
518
- . unwrap_or ( T ! [ '(' ] ) ;
519
- let last = tt
520
- . right_delimiter_token ( )
521
- . map ( |it| it. kind ( ) )
522
- . unwrap_or ( T ! [ . ] ) ;
482
+ let first = tt. left_delimiter_token ( ) . map ( |it| it. kind ( ) ) . unwrap_or ( T ! [ '(' ] ) ;
483
+ let last = tt. right_delimiter_token ( ) . map ( |it| it. kind ( ) ) . unwrap_or ( T ! [ . ] ) ;
523
484
524
485
let mismatched_delimiters = !matches ! (
525
486
( first, last) ,
@@ -564,11 +525,7 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
564
525
MacroCallKind :: Derive { .. } => {
565
526
unreachable ! ( "`ExpandDatabase::macro_arg` called with `MacroCallKind::Derive`" )
566
527
}
567
- MacroCallKind :: Attr {
568
- ast_id,
569
- invoc_attr_index,
570
- ..
571
- } => {
528
+ MacroCallKind :: Attr { ast_id, invoc_attr_index, .. } => {
572
529
let node = ast_id. to_ptr ( db) . to_node ( & root) ;
573
530
let attr_source = attr_source ( invoc_attr_index, & node) ;
574
531
@@ -586,14 +543,7 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
586
543
let censor_derive_input = censor_derive_input ( invoc_attr_index, & adt) ;
587
544
( censor_derive_input, node, span)
588
545
} else {
589
- (
590
- attr_source
591
- . into_iter ( )
592
- . map ( |it| it. syntax ( ) . clone ( ) . into ( ) )
593
- . collect ( ) ,
594
- node,
595
- span,
596
- )
546
+ ( attr_source. into_iter ( ) . map ( |it| it. syntax ( ) . clone ( ) . into ( ) ) . collect ( ) , node, span)
597
547
}
598
548
}
599
549
} ;
@@ -653,9 +603,7 @@ fn censor_derive_input(derive_attr_index: AttrId, node: &ast::Adt) -> FxHashSet<
653
603
fn attr_source ( invoc_attr_index : AttrId , node : & ast:: Item ) -> Option < ast:: Attr > {
654
604
// FIXME: handle `cfg_attr`
655
605
cov_mark:: hit!( attribute_macro_attr_censoring) ;
656
- collect_attrs ( node)
657
- . nth ( invoc_attr_index. ast_index ( ) )
658
- . and_then ( |( _, attr) | Either :: left ( attr) )
606
+ collect_attrs ( node) . nth ( invoc_attr_index. ast_index ( ) ) . and_then ( |( _, attr) | Either :: left ( attr) )
659
607
}
660
608
661
609
impl TokenExpander {
@@ -685,13 +633,7 @@ fn macro_expand(
685
633
) -> ExpandResult < ( CowArc < tt:: TopSubtree > , MatchedArmIndex ) > {
686
634
let _p = tracing:: info_span!( "macro_expand" ) . entered ( ) ;
687
635
688
- let (
689
- ExpandResult {
690
- value : ( tt, matched_arm) ,
691
- err,
692
- } ,
693
- span,
694
- ) = match loc. def . kind {
636
+ let ( ExpandResult { value : ( tt, matched_arm) , err } , span) = match loc. def . kind {
695
637
MacroDefKind :: ProcMacro ( ..) => {
696
638
return db. expand_proc_macro ( macro_call_id) . map ( CowArc :: Arc ) . zip_val ( None ) ;
697
639
}
@@ -705,14 +647,12 @@ fn macro_expand(
705
647
MacroDefKind :: Declarative ( id) => db
706
648
. decl_macro_expander ( loc. def . krate , id)
707
649
. expand ( db, arg. clone ( ) , macro_call_id, span) ,
708
- MacroDefKind :: BuiltIn ( _, it) => it
709
- . expand ( db, macro_call_id, arg, span)
710
- . map_err ( Into :: into)
711
- . zip_val ( None ) ,
712
- MacroDefKind :: BuiltInDerive ( _, it) => it
713
- . expand ( db, macro_call_id, arg, span)
714
- . map_err ( Into :: into)
715
- . zip_val ( None ) ,
650
+ MacroDefKind :: BuiltIn ( _, it) => {
651
+ it. expand ( db, macro_call_id, arg, span) . map_err ( Into :: into) . zip_val ( None )
652
+ }
653
+ MacroDefKind :: BuiltInDerive ( _, it) => {
654
+ it. expand ( db, macro_call_id, arg, span) . map_err ( Into :: into) . zip_val ( None )
655
+ }
716
656
MacroDefKind :: BuiltInEager ( _, it) => {
717
657
// This might look a bit odd, but we do not expand the inputs to eager macros here.
718
658
// Eager macros inputs are expanded, well, eagerly when we collect the macro calls.
@@ -725,9 +665,7 @@ fn macro_expand(
725
665
return ExpandResult :: ok ( CowArc :: Arc ( macro_arg. clone ( ) ) )
726
666
. zip_val ( None ) ;
727
667
}
728
- MacroCallKind :: FnLike {
729
- eager : Some ( eager) , ..
730
- } => Some ( & * * eager) ,
668
+ MacroCallKind :: FnLike { eager : Some ( eager) , .. } => Some ( & * * eager) ,
731
669
_ => None ,
732
670
} ;
733
671
@@ -746,13 +684,7 @@ fn macro_expand(
746
684
}
747
685
MacroDefKind :: ProcMacro ( _, _, _) => unreachable ! ( ) ,
748
686
} ;
749
- (
750
- ExpandResult {
751
- value : res. value ,
752
- err : res. err ,
753
- } ,
754
- span,
755
- )
687
+ ( ExpandResult { value : res. value , err : res. err } , span)
756
688
}
757
689
} ;
758
690
@@ -766,10 +698,7 @@ fn macro_expand(
766
698
}
767
699
}
768
700
769
- ExpandResult {
770
- value : ( CowArc :: Owned ( tt) , matched_arm) ,
771
- err,
772
- }
701
+ ExpandResult { value : ( CowArc :: Owned ( tt) , matched_arm) , err }
773
702
}
774
703
775
704
fn proc_macro_span ( db : & dyn ExpandDatabase , ast : AstId < ast:: Fn > ) -> Span {
@@ -778,10 +707,8 @@ fn proc_macro_span(db: &dyn ExpandDatabase, ast: AstId<ast::Fn>) -> Span {
778
707
let span_map = & db. span_map ( ast. file_id ) ;
779
708
780
709
let node = ast_id_map. get ( ast. value ) . to_node ( & root) ;
781
- let range = ast:: HasName :: name ( & node) . map_or_else (
782
- || node. syntax ( ) . text_range ( ) ,
783
- |name| name. syntax ( ) . text_range ( ) ,
784
- ) ;
710
+ let range = ast:: HasName :: name ( & node)
711
+ . map_or_else ( || node. syntax ( ) . text_range ( ) , |name| name. syntax ( ) . text_range ( ) ) ;
785
712
span_map. span_for_range ( range)
786
713
}
787
714
@@ -798,10 +725,7 @@ fn expand_proc_macro(
798
725
} ;
799
726
800
727
let attr_arg = match & loc. kind {
801
- MacroCallKind :: Attr {
802
- attr_args : Some ( attr_args) ,
803
- ..
804
- } => Some ( & * * attr_args) ,
728
+ MacroCallKind :: Attr { attr_args : Some ( attr_args) , .. } => Some ( & * * attr_args) ,
805
729
_ => None ,
806
730
} ;
807
731
@@ -826,10 +750,7 @@ fn expand_proc_macro(
826
750
827
751
fixup:: reverse_fixups ( & mut tt, & undo_info) ;
828
752
829
- ExpandResult {
830
- value : Arc :: new ( tt) ,
831
- err,
832
- }
753
+ ExpandResult { value : Arc :: new ( tt) , err }
833
754
}
834
755
835
756
pub ( crate ) fn token_tree_to_syntax_node (
0 commit comments