@@ -11,6 +11,7 @@ use crate::quote;
1111#[ derive( Debug ,  Clone ,  Copy ,  PartialEq ,  Eq ,  Hash ) ]  
1212pub  enum  BuiltinExpander  { 
1313    Line , 
14+     Stringify , 
1415} 
1516
1617impl  BuiltinExpander  { 
@@ -22,6 +23,7 @@ impl BuiltinExpander {
2223    )  -> Result < tt:: Subtree ,  mbe:: ExpandError >  { 
2324        match  self  { 
2425            BuiltinExpander :: Line  => line_expand ( db,  id,  tt) , 
26+             BuiltinExpander :: Stringify  => stringify_expand ( db,  id,  tt) , 
2527        } 
2628    } 
2729} 
@@ -34,6 +36,8 @@ pub fn find_builtin_macro(
3436    // FIXME: Better registering method 
3537    if  ident == & name:: LINE_MACRO  { 
3638        Some ( MacroDefId  {  krate,  ast_id,  kind :  MacroDefKind :: BuiltIn ( BuiltinExpander :: Line )  } ) 
39+     }  else  if  ident == & name:: STRINGIFY_MACRO  { 
40+         Some ( MacroDefId  {  krate,  ast_id,  kind :  MacroDefKind :: BuiltIn ( BuiltinExpander :: Stringify )  } ) 
3741    }  else  { 
3842        None 
3943    } 
@@ -78,3 +82,26 @@ fn line_expand(
7882
7983    Ok ( expanded) 
8084} 
85+ 
86+ fn  stringify_expand ( 
87+     db :  & dyn  AstDatabase , 
88+     id :  MacroCallId , 
89+     _tt :  & tt:: Subtree , 
90+ )  -> Result < tt:: Subtree ,  mbe:: ExpandError >  { 
91+     let  loc = db. lookup_intern_macro ( id) ; 
92+     let  macro_call = loc. ast_id . to_node ( db) ; 
93+ 
94+     let  macro_content = { 
95+         let  arg = macro_call. token_tree ( ) . ok_or_else ( || mbe:: ExpandError :: UnexpectedToken ) ?; 
96+         let  macro_args = arg. syntax ( ) . clone ( ) ; 
97+         let  text = macro_args. text ( ) ; 
98+         let  without_parens = TextUnit :: of_char ( '(' ) ..text. len ( )  - TextUnit :: of_char ( ')' ) ; 
99+         text. slice ( without_parens) . to_string ( ) 
100+     } ; 
101+ 
102+     let  expanded = quote !  { 
103+         #macro_content
104+     } ; 
105+ 
106+     Ok ( expanded) 
107+ } 
0 commit comments