@@ -108,7 +108,7 @@ pub trait ExpandDatabase: SourceDatabase {
108
108
fn parse_macro_expansion (
109
109
& self ,
110
110
macro_file : MacroFile ,
111
- ) -> ExpandResult < Option < ( Parse < SyntaxNode > , Arc < mbe:: TokenMap > ) > > ;
111
+ ) -> ExpandResult < ( Parse < SyntaxNode > , Arc < mbe:: TokenMap > ) > ;
112
112
113
113
/// Macro ids. That's probably the tricksiest bit in rust-analyzer, and the
114
114
/// reason why we use salsa at all.
@@ -123,7 +123,7 @@ pub trait ExpandDatabase: SourceDatabase {
123
123
fn macro_arg (
124
124
& self ,
125
125
id : MacroCallId ,
126
- ) -> Option < Arc < ( tt:: Subtree , mbe:: TokenMap , fixup:: SyntaxFixupUndoInfo ) > > ;
126
+ ) -> Arc < ( tt:: Subtree , mbe:: TokenMap , fixup:: SyntaxFixupUndoInfo ) > ;
127
127
/// Extracts syntax node, corresponding to a macro call. That's a firewall
128
128
/// query, only typing in the macro call itself changes the returned
129
129
/// subtree.
@@ -133,7 +133,7 @@ pub trait ExpandDatabase: SourceDatabase {
133
133
fn macro_def ( & self , id : MacroDefId ) -> Result < Arc < TokenExpander > , mbe:: ParseError > ;
134
134
135
135
/// Expand macro call to a token tree.
136
- fn macro_expand ( & self , macro_call : MacroCallId ) -> ExpandResult < Option < Arc < tt:: Subtree > > > ;
136
+ fn macro_expand ( & self , macro_call : MacroCallId ) -> ExpandResult < Arc < tt:: Subtree > > ;
137
137
/// Special case of the previous query for procedural macros. We can't LRU
138
138
/// proc macros, since they are not deterministic in general, and
139
139
/// non-determinism breaks salsa in a very, very, very bad way. @edwin0cheng
@@ -143,7 +143,7 @@ pub trait ExpandDatabase: SourceDatabase {
143
143
fn parse_macro_expansion_error (
144
144
& self ,
145
145
macro_call : MacroCallId ,
146
- ) -> ExpandResult < Option < Box < [ SyntaxError ] > > > ;
146
+ ) -> ExpandResult < Box < [ SyntaxError ] > > ;
147
147
148
148
fn hygiene_frame ( & self , file_id : HirFileId ) -> Arc < HygieneFrame > ;
149
149
}
@@ -257,12 +257,12 @@ fn ast_id_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
257
257
}
258
258
259
259
fn parse_or_expand ( db : & dyn ExpandDatabase , file_id : HirFileId ) -> Option < SyntaxNode > {
260
- match file_id. repr ( ) {
261
- HirFileIdRepr :: FileId ( file_id) => Some ( db. parse ( file_id) . tree ( ) . syntax ( ) . clone ( ) ) ,
260
+ Some ( match file_id. repr ( ) {
261
+ HirFileIdRepr :: FileId ( file_id) => db. parse ( file_id) . tree ( ) . syntax ( ) . clone ( ) ,
262
262
HirFileIdRepr :: MacroFile ( macro_file) => {
263
- db. parse_macro_expansion ( macro_file) . value . map ( | ( it , _ ) | it . syntax_node ( ) )
263
+ db. parse_macro_expansion ( macro_file) . value . 0 . syntax_node ( )
264
264
}
265
- }
265
+ } )
266
266
}
267
267
268
268
fn parse_or_expand_with_err (
@@ -272,17 +272,17 @@ fn parse_or_expand_with_err(
272
272
match file_id. repr ( ) {
273
273
HirFileIdRepr :: FileId ( file_id) => ExpandResult :: ok ( Some ( db. parse ( file_id) . to_syntax ( ) ) ) ,
274
274
HirFileIdRepr :: MacroFile ( macro_file) => {
275
- db. parse_macro_expansion ( macro_file) . map ( |it| it. map ( | ( parse , _ ) | parse ) )
275
+ db. parse_macro_expansion ( macro_file) . map ( |it| Some ( it. 0 ) )
276
276
}
277
277
}
278
278
}
279
279
280
280
fn parse_macro_expansion (
281
281
db : & dyn ExpandDatabase ,
282
282
macro_file : MacroFile ,
283
- ) -> ExpandResult < Option < ( Parse < SyntaxNode > , Arc < mbe:: TokenMap > ) > > {
283
+ ) -> ExpandResult < ( Parse < SyntaxNode > , Arc < mbe:: TokenMap > ) > {
284
284
let _p = profile:: span ( "parse_macro_expansion" ) ;
285
- let mbe:: ValueResult { value, err } = db. macro_expand ( macro_file. macro_call_id ) ;
285
+ let mbe:: ValueResult { value : tt , err } = db. macro_expand ( macro_file. macro_call_id ) ;
286
286
287
287
if let Some ( err) = & err {
288
288
if tracing:: enabled!( tracing:: Level :: DEBUG ) {
@@ -308,10 +308,6 @@ fn parse_macro_expansion(
308
308
) ;
309
309
}
310
310
}
311
- let tt = match value {
312
- Some ( tt) => tt,
313
- None => return ExpandResult { value : None , err } ,
314
- } ;
315
311
316
312
let expand_to = macro_expand_to ( db, macro_file. macro_call_id ) ;
317
313
@@ -320,14 +316,23 @@ fn parse_macro_expansion(
320
316
321
317
let ( parse, rev_token_map) = token_tree_to_syntax_node ( & tt, expand_to) ;
322
318
323
- ExpandResult { value : Some ( ( parse, Arc :: new ( rev_token_map) ) ) , err }
319
+ ExpandResult { value : ( parse, Arc :: new ( rev_token_map) ) , err }
324
320
}
325
321
326
322
fn macro_arg (
327
323
db : & dyn ExpandDatabase ,
328
324
id : MacroCallId ,
329
- ) -> Option < Arc < ( tt:: Subtree , mbe:: TokenMap , fixup:: SyntaxFixupUndoInfo ) > > {
330
- let arg = db. macro_arg_text ( id) ?;
325
+ ) -> Arc < ( tt:: Subtree , mbe:: TokenMap , fixup:: SyntaxFixupUndoInfo ) > {
326
+ let Some ( arg) = db. macro_arg_text ( id) else {
327
+ return Arc :: new ( (
328
+ tt:: Subtree {
329
+ delimiter : tt:: Delimiter :: UNSPECIFIED ,
330
+ token_trees : Vec :: new ( ) ,
331
+ } ,
332
+ Default :: default ( ) ,
333
+ Default :: default ( ) )
334
+ ) ;
335
+ } ;
331
336
let loc = db. lookup_intern_macro_call ( id) ;
332
337
333
338
let node = SyntaxNode :: new_root ( arg) ;
@@ -346,7 +351,7 @@ fn macro_arg(
346
351
// proc macros expect their inputs without parentheses, MBEs expect it with them included
347
352
tt. delimiter = tt:: Delimiter :: unspecified ( ) ;
348
353
}
349
- Some ( Arc :: new ( ( tt, tmap, fixups. undo_info ) ) )
354
+ Arc :: new ( ( tt, tmap, fixups. undo_info ) )
350
355
}
351
356
352
357
fn censor_for_macro_input ( loc : & MacroCallLoc , node : & SyntaxNode ) -> FxHashSet < SyntaxNode > {
@@ -448,79 +453,66 @@ fn macro_def(
448
453
}
449
454
}
450
455
451
- fn macro_expand (
452
- db : & dyn ExpandDatabase ,
453
- id : MacroCallId ,
454
- // FIXME: Remove the OPtion if possible
455
- ) -> ExpandResult < Option < Arc < tt:: Subtree > > > {
456
+ fn macro_expand ( db : & dyn ExpandDatabase , id : MacroCallId ) -> ExpandResult < Arc < tt:: Subtree > > {
456
457
let _p = profile:: span ( "macro_expand" ) ;
457
458
let loc: MacroCallLoc = db. lookup_intern_macro_call ( id) ;
458
459
if let Some ( eager) = & loc. eager {
459
- return ExpandResult {
460
- value : Some ( eager. arg_or_expansion . clone ( ) ) ,
461
- err : eager. error . clone ( ) ,
462
- } ;
460
+ return ExpandResult { value : eager. arg_or_expansion . clone ( ) , err : eager. error . clone ( ) } ;
463
461
}
464
462
465
- let macro_arg = match db. macro_arg ( id) {
466
- Some ( it) => it,
467
- None => {
468
- return ExpandResult :: only_err ( ExpandError :: Other (
469
- "Failed to lower macro args to token tree" . into ( ) ,
470
- ) )
471
- }
472
- } ;
473
-
474
463
let expander = match db. macro_def ( loc. def ) {
475
464
Ok ( it) => it,
476
465
// FIXME: This is weird -- we effectively report macro *definition*
477
466
// errors lazily, when we try to expand the macro. Instead, they should
478
467
// be reported at the definition site when we construct a def map.
479
468
// (Note we do report them also at the definition site in the late diagnostic pass)
480
469
Err ( err) => {
481
- return ExpandResult :: only_err ( ExpandError :: Other (
482
- format ! ( "invalid macro definition: {err}" ) . into ( ) ,
483
- ) )
470
+ return ExpandResult {
471
+ value : Arc :: new ( tt:: Subtree {
472
+ delimiter : tt:: Delimiter :: UNSPECIFIED ,
473
+ token_trees : vec ! [ ] ,
474
+ } ) ,
475
+ err : Some ( ExpandError :: Other ( format ! ( "invalid macro definition: {err}" ) . into ( ) ) ) ,
476
+ }
484
477
}
485
478
} ;
479
+ let macro_arg = db. macro_arg ( id) ;
486
480
let ExpandResult { value : mut tt, err } = expander. expand ( db, id, & macro_arg. 0 ) ;
487
481
// Set a hard limit for the expanded tt
488
482
let count = tt. count ( ) ;
489
483
if TOKEN_LIMIT . check ( count) . is_err ( ) {
490
- return ExpandResult :: only_err ( ExpandError :: Other (
491
- format ! (
492
- "macro invocation exceeds token limit: produced {} tokens, limit is {}" ,
493
- count,
494
- TOKEN_LIMIT . inner( ) ,
495
- )
496
- . into ( ) ,
497
- ) ) ;
484
+ return ExpandResult {
485
+ value : Arc :: new ( tt:: Subtree {
486
+ delimiter : tt:: Delimiter :: UNSPECIFIED ,
487
+ token_trees : vec ! [ ] ,
488
+ } ) ,
489
+ err : Some ( ExpandError :: Other (
490
+ format ! (
491
+ "macro invocation exceeds token limit: produced {} tokens, limit is {}" ,
492
+ count,
493
+ TOKEN_LIMIT . inner( ) ,
494
+ )
495
+ . into ( ) ,
496
+ ) ) ,
497
+ } ;
498
498
}
499
499
500
500
fixup:: reverse_fixups ( & mut tt, & macro_arg. 1 , & macro_arg. 2 ) ;
501
501
502
- ExpandResult { value : Some ( Arc :: new ( tt) ) , err }
502
+ ExpandResult { value : Arc :: new ( tt) , err }
503
503
}
504
504
505
505
fn parse_macro_expansion_error (
506
506
db : & dyn ExpandDatabase ,
507
507
macro_call_id : MacroCallId ,
508
- ) -> ExpandResult < Option < Box < [ SyntaxError ] > > > {
508
+ ) -> ExpandResult < Box < [ SyntaxError ] > > {
509
509
db. parse_macro_expansion ( MacroFile { macro_call_id } )
510
- . map ( |it| it. map ( | ( it , _ ) | it . errors ( ) . to_vec ( ) . into_boxed_slice ( ) ) )
510
+ . map ( |it| it. 0 . errors ( ) . to_vec ( ) . into_boxed_slice ( ) )
511
511
}
512
512
513
513
fn expand_proc_macro ( db : & dyn ExpandDatabase , id : MacroCallId ) -> ExpandResult < tt:: Subtree > {
514
514
let loc: MacroCallLoc = db. lookup_intern_macro_call ( id) ;
515
- let macro_arg = match db. macro_arg ( id) {
516
- Some ( it) => it,
517
- None => {
518
- return ExpandResult :: with_err (
519
- tt:: Subtree :: empty ( ) ,
520
- ExpandError :: Other ( "No arguments for proc-macro" . into ( ) ) ,
521
- )
522
- }
523
- } ;
515
+ let macro_arg = db. macro_arg ( id) ;
524
516
525
517
let expander = match loc. def . kind {
526
518
MacroDefKind :: ProcMacro ( expander, ..) => expander,
0 commit comments