@@ -390,7 +390,13 @@ fn parse_macro_expansion(
390
390
let expand_to = loc. expand_to ( ) ;
391
391
let mbe:: ValueResult { value : tt, err } = macro_expand ( db, macro_file. macro_call_id , loc) ;
392
392
393
- let ( parse, rev_token_map) = token_tree_to_syntax_node ( & tt, expand_to) ;
393
+ let ( parse, rev_token_map) = token_tree_to_syntax_node (
394
+ match & tt {
395
+ CowArc :: Arc ( it) => it,
396
+ CowArc :: Owned ( it) => it,
397
+ } ,
398
+ expand_to,
399
+ ) ;
394
400
395
401
ExpandResult { value : ( parse, Arc :: new ( rev_token_map) ) , err }
396
402
}
@@ -669,15 +675,20 @@ fn macro_expander(db: &dyn ExpandDatabase, id: MacroDefId) -> TokenExpander {
669
675
}
670
676
}
671
677
678
+ enum CowArc < T > {
679
+ Arc ( Arc < T > ) ,
680
+ Owned ( T ) ,
681
+ }
682
+
672
683
fn macro_expand (
673
684
db : & dyn ExpandDatabase ,
674
685
macro_call_id : MacroCallId ,
675
686
loc : MacroCallLoc ,
676
- ) -> ExpandResult < Arc < tt:: Subtree > > {
687
+ ) -> ExpandResult < CowArc < tt:: Subtree > > {
677
688
let _p = profile:: span ( "macro_expand" ) ;
678
689
679
690
let ExpandResult { value : tt, mut err } = match loc. def . kind {
680
- MacroDefKind :: ProcMacro ( ..) => return db. expand_proc_macro ( macro_call_id) ,
691
+ MacroDefKind :: ProcMacro ( ..) => return db. expand_proc_macro ( macro_call_id) . map ( CowArc :: Arc ) ,
681
692
MacroDefKind :: BuiltInDerive ( expander, ..) => {
682
693
let ( root, map) = parse_with_map ( db, loc. kind . file_id ( ) ) ;
683
694
let root = root. syntax_node ( ) ;
@@ -692,7 +703,7 @@ fn macro_expand(
692
703
let ValueResult { value, err } = db. macro_arg ( macro_call_id) ;
693
704
let Some ( ( macro_arg, undo_info) ) = value else {
694
705
return ExpandResult {
695
- value : Arc :: new ( tt:: Subtree {
706
+ value : CowArc :: Owned ( tt:: Subtree {
696
707
delimiter : tt:: Delimiter :: invisible_spanned ( loc. call_site ) ,
697
708
token_trees : Vec :: new ( ) ,
698
709
} ) ,
@@ -718,7 +729,7 @@ fn macro_expand(
718
729
// As such we just return the input subtree here.
719
730
MacroDefKind :: BuiltInEager ( ..) if loc. eager . is_none ( ) => {
720
731
return ExpandResult {
721
- value : macro_arg. clone ( ) ,
732
+ value : CowArc :: Arc ( macro_arg. clone ( ) ) ,
722
733
err : err. map ( |err| {
723
734
let mut buf = String :: new ( ) ;
724
735
for err in & * * err {
@@ -752,12 +763,17 @@ fn macro_expand(
752
763
// Skip checking token tree limit for include! macro call
753
764
if !loc. def . is_include ( ) {
754
765
// Set a hard limit for the expanded tt
755
- if let Err ( value) = check_tt_count ( & tt, loc. call_site ) {
756
- return value;
766
+ if let Err ( value) = check_tt_count ( & tt) {
767
+ return value. map ( |( ) | {
768
+ CowArc :: Owned ( tt:: Subtree {
769
+ delimiter : tt:: Delimiter :: invisible_spanned ( loc. call_site ) ,
770
+ token_trees : vec ! [ ] ,
771
+ } )
772
+ } ) ;
757
773
}
758
774
}
759
775
760
- ExpandResult { value : Arc :: new ( tt) , err }
776
+ ExpandResult { value : CowArc :: Owned ( tt) , err }
761
777
}
762
778
763
779
fn expand_proc_macro ( db : & dyn ExpandDatabase , id : MacroCallId ) -> ExpandResult < Arc < tt:: Subtree > > {
@@ -796,8 +812,13 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
796
812
) ;
797
813
798
814
// Set a hard limit for the expanded tt
799
- if let Err ( value) = check_tt_count ( & tt, loc. call_site ) {
800
- return value;
815
+ if let Err ( value) = check_tt_count ( & tt) {
816
+ return value. map ( |( ) | {
817
+ Arc :: new ( tt:: Subtree {
818
+ delimiter : tt:: Delimiter :: invisible_spanned ( loc. call_site ) ,
819
+ token_trees : vec ! [ ] ,
820
+ } )
821
+ } ) ;
801
822
}
802
823
803
824
fixup:: reverse_fixups ( & mut tt, & undo_info) ;
@@ -819,14 +840,11 @@ fn token_tree_to_syntax_node(
819
840
mbe:: token_tree_to_syntax_node ( tt, entry_point)
820
841
}
821
842
822
- fn check_tt_count ( tt : & tt:: Subtree , call_site : Span ) -> Result < ( ) , ExpandResult < Arc < tt :: Subtree > > > {
843
+ fn check_tt_count ( tt : & tt:: Subtree ) -> Result < ( ) , ExpandResult < ( ) > > {
823
844
let count = tt. count ( ) ;
824
845
if TOKEN_LIMIT . check ( count) . is_err ( ) {
825
846
Err ( ExpandResult {
826
- value : Arc :: new ( tt:: Subtree {
827
- delimiter : tt:: Delimiter :: invisible_spanned ( call_site) ,
828
- token_trees : vec ! [ ] ,
829
- } ) ,
847
+ value : ( ) ,
830
848
err : Some ( ExpandError :: other ( format ! (
831
849
"macro invocation exceeds token limit: produced {} tokens, limit is {}" ,
832
850
count,
0 commit comments