@@ -108,7 +108,7 @@ pub trait ExpandDatabase: SourceDatabase {
108108 fn parse_macro_expansion (
109109 & self ,
110110 macro_file : MacroFile ,
111- ) -> ExpandResult < Option < ( Parse < SyntaxNode > , Arc < mbe:: TokenMap > ) > > ;
111+ ) -> ExpandResult < ( Parse < SyntaxNode > , Arc < mbe:: TokenMap > ) > ;
112112
113113 /// Macro ids. That's probably the tricksiest bit in rust-analyzer, and the
114114 /// reason why we use salsa at all.
@@ -123,7 +123,7 @@ pub trait ExpandDatabase: SourceDatabase {
123123 fn macro_arg (
124124 & self ,
125125 id : MacroCallId ,
126- ) -> Option < Arc < ( tt:: Subtree , mbe:: TokenMap , fixup:: SyntaxFixupUndoInfo ) > > ;
126+ ) -> Arc < ( tt:: Subtree , mbe:: TokenMap , fixup:: SyntaxFixupUndoInfo ) > ;
127127 /// Extracts syntax node, corresponding to a macro call. That's a firewall
128128 /// query, only typing in the macro call itself changes the returned
129129 /// subtree.
@@ -133,7 +133,7 @@ pub trait ExpandDatabase: SourceDatabase {
133133 fn macro_def ( & self , id : MacroDefId ) -> Result < Arc < TokenExpander > , mbe:: ParseError > ;
134134
135135 /// 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 > > ;
137137 /// Special case of the previous query for procedural macros. We can't LRU
138138 /// proc macros, since they are not deterministic in general, and
139139 /// non-determinism breaks salsa in a very, very, very bad way. @edwin0cheng
@@ -143,7 +143,7 @@ pub trait ExpandDatabase: SourceDatabase {
143143 fn parse_macro_expansion_error (
144144 & self ,
145145 macro_call : MacroCallId ,
146- ) -> ExpandResult < Option < Box < [ SyntaxError ] > > > ;
146+ ) -> ExpandResult < Box < [ SyntaxError ] > > ;
147147
148148 fn hygiene_frame ( & self , file_id : HirFileId ) -> Arc < HygieneFrame > ;
149149}
@@ -257,12 +257,12 @@ fn ast_id_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
257257}
258258
259259fn 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 ( ) ,
262262 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 ( )
264264 }
265- }
265+ } )
266266}
267267
268268fn parse_or_expand_with_err (
@@ -272,17 +272,17 @@ fn parse_or_expand_with_err(
272272 match file_id. repr ( ) {
273273 HirFileIdRepr :: FileId ( file_id) => ExpandResult :: ok ( Some ( db. parse ( file_id) . to_syntax ( ) ) ) ,
274274 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 ) )
276276 }
277277 }
278278}
279279
280280fn parse_macro_expansion (
281281 db : & dyn ExpandDatabase ,
282282 macro_file : MacroFile ,
283- ) -> ExpandResult < Option < ( Parse < SyntaxNode > , Arc < mbe:: TokenMap > ) > > {
283+ ) -> ExpandResult < ( Parse < SyntaxNode > , Arc < mbe:: TokenMap > ) > {
284284 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 ) ;
286286
287287 if let Some ( err) = & err {
288288 if tracing:: enabled!( tracing:: Level :: DEBUG ) {
@@ -308,10 +308,6 @@ fn parse_macro_expansion(
308308 ) ;
309309 }
310310 }
311- let tt = match value {
312- Some ( tt) => tt,
313- None => return ExpandResult { value : None , err } ,
314- } ;
315311
316312 let expand_to = macro_expand_to ( db, macro_file. macro_call_id ) ;
317313
@@ -320,14 +316,23 @@ fn parse_macro_expansion(
320316
321317 let ( parse, rev_token_map) = token_tree_to_syntax_node ( & tt, expand_to) ;
322318
323- ExpandResult { value : Some ( ( parse, Arc :: new ( rev_token_map) ) ) , err }
319+ ExpandResult { value : ( parse, Arc :: new ( rev_token_map) ) , err }
324320}
325321
326322fn macro_arg (
327323 db : & dyn ExpandDatabase ,
328324 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+ } ;
331336 let loc = db. lookup_intern_macro_call ( id) ;
332337
333338 let node = SyntaxNode :: new_root ( arg) ;
@@ -346,7 +351,7 @@ fn macro_arg(
346351 // proc macros expect their inputs without parentheses, MBEs expect it with them included
347352 tt. delimiter = tt:: Delimiter :: unspecified ( ) ;
348353 }
349- Some ( Arc :: new ( ( tt, tmap, fixups. undo_info ) ) )
354+ Arc :: new ( ( tt, tmap, fixups. undo_info ) )
350355}
351356
352357fn censor_for_macro_input ( loc : & MacroCallLoc , node : & SyntaxNode ) -> FxHashSet < SyntaxNode > {
@@ -448,79 +453,66 @@ fn macro_def(
448453 }
449454}
450455
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 > > {
456457 let _p = profile:: span ( "macro_expand" ) ;
457458 let loc: MacroCallLoc = db. lookup_intern_macro_call ( id) ;
458459 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 ( ) } ;
463461 }
464462
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-
474463 let expander = match db. macro_def ( loc. def ) {
475464 Ok ( it) => it,
476465 // FIXME: This is weird -- we effectively report macro *definition*
477466 // errors lazily, when we try to expand the macro. Instead, they should
478467 // be reported at the definition site when we construct a def map.
479468 // (Note we do report them also at the definition site in the late diagnostic pass)
480469 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+ }
484477 }
485478 } ;
479+ let macro_arg = db. macro_arg ( id) ;
486480 let ExpandResult { value : mut tt, err } = expander. expand ( db, id, & macro_arg. 0 ) ;
487481 // Set a hard limit for the expanded tt
488482 let count = tt. count ( ) ;
489483 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+ } ;
498498 }
499499
500500 fixup:: reverse_fixups ( & mut tt, & macro_arg. 1 , & macro_arg. 2 ) ;
501501
502- ExpandResult { value : Some ( Arc :: new ( tt) ) , err }
502+ ExpandResult { value : Arc :: new ( tt) , err }
503503}
504504
505505fn parse_macro_expansion_error (
506506 db : & dyn ExpandDatabase ,
507507 macro_call_id : MacroCallId ,
508- ) -> ExpandResult < Option < Box < [ SyntaxError ] > > > {
508+ ) -> ExpandResult < Box < [ SyntaxError ] > > {
509509 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 ( ) )
511511}
512512
513513fn expand_proc_macro ( db : & dyn ExpandDatabase , id : MacroCallId ) -> ExpandResult < tt:: Subtree > {
514514 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) ;
524516
525517 let expander = match loc. def . kind {
526518 MacroDefKind :: ProcMacro ( expander, ..) => expander,
0 commit comments