@@ -35,7 +35,7 @@ pub struct DeclarativeMacroExpander {
3535}
3636
3737impl DeclarativeMacroExpander {
38- pub fn expand ( & self , tt : & tt:: Subtree ) -> ExpandResult < tt:: Subtree > {
38+ pub fn expand ( & self , tt : tt:: Subtree ) -> ExpandResult < tt:: Subtree > {
3939 match self . mac . err ( ) {
4040 Some ( e) => ExpandResult :: new (
4141 tt:: Subtree :: empty ( ) ,
@@ -44,6 +44,14 @@ impl DeclarativeMacroExpander {
4444 None => self . mac . expand ( tt) . map_err ( Into :: into) ,
4545 }
4646 }
47+
48+ pub fn map_id_down ( & self , token_id : tt:: TokenId ) -> tt:: TokenId {
49+ self . mac . map_id_down ( token_id)
50+ }
51+
52+ pub fn map_id_up ( & self , token_id : tt:: TokenId ) -> ( tt:: TokenId , mbe:: Origin ) {
53+ self . mac . map_id_up ( token_id)
54+ }
4755}
4856
4957#[ derive( Debug , Clone , Eq , PartialEq ) ]
@@ -61,10 +69,11 @@ pub enum TokenExpander {
6169 ProcMacro ( ProcMacroExpander ) ,
6270}
6371
72+ // FIXME: Get rid of these methods
6473impl TokenExpander {
6574 pub ( crate ) fn map_id_down ( & self , id : tt:: TokenId ) -> tt:: TokenId {
6675 match self {
67- TokenExpander :: DeclarativeMacro ( expander) => expander. mac . map_id_down ( id) ,
76+ TokenExpander :: DeclarativeMacro ( expander) => expander. map_id_down ( id) ,
6877 TokenExpander :: BuiltIn ( ..)
6978 | TokenExpander :: BuiltInEager ( ..)
7079 | TokenExpander :: BuiltInAttr ( ..)
@@ -75,7 +84,7 @@ impl TokenExpander {
7584
7685 pub ( crate ) fn map_id_up ( & self , id : tt:: TokenId ) -> ( tt:: TokenId , mbe:: Origin ) {
7786 match self {
78- TokenExpander :: DeclarativeMacro ( expander) => expander. mac . map_id_up ( id) ,
87+ TokenExpander :: DeclarativeMacro ( expander) => expander. map_id_up ( id) ,
7988 TokenExpander :: BuiltIn ( ..)
8089 | TokenExpander :: BuiltInEager ( ..)
8190 | TokenExpander :: BuiltInAttr ( ..)
@@ -167,7 +176,6 @@ pub fn expand_speculative(
167176 token_to_map : SyntaxToken ,
168177) -> Option < ( SyntaxNode , SyntaxToken ) > {
169178 let loc = db. lookup_intern_macro_call ( actual_macro_call) ;
170- let macro_def = db. macro_def ( loc. def ) ;
171179 let token_range = token_to_map. text_range ( ) ;
172180
173181 // Build the subtree and token mapping for the speculative args
@@ -225,7 +233,12 @@ pub fn expand_speculative(
225233 None => {
226234 let range = token_range. checked_sub ( speculative_args. text_range ( ) . start ( ) ) ?;
227235 let token_id = spec_args_tmap. token_by_range ( range) ?;
228- macro_def. map_id_down ( token_id)
236+ match loc. def . kind {
237+ MacroDefKind :: Declarative ( it) => {
238+ db. decl_macro_expander ( loc. krate , it) . map_id_down ( token_id)
239+ }
240+ _ => token_id,
241+ }
229242 }
230243 } ;
231244
@@ -244,7 +257,7 @@ pub fn expand_speculative(
244257 let adt = ast:: Adt :: cast ( speculative_args. clone ( ) ) . unwrap ( ) ;
245258 expander. expand ( db, actual_macro_call, & adt, & spec_args_tmap)
246259 }
247- MacroDefKind :: Declarative ( it) => db. decl_macro_expander ( loc. krate , it) . expand ( & tt) ,
260+ MacroDefKind :: Declarative ( it) => db. decl_macro_expander ( loc. krate , it) . expand ( tt) ,
248261 MacroDefKind :: BuiltIn ( it, _) => it. expand ( db, actual_macro_call, & tt) . map_err ( Into :: into) ,
249262 MacroDefKind :: BuiltInEager ( it, _) => {
250263 it. expand ( db, actual_macro_call, & tt) . map_err ( Into :: into)
@@ -518,7 +531,7 @@ fn macro_expand(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<Arc<tt
518531 let ( arg, arg_tm, undo_info) = & * macro_arg;
519532 let mut res = match loc. def . kind {
520533 MacroDefKind :: Declarative ( id) => {
521- db. decl_macro_expander ( loc. def . krate , id) . expand ( & arg)
534+ db. decl_macro_expander ( loc. def . krate , id) . expand ( arg. clone ( ) )
522535 }
523536 MacroDefKind :: BuiltIn ( it, _) => it. expand ( db, id, & arg) . map_err ( Into :: into) ,
524537 MacroDefKind :: BuiltInEager ( it, _) => it. expand ( db, id, & arg) . map_err ( Into :: into) ,
0 commit comments