@@ -373,17 +373,10 @@ pub fn compile_declarative_macro(
373
373
node_id : NodeId ,
374
374
edition : Edition ,
375
375
) -> ( SyntaxExtension , usize ) {
376
+ let is_local = node_id != DUMMY_NODE_ID ;
376
377
let mk_syn_ext = |expander| {
377
- SyntaxExtension :: new (
378
- sess,
379
- SyntaxExtensionKind :: LegacyBang ( expander) ,
380
- span,
381
- Vec :: new ( ) ,
382
- edition,
383
- ident. name ,
384
- attrs,
385
- node_id != DUMMY_NODE_ID ,
386
- )
378
+ let kind = SyntaxExtensionKind :: LegacyBang ( expander) ;
379
+ SyntaxExtension :: new ( sess, kind, span, Vec :: new ( ) , edition, ident. name , attrs, is_local)
387
380
} ;
388
381
let dummy_syn_ext = |guar| ( mk_syn_ext ( Arc :: new ( DummyExpander ( guar) ) ) , 0 ) ;
389
382
@@ -393,7 +386,8 @@ pub fn compile_declarative_macro(
393
386
let body = macro_def. body . tokens . clone ( ) ;
394
387
let mut p = Parser :: new ( & sess. psess , body, rustc_parse:: MACRO_ARGUMENTS ) ;
395
388
396
- // Don't abort iteration early, so that multiple errors can be reported.
389
+ // Don't abort iteration early, so that multiple errors can be reported. We only abort early on
390
+ // parse failures we can't recover from.
397
391
let mut guar = None ;
398
392
let mut check_emission = |ret : Result < ( ) , ErrorGuaranteed > | guar = guar. or ( ret. err ( ) ) ;
399
393
@@ -402,20 +396,11 @@ pub fn compile_declarative_macro(
402
396
while p. token != token:: Eof {
403
397
let lhs_tt = p. parse_token_tree ( ) ;
404
398
let lhs_tt = parse_one_tt ( lhs_tt, RulePart :: Pattern , sess, node_id, features, edition) ;
405
- // We don't handle errors here, the driver will abort after parsing/expansion. We can
406
- // report every error in every macro this way.
407
- check_emission ( check_lhs_nt_follows ( sess, node_id, & lhs_tt) ) ;
408
- check_emission ( check_lhs_no_empty_seq ( sess, slice:: from_ref ( & lhs_tt) ) ) ;
399
+ check_emission ( check_lhs ( sess, node_id, & lhs_tt) ) ;
409
400
if let Err ( e) = p. expect ( exp ! ( FatArrow ) ) {
410
401
return dummy_syn_ext ( e. emit ( ) ) ;
411
402
}
412
- if p. token == token:: Eof {
413
- let err_sp = p. token . span . shrink_to_hi ( ) ;
414
- let guar = sess
415
- . dcx ( )
416
- . struct_span_err ( err_sp, "macro definition ended unexpectedly" )
417
- . with_span_label ( err_sp, "expected right-hand side of macro rule" )
418
- . emit ( ) ;
403
+ if let Some ( guar) = check_no_eof ( sess, & p, "expected right-hand side of macro rule" ) {
419
404
return dummy_syn_ext ( guar) ;
420
405
}
421
406
let rhs_tt = p. parse_token_tree ( ) ;
@@ -454,13 +439,32 @@ pub fn compile_declarative_macro(
454
439
}
455
440
456
441
// Return the number of rules for unused rule linting, if this is a local macro.
457
- let nrules = if node_id != DUMMY_NODE_ID { rules. len ( ) } else { 0 } ;
442
+ let nrules = if is_local { rules. len ( ) } else { 0 } ;
458
443
459
444
let expander =
460
445
Arc :: new ( MacroRulesMacroExpander { name : ident, span, node_id, transparency, rules } ) ;
461
446
( mk_syn_ext ( expander) , nrules)
462
447
}
463
448
449
+ fn check_no_eof ( sess : & Session , p : & Parser < ' _ > , msg : & ' static str ) -> Option < ErrorGuaranteed > {
450
+ if p. token == token:: Eof {
451
+ let err_sp = p. token . span . shrink_to_hi ( ) ;
452
+ let guar = sess
453
+ . dcx ( )
454
+ . struct_span_err ( err_sp, "macro definition ended unexpectedly" )
455
+ . with_span_label ( err_sp, msg)
456
+ . emit ( ) ;
457
+ return Some ( guar) ;
458
+ }
459
+ None
460
+ }
461
+
462
+ fn check_lhs ( sess : & Session , node_id : NodeId , lhs : & mbe:: TokenTree ) -> Result < ( ) , ErrorGuaranteed > {
463
+ let e1 = check_lhs_nt_follows ( sess, node_id, lhs) ;
464
+ let e2 = check_lhs_no_empty_seq ( sess, slice:: from_ref ( lhs) ) ;
465
+ e1. and ( e2)
466
+ }
467
+
464
468
fn check_lhs_nt_follows (
465
469
sess : & Session ,
466
470
node_id : NodeId ,
0 commit comments