33use base_db:: { salsa, CrateId , FileId , SourceDatabase } ;
44use either:: Either ;
55use limit:: Limit ;
6- use mbe:: syntax_node_to_token_tree;
6+ use mbe:: { syntax_node_to_token_tree, MatchedArmIndex } ;
77use rustc_hash:: FxHashSet ;
88use span:: { AstIdMap , Span , SyntaxContextData , SyntaxContextId } ;
99use syntax:: { ast, AstNode , Parse , SyntaxElement , SyntaxError , SyntaxNode , SyntaxToken , T } ;
@@ -313,16 +313,18 @@ fn parse_macro_expansion(
313313 let loc = db. lookup_intern_macro_call ( macro_file. macro_call_id ) ;
314314 let edition = loc. def . edition ;
315315 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) ;
317318
318- let ( parse, rev_token_map) = token_tree_to_syntax_node (
319+ let ( parse, mut rev_token_map) = token_tree_to_syntax_node (
319320 match & tt {
320321 CowArc :: Arc ( it) => it,
321322 CowArc :: Owned ( it) => it,
322323 } ,
323324 expand_to,
324325 edition,
325326 ) ;
327+ rev_token_map. matched_arm = matched_arm;
326328
327329 ExpandResult { value : ( parse, Arc :: new ( rev_token_map) ) , err }
328330}
@@ -544,11 +546,13 @@ fn macro_expand(
544546 db : & dyn ExpandDatabase ,
545547 macro_call_id : MacroCallId ,
546548 loc : MacroCallLoc ,
547- ) -> ExpandResult < CowArc < tt:: Subtree > > {
549+ ) -> ExpandResult < ( CowArc < tt:: Subtree > , MatchedArmIndex ) > {
548550 let _p = tracing:: span!( tracing:: Level :: INFO , "macro_expand" ) . entered ( ) ;
549551
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+ }
552556 _ => {
553557 let ( macro_arg, undo_info, span) =
554558 db. macro_arg_considering_derives ( macro_call_id, & loc. kind ) ;
@@ -560,10 +564,10 @@ fn macro_expand(
560564 . decl_macro_expander ( loc. def . krate , id)
561565 . expand ( db, arg. clone ( ) , macro_call_id, span) ,
562566 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 )
564568 }
565569 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 )
567571 }
568572 MacroDefKind :: BuiltInEager ( it, _) => {
569573 // This might look a bit odd, but we do not expand the inputs to eager macros here.
@@ -574,7 +578,8 @@ fn macro_expand(
574578 // As such we just return the input subtree here.
575579 let eager = match & loc. kind {
576580 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 ) ;
578583 }
579584 MacroCallKind :: FnLike { eager : Some ( eager) , .. } => Some ( & * * eager) ,
580585 _ => None ,
@@ -586,12 +591,12 @@ fn macro_expand(
586591 // FIXME: We should report both errors!
587592 res. err = error. clone ( ) . or ( res. err ) ;
588593 }
589- res
594+ res. zip_val ( None )
590595 }
591596 MacroDefKind :: BuiltInAttr ( it, _) => {
592597 let mut res = it. expand ( db, macro_call_id, arg, span) ;
593598 fixup:: reverse_fixups ( & mut res. value , & undo_info) ;
594- res
599+ res. zip_val ( None )
595600 }
596601 _ => unreachable ! ( ) ,
597602 } ;
@@ -603,16 +608,18 @@ fn macro_expand(
603608 if !loc. def . is_include ( ) {
604609 // Set a hard limit for the expanded tt
605610 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+ } )
610617 } )
611- } ) ;
618+ . zip_val ( matched_arm ) ;
612619 }
613620 }
614621
615- ExpandResult { value : CowArc :: Owned ( tt) , err }
622+ ExpandResult { value : ( CowArc :: Owned ( tt) , matched_arm ) , err }
616623}
617624
618625fn proc_macro_span ( db : & dyn ExpandDatabase , ast : AstId < ast:: Fn > ) -> Span {
0 commit comments