3
3
use base_db:: { salsa, CrateId , FileId , SourceDatabase } ;
4
4
use either:: Either ;
5
5
use limit:: Limit ;
6
- use mbe:: syntax_node_to_token_tree;
6
+ use mbe:: { syntax_node_to_token_tree, MatchedArmIndex } ;
7
7
use rustc_hash:: FxHashSet ;
8
8
use span:: { AstIdMap , Span , SyntaxContextData , SyntaxContextId } ;
9
9
use syntax:: { ast, AstNode , Parse , SyntaxElement , SyntaxError , SyntaxNode , SyntaxToken , T } ;
@@ -313,16 +313,18 @@ fn parse_macro_expansion(
313
313
let loc = db. lookup_intern_macro_call ( macro_file. macro_call_id ) ;
314
314
let edition = loc. def . edition ;
315
315
let expand_to = loc. expand_to ( ) ;
316
- let mbe:: ValueResult { value : tt, err } = macro_expand ( db, macro_file. macro_call_id , loc) ;
316
+ let mbe:: ValueResult { value : ( tt, matched_arm) , err } =
317
+ macro_expand ( db, macro_file. macro_call_id , loc) ;
317
318
318
- let ( parse, rev_token_map) = token_tree_to_syntax_node (
319
+ let ( parse, mut rev_token_map) = token_tree_to_syntax_node (
319
320
match & tt {
320
321
CowArc :: Arc ( it) => it,
321
322
CowArc :: Owned ( it) => it,
322
323
} ,
323
324
expand_to,
324
325
edition,
325
326
) ;
327
+ rev_token_map. matched_arm = matched_arm;
326
328
327
329
ExpandResult { value : ( parse, Arc :: new ( rev_token_map) ) , err }
328
330
}
@@ -544,11 +546,13 @@ fn macro_expand(
544
546
db : & dyn ExpandDatabase ,
545
547
macro_call_id : MacroCallId ,
546
548
loc : MacroCallLoc ,
547
- ) -> ExpandResult < CowArc < tt:: Subtree > > {
549
+ ) -> ExpandResult < ( CowArc < tt:: Subtree > , MatchedArmIndex ) > {
548
550
let _p = tracing:: span!( tracing:: Level :: INFO , "macro_expand" ) . entered ( ) ;
549
551
550
- let ( ExpandResult { value : tt, err } , span) = match loc. def . kind {
551
- MacroDefKind :: ProcMacro ( ..) => return db. expand_proc_macro ( macro_call_id) . map ( CowArc :: Arc ) ,
552
+ let ( ExpandResult { value : ( tt, matched_arm) , err } , span) = match loc. def . kind {
553
+ MacroDefKind :: ProcMacro ( ..) => {
554
+ return db. expand_proc_macro ( macro_call_id) . map ( CowArc :: Arc ) . zip_val ( None )
555
+ }
552
556
_ => {
553
557
let ( macro_arg, undo_info, span) =
554
558
db. macro_arg_considering_derives ( macro_call_id, & loc. kind ) ;
@@ -560,10 +564,10 @@ fn macro_expand(
560
564
. decl_macro_expander ( loc. def . krate , id)
561
565
. expand ( db, arg. clone ( ) , macro_call_id, span) ,
562
566
MacroDefKind :: BuiltIn ( it, _) => {
563
- it. expand ( db, macro_call_id, arg, span) . map_err ( Into :: into)
567
+ it. expand ( db, macro_call_id, arg, span) . map_err ( Into :: into) . zip_val ( None )
564
568
}
565
569
MacroDefKind :: BuiltInDerive ( it, _) => {
566
- it. expand ( db, macro_call_id, arg, span) . map_err ( Into :: into)
570
+ it. expand ( db, macro_call_id, arg, span) . map_err ( Into :: into) . zip_val ( None )
567
571
}
568
572
MacroDefKind :: BuiltInEager ( it, _) => {
569
573
// This might look a bit odd, but we do not expand the inputs to eager macros here.
@@ -574,7 +578,8 @@ fn macro_expand(
574
578
// As such we just return the input subtree here.
575
579
let eager = match & loc. kind {
576
580
MacroCallKind :: FnLike { eager : None , .. } => {
577
- return ExpandResult :: ok ( CowArc :: Arc ( macro_arg. clone ( ) ) ) ;
581
+ return ExpandResult :: ok ( CowArc :: Arc ( macro_arg. clone ( ) ) )
582
+ . zip_val ( None ) ;
578
583
}
579
584
MacroCallKind :: FnLike { eager : Some ( eager) , .. } => Some ( & * * eager) ,
580
585
_ => None ,
@@ -586,12 +591,12 @@ fn macro_expand(
586
591
// FIXME: We should report both errors!
587
592
res. err = error. clone ( ) . or ( res. err ) ;
588
593
}
589
- res
594
+ res. zip_val ( None )
590
595
}
591
596
MacroDefKind :: BuiltInAttr ( it, _) => {
592
597
let mut res = it. expand ( db, macro_call_id, arg, span) ;
593
598
fixup:: reverse_fixups ( & mut res. value , & undo_info) ;
594
- res
599
+ res. zip_val ( None )
595
600
}
596
601
_ => unreachable ! ( ) ,
597
602
} ;
@@ -603,16 +608,18 @@ fn macro_expand(
603
608
if !loc. def . is_include ( ) {
604
609
// Set a hard limit for the expanded tt
605
610
if let Err ( value) = check_tt_count ( & tt) {
606
- return value. map ( |( ) | {
607
- CowArc :: Owned ( tt:: Subtree {
608
- delimiter : tt:: Delimiter :: invisible_spanned ( span) ,
609
- token_trees : Box :: new ( [ ] ) ,
611
+ return value
612
+ . map ( |( ) | {
613
+ CowArc :: Owned ( tt:: Subtree {
614
+ delimiter : tt:: Delimiter :: invisible_spanned ( span) ,
615
+ token_trees : Box :: new ( [ ] ) ,
616
+ } )
610
617
} )
611
- } ) ;
618
+ . zip_val ( matched_arm ) ;
612
619
}
613
620
}
614
621
615
- ExpandResult { value : CowArc :: Owned ( tt) , err }
622
+ ExpandResult { value : ( CowArc :: Owned ( tt) , matched_arm ) , err }
616
623
}
617
624
618
625
fn proc_macro_span ( db : & dyn ExpandDatabase , ast : AstId < ast:: Fn > ) -> Span {
0 commit comments