@@ -14,10 +14,10 @@ use syntax::{
1414} ;
1515
1616use crate :: {
17- ast_id_map:: AstIdMap , fixup , hygiene :: HygieneFrame , BuiltinAttrExpander , BuiltinDeriveExpander ,
18- BuiltinFnLikeExpander , ExpandError , ExpandResult , ExpandTo , HirFileId , HirFileIdRepr ,
19- MacroCallId , MacroCallKind , MacroCallLoc , MacroDefId , MacroDefKind , MacroFile ,
20- ProcMacroExpander ,
17+ ast_id_map:: AstIdMap , builtin_attr_macro :: pseudo_derive_attr_expansion , fixup ,
18+ hygiene :: HygieneFrame , BuiltinAttrExpander , BuiltinDeriveExpander , BuiltinFnLikeExpander ,
19+ ExpandError , ExpandResult , ExpandTo , HirFileId , HirFileIdRepr , MacroCallId , MacroCallKind ,
20+ MacroCallLoc , MacroDefId , MacroDefKind , MacroFile , ProcMacroExpander ,
2121} ;
2222
2323/// Total limit on the number of tokens produced by any macro invocation.
@@ -161,14 +161,16 @@ pub fn expand_speculative(
161161 ) ;
162162
163163 let ( attr_arg, token_id) = match loc. kind {
164- MacroCallKind :: Attr { invoc_attr_index, .. } => {
165- // Attributes may have an input token tree, build the subtree and map for this as well
166- // then try finding a token id for our token if it is inside this input subtree.
167- let item = ast:: Item :: cast ( speculative_args. clone ( ) ) ?;
168- let attr = item
169- . doc_comments_and_attrs ( )
170- . nth ( invoc_attr_index as usize )
171- . and_then ( Either :: left) ?;
164+ MacroCallKind :: Attr { invoc_attr_index, is_derive, .. } => {
165+ let attr = if is_derive {
166+ // for pseudo-derive expansion we actually pass the attribute itself only
167+ ast:: Attr :: cast ( speculative_args. clone ( ) )
168+ } else {
169+ // Attributes may have an input token tree, build the subtree and map for this as well
170+ // then try finding a token id for our token if it is inside this input subtree.
171+ let item = ast:: Item :: cast ( speculative_args. clone ( ) ) ?;
172+ item. doc_comments_and_attrs ( ) . nth ( invoc_attr_index as usize ) . and_then ( Either :: left)
173+ } ?;
172174 match attr. token_tree ( ) {
173175 Some ( token_tree) => {
174176 let ( mut tree, map) = syntax_node_to_token_tree ( attr. token_tree ( ) ?. syntax ( ) ) ;
@@ -205,11 +207,15 @@ pub fn expand_speculative(
205207
206208 // Do the actual expansion, we need to directly expand the proc macro due to the attribute args
207209 // Otherwise the expand query will fetch the non speculative attribute args and pass those instead.
208- let mut speculative_expansion = if let MacroDefKind :: ProcMacro ( expander, ..) = loc. def . kind {
209- tt. delimiter = None ;
210- expander. expand ( db, loc. krate , & tt, attr_arg. as_ref ( ) )
211- } else {
212- macro_def. expand ( db, actual_macro_call, & tt)
210+ let mut speculative_expansion = match loc. def . kind {
211+ MacroDefKind :: ProcMacro ( expander, ..) => {
212+ tt. delimiter = None ;
213+ expander. expand ( db, loc. krate , & tt, attr_arg. as_ref ( ) )
214+ }
215+ MacroDefKind :: BuiltInAttr ( BuiltinAttrExpander :: Derive , _) => {
216+ pseudo_derive_attr_expansion ( & tt, attr_arg. as_ref ( ) ?)
217+ }
218+ _ => macro_def. expand ( db, actual_macro_call, & tt) ,
213219 } ;
214220
215221 let expand_to = macro_expand_to ( db, actual_macro_call) ;
0 commit comments