@@ -190,15 +190,16 @@ pub fn expand_speculative(
190
190
speculative_args : & SyntaxNode ,
191
191
token_to_map : SyntaxToken ,
192
192
) -> Option < ( SyntaxNode , SyntaxToken ) > {
193
- // FIXME spanmaps
194
193
let loc = db. lookup_intern_macro_call ( actual_macro_call) ;
195
194
196
195
// Build the subtree and token mapping for the speculative args
197
196
let _censor = censor_for_macro_input ( & loc, speculative_args) ;
197
+ let span_map = RealSpanMap :: absolute ( SpanAnchor :: DUMMY . file_id ) ;
198
+ let span_map = SpanMapRef :: RealSpanMap ( & span_map) ;
198
199
let mut tt = mbe:: syntax_node_to_token_tree (
199
200
speculative_args,
200
201
// we don't leak these spans into any query so its fine to make them absolute
201
- SpanMapRef :: RealSpanMap ( & RealSpanMap :: empty ( SpanAnchor :: DUMMY . file_id ) ) ,
202
+ span_map ,
202
203
) ;
203
204
204
205
let attr_arg = match loc. kind {
@@ -216,10 +217,7 @@ pub fn expand_speculative(
216
217
} ?;
217
218
match attr. token_tree ( ) {
218
219
Some ( token_tree) => {
219
- let mut tree = syntax_node_to_token_tree (
220
- token_tree. syntax ( ) ,
221
- SpanMapRef :: RealSpanMap ( & RealSpanMap :: empty ( SpanAnchor :: DUMMY . file_id ) ) ,
222
- ) ;
220
+ let mut tree = syntax_node_to_token_tree ( token_tree. syntax ( ) , span_map) ;
223
221
tree. delimiter = tt:: Delimiter :: UNSPECIFIED ;
224
222
225
223
Some ( tree)
@@ -243,12 +241,7 @@ pub fn expand_speculative(
243
241
MacroDefKind :: BuiltInDerive ( expander, ..) => {
244
242
// this cast is a bit sus, can we avoid losing the typedness here?
245
243
let adt = ast:: Adt :: cast ( speculative_args. clone ( ) ) . unwrap ( ) ;
246
- expander. expand (
247
- db,
248
- actual_macro_call,
249
- & adt,
250
- SpanMapRef :: RealSpanMap ( & RealSpanMap :: empty ( SpanAnchor :: DUMMY . file_id ) ) ,
251
- )
244
+ expander. expand ( db, actual_macro_call, & adt, span_map)
252
245
}
253
246
MacroDefKind :: Declarative ( it) => {
254
247
db. decl_macro_expander ( loc. krate , it) . expand_unhygienic ( tt)
@@ -305,6 +298,8 @@ fn parse_or_expand_with_err(
305
298
}
306
299
}
307
300
301
+ // FIXME: We should verify that the parsed node is one of the many macro node variants we expect
302
+ // instead of having it be untyped
308
303
fn parse_macro_expansion (
309
304
db : & dyn ExpandDatabase ,
310
305
macro_file : MacroFileId ,
@@ -330,6 +325,18 @@ fn parse_macro_expansion_error(
330
325
. map ( |it| it. 0 . errors ( ) . to_vec ( ) . into_boxed_slice ( ) )
331
326
}
332
327
328
+ fn parse_with_map ( db : & dyn ExpandDatabase , file_id : HirFileId ) -> ( Parse < SyntaxNode > , SpanMap ) {
329
+ match file_id. repr ( ) {
330
+ HirFileIdRepr :: FileId ( file_id) => {
331
+ ( db. parse ( file_id) . to_syntax ( ) , SpanMap :: RealSpanMap ( db. real_span_map ( file_id) ) )
332
+ }
333
+ HirFileIdRepr :: MacroFile ( macro_file) => {
334
+ let ( parse, map) = db. parse_macro_expansion ( macro_file) . value ;
335
+ ( parse, SpanMap :: ExpansionSpanMap ( map) )
336
+ }
337
+ }
338
+ }
339
+
333
340
fn macro_arg (
334
341
db : & dyn ExpandDatabase ,
335
342
id : MacroCallId ,
@@ -361,32 +368,22 @@ fn macro_arg(
361
368
. then ( || loc. eager . as_deref ( ) )
362
369
. flatten ( )
363
370
{
364
- ValueResult :: ok ( Some ( Arc :: new ( arg. 0 . clone ( ) ) ) )
371
+ ValueResult :: ok ( Some ( arg. clone ( ) ) )
365
372
} else {
366
- //FIXME: clean this up, the ast id map lookup is done twice here
367
- let ( parse, map) = match loc. kind . file_id ( ) . repr ( ) {
368
- HirFileIdRepr :: FileId ( file_id) => {
369
- let syntax = db. parse ( file_id) . to_syntax ( ) ;
370
-
371
- ( syntax, SpanMap :: RealSpanMap ( db. real_span_map ( file_id) ) )
372
- }
373
- HirFileIdRepr :: MacroFile ( macro_file) => {
374
- let ( parse, map) = db. parse_macro_expansion ( macro_file) . value ;
375
- ( parse, SpanMap :: ExpansionSpanMap ( map) )
376
- }
377
- } ;
373
+ let ( parse, map) = parse_with_map ( db, loc. kind . file_id ( ) ) ;
378
374
let root = parse. syntax_node ( ) ;
379
375
380
376
let syntax = match loc. kind {
381
377
MacroCallKind :: FnLike { ast_id, .. } => {
382
378
let node = & ast_id. to_ptr ( db) . to_node ( & root) ;
383
379
let offset = node. syntax ( ) . text_range ( ) . start ( ) ;
384
- match node. token_tree ( ) . map ( |it| it . syntax ( ) . clone ( ) ) {
380
+ match node. token_tree ( ) {
385
381
Some ( tt) => {
386
- if let Some ( e) = mismatched_delimiters ( & tt) {
382
+ let tt = tt. syntax ( ) ;
383
+ if let Some ( e) = mismatched_delimiters ( tt) {
387
384
return ValueResult :: only_err ( e) ;
388
385
}
389
- tt
386
+ tt. clone ( )
390
387
}
391
388
None => {
392
389
return ValueResult :: only_err ( Arc :: new ( Box :: new ( [
@@ -479,17 +476,8 @@ fn decl_macro_expander(
479
476
id : AstId < ast:: Macro > ,
480
477
) -> Arc < DeclarativeMacroExpander > {
481
478
let is_2021 = db. crate_graph ( ) [ def_crate] . edition >= Edition :: Edition2021 ;
482
- let ( root, map) = match id. file_id . repr ( ) {
483
- HirFileIdRepr :: FileId ( file_id) => {
484
- // FIXME: Arc
485
- // FIXME: id.to_ptr duplicated, expensive
486
- ( db. parse ( file_id) . syntax_node ( ) , SpanMap :: RealSpanMap ( db. real_span_map ( file_id) ) )
487
- }
488
- HirFileIdRepr :: MacroFile ( macro_file) => {
489
- let ( parse, map) = db. parse_macro_expansion ( macro_file) . value ;
490
- ( parse. syntax_node ( ) , SpanMap :: ExpansionSpanMap ( map) )
491
- }
492
- } ;
479
+ let ( root, map) = parse_with_map ( db, id. file_id ) ;
480
+ let root = root. syntax_node ( ) ;
493
481
494
482
let transparency = |node| {
495
483
// ... would be nice to have the item tree here
@@ -568,21 +556,8 @@ fn macro_expand(
568
556
let ExpandResult { value : tt, mut err } = match loc. def . kind {
569
557
MacroDefKind :: ProcMacro ( ..) => return db. expand_proc_macro ( macro_call_id) ,
570
558
MacroDefKind :: BuiltInDerive ( expander, ..) => {
571
- // FIXME: add firewall query for this?
572
- let hir_file_id = loc. kind . file_id ( ) ;
573
- let ( root, map) = match hir_file_id. repr ( ) {
574
- HirFileIdRepr :: FileId ( file_id) => {
575
- // FIXME: query for span map
576
- (
577
- db. parse ( file_id) . syntax_node ( ) ,
578
- SpanMap :: RealSpanMap ( db. real_span_map ( file_id) ) ,
579
- )
580
- }
581
- HirFileIdRepr :: MacroFile ( macro_file) => {
582
- let ( parse, map) = db. parse_macro_expansion ( macro_file) . value ;
583
- ( parse. syntax_node ( ) , SpanMap :: ExpansionSpanMap ( map) )
584
- }
585
- } ;
559
+ let ( root, map) = parse_with_map ( db, loc. kind . file_id ( ) ) ;
560
+ let root = root. syntax_node ( ) ;
586
561
let MacroCallKind :: Derive { ast_id, .. } = loc. kind else { unreachable ! ( ) } ;
587
562
let node = ast_id. to_ptr ( db) . to_node ( & root) ;
588
563
@@ -710,9 +685,9 @@ fn token_tree_to_syntax_node(
710
685
ExpandTo :: Type => mbe:: TopEntryPoint :: Type ,
711
686
ExpandTo :: Expr => mbe:: TopEntryPoint :: Expr ,
712
687
} ;
713
- let mut tm = mbe:: token_tree_to_syntax_node ( tt, entry_point) ;
688
+ let ( parse , mut span_map ) = mbe:: token_tree_to_syntax_node ( tt, entry_point) ;
714
689
// FIXME: now what the hell is going on here
715
- tm . 1 . span_map . sort_by ( |( _, a) , ( _, b) | {
690
+ span_map . span_map . sort_by ( |( _, a) , ( _, b) | {
716
691
a. anchor . file_id . cmp ( & b. anchor . file_id ) . then_with ( || {
717
692
let map = db. ast_id_map ( a. anchor . file_id . into ( ) ) ;
718
693
map. get_erased ( a. anchor . ast_id )
@@ -721,7 +696,7 @@ fn token_tree_to_syntax_node(
721
696
. cmp ( & map. get_erased ( b. anchor . ast_id ) . text_range ( ) . start ( ) )
722
697
} )
723
698
} ) ;
724
- tm
699
+ ( parse , span_map )
725
700
}
726
701
727
702
fn check_tt_count ( tt : & tt:: Subtree ) -> Result < ( ) , ExpandResult < Arc < tt:: Subtree > > > {
0 commit comments