@@ -628,6 +628,40 @@ fn check_lhs_nt_follows(sess: &ParseSess, def: &ast::Item, lhs: &mbe::TokenTree)
628628 // after parsing/expansion. we can report every error in every macro this way.
629629}
630630
631+ fn is_empty_token_tree ( sess : & ParseSess , seq : & mbe:: SequenceRepetition ) -> bool {
632+ if seq. separator . is_some ( ) {
633+ false
634+ } else {
635+ let mut is_empty = true ;
636+ let mut iter = seq. tts . iter ( ) . peekable ( ) ;
637+ while let Some ( tt) = iter. next ( ) {
638+ match tt {
639+ mbe:: TokenTree :: MetaVarDecl ( _, _, Some ( NonterminalKind :: Vis ) ) => { }
640+ mbe:: TokenTree :: Token ( t @ Token { kind : DocComment ( ..) , .. } ) => {
641+ let mut now = t;
642+ while let Some ( & mbe:: TokenTree :: Token (
643+ next @ Token { kind : DocComment ( ..) , .. } ,
644+ ) ) = iter. peek ( )
645+ {
646+ now = next;
647+ iter. next ( ) ;
648+ }
649+ let span = t. span . to ( now. span ) ;
650+ sess. span_diagnostic . span_note_without_error (
651+ span,
652+ "doc comments are ignored in matcher position" ,
653+ ) ;
654+ }
655+ mbe:: TokenTree :: Sequence ( _, sub_seq)
656+ if ( sub_seq. kleene . op == mbe:: KleeneOp :: ZeroOrMore
657+ || sub_seq. kleene . op == mbe:: KleeneOp :: ZeroOrOne ) => { }
658+ _ => is_empty = false ,
659+ }
660+ }
661+ is_empty
662+ }
663+ }
664+
631665/// Checks that the lhs contains no repetition which could match an empty token
632666/// tree, because then the matcher would hang indefinitely.
633667fn check_lhs_no_empty_seq ( sess : & ParseSess , tts : & [ mbe:: TokenTree ] ) -> bool {
@@ -644,17 +678,7 @@ fn check_lhs_no_empty_seq(sess: &ParseSess, tts: &[mbe::TokenTree]) -> bool {
644678 }
645679 }
646680 TokenTree :: Sequence ( span, seq) => {
647- if seq. separator . is_none ( )
648- && seq. tts . iter ( ) . all ( |seq_tt| match seq_tt {
649- TokenTree :: MetaVarDecl ( _, _, Some ( NonterminalKind :: Vis ) ) => true ,
650- TokenTree :: Token ( t) => matches ! ( t, Token { kind: DocComment ( ..) , .. } ) ,
651- TokenTree :: Sequence ( _, sub_seq) => {
652- sub_seq. kleene . op == mbe:: KleeneOp :: ZeroOrMore
653- || sub_seq. kleene . op == mbe:: KleeneOp :: ZeroOrOne
654- }
655- _ => false ,
656- } )
657- {
681+ if is_empty_token_tree ( sess, seq) {
658682 let sp = span. entire ( ) ;
659683 sess. span_diagnostic . span_err ( sp, "repetition matches empty token tree" ) ;
660684 return false ;
0 commit comments