@@ -10,10 +10,12 @@ use crate::{
1010 hygiene:: span_with_def_site_ctxt,
1111 name:: { AsName , Name } ,
1212 quote:: dollar_crate,
13- span_map:: SpanMapRef ,
13+ span_map:: ExpansionSpanMap ,
1414 tt,
1515} ;
16- use syntax:: ast:: { self , AstNode , FieldList , HasAttrs , HasGenericParams , HasName , HasTypeBounds } ;
16+ use syntax:: ast:: {
17+ self , AstNode , FieldList , HasAttrs , HasGenericParams , HasModuleItem , HasName , HasTypeBounds ,
18+ } ;
1719
1820use crate :: { db:: ExpandDatabase , name, quote, ExpandError , ExpandResult } ;
1921
@@ -25,7 +27,7 @@ macro_rules! register_builtin {
2527 }
2628
2729 impl BuiltinDeriveExpander {
28- pub fn expander( & self ) -> fn ( Span , & ast :: Adt , SpanMapRef < ' _> ) -> ExpandResult <tt:: Subtree > {
30+ pub fn expander( & self ) -> fn ( Span , & tt :: Subtree ) -> ExpandResult <tt:: Subtree > {
2931 match * self {
3032 $( BuiltinDeriveExpander :: $trait => $expand, ) *
3133 }
@@ -47,12 +49,11 @@ impl BuiltinDeriveExpander {
4749 & self ,
4850 db : & dyn ExpandDatabase ,
4951 id : MacroCallId ,
50- tt : & ast:: Adt ,
51- token_map : SpanMapRef < ' _ > ,
52+ tt : & tt:: Subtree ,
5253 ) -> ExpandResult < tt:: Subtree > {
5354 let span = db. lookup_intern_macro_call ( id) . call_site ;
5455 let span = span_with_def_site_ctxt ( db, span, id) ;
55- self . expander ( ) ( span, tt, token_map )
56+ self . expander ( ) ( span, tt)
5657 }
5758}
5859
@@ -126,7 +127,7 @@ impl VariantShape {
126127 }
127128 }
128129
129- fn from ( tm : SpanMapRef < ' _ > , value : Option < FieldList > ) -> Result < Self , ExpandError > {
130+ fn from ( tm : & ExpansionSpanMap , value : Option < FieldList > ) -> Result < Self , ExpandError > {
130131 let r = match value {
131132 None => VariantShape :: Unit ,
132133 Some ( FieldList :: RecordFieldList ( it) ) => VariantShape :: Struct (
@@ -202,11 +203,13 @@ struct BasicAdtInfo {
202203 associated_types : Vec < tt:: Subtree > ,
203204}
204205
205- fn parse_adt (
206- tm : SpanMapRef < ' _ > ,
207- adt : & ast:: Adt ,
208- call_site : Span ,
209- ) -> Result < BasicAdtInfo , ExpandError > {
206+ fn parse_adt ( tt : & tt:: Subtree , call_site : Span ) -> Result < BasicAdtInfo , ExpandError > {
207+ let ( parsed, tm) = & mbe:: token_tree_to_syntax_node ( tt, mbe:: TopEntryPoint :: MacroItems ) ;
208+ let macro_items = ast:: MacroItems :: cast ( parsed. syntax_node ( ) )
209+ . ok_or_else ( || ExpandError :: other ( "invalid item definition" ) ) ?;
210+ let item = macro_items. items ( ) . next ( ) . ok_or_else ( || ExpandError :: other ( "no item found" ) ) ?;
211+ let adt = & ast:: Adt :: cast ( item. syntax ( ) . clone ( ) )
212+ . ok_or_else ( || ExpandError :: other ( "expected struct, enum or union" ) ) ?;
210213 let ( name, generic_param_list, where_clause, shape) = match adt {
211214 ast:: Adt :: Struct ( it) => (
212215 it. name ( ) ,
@@ -322,14 +325,14 @@ fn parse_adt(
322325}
323326
324327fn name_to_token (
325- token_map : SpanMapRef < ' _ > ,
328+ token_map : & ExpansionSpanMap ,
326329 name : Option < ast:: Name > ,
327330) -> Result < tt:: Ident , ExpandError > {
328331 let name = name. ok_or_else ( || {
329332 debug ! ( "parsed item has no name" ) ;
330333 ExpandError :: other ( "missing name" )
331334 } ) ?;
332- let span = token_map. span_for_range ( name. syntax ( ) . text_range ( ) ) ;
335+ let span = token_map. span_at ( name. syntax ( ) . text_range ( ) . start ( ) ) ;
333336 let name_token = tt:: Ident { span, text : name. text ( ) . into ( ) } ;
334337 Ok ( name_token)
335338}
@@ -366,14 +369,12 @@ fn name_to_token(
366369/// where B1, ..., BN are the bounds given by `bounds_paths`. Z is a phantom type, and
367370/// therefore does not get bound by the derived trait.
368371fn expand_simple_derive (
369- // FIXME: use
370372 invoc_span : Span ,
371- tt : & ast:: Adt ,
372- tm : SpanMapRef < ' _ > ,
373+ tt : & tt:: Subtree ,
373374 trait_path : tt:: Subtree ,
374375 make_trait_body : impl FnOnce ( & BasicAdtInfo ) -> tt:: Subtree ,
375376) -> ExpandResult < tt:: Subtree > {
376- let info = match parse_adt ( tm , tt, invoc_span) {
377+ let info = match parse_adt ( tt, invoc_span) {
377378 Ok ( info) => info,
378379 Err ( e) => {
379380 return ExpandResult :: new (
@@ -416,14 +417,14 @@ fn expand_simple_derive(
416417 ExpandResult :: ok ( expanded)
417418}
418419
419- fn copy_expand ( span : Span , tt : & ast :: Adt , tm : SpanMapRef < ' _ > ) -> ExpandResult < tt:: Subtree > {
420+ fn copy_expand ( span : Span , tt : & tt :: Subtree ) -> ExpandResult < tt:: Subtree > {
420421 let krate = dollar_crate ( span) ;
421- expand_simple_derive ( span, tt, tm , quote ! { span => #krate:: marker:: Copy } , |_| quote ! { span =>} )
422+ expand_simple_derive ( span, tt, quote ! { span => #krate:: marker:: Copy } , |_| quote ! { span =>} )
422423}
423424
424- fn clone_expand ( span : Span , tt : & ast :: Adt , tm : SpanMapRef < ' _ > ) -> ExpandResult < tt:: Subtree > {
425+ fn clone_expand ( span : Span , tt : & tt :: Subtree ) -> ExpandResult < tt:: Subtree > {
425426 let krate = dollar_crate ( span) ;
426- expand_simple_derive ( span, tt, tm , quote ! { span => #krate:: clone:: Clone } , |adt| {
427+ expand_simple_derive ( span, tt, quote ! { span => #krate:: clone:: Clone } , |adt| {
427428 if matches ! ( adt. shape, AdtShape :: Union ) {
428429 let star = tt:: Punct { char : '*' , spacing : :: tt:: Spacing :: Alone , span } ;
429430 return quote ! { span =>
@@ -472,9 +473,9 @@ fn and_and(span: Span) -> tt::Subtree {
472473 quote ! { span => #and& }
473474}
474475
475- fn default_expand ( span : Span , tt : & ast :: Adt , tm : SpanMapRef < ' _ > ) -> ExpandResult < tt:: Subtree > {
476+ fn default_expand ( span : Span , tt : & tt :: Subtree ) -> ExpandResult < tt:: Subtree > {
476477 let krate = & dollar_crate ( span) ;
477- expand_simple_derive ( span, tt, tm , quote ! { span => #krate:: default :: Default } , |adt| {
478+ expand_simple_derive ( span, tt, quote ! { span => #krate:: default :: Default } , |adt| {
478479 let body = match & adt. shape {
479480 AdtShape :: Struct ( fields) => {
480481 let name = & adt. name ;
@@ -511,9 +512,9 @@ fn default_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult
511512 } )
512513}
513514
514- fn debug_expand ( span : Span , tt : & ast :: Adt , tm : SpanMapRef < ' _ > ) -> ExpandResult < tt:: Subtree > {
515+ fn debug_expand ( span : Span , tt : & tt :: Subtree ) -> ExpandResult < tt:: Subtree > {
515516 let krate = & dollar_crate ( span) ;
516- expand_simple_derive ( span, tt, tm , quote ! { span => #krate:: fmt:: Debug } , |adt| {
517+ expand_simple_derive ( span, tt, quote ! { span => #krate:: fmt:: Debug } , |adt| {
517518 let for_variant = |name : String , v : & VariantShape | match v {
518519 VariantShape :: Struct ( fields) => {
519520 let for_fields = fields. iter ( ) . map ( |it| {
@@ -583,9 +584,9 @@ fn debug_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<t
583584 } )
584585}
585586
586- fn hash_expand ( span : Span , tt : & ast :: Adt , tm : SpanMapRef < ' _ > ) -> ExpandResult < tt:: Subtree > {
587+ fn hash_expand ( span : Span , tt : & tt :: Subtree ) -> ExpandResult < tt:: Subtree > {
587588 let krate = & dollar_crate ( span) ;
588- expand_simple_derive ( span, tt, tm , quote ! { span => #krate:: hash:: Hash } , |adt| {
589+ expand_simple_derive ( span, tt, quote ! { span => #krate:: hash:: Hash } , |adt| {
589590 if matches ! ( adt. shape, AdtShape :: Union ) {
590591 // FIXME: Return expand error here
591592 return quote ! { span =>} ;
@@ -630,14 +631,14 @@ fn hash_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt
630631 } )
631632}
632633
633- fn eq_expand ( span : Span , tt : & ast :: Adt , tm : SpanMapRef < ' _ > ) -> ExpandResult < tt:: Subtree > {
634+ fn eq_expand ( span : Span , tt : & tt :: Subtree ) -> ExpandResult < tt:: Subtree > {
634635 let krate = dollar_crate ( span) ;
635- expand_simple_derive ( span, tt, tm , quote ! { span => #krate:: cmp:: Eq } , |_| quote ! { span =>} )
636+ expand_simple_derive ( span, tt, quote ! { span => #krate:: cmp:: Eq } , |_| quote ! { span =>} )
636637}
637638
638- fn partial_eq_expand ( span : Span , tt : & ast :: Adt , tm : SpanMapRef < ' _ > ) -> ExpandResult < tt:: Subtree > {
639+ fn partial_eq_expand ( span : Span , tt : & tt :: Subtree ) -> ExpandResult < tt:: Subtree > {
639640 let krate = dollar_crate ( span) ;
640- expand_simple_derive ( span, tt, tm , quote ! { span => #krate:: cmp:: PartialEq } , |adt| {
641+ expand_simple_derive ( span, tt, quote ! { span => #krate:: cmp:: PartialEq } , |adt| {
641642 if matches ! ( adt. shape, AdtShape :: Union ) {
642643 // FIXME: Return expand error here
643644 return quote ! { span =>} ;
@@ -707,9 +708,9 @@ fn self_and_other_patterns(
707708 ( self_patterns, other_patterns)
708709}
709710
710- fn ord_expand ( span : Span , tt : & ast :: Adt , tm : SpanMapRef < ' _ > ) -> ExpandResult < tt:: Subtree > {
711+ fn ord_expand ( span : Span , tt : & tt :: Subtree ) -> ExpandResult < tt:: Subtree > {
711712 let krate = & dollar_crate ( span) ;
712- expand_simple_derive ( span, tt, tm , quote ! { span => #krate:: cmp:: Ord } , |adt| {
713+ expand_simple_derive ( span, tt, quote ! { span => #krate:: cmp:: Ord } , |adt| {
713714 fn compare (
714715 krate : & tt:: Ident ,
715716 left : tt:: Subtree ,
@@ -765,9 +766,9 @@ fn ord_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt:
765766 } )
766767}
767768
768- fn partial_ord_expand ( span : Span , tt : & ast :: Adt , tm : SpanMapRef < ' _ > ) -> ExpandResult < tt:: Subtree > {
769+ fn partial_ord_expand ( span : Span , tt : & tt :: Subtree ) -> ExpandResult < tt:: Subtree > {
769770 let krate = & dollar_crate ( span) ;
770- expand_simple_derive ( span, tt, tm , quote ! { span => #krate:: cmp:: PartialOrd } , |adt| {
771+ expand_simple_derive ( span, tt, quote ! { span => #krate:: cmp:: PartialOrd } , |adt| {
771772 fn compare (
772773 krate : & tt:: Ident ,
773774 left : tt:: Subtree ,
0 commit comments