]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_expand/src/mbe/macro_rules.rs
Use &mut Diagnostic instead of &mut DiagnosticBuilder unless needed
[rust.git] / compiler / rustc_expand / src / mbe / macro_rules.rs
1 use crate::base::{DummyResult, ExtCtxt, MacResult, TTMacroExpander};
2 use crate::base::{SyntaxExtension, SyntaxExtensionKind};
3 use crate::expand::{ensure_complete_parse, parse_ast_fragment, AstFragment, AstFragmentKind};
4 use crate::mbe;
5 use crate::mbe::macro_check;
6 use crate::mbe::macro_parser::{Error, ErrorReported, Failure, Success, TtParser};
7 use crate::mbe::macro_parser::{MatchedSeq, MatchedTokenTree, MatcherLoc};
8 use crate::mbe::transcribe::transcribe;
9
10 use rustc_ast as ast;
11 use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind, TokenKind::*};
12 use rustc_ast::tokenstream::{DelimSpan, TokenStream};
13 use rustc_ast::{NodeId, DUMMY_NODE_ID};
14 use rustc_ast_pretty::pprust;
15 use rustc_attr::{self as attr, TransparencyError};
16 use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
17 use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder};
18 use rustc_feature::Features;
19 use rustc_lint_defs::builtin::{
20     RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
21 };
22 use rustc_lint_defs::BuiltinLintDiagnostics;
23 use rustc_parse::parser::Parser;
24 use rustc_session::parse::ParseSess;
25 use rustc_session::Session;
26 use rustc_span::edition::Edition;
27 use rustc_span::hygiene::Transparency;
28 use rustc_span::source_map::SourceMap;
29 use rustc_span::symbol::{kw, sym, Ident, MacroRulesNormalizedIdent};
30 use rustc_span::Span;
31
32 use std::borrow::Cow;
33 use std::collections::hash_map::Entry;
34 use std::{mem, slice};
35 use tracing::debug;
36
37 pub(crate) struct ParserAnyMacro<'a> {
38     parser: Parser<'a>,
39
40     /// Span of the expansion site of the macro this parser is for
41     site_span: Span,
42     /// The ident of the macro we're parsing
43     macro_ident: Ident,
44     lint_node_id: NodeId,
45     is_trailing_mac: bool,
46     arm_span: Span,
47     /// Whether or not this macro is defined in the current crate
48     is_local: bool,
49 }
50
51 pub(crate) fn annotate_err_with_kind(err: &mut Diagnostic, kind: AstFragmentKind, span: Span) {
52     match kind {
53         AstFragmentKind::Ty => {
54             err.span_label(span, "this macro call doesn't expand to a type");
55         }
56         AstFragmentKind::Pat => {
57             err.span_label(span, "this macro call doesn't expand to a pattern");
58         }
59         _ => {}
60     };
61 }
62
63 fn emit_frag_parse_err(
64     mut e: DiagnosticBuilder<'_, rustc_errors::ErrorGuaranteed>,
65     parser: &Parser<'_>,
66     orig_parser: &mut Parser<'_>,
67     site_span: Span,
68     arm_span: Span,
69     kind: AstFragmentKind,
70 ) {
71     // FIXME(davidtwco): avoid depending on the error message text
72     if parser.token == token::Eof && e.message[0].0.expect_str().ends_with(", found `<eof>`") {
73         if !e.span.is_dummy() {
74             // early end of macro arm (#52866)
75             e.replace_span_with(parser.sess.source_map().next_point(parser.token.span));
76         }
77         let msg = &e.message[0];
78         e.message[0] = (
79             rustc_errors::DiagnosticMessage::Str(format!(
80                 "macro expansion ends with an incomplete expression: {}",
81                 msg.0.expect_str().replace(", found `<eof>`", ""),
82             )),
83             msg.1,
84         );
85     }
86     if e.span.is_dummy() {
87         // Get around lack of span in error (#30128)
88         e.replace_span_with(site_span);
89         if !parser.sess.source_map().is_imported(arm_span) {
90             e.span_label(arm_span, "in this macro arm");
91         }
92     } else if parser.sess.source_map().is_imported(parser.token.span) {
93         e.span_label(site_span, "in this macro invocation");
94     }
95     match kind {
96         // Try a statement if an expression is wanted but failed and suggest adding `;` to call.
97         AstFragmentKind::Expr => match parse_ast_fragment(orig_parser, AstFragmentKind::Stmts) {
98             Err(err) => err.cancel(),
99             Ok(_) => {
100                 e.note(
101                     "the macro call doesn't expand to an expression, but it can expand to a statement",
102                 );
103                 e.span_suggestion_verbose(
104                     site_span.shrink_to_hi(),
105                     "add `;` to interpret the expansion as a statement",
106                     ";",
107                     Applicability::MaybeIncorrect,
108                 );
109             }
110         },
111         _ => annotate_err_with_kind(&mut e, kind, site_span),
112     };
113     e.emit();
114 }
115
116 impl<'a> ParserAnyMacro<'a> {
117     pub(crate) fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
118         let ParserAnyMacro {
119             site_span,
120             macro_ident,
121             ref mut parser,
122             lint_node_id,
123             arm_span,
124             is_trailing_mac,
125             is_local,
126         } = *self;
127         let snapshot = &mut parser.create_snapshot_for_diagnostic();
128         let fragment = match parse_ast_fragment(parser, kind) {
129             Ok(f) => f,
130             Err(err) => {
131                 emit_frag_parse_err(err, parser, snapshot, site_span, arm_span, kind);
132                 return kind.dummy(site_span);
133             }
134         };
135
136         // We allow semicolons at the end of expressions -- e.g., the semicolon in
137         // `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
138         // but `m!()` is allowed in expression positions (cf. issue #34706).
139         if kind == AstFragmentKind::Expr && parser.token == token::Semi {
140             if is_local {
141                 parser.sess.buffer_lint_with_diagnostic(
142                     SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
143                     parser.token.span,
144                     lint_node_id,
145                     "trailing semicolon in macro used in expression position",
146                     BuiltinLintDiagnostics::TrailingMacro(is_trailing_mac, macro_ident),
147                 );
148             }
149             parser.bump();
150         }
151
152         // Make sure we don't have any tokens left to parse so we don't silently drop anything.
153         let path = ast::Path::from_ident(macro_ident.with_span_pos(site_span));
154         ensure_complete_parse(parser, &path, kind.name(), site_span);
155         fragment
156     }
157 }
158
159 struct MacroRulesMacroExpander {
160     node_id: NodeId,
161     name: Ident,
162     span: Span,
163     transparency: Transparency,
164     lhses: Vec<Vec<MatcherLoc>>,
165     rhses: Vec<mbe::TokenTree>,
166     valid: bool,
167 }
168
169 impl TTMacroExpander for MacroRulesMacroExpander {
170     fn expand<'cx>(
171         &self,
172         cx: &'cx mut ExtCtxt<'_>,
173         sp: Span,
174         input: TokenStream,
175     ) -> Box<dyn MacResult + 'cx> {
176         if !self.valid {
177             return DummyResult::any(sp);
178         }
179         expand_macro(
180             cx,
181             sp,
182             self.span,
183             self.node_id,
184             self.name,
185             self.transparency,
186             input,
187             &self.lhses,
188             &self.rhses,
189         )
190     }
191 }
192
193 fn macro_rules_dummy_expander<'cx>(
194     _: &'cx mut ExtCtxt<'_>,
195     span: Span,
196     _: TokenStream,
197 ) -> Box<dyn MacResult + 'cx> {
198     DummyResult::any(span)
199 }
200
201 fn trace_macros_note(cx_expansions: &mut FxIndexMap<Span, Vec<String>>, sp: Span, message: String) {
202     let sp = sp.macro_backtrace().last().map_or(sp, |trace| trace.call_site);
203     cx_expansions.entry(sp).or_default().push(message);
204 }
205
206 /// Expands the rules based macro defined by `lhses` and `rhses` for a given
207 /// input `arg`.
208 fn expand_macro<'cx>(
209     cx: &'cx mut ExtCtxt<'_>,
210     sp: Span,
211     def_span: Span,
212     node_id: NodeId,
213     name: Ident,
214     transparency: Transparency,
215     arg: TokenStream,
216     lhses: &[Vec<MatcherLoc>],
217     rhses: &[mbe::TokenTree],
218 ) -> Box<dyn MacResult + 'cx> {
219     let sess = &cx.sess.parse_sess;
220     // Macros defined in the current crate have a real node id,
221     // whereas macros from an external crate have a dummy id.
222     let is_local = node_id != DUMMY_NODE_ID;
223
224     if cx.trace_macros() {
225         let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(&arg));
226         trace_macros_note(&mut cx.expansions, sp, msg);
227     }
228
229     // Which arm's failure should we report? (the one furthest along)
230     let mut best_failure: Option<(Token, &str)> = None;
231
232     // We create a base parser that can be used for the "black box" parts.
233     // Every iteration needs a fresh copy of that parser. However, the parser
234     // is not mutated on many of the iterations, particularly when dealing with
235     // macros like this:
236     //
237     // macro_rules! foo {
238     //     ("a") => (A);
239     //     ("b") => (B);
240     //     ("c") => (C);
241     //     // ... etc. (maybe hundreds more)
242     // }
243     //
244     // as seen in the `html5ever` benchmark. We use a `Cow` so that the base
245     // parser is only cloned when necessary (upon mutation). Furthermore, we
246     // reinitialize the `Cow` with the base parser at the start of every
247     // iteration, so that any mutated parsers are not reused. This is all quite
248     // hacky, but speeds up the `html5ever` benchmark significantly. (Issue
249     // 68836 suggests a more comprehensive but more complex change to deal with
250     // this situation.)
251     let parser = parser_from_cx(sess, arg.clone());
252
253     // Try each arm's matchers.
254     let mut tt_parser = TtParser::new(name);
255     for (i, lhs) in lhses.iter().enumerate() {
256         // Take a snapshot of the state of pre-expansion gating at this point.
257         // This is used so that if a matcher is not `Success(..)`ful,
258         // then the spans which became gated when parsing the unsuccessful matcher
259         // are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
260         let mut gated_spans_snapshot = mem::take(&mut *sess.gated_spans.spans.borrow_mut());
261
262         match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs) {
263             Success(named_matches) => {
264                 // The matcher was `Success(..)`ful.
265                 // Merge the gated spans from parsing the matcher with the pre-existing ones.
266                 sess.gated_spans.merge(gated_spans_snapshot);
267
268                 let (rhs, rhs_span): (&mbe::Delimited, DelimSpan) = match &rhses[i] {
269                     mbe::TokenTree::Delimited(span, delimited) => (&delimited, *span),
270                     _ => cx.span_bug(sp, "malformed macro rhs"),
271                 };
272                 let arm_span = rhses[i].span();
273
274                 let rhs_spans = rhs.tts.iter().map(|t| t.span()).collect::<Vec<_>>();
275                 // rhs has holes ( `$id` and `$(...)` that need filled)
276                 let mut tts = match transcribe(cx, &named_matches, &rhs, rhs_span, transparency) {
277                     Ok(tts) => tts,
278                     Err(mut err) => {
279                         err.emit();
280                         return DummyResult::any(arm_span);
281                     }
282                 };
283
284                 // Replace all the tokens for the corresponding positions in the macro, to maintain
285                 // proper positions in error reporting, while maintaining the macro_backtrace.
286                 if rhs_spans.len() == tts.len() {
287                     tts = tts.map_enumerated(|i, tt| {
288                         let mut tt = tt.clone();
289                         let mut sp = rhs_spans[i];
290                         sp = sp.with_ctxt(tt.span().ctxt());
291                         tt.set_span(sp);
292                         tt
293                     });
294                 }
295
296                 if cx.trace_macros() {
297                     let msg = format!("to `{}`", pprust::tts_to_string(&tts));
298                     trace_macros_note(&mut cx.expansions, sp, msg);
299                 }
300
301                 let mut p = Parser::new(sess, tts, false, None);
302                 p.last_type_ascription = cx.current_expansion.prior_type_ascription;
303
304                 if is_local {
305                     cx.resolver.record_macro_rule_usage(node_id, i);
306                 }
307
308                 // Let the context choose how to interpret the result.
309                 // Weird, but useful for X-macros.
310                 return Box::new(ParserAnyMacro {
311                     parser: p,
312
313                     // Pass along the original expansion site and the name of the macro
314                     // so we can print a useful error message if the parse of the expanded
315                     // macro leaves unparsed tokens.
316                     site_span: sp,
317                     macro_ident: name,
318                     lint_node_id: cx.current_expansion.lint_node_id,
319                     is_trailing_mac: cx.current_expansion.is_trailing_mac,
320                     arm_span,
321                     is_local,
322                 });
323             }
324             Failure(token, msg) => match best_failure {
325                 Some((ref best_token, _)) if best_token.span.lo() >= token.span.lo() => {}
326                 _ => best_failure = Some((token, msg)),
327             },
328             Error(err_sp, ref msg) => {
329                 let span = err_sp.substitute_dummy(sp);
330                 cx.struct_span_err(span, &msg).emit();
331                 return DummyResult::any(span);
332             }
333             ErrorReported => return DummyResult::any(sp),
334         }
335
336         // The matcher was not `Success(..)`ful.
337         // Restore to the state before snapshotting and maybe try again.
338         mem::swap(&mut gated_spans_snapshot, &mut sess.gated_spans.spans.borrow_mut());
339     }
340     drop(parser);
341
342     let (token, label) = best_failure.expect("ran no matchers");
343     let span = token.span.substitute_dummy(sp);
344     let mut err = cx.struct_span_err(span, &parse_failure_msg(&token));
345     err.span_label(span, label);
346     if !def_span.is_dummy() && !cx.source_map().is_imported(def_span) {
347         err.span_label(cx.source_map().guess_head_span(def_span), "when calling this macro");
348     }
349     annotate_doc_comment(&mut err, sess.source_map(), span);
350     // Check whether there's a missing comma in this macro call, like `println!("{}" a);`
351     if let Some((arg, comma_span)) = arg.add_comma() {
352         for lhs in lhses {
353             let parser = parser_from_cx(sess, arg.clone());
354             if let Success(_) = tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs) {
355                 if comma_span.is_dummy() {
356                     err.note("you might be missing a comma");
357                 } else {
358                     err.span_suggestion_short(
359                         comma_span,
360                         "missing comma here",
361                         ", ",
362                         Applicability::MachineApplicable,
363                     );
364                 }
365             }
366         }
367     }
368     err.emit();
369     cx.trace_macros_diag();
370     DummyResult::any(sp)
371 }
372
373 // Note that macro-by-example's input is also matched against a token tree:
374 //                   $( $lhs:tt => $rhs:tt );+
375 //
376 // Holy self-referential!
377
378 /// Converts a macro item into a syntax extension.
379 pub fn compile_declarative_macro(
380     sess: &Session,
381     features: &Features,
382     def: &ast::Item,
383     edition: Edition,
384 ) -> (SyntaxExtension, Vec<(usize, Span)>) {
385     debug!("compile_declarative_macro: {:?}", def);
386     let mk_syn_ext = |expander| {
387         SyntaxExtension::new(
388             sess,
389             SyntaxExtensionKind::LegacyBang(expander),
390             def.span,
391             Vec::new(),
392             edition,
393             def.ident.name,
394             &def.attrs,
395         )
396     };
397     let dummy_syn_ext = || (mk_syn_ext(Box::new(macro_rules_dummy_expander)), Vec::new());
398
399     let diag = &sess.parse_sess.span_diagnostic;
400     let lhs_nm = Ident::new(sym::lhs, def.span);
401     let rhs_nm = Ident::new(sym::rhs, def.span);
402     let tt_spec = Some(NonterminalKind::TT);
403
404     // Parse the macro_rules! invocation
405     let (macro_rules, body) = match &def.kind {
406         ast::ItemKind::MacroDef(def) => (def.macro_rules, def.body.inner_tokens()),
407         _ => unreachable!(),
408     };
409
410     // The pattern that macro_rules matches.
411     // The grammar for macro_rules! is:
412     // $( $lhs:tt => $rhs:tt );+
413     // ...quasiquoting this would be nice.
414     // These spans won't matter, anyways
415     let argument_gram = vec![
416         mbe::TokenTree::Sequence(
417             DelimSpan::dummy(),
418             mbe::SequenceRepetition {
419                 tts: vec![
420                     mbe::TokenTree::MetaVarDecl(def.span, lhs_nm, tt_spec),
421                     mbe::TokenTree::token(token::FatArrow, def.span),
422                     mbe::TokenTree::MetaVarDecl(def.span, rhs_nm, tt_spec),
423                 ],
424                 separator: Some(Token::new(
425                     if macro_rules { token::Semi } else { token::Comma },
426                     def.span,
427                 )),
428                 kleene: mbe::KleeneToken::new(mbe::KleeneOp::OneOrMore, def.span),
429                 num_captures: 2,
430             },
431         ),
432         // to phase into semicolon-termination instead of semicolon-separation
433         mbe::TokenTree::Sequence(
434             DelimSpan::dummy(),
435             mbe::SequenceRepetition {
436                 tts: vec![mbe::TokenTree::token(
437                     if macro_rules { token::Semi } else { token::Comma },
438                     def.span,
439                 )],
440                 separator: None,
441                 kleene: mbe::KleeneToken::new(mbe::KleeneOp::ZeroOrMore, def.span),
442                 num_captures: 0,
443             },
444         ),
445     ];
446     // Convert it into `MatcherLoc` form.
447     let argument_gram = mbe::macro_parser::compute_locs(&argument_gram);
448
449     let parser = Parser::new(&sess.parse_sess, body, true, rustc_parse::MACRO_ARGUMENTS);
450     let mut tt_parser =
451         TtParser::new(Ident::with_dummy_span(if macro_rules { kw::MacroRules } else { kw::Macro }));
452     let argument_map = match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), &argument_gram) {
453         Success(m) => m,
454         Failure(token, msg) => {
455             let s = parse_failure_msg(&token);
456             let sp = token.span.substitute_dummy(def.span);
457             let mut err = sess.parse_sess.span_diagnostic.struct_span_err(sp, &s);
458             err.span_label(sp, msg);
459             annotate_doc_comment(&mut err, sess.source_map(), sp);
460             err.emit();
461             return dummy_syn_ext();
462         }
463         Error(sp, msg) => {
464             sess.parse_sess
465                 .span_diagnostic
466                 .struct_span_err(sp.substitute_dummy(def.span), &msg)
467                 .emit();
468             return dummy_syn_ext();
469         }
470         ErrorReported => {
471             return dummy_syn_ext();
472         }
473     };
474
475     let mut valid = true;
476
477     // Extract the arguments:
478     let lhses = match argument_map[&MacroRulesNormalizedIdent::new(lhs_nm)] {
479         MatchedSeq(ref s) => s
480             .iter()
481             .map(|m| {
482                 if let MatchedTokenTree(ref tt) = *m {
483                     let tt = mbe::quoted::parse(
484                         TokenStream::new(vec![tt.clone()]),
485                         true,
486                         &sess.parse_sess,
487                         def.id,
488                         features,
489                         edition,
490                     )
491                     .pop()
492                     .unwrap();
493                     valid &= check_lhs_nt_follows(&sess.parse_sess, &def, &tt);
494                     return tt;
495                 }
496                 sess.parse_sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
497             })
498             .collect::<Vec<mbe::TokenTree>>(),
499         _ => sess.parse_sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs"),
500     };
501
502     let rhses = match argument_map[&MacroRulesNormalizedIdent::new(rhs_nm)] {
503         MatchedSeq(ref s) => s
504             .iter()
505             .map(|m| {
506                 if let MatchedTokenTree(ref tt) = *m {
507                     return mbe::quoted::parse(
508                         TokenStream::new(vec![tt.clone()]),
509                         false,
510                         &sess.parse_sess,
511                         def.id,
512                         features,
513                         edition,
514                     )
515                     .pop()
516                     .unwrap();
517                 }
518                 sess.parse_sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
519             })
520             .collect::<Vec<mbe::TokenTree>>(),
521         _ => sess.parse_sess.span_diagnostic.span_bug(def.span, "wrong-structured rhs"),
522     };
523
524     for rhs in &rhses {
525         valid &= check_rhs(&sess.parse_sess, rhs);
526     }
527
528     // don't abort iteration early, so that errors for multiple lhses can be reported
529     for lhs in &lhses {
530         valid &= check_lhs_no_empty_seq(&sess.parse_sess, slice::from_ref(lhs));
531     }
532
533     valid &= macro_check::check_meta_variables(&sess.parse_sess, def.id, def.span, &lhses, &rhses);
534
535     let (transparency, transparency_error) = attr::find_transparency(&def.attrs, macro_rules);
536     match transparency_error {
537         Some(TransparencyError::UnknownTransparency(value, span)) => {
538             diag.span_err(span, &format!("unknown macro transparency: `{}`", value));
539         }
540         Some(TransparencyError::MultipleTransparencyAttrs(old_span, new_span)) => {
541             diag.span_err(vec![old_span, new_span], "multiple macro transparency attributes");
542         }
543         None => {}
544     }
545
546     // Compute the spans of the macro rules for unused rule linting.
547     // To avoid warning noise, only consider the rules of this
548     // macro for the lint, if all rules are valid.
549     // Also, we are only interested in non-foreign macros.
550     let rule_spans = if valid && def.id != DUMMY_NODE_ID {
551         lhses
552             .iter()
553             .zip(rhses.iter())
554             .enumerate()
555             // If the rhs contains an invocation like compile_error!,
556             // don't consider the rule for the unused rule lint.
557             .filter(|(_idx, (_lhs, rhs))| !has_compile_error_macro(rhs))
558             // We only take the span of the lhs here,
559             // so that the spans of created warnings are smaller.
560             .map(|(idx, (lhs, _rhs))| (idx, lhs.span()))
561             .collect::<Vec<_>>()
562     } else {
563         Vec::new()
564     };
565
566     // Convert the lhses into `MatcherLoc` form, which is better for doing the
567     // actual matching. Unless the matcher is invalid.
568     let lhses = if valid {
569         lhses
570             .iter()
571             .map(|lhs| {
572                 // Ignore the delimiters around the matcher.
573                 match lhs {
574                     mbe::TokenTree::Delimited(_, delimited) => {
575                         mbe::macro_parser::compute_locs(&delimited.tts)
576                     }
577                     _ => sess.parse_sess.span_diagnostic.span_bug(def.span, "malformed macro lhs"),
578                 }
579             })
580             .collect()
581     } else {
582         vec![]
583     };
584
585     let expander = Box::new(MacroRulesMacroExpander {
586         name: def.ident,
587         span: def.span,
588         node_id: def.id,
589         transparency,
590         lhses,
591         rhses,
592         valid,
593     });
594     (mk_syn_ext(expander), rule_spans)
595 }
596
597 #[derive(SessionSubdiagnostic)]
598 enum ExplainDocComment {
599     #[label(expand::explain_doc_comment_inner)]
600     Inner {
601         #[primary_span]
602         span: Span,
603     },
604     #[label(expand::explain_doc_comment_outer)]
605     Outer {
606         #[primary_span]
607         span: Span,
608     },
609 }
610
611 fn annotate_doc_comment(err: &mut Diagnostic, sm: &SourceMap, span: Span) {
612     if let Ok(src) = sm.span_to_snippet(span) {
613         if src.starts_with("///") || src.starts_with("/**") {
614             err.subdiagnostic(ExplainDocComment::Outer { span });
615         } else if src.starts_with("//!") || src.starts_with("/*!") {
616             err.subdiagnostic(ExplainDocComment::Inner { span });
617         }
618     }
619 }
620
621 fn check_lhs_nt_follows(sess: &ParseSess, def: &ast::Item, lhs: &mbe::TokenTree) -> bool {
622     // lhs is going to be like TokenTree::Delimited(...), where the
623     // entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
624     if let mbe::TokenTree::Delimited(_, delimited) = lhs {
625         check_matcher(sess, def, &delimited.tts)
626     } else {
627         let msg = "invalid macro matcher; matchers must be contained in balanced delimiters";
628         sess.span_diagnostic.span_err(lhs.span(), msg);
629         false
630     }
631     // we don't abort on errors on rejection, the driver will do that for us
632     // after parsing/expansion. we can report every error in every macro this way.
633 }
634
635 /// Checks that the lhs contains no repetition which could match an empty token
636 /// tree, because then the matcher would hang indefinitely.
637 fn check_lhs_no_empty_seq(sess: &ParseSess, tts: &[mbe::TokenTree]) -> bool {
638     use mbe::TokenTree;
639     for tt in tts {
640         match *tt {
641             TokenTree::Token(..)
642             | TokenTree::MetaVar(..)
643             | TokenTree::MetaVarDecl(..)
644             | TokenTree::MetaVarExpr(..) => (),
645             TokenTree::Delimited(_, ref del) => {
646                 if !check_lhs_no_empty_seq(sess, &del.tts) {
647                     return false;
648                 }
649             }
650             TokenTree::Sequence(span, ref seq) => {
651                 if seq.separator.is_none()
652                     && seq.tts.iter().all(|seq_tt| match *seq_tt {
653                         TokenTree::MetaVarDecl(_, _, Some(NonterminalKind::Vis)) => true,
654                         TokenTree::Sequence(_, ref sub_seq) => {
655                             sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
656                                 || sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne
657                         }
658                         _ => false,
659                     })
660                 {
661                     let sp = span.entire();
662                     sess.span_diagnostic.span_err(sp, "repetition matches empty token tree");
663                     return false;
664                 }
665                 if !check_lhs_no_empty_seq(sess, &seq.tts) {
666                     return false;
667                 }
668             }
669         }
670     }
671
672     true
673 }
674
675 fn check_rhs(sess: &ParseSess, rhs: &mbe::TokenTree) -> bool {
676     match *rhs {
677         mbe::TokenTree::Delimited(..) => return true,
678         _ => {
679             sess.span_diagnostic.span_err(rhs.span(), "macro rhs must be delimited");
680         }
681     }
682     false
683 }
684
685 fn check_matcher(sess: &ParseSess, def: &ast::Item, matcher: &[mbe::TokenTree]) -> bool {
686     let first_sets = FirstSets::new(matcher);
687     let empty_suffix = TokenSet::empty();
688     let err = sess.span_diagnostic.err_count();
689     check_matcher_core(sess, def, &first_sets, matcher, &empty_suffix);
690     err == sess.span_diagnostic.err_count()
691 }
692
693 fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool {
694     match rhs {
695         mbe::TokenTree::Delimited(_sp, d) => {
696             let has_compile_error = d.tts.array_windows::<3>().any(|[ident, bang, args]| {
697                 if let mbe::TokenTree::Token(ident) = ident &&
698                         let TokenKind::Ident(ident, _) = ident.kind &&
699                         ident == sym::compile_error &&
700                         let mbe::TokenTree::Token(bang) = bang &&
701                         let TokenKind::Not = bang.kind &&
702                         let mbe::TokenTree::Delimited(_, del) = args &&
703                         del.delim != Delimiter::Invisible
704                     {
705                         true
706                     } else {
707                         false
708                     }
709             });
710             if has_compile_error { true } else { d.tts.iter().any(has_compile_error_macro) }
711         }
712         _ => false,
713     }
714 }
715
716 // `The FirstSets` for a matcher is a mapping from subsequences in the
717 // matcher to the FIRST set for that subsequence.
718 //
719 // This mapping is partially precomputed via a backwards scan over the
720 // token trees of the matcher, which provides a mapping from each
721 // repetition sequence to its *first* set.
722 //
723 // (Hypothetically, sequences should be uniquely identifiable via their
724 // spans, though perhaps that is false, e.g., for macro-generated macros
725 // that do not try to inject artificial span information. My plan is
726 // to try to catch such cases ahead of time and not include them in
727 // the precomputed mapping.)
728 struct FirstSets<'tt> {
729     // this maps each TokenTree::Sequence `$(tt ...) SEP OP` that is uniquely identified by its
730     // span in the original matcher to the First set for the inner sequence `tt ...`.
731     //
732     // If two sequences have the same span in a matcher, then map that
733     // span to None (invalidating the mapping here and forcing the code to
734     // use a slow path).
735     first: FxHashMap<Span, Option<TokenSet<'tt>>>,
736 }
737
738 impl<'tt> FirstSets<'tt> {
739     fn new(tts: &'tt [mbe::TokenTree]) -> FirstSets<'tt> {
740         use mbe::TokenTree;
741
742         let mut sets = FirstSets { first: FxHashMap::default() };
743         build_recur(&mut sets, tts);
744         return sets;
745
746         // walks backward over `tts`, returning the FIRST for `tts`
747         // and updating `sets` at the same time for all sequence
748         // substructure we find within `tts`.
749         fn build_recur<'tt>(sets: &mut FirstSets<'tt>, tts: &'tt [TokenTree]) -> TokenSet<'tt> {
750             let mut first = TokenSet::empty();
751             for tt in tts.iter().rev() {
752                 match *tt {
753                     TokenTree::Token(..)
754                     | TokenTree::MetaVar(..)
755                     | TokenTree::MetaVarDecl(..)
756                     | TokenTree::MetaVarExpr(..) => {
757                         first.replace_with(TtHandle::TtRef(tt));
758                     }
759                     TokenTree::Delimited(span, ref delimited) => {
760                         build_recur(sets, &delimited.tts);
761                         first.replace_with(TtHandle::from_token_kind(
762                             token::OpenDelim(delimited.delim),
763                             span.open,
764                         ));
765                     }
766                     TokenTree::Sequence(sp, ref seq_rep) => {
767                         let subfirst = build_recur(sets, &seq_rep.tts);
768
769                         match sets.first.entry(sp.entire()) {
770                             Entry::Vacant(vac) => {
771                                 vac.insert(Some(subfirst.clone()));
772                             }
773                             Entry::Occupied(mut occ) => {
774                                 // if there is already an entry, then a span must have collided.
775                                 // This should not happen with typical macro_rules macros,
776                                 // but syntax extensions need not maintain distinct spans,
777                                 // so distinct syntax trees can be assigned the same span.
778                                 // In such a case, the map cannot be trusted; so mark this
779                                 // entry as unusable.
780                                 occ.insert(None);
781                             }
782                         }
783
784                         // If the sequence contents can be empty, then the first
785                         // token could be the separator token itself.
786
787                         if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
788                             first.add_one_maybe(TtHandle::from_token(sep.clone()));
789                         }
790
791                         // Reverse scan: Sequence comes before `first`.
792                         if subfirst.maybe_empty
793                             || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
794                             || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
795                         {
796                             // If sequence is potentially empty, then
797                             // union them (preserving first emptiness).
798                             first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
799                         } else {
800                             // Otherwise, sequence guaranteed
801                             // non-empty; replace first.
802                             first = subfirst;
803                         }
804                     }
805                 }
806             }
807
808             first
809         }
810     }
811
812     // walks forward over `tts` until all potential FIRST tokens are
813     // identified.
814     fn first(&self, tts: &'tt [mbe::TokenTree]) -> TokenSet<'tt> {
815         use mbe::TokenTree;
816
817         let mut first = TokenSet::empty();
818         for tt in tts.iter() {
819             assert!(first.maybe_empty);
820             match *tt {
821                 TokenTree::Token(..)
822                 | TokenTree::MetaVar(..)
823                 | TokenTree::MetaVarDecl(..)
824                 | TokenTree::MetaVarExpr(..) => {
825                     first.add_one(TtHandle::TtRef(tt));
826                     return first;
827                 }
828                 TokenTree::Delimited(span, ref delimited) => {
829                     first.add_one(TtHandle::from_token_kind(
830                         token::OpenDelim(delimited.delim),
831                         span.open,
832                     ));
833                     return first;
834                 }
835                 TokenTree::Sequence(sp, ref seq_rep) => {
836                     let subfirst_owned;
837                     let subfirst = match self.first.get(&sp.entire()) {
838                         Some(&Some(ref subfirst)) => subfirst,
839                         Some(&None) => {
840                             subfirst_owned = self.first(&seq_rep.tts);
841                             &subfirst_owned
842                         }
843                         None => {
844                             panic!("We missed a sequence during FirstSets construction");
845                         }
846                     };
847
848                     // If the sequence contents can be empty, then the first
849                     // token could be the separator token itself.
850                     if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
851                         first.add_one_maybe(TtHandle::from_token(sep.clone()));
852                     }
853
854                     assert!(first.maybe_empty);
855                     first.add_all(subfirst);
856                     if subfirst.maybe_empty
857                         || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
858                         || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
859                     {
860                         // Continue scanning for more first
861                         // tokens, but also make sure we
862                         // restore empty-tracking state.
863                         first.maybe_empty = true;
864                         continue;
865                     } else {
866                         return first;
867                     }
868                 }
869             }
870         }
871
872         // we only exit the loop if `tts` was empty or if every
873         // element of `tts` matches the empty sequence.
874         assert!(first.maybe_empty);
875         first
876     }
877 }
878
879 // Most `mbe::TokenTree`s are pre-existing in the matcher, but some are defined
880 // implicitly, such as opening/closing delimiters and sequence repetition ops.
881 // This type encapsulates both kinds. It implements `Clone` while avoiding the
882 // need for `mbe::TokenTree` to implement `Clone`.
883 #[derive(Debug)]
884 enum TtHandle<'tt> {
885     /// This is used in most cases.
886     TtRef(&'tt mbe::TokenTree),
887
888     /// This is only used for implicit token trees. The `mbe::TokenTree` *must*
889     /// be `mbe::TokenTree::Token`. No other variants are allowed. We store an
890     /// `mbe::TokenTree` rather than a `Token` so that `get()` can return a
891     /// `&mbe::TokenTree`.
892     Token(mbe::TokenTree),
893 }
894
895 impl<'tt> TtHandle<'tt> {
896     fn from_token(tok: Token) -> Self {
897         TtHandle::Token(mbe::TokenTree::Token(tok))
898     }
899
900     fn from_token_kind(kind: TokenKind, span: Span) -> Self {
901         TtHandle::from_token(Token::new(kind, span))
902     }
903
904     // Get a reference to a token tree.
905     fn get(&'tt self) -> &'tt mbe::TokenTree {
906         match self {
907             TtHandle::TtRef(tt) => tt,
908             TtHandle::Token(token_tt) => &token_tt,
909         }
910     }
911 }
912
913 impl<'tt> PartialEq for TtHandle<'tt> {
914     fn eq(&self, other: &TtHandle<'tt>) -> bool {
915         self.get() == other.get()
916     }
917 }
918
919 impl<'tt> Clone for TtHandle<'tt> {
920     fn clone(&self) -> Self {
921         match self {
922             TtHandle::TtRef(tt) => TtHandle::TtRef(tt),
923
924             // This variant *must* contain a `mbe::TokenTree::Token`, and not
925             // any other variant of `mbe::TokenTree`.
926             TtHandle::Token(mbe::TokenTree::Token(tok)) => {
927                 TtHandle::Token(mbe::TokenTree::Token(tok.clone()))
928             }
929
930             _ => unreachable!(),
931         }
932     }
933 }
934
935 // A set of `mbe::TokenTree`s, which may include `TokenTree::Match`s
936 // (for macro-by-example syntactic variables). It also carries the
937 // `maybe_empty` flag; that is true if and only if the matcher can
938 // match an empty token sequence.
939 //
940 // The First set is computed on submatchers like `$($a:expr b),* $(c)* d`,
941 // which has corresponding FIRST = {$a:expr, c, d}.
942 // Likewise, `$($a:expr b),* $(c)+ d` has FIRST = {$a:expr, c}.
943 //
944 // (Notably, we must allow for *-op to occur zero times.)
945 #[derive(Clone, Debug)]
946 struct TokenSet<'tt> {
947     tokens: Vec<TtHandle<'tt>>,
948     maybe_empty: bool,
949 }
950
951 impl<'tt> TokenSet<'tt> {
952     // Returns a set for the empty sequence.
953     fn empty() -> Self {
954         TokenSet { tokens: Vec::new(), maybe_empty: true }
955     }
956
957     // Returns the set `{ tok }` for the single-token (and thus
958     // non-empty) sequence [tok].
959     fn singleton(tt: TtHandle<'tt>) -> Self {
960         TokenSet { tokens: vec![tt], maybe_empty: false }
961     }
962
963     // Changes self to be the set `{ tok }`.
964     // Since `tok` is always present, marks self as non-empty.
965     fn replace_with(&mut self, tt: TtHandle<'tt>) {
966         self.tokens.clear();
967         self.tokens.push(tt);
968         self.maybe_empty = false;
969     }
970
971     // Changes self to be the empty set `{}`; meant for use when
972     // the particular token does not matter, but we want to
973     // record that it occurs.
974     fn replace_with_irrelevant(&mut self) {
975         self.tokens.clear();
976         self.maybe_empty = false;
977     }
978
979     // Adds `tok` to the set for `self`, marking sequence as non-empy.
980     fn add_one(&mut self, tt: TtHandle<'tt>) {
981         if !self.tokens.contains(&tt) {
982             self.tokens.push(tt);
983         }
984         self.maybe_empty = false;
985     }
986
987     // Adds `tok` to the set for `self`. (Leaves `maybe_empty` flag alone.)
988     fn add_one_maybe(&mut self, tt: TtHandle<'tt>) {
989         if !self.tokens.contains(&tt) {
990             self.tokens.push(tt);
991         }
992     }
993
994     // Adds all elements of `other` to this.
995     //
996     // (Since this is a set, we filter out duplicates.)
997     //
998     // If `other` is potentially empty, then preserves the previous
999     // setting of the empty flag of `self`. If `other` is guaranteed
1000     // non-empty, then `self` is marked non-empty.
1001     fn add_all(&mut self, other: &Self) {
1002         for tt in &other.tokens {
1003             if !self.tokens.contains(tt) {
1004                 self.tokens.push(tt.clone());
1005             }
1006         }
1007         if !other.maybe_empty {
1008             self.maybe_empty = false;
1009         }
1010     }
1011 }
1012
1013 // Checks that `matcher` is internally consistent and that it
1014 // can legally be followed by a token `N`, for all `N` in `follow`.
1015 // (If `follow` is empty, then it imposes no constraint on
1016 // the `matcher`.)
1017 //
1018 // Returns the set of NT tokens that could possibly come last in
1019 // `matcher`. (If `matcher` matches the empty sequence, then
1020 // `maybe_empty` will be set to true.)
1021 //
1022 // Requires that `first_sets` is pre-computed for `matcher`;
1023 // see `FirstSets::new`.
1024 fn check_matcher_core<'tt>(
1025     sess: &ParseSess,
1026     def: &ast::Item,
1027     first_sets: &FirstSets<'tt>,
1028     matcher: &'tt [mbe::TokenTree],
1029     follow: &TokenSet<'tt>,
1030 ) -> TokenSet<'tt> {
1031     use mbe::TokenTree;
1032
1033     let mut last = TokenSet::empty();
1034
1035     // 2. For each token and suffix  [T, SUFFIX] in M:
1036     // ensure that T can be followed by SUFFIX, and if SUFFIX may be empty,
1037     // then ensure T can also be followed by any element of FOLLOW.
1038     'each_token: for i in 0..matcher.len() {
1039         let token = &matcher[i];
1040         let suffix = &matcher[i + 1..];
1041
1042         let build_suffix_first = || {
1043             let mut s = first_sets.first(suffix);
1044             if s.maybe_empty {
1045                 s.add_all(follow);
1046             }
1047             s
1048         };
1049
1050         // (we build `suffix_first` on demand below; you can tell
1051         // which cases are supposed to fall through by looking for the
1052         // initialization of this variable.)
1053         let suffix_first;
1054
1055         // First, update `last` so that it corresponds to the set
1056         // of NT tokens that might end the sequence `... token`.
1057         match *token {
1058             TokenTree::Token(..)
1059             | TokenTree::MetaVar(..)
1060             | TokenTree::MetaVarDecl(..)
1061             | TokenTree::MetaVarExpr(..) => {
1062                 if token_can_be_followed_by_any(token) {
1063                     // don't need to track tokens that work with any,
1064                     last.replace_with_irrelevant();
1065                     // ... and don't need to check tokens that can be
1066                     // followed by anything against SUFFIX.
1067                     continue 'each_token;
1068                 } else {
1069                     last.replace_with(TtHandle::TtRef(token));
1070                     suffix_first = build_suffix_first();
1071                 }
1072             }
1073             TokenTree::Delimited(span, ref d) => {
1074                 let my_suffix = TokenSet::singleton(TtHandle::from_token_kind(
1075                     token::CloseDelim(d.delim),
1076                     span.close,
1077                 ));
1078                 check_matcher_core(sess, def, first_sets, &d.tts, &my_suffix);
1079                 // don't track non NT tokens
1080                 last.replace_with_irrelevant();
1081
1082                 // also, we don't need to check delimited sequences
1083                 // against SUFFIX
1084                 continue 'each_token;
1085             }
1086             TokenTree::Sequence(_, ref seq_rep) => {
1087                 suffix_first = build_suffix_first();
1088                 // The trick here: when we check the interior, we want
1089                 // to include the separator (if any) as a potential
1090                 // (but not guaranteed) element of FOLLOW. So in that
1091                 // case, we make a temp copy of suffix and stuff
1092                 // delimiter in there.
1093                 //
1094                 // FIXME: Should I first scan suffix_first to see if
1095                 // delimiter is already in it before I go through the
1096                 // work of cloning it? But then again, this way I may
1097                 // get a "tighter" span?
1098                 let mut new;
1099                 let my_suffix = if let Some(sep) = &seq_rep.separator {
1100                     new = suffix_first.clone();
1101                     new.add_one_maybe(TtHandle::from_token(sep.clone()));
1102                     &new
1103                 } else {
1104                     &suffix_first
1105                 };
1106
1107                 // At this point, `suffix_first` is built, and
1108                 // `my_suffix` is some TokenSet that we can use
1109                 // for checking the interior of `seq_rep`.
1110                 let next = check_matcher_core(sess, def, first_sets, &seq_rep.tts, my_suffix);
1111                 if next.maybe_empty {
1112                     last.add_all(&next);
1113                 } else {
1114                     last = next;
1115                 }
1116
1117                 // the recursive call to check_matcher_core already ran the 'each_last
1118                 // check below, so we can just keep going forward here.
1119                 continue 'each_token;
1120             }
1121         }
1122
1123         // (`suffix_first` guaranteed initialized once reaching here.)
1124
1125         // Now `last` holds the complete set of NT tokens that could
1126         // end the sequence before SUFFIX. Check that every one works with `suffix`.
1127         for tt in &last.tokens {
1128             if let &TokenTree::MetaVarDecl(span, name, Some(kind)) = tt.get() {
1129                 for next_token in &suffix_first.tokens {
1130                     let next_token = next_token.get();
1131
1132                     // Check if the old pat is used and the next token is `|`
1133                     // to warn about incompatibility with Rust 2021.
1134                     // We only emit this lint if we're parsing the original
1135                     // definition of this macro_rules, not while (re)parsing
1136                     // the macro when compiling another crate that is using the
1137                     // macro. (See #86567.)
1138                     // Macros defined in the current crate have a real node id,
1139                     // whereas macros from an external crate have a dummy id.
1140                     if def.id != DUMMY_NODE_ID
1141                         && matches!(kind, NonterminalKind::PatParam { inferred: true })
1142                         && matches!(next_token, TokenTree::Token(token) if token.kind == BinOp(token::BinOpToken::Or))
1143                     {
1144                         // It is suggestion to use pat_param, for example: $x:pat -> $x:pat_param.
1145                         let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl(
1146                             span,
1147                             name,
1148                             Some(NonterminalKind::PatParam { inferred: false }),
1149                         ));
1150                         sess.buffer_lint_with_diagnostic(
1151                             &RUST_2021_INCOMPATIBLE_OR_PATTERNS,
1152                             span,
1153                             ast::CRATE_NODE_ID,
1154                             "the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro",
1155                             BuiltinLintDiagnostics::OrPatternsBackCompat(span, suggestion),
1156                         );
1157                     }
1158                     match is_in_follow(next_token, kind) {
1159                         IsInFollow::Yes => {}
1160                         IsInFollow::No(possible) => {
1161                             let may_be = if last.tokens.len() == 1 && suffix_first.tokens.len() == 1
1162                             {
1163                                 "is"
1164                             } else {
1165                                 "may be"
1166                             };
1167
1168                             let sp = next_token.span();
1169                             let mut err = sess.span_diagnostic.struct_span_err(
1170                                 sp,
1171                                 &format!(
1172                                     "`${name}:{frag}` {may_be} followed by `{next}`, which \
1173                                      is not allowed for `{frag}` fragments",
1174                                     name = name,
1175                                     frag = kind,
1176                                     next = quoted_tt_to_string(next_token),
1177                                     may_be = may_be
1178                                 ),
1179                             );
1180                             err.span_label(sp, format!("not allowed after `{}` fragments", kind));
1181
1182                             if kind == NonterminalKind::PatWithOr
1183                                 && sess.edition.rust_2021()
1184                                 && next_token.is_token(&BinOp(token::BinOpToken::Or))
1185                             {
1186                                 let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl(
1187                                     span,
1188                                     name,
1189                                     Some(NonterminalKind::PatParam { inferred: false }),
1190                                 ));
1191                                 err.span_suggestion(
1192                                     span,
1193                                     "try a `pat_param` fragment specifier instead",
1194                                     suggestion,
1195                                     Applicability::MaybeIncorrect,
1196                                 );
1197                             }
1198
1199                             let msg = "allowed there are: ";
1200                             match possible {
1201                                 &[] => {}
1202                                 &[t] => {
1203                                     err.note(&format!(
1204                                         "only {} is allowed after `{}` fragments",
1205                                         t, kind,
1206                                     ));
1207                                 }
1208                                 ts => {
1209                                     err.note(&format!(
1210                                         "{}{} or {}",
1211                                         msg,
1212                                         ts[..ts.len() - 1]
1213                                             .iter()
1214                                             .copied()
1215                                             .collect::<Vec<_>>()
1216                                             .join(", "),
1217                                         ts[ts.len() - 1],
1218                                     ));
1219                                 }
1220                             }
1221                             err.emit();
1222                         }
1223                     }
1224                 }
1225             }
1226         }
1227     }
1228     last
1229 }
1230
1231 fn token_can_be_followed_by_any(tok: &mbe::TokenTree) -> bool {
1232     if let mbe::TokenTree::MetaVarDecl(_, _, Some(kind)) = *tok {
1233         frag_can_be_followed_by_any(kind)
1234     } else {
1235         // (Non NT's can always be followed by anything in matchers.)
1236         true
1237     }
1238 }
1239
1240 /// Returns `true` if a fragment of type `frag` can be followed by any sort of
1241 /// token. We use this (among other things) as a useful approximation
1242 /// for when `frag` can be followed by a repetition like `$(...)*` or
1243 /// `$(...)+`. In general, these can be a bit tricky to reason about,
1244 /// so we adopt a conservative position that says that any fragment
1245 /// specifier which consumes at most one token tree can be followed by
1246 /// a fragment specifier (indeed, these fragments can be followed by
1247 /// ANYTHING without fear of future compatibility hazards).
1248 fn frag_can_be_followed_by_any(kind: NonterminalKind) -> bool {
1249     matches!(
1250         kind,
1251         NonterminalKind::Item           // always terminated by `}` or `;`
1252         | NonterminalKind::Block        // exactly one token tree
1253         | NonterminalKind::Ident        // exactly one token tree
1254         | NonterminalKind::Literal      // exactly one token tree
1255         | NonterminalKind::Meta         // exactly one token tree
1256         | NonterminalKind::Lifetime     // exactly one token tree
1257         | NonterminalKind::TT // exactly one token tree
1258     )
1259 }
1260
1261 enum IsInFollow {
1262     Yes,
1263     No(&'static [&'static str]),
1264 }
1265
1266 /// Returns `true` if `frag` can legally be followed by the token `tok`. For
1267 /// fragments that can consume an unbounded number of tokens, `tok`
1268 /// must be within a well-defined follow set. This is intended to
1269 /// guarantee future compatibility: for example, without this rule, if
1270 /// we expanded `expr` to include a new binary operator, we might
1271 /// break macros that were relying on that binary operator as a
1272 /// separator.
1273 // when changing this do not forget to update doc/book/macros.md!
1274 fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
1275     use mbe::TokenTree;
1276
1277     if let TokenTree::Token(Token { kind: token::CloseDelim(_), .. }) = *tok {
1278         // closing a token tree can never be matched by any fragment;
1279         // iow, we always require that `(` and `)` match, etc.
1280         IsInFollow::Yes
1281     } else {
1282         match kind {
1283             NonterminalKind::Item => {
1284                 // since items *must* be followed by either a `;` or a `}`, we can
1285                 // accept anything after them
1286                 IsInFollow::Yes
1287             }
1288             NonterminalKind::Block => {
1289                 // anything can follow block, the braces provide an easy boundary to
1290                 // maintain
1291                 IsInFollow::Yes
1292             }
1293             NonterminalKind::Stmt | NonterminalKind::Expr => {
1294                 const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
1295                 match tok {
1296                     TokenTree::Token(token) => match token.kind {
1297                         FatArrow | Comma | Semi => IsInFollow::Yes,
1298                         _ => IsInFollow::No(TOKENS),
1299                     },
1300                     _ => IsInFollow::No(TOKENS),
1301                 }
1302             }
1303             NonterminalKind::PatParam { .. } => {
1304                 const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`in`"];
1305                 match tok {
1306                     TokenTree::Token(token) => match token.kind {
1307                         FatArrow | Comma | Eq | BinOp(token::Or) => IsInFollow::Yes,
1308                         Ident(name, false) if name == kw::If || name == kw::In => IsInFollow::Yes,
1309                         _ => IsInFollow::No(TOKENS),
1310                     },
1311                     _ => IsInFollow::No(TOKENS),
1312                 }
1313             }
1314             NonterminalKind::PatWithOr { .. } => {
1315                 const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`if`", "`in`"];
1316                 match tok {
1317                     TokenTree::Token(token) => match token.kind {
1318                         FatArrow | Comma | Eq => IsInFollow::Yes,
1319                         Ident(name, false) if name == kw::If || name == kw::In => IsInFollow::Yes,
1320                         _ => IsInFollow::No(TOKENS),
1321                     },
1322                     _ => IsInFollow::No(TOKENS),
1323                 }
1324             }
1325             NonterminalKind::Path | NonterminalKind::Ty => {
1326                 const TOKENS: &[&str] = &[
1327                     "`{`", "`[`", "`=>`", "`,`", "`>`", "`=`", "`:`", "`;`", "`|`", "`as`",
1328                     "`where`",
1329                 ];
1330                 match tok {
1331                     TokenTree::Token(token) => match token.kind {
1332                         OpenDelim(Delimiter::Brace)
1333                         | OpenDelim(Delimiter::Bracket)
1334                         | Comma
1335                         | FatArrow
1336                         | Colon
1337                         | Eq
1338                         | Gt
1339                         | BinOp(token::Shr)
1340                         | Semi
1341                         | BinOp(token::Or) => IsInFollow::Yes,
1342                         Ident(name, false) if name == kw::As || name == kw::Where => {
1343                             IsInFollow::Yes
1344                         }
1345                         _ => IsInFollow::No(TOKENS),
1346                     },
1347                     TokenTree::MetaVarDecl(_, _, Some(NonterminalKind::Block)) => IsInFollow::Yes,
1348                     _ => IsInFollow::No(TOKENS),
1349                 }
1350             }
1351             NonterminalKind::Ident | NonterminalKind::Lifetime => {
1352                 // being a single token, idents and lifetimes are harmless
1353                 IsInFollow::Yes
1354             }
1355             NonterminalKind::Literal => {
1356                 // literals may be of a single token, or two tokens (negative numbers)
1357                 IsInFollow::Yes
1358             }
1359             NonterminalKind::Meta | NonterminalKind::TT => {
1360                 // being either a single token or a delimited sequence, tt is
1361                 // harmless
1362                 IsInFollow::Yes
1363             }
1364             NonterminalKind::Vis => {
1365                 // Explicitly disallow `priv`, on the off chance it comes back.
1366                 const TOKENS: &[&str] = &["`,`", "an ident", "a type"];
1367                 match tok {
1368                     TokenTree::Token(token) => match token.kind {
1369                         Comma => IsInFollow::Yes,
1370                         Ident(name, is_raw) if is_raw || name != kw::Priv => IsInFollow::Yes,
1371                         _ => {
1372                             if token.can_begin_type() {
1373                                 IsInFollow::Yes
1374                             } else {
1375                                 IsInFollow::No(TOKENS)
1376                             }
1377                         }
1378                     },
1379                     TokenTree::MetaVarDecl(
1380                         _,
1381                         _,
1382                         Some(NonterminalKind::Ident | NonterminalKind::Ty | NonterminalKind::Path),
1383                     ) => IsInFollow::Yes,
1384                     _ => IsInFollow::No(TOKENS),
1385                 }
1386             }
1387         }
1388     }
1389 }
1390
1391 fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
1392     match *tt {
1393         mbe::TokenTree::Token(ref token) => pprust::token_to_string(&token).into(),
1394         mbe::TokenTree::MetaVar(_, name) => format!("${}", name),
1395         mbe::TokenTree::MetaVarDecl(_, name, Some(kind)) => format!("${}:{}", name, kind),
1396         mbe::TokenTree::MetaVarDecl(_, name, None) => format!("${}:", name),
1397         _ => panic!(
1398             "{}",
1399             "unexpected mbe::TokenTree::{Sequence or Delimited} \
1400              in follow set checker"
1401         ),
1402     }
1403 }
1404
1405 fn parser_from_cx(sess: &ParseSess, tts: TokenStream) -> Parser<'_> {
1406     Parser::new(sess, tts, true, rustc_parse::MACRO_ARGUMENTS)
1407 }
1408
1409 /// Generates an appropriate parsing failure message. For EOF, this is "unexpected end...". For
1410 /// other tokens, this is "unexpected token...".
1411 fn parse_failure_msg(tok: &Token) -> String {
1412     match tok.kind {
1413         token::Eof => "unexpected end of macro invocation".to_string(),
1414         _ => format!("no rules expected the token `{}`", pprust::token_to_string(tok),),
1415     }
1416 }