@@ -10,10 +10,12 @@ use crate::{
10
10
hygiene:: span_with_def_site_ctxt,
11
11
name:: { AsName , Name } ,
12
12
quote:: dollar_crate,
13
- span_map:: SpanMapRef ,
13
+ span_map:: ExpansionSpanMap ,
14
14
tt,
15
15
} ;
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
+ } ;
17
19
18
20
use crate :: { db:: ExpandDatabase , name, quote, ExpandError , ExpandResult } ;
19
21
@@ -25,7 +27,7 @@ macro_rules! register_builtin {
25
27
}
26
28
27
29
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 > {
29
31
match * self {
30
32
$( BuiltinDeriveExpander :: $trait => $expand, ) *
31
33
}
@@ -47,12 +49,11 @@ impl BuiltinDeriveExpander {
47
49
& self ,
48
50
db : & dyn ExpandDatabase ,
49
51
id : MacroCallId ,
50
- tt : & ast:: Adt ,
51
- token_map : SpanMapRef < ' _ > ,
52
+ tt : & tt:: Subtree ,
52
53
) -> ExpandResult < tt:: Subtree > {
53
54
let span = db. lookup_intern_macro_call ( id) . call_site ;
54
55
let span = span_with_def_site_ctxt ( db, span, id) ;
55
- self . expander ( ) ( span, tt, token_map )
56
+ self . expander ( ) ( span, tt)
56
57
}
57
58
}
58
59
@@ -126,7 +127,7 @@ impl VariantShape {
126
127
}
127
128
}
128
129
129
- fn from ( tm : SpanMapRef < ' _ > , value : Option < FieldList > ) -> Result < Self , ExpandError > {
130
+ fn from ( tm : & ExpansionSpanMap , value : Option < FieldList > ) -> Result < Self , ExpandError > {
130
131
let r = match value {
131
132
None => VariantShape :: Unit ,
132
133
Some ( FieldList :: RecordFieldList ( it) ) => VariantShape :: Struct (
@@ -202,11 +203,13 @@ struct BasicAdtInfo {
202
203
associated_types : Vec < tt:: Subtree > ,
203
204
}
204
205
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" ) ) ?;
210
213
let ( name, generic_param_list, where_clause, shape) = match adt {
211
214
ast:: Adt :: Struct ( it) => (
212
215
it. name ( ) ,
@@ -322,14 +325,14 @@ fn parse_adt(
322
325
}
323
326
324
327
fn name_to_token (
325
- token_map : SpanMapRef < ' _ > ,
328
+ token_map : & ExpansionSpanMap ,
326
329
name : Option < ast:: Name > ,
327
330
) -> Result < tt:: Ident , ExpandError > {
328
331
let name = name. ok_or_else ( || {
329
332
debug ! ( "parsed item has no name" ) ;
330
333
ExpandError :: other ( "missing name" )
331
334
} ) ?;
332
- let span = token_map. span_for_range ( name. syntax ( ) . text_range ( ) ) ;
335
+ let span = token_map. span_at ( name. syntax ( ) . text_range ( ) . start ( ) ) ;
333
336
let name_token = tt:: Ident { span, text : name. text ( ) . into ( ) } ;
334
337
Ok ( name_token)
335
338
}
@@ -366,14 +369,12 @@ fn name_to_token(
366
369
/// where B1, ..., BN are the bounds given by `bounds_paths`. Z is a phantom type, and
367
370
/// therefore does not get bound by the derived trait.
368
371
fn expand_simple_derive (
369
- // FIXME: use
370
372
invoc_span : Span ,
371
- tt : & ast:: Adt ,
372
- tm : SpanMapRef < ' _ > ,
373
+ tt : & tt:: Subtree ,
373
374
trait_path : tt:: Subtree ,
374
375
make_trait_body : impl FnOnce ( & BasicAdtInfo ) -> tt:: Subtree ,
375
376
) -> ExpandResult < tt:: Subtree > {
376
- let info = match parse_adt ( tm , tt, invoc_span) {
377
+ let info = match parse_adt ( tt, invoc_span) {
377
378
Ok ( info) => info,
378
379
Err ( e) => {
379
380
return ExpandResult :: new (
@@ -416,14 +417,14 @@ fn expand_simple_derive(
416
417
ExpandResult :: ok ( expanded)
417
418
}
418
419
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 > {
420
421
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 =>} )
422
423
}
423
424
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 > {
425
426
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| {
427
428
if matches ! ( adt. shape, AdtShape :: Union ) {
428
429
let star = tt:: Punct { char : '*' , spacing : :: tt:: Spacing :: Alone , span } ;
429
430
return quote ! { span =>
@@ -472,9 +473,9 @@ fn and_and(span: Span) -> tt::Subtree {
472
473
quote ! { span => #and& }
473
474
}
474
475
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 > {
476
477
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| {
478
479
let body = match & adt. shape {
479
480
AdtShape :: Struct ( fields) => {
480
481
let name = & adt. name ;
@@ -511,9 +512,9 @@ fn default_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult
511
512
} )
512
513
}
513
514
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 > {
515
516
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| {
517
518
let for_variant = |name : String , v : & VariantShape | match v {
518
519
VariantShape :: Struct ( fields) => {
519
520
let for_fields = fields. iter ( ) . map ( |it| {
@@ -583,9 +584,9 @@ fn debug_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<t
583
584
} )
584
585
}
585
586
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 > {
587
588
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| {
589
590
if matches ! ( adt. shape, AdtShape :: Union ) {
590
591
// FIXME: Return expand error here
591
592
return quote ! { span =>} ;
@@ -630,14 +631,14 @@ fn hash_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt
630
631
} )
631
632
}
632
633
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 > {
634
635
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 =>} )
636
637
}
637
638
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 > {
639
640
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| {
641
642
if matches ! ( adt. shape, AdtShape :: Union ) {
642
643
// FIXME: Return expand error here
643
644
return quote ! { span =>} ;
@@ -707,9 +708,9 @@ fn self_and_other_patterns(
707
708
( self_patterns, other_patterns)
708
709
}
709
710
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 > {
711
712
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| {
713
714
fn compare (
714
715
krate : & tt:: Ident ,
715
716
left : tt:: Subtree ,
@@ -765,9 +766,9 @@ fn ord_expand(span: Span, tt: &ast::Adt, tm: SpanMapRef<'_>) -> ExpandResult<tt:
765
766
} )
766
767
}
767
768
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 > {
769
770
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| {
771
772
fn compare (
772
773
krate : & tt:: Ident ,
773
774
left : tt:: Subtree ,
0 commit comments