@@ -892,7 +892,7 @@ impl<'db> SemanticsImpl<'db> {
892892
893893 if first == last {
894894 // node is just the token, so descend the token
895- self . descend_into_macros_impl (
895+ self . descend_into_macros_all (
896896 InFile :: new ( file. file_id , first) ,
897897 false ,
898898 & mut |InFile { value, .. } , _ctx| {
@@ -903,23 +903,19 @@ impl<'db> SemanticsImpl<'db> {
903903 {
904904 res. push ( node)
905905 }
906- CONTINUE_NO_BREAKS
907906 } ,
908907 ) ;
909908 } else {
910909 // Descend first and last token, then zip them to look for the node they belong to
911910 let mut scratch: SmallVec < [ _ ; 1 ] > = smallvec ! [ ] ;
912- self . descend_into_macros_impl (
911+ self . descend_into_macros_all (
913912 InFile :: new ( file. file_id , first) ,
914913 false ,
915- & mut |token, _ctx| {
916- scratch. push ( token) ;
917- CONTINUE_NO_BREAKS
918- } ,
914+ & mut |token, _ctx| scratch. push ( token) ,
919915 ) ;
920916
921917 let mut scratch = scratch. into_iter ( ) ;
922- self . descend_into_macros_impl (
918+ self . descend_into_macros_all (
923919 InFile :: new ( file. file_id , last) ,
924920 false ,
925921 & mut |InFile { value : last, file_id : last_fid } , _ctx| {
@@ -938,17 +934,18 @@ impl<'db> SemanticsImpl<'db> {
938934 }
939935 }
940936 }
941- CONTINUE_NO_BREAKS
942937 } ,
943938 ) ;
944939 }
945940 res
946941 }
947942
948- pub fn is_inside_macro_call ( & self , token : InFile < & SyntaxToken > ) -> bool {
949- // FIXME: Maybe `ancestors_with_macros()` is more suitable here? Currently
950- // this is only used on real (not macro) files so this is not a problem.
951- token. value . parent_ancestors ( ) . any ( |ancestor| {
943+ /// Returns true if the given input is within a macro call.
944+ ///
945+ /// Note that if this token itself is within the context of a macro expansion does not matter.
946+ /// That is, we strictly check if it lies inside the input of a macro call.
947+ pub fn is_inside_macro_call ( & self , token @ InFile { value, .. } : InFile < & SyntaxToken > ) -> bool {
948+ value. parent_ancestors ( ) . any ( |ancestor| {
952949 if ast:: MacroCall :: can_cast ( ancestor. kind ( ) ) {
953950 return true ;
954951 }
@@ -983,21 +980,17 @@ impl<'db> SemanticsImpl<'db> {
983980 token : SyntaxToken ,
984981 mut cb : impl FnMut ( InFile < SyntaxToken > , SyntaxContext ) ,
985982 ) {
986- self . descend_into_macros_impl ( self . wrap_token_infile ( token) , false , & mut |t, ctx| {
987- cb ( t, ctx) ;
988- CONTINUE_NO_BREAKS
983+ self . descend_into_macros_all ( self . wrap_token_infile ( token) , false , & mut |t, ctx| {
984+ cb ( t, ctx)
989985 } ) ;
990986 }
991987
992988 pub fn descend_into_macros ( & self , token : SyntaxToken ) -> SmallVec < [ SyntaxToken ; 1 ] > {
993989 let mut res = smallvec ! [ ] ;
994- self . descend_into_macros_impl (
990+ self . descend_into_macros_all (
995991 self . wrap_token_infile ( token. clone ( ) ) ,
996992 false ,
997- & mut |t, _ctx| {
998- res. push ( t. value ) ;
999- CONTINUE_NO_BREAKS
1000- } ,
993+ & mut |t, _ctx| res. push ( t. value ) ,
1001994 ) ;
1002995 if res. is_empty ( ) {
1003996 res. push ( token) ;
@@ -1011,12 +1004,11 @@ impl<'db> SemanticsImpl<'db> {
10111004 ) -> SmallVec < [ InFile < SyntaxToken > ; 1 ] > {
10121005 let mut res = smallvec ! [ ] ;
10131006 let token = self . wrap_token_infile ( token) ;
1014- self . descend_into_macros_impl ( token. clone ( ) , true , & mut |t, ctx| {
1007+ self . descend_into_macros_all ( token. clone ( ) , true , & mut |t, ctx| {
10151008 if !ctx. is_opaque ( self . db ) {
10161009 // Don't descend into opaque contexts
10171010 res. push ( t) ;
10181011 }
1019- CONTINUE_NO_BREAKS
10201012 } ) ;
10211013 if res. is_empty ( ) {
10221014 res. push ( token) ;
@@ -1099,6 +1091,18 @@ impl<'db> SemanticsImpl<'db> {
10991091 . unwrap_or ( token)
11001092 }
11011093
1094+ fn descend_into_macros_all (
1095+ & self ,
1096+ token : InFile < SyntaxToken > ,
1097+ always_descend_into_derives : bool ,
1098+ f : & mut dyn FnMut ( InFile < SyntaxToken > , SyntaxContext ) ,
1099+ ) {
1100+ self . descend_into_macros_impl ( token, always_descend_into_derives, & mut |tok, ctx| {
1101+ f ( tok, ctx) ;
1102+ CONTINUE_NO_BREAKS
1103+ } ) ;
1104+ }
1105+
11021106 fn descend_into_macros_impl < T > (
11031107 & self ,
11041108 InFile { value : token, file_id } : InFile < SyntaxToken > ,
@@ -1467,25 +1471,31 @@ impl<'db> SemanticsImpl<'db> {
14671471 }
14681472
14691473 /// Iterates the ancestors of the given node, climbing up macro expansions while doing so.
1474+ // FIXME: Replace with `ancestors_with_macros_file` when all usages are updated.
14701475 pub fn ancestors_with_macros (
14711476 & self ,
14721477 node : SyntaxNode ,
14731478 ) -> impl Iterator < Item = SyntaxNode > + Clone + ' _ {
14741479 let node = self . find_file ( & node) ;
1475- iter:: successors ( Some ( node. cloned ( ) ) , move |& InFile { file_id, ref value } | {
1476- match value. parent ( ) {
1477- Some ( parent) => Some ( InFile :: new ( file_id, parent) ) ,
1478- None => {
1479- let macro_file = file_id. macro_file ( ) ?;
1480-
1481- self . with_ctx ( |ctx| {
1482- let expansion_info = ctx. cache . get_or_insert_expansion ( ctx. db , macro_file) ;
1483- expansion_info. arg ( ) . map ( |node| node?. parent ( ) ) . transpose ( )
1484- } )
1485- }
1480+ self . ancestors_with_macros_file ( node. cloned ( ) ) . map ( |it| it. value )
1481+ }
1482+
1483+ /// Iterates the ancestors of the given node, climbing up macro expansions while doing so.
1484+ pub fn ancestors_with_macros_file (
1485+ & self ,
1486+ node : InFile < SyntaxNode > ,
1487+ ) -> impl Iterator < Item = InFile < SyntaxNode > > + Clone + ' _ {
1488+ iter:: successors ( Some ( node) , move |& InFile { file_id, ref value } | match value. parent ( ) {
1489+ Some ( parent) => Some ( InFile :: new ( file_id, parent) ) ,
1490+ None => {
1491+ let macro_file = file_id. macro_file ( ) ?;
1492+
1493+ self . with_ctx ( |ctx| {
1494+ let expansion_info = ctx. cache . get_or_insert_expansion ( ctx. db , macro_file) ;
1495+ expansion_info. arg ( ) . map ( |node| node?. parent ( ) ) . transpose ( )
1496+ } )
14861497 }
14871498 } )
1488- . map ( |it| it. value )
14891499 }
14901500
14911501 pub fn ancestors_at_offset_with_macros (
0 commit comments