]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_expand/mbe/macro_rules.rs
a1d8b5a53386a06ea4980170f29da15d8357843b
[rust.git] / src / libsyntax_expand / mbe / macro_rules.rs
1 use crate::base::{DummyResult, ExtCtxt, MacResult, TTMacroExpander};
2 use crate::base::{SyntaxExtension, SyntaxExtensionKind};
3 use crate::expand::{AstFragment, AstFragmentKind, ensure_complete_parse, parse_ast_fragment};
4 use crate::mbe;
5 use crate::mbe::macro_check;
6 use crate::mbe::macro_parser::parse;
7 use crate::mbe::macro_parser::{Error, Failure, Success};
8 use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, NamedParseResult};
9 use crate::mbe::transcribe::transcribe;
10
11 use rustc_parse::parser::Parser;
12 use rustc_parse::Directory;
13 use syntax::ast;
14 use syntax::attr::{self, TransparencyError};
15 use syntax::edition::Edition;
16 use syntax::feature_gate::Features;
17 use syntax::print::pprust;
18 use syntax::sess::ParseSess;
19 use syntax::symbol::{kw, sym, Symbol};
20 use syntax::token::{self, NtTT, Token, TokenKind::*};
21 use syntax::tokenstream::{DelimSpan, TokenStream};
22 use syntax_pos::hygiene::Transparency;
23 use syntax_pos::Span;
24
25 use errors::{DiagnosticBuilder, FatalError};
26 use log::debug;
27
28 use rustc_data_structures::fx::FxHashMap;
29 use rustc_data_structures::sync::Lrc;
30 use std::borrow::Cow;
31 use std::collections::hash_map::Entry;
32 use std::{mem, slice};
33
34 use errors::Applicability;
35
36 const VALID_FRAGMENT_NAMES_MSG: &str = "valid fragment specifiers are \
37                                         `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, \
38                                         `literal`, `path`, `meta`, `tt`, `item` and `vis`";
39
40 crate struct ParserAnyMacro<'a> {
41     parser: Parser<'a>,
42
43     /// Span of the expansion site of the macro this parser is for
44     site_span: Span,
45     /// The ident of the macro we're parsing
46     macro_ident: ast::Ident,
47     arm_span: Span,
48 }
49
50 crate fn annotate_err_with_kind(
51     err: &mut DiagnosticBuilder<'_>,
52     kind: AstFragmentKind,
53     span: Span,
54 ) {
55     match kind {
56         AstFragmentKind::Ty => {
57             err.span_label(span, "this macro call doesn't expand to a type");
58         }
59         AstFragmentKind::Pat => {
60             err.span_label(span, "this macro call doesn't expand to a pattern");
61         }
62         _ => {}
63     };
64 }
65
66 impl<'a> ParserAnyMacro<'a> {
67     crate fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
68         let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
69         let fragment = panictry!(parse_ast_fragment(parser, kind, true).map_err(|mut e| {
70             if parser.token == token::Eof && e.message().ends_with(", found `<eof>`") {
71                 if !e.span.is_dummy() {
72                     // early end of macro arm (#52866)
73                     e.replace_span_with(parser.sess.source_map().next_point(parser.token.span));
74                 }
75                 let msg = &e.message[0];
76                 e.message[0] = (
77                     format!(
78                         "macro expansion ends with an incomplete expression: {}",
79                         msg.0.replace(", found `<eof>`", ""),
80                     ),
81                     msg.1,
82                 );
83             }
84             if e.span.is_dummy() {
85                 // Get around lack of span in error (#30128)
86                 e.replace_span_with(site_span);
87                 if parser.sess.source_map().span_to_filename(arm_span).is_real() {
88                     e.span_label(arm_span, "in this macro arm");
89                 }
90             } else if !parser.sess.source_map().span_to_filename(parser.token.span).is_real() {
91                 e.span_label(site_span, "in this macro invocation");
92             }
93             match kind {
94                 AstFragmentKind::Pat if macro_ident.name == sym::vec => {
95                     let mut suggestion = None;
96                     if let Ok(code) = parser.sess.source_map().span_to_snippet(site_span) {
97                         if let Some(bang) = code.find('!') {
98                             suggestion = Some(code[bang + 1..].to_string());
99                         }
100                     }
101                     if let Some(suggestion) = suggestion {
102                         e.span_suggestion(
103                             site_span,
104                             "use a slice pattern here instead",
105                             suggestion,
106                             Applicability::MachineApplicable,
107                         );
108                     } else {
109                         e.span_label(
110                             site_span,
111                             "use a slice pattern here instead",
112                         );
113                     }
114                     e.help("for more information, see https://doc.rust-lang.org/edition-guide/\
115                             rust-2018/slice-patterns.html");
116                 }
117                 _ => annotate_err_with_kind(&mut e, kind, site_span),
118             };
119             e
120         }));
121
122         // We allow semicolons at the end of expressions -- e.g., the semicolon in
123         // `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
124         // but `m!()` is allowed in expression positions (cf. issue #34706).
125         if kind == AstFragmentKind::Expr && parser.token == token::Semi {
126             parser.bump();
127         }
128
129         // Make sure we don't have any tokens left to parse so we don't silently drop anything.
130         let path = ast::Path::from_ident(macro_ident.with_span_pos(site_span));
131         ensure_complete_parse(parser, &path, kind.name(), site_span);
132         fragment
133     }
134 }
135
136 struct MacroRulesMacroExpander {
137     name: ast::Ident,
138     span: Span,
139     transparency: Transparency,
140     lhses: Vec<mbe::TokenTree>,
141     rhses: Vec<mbe::TokenTree>,
142     valid: bool,
143 }
144
145 impl TTMacroExpander for MacroRulesMacroExpander {
146     fn expand<'cx>(
147         &self,
148         cx: &'cx mut ExtCtxt<'_>,
149         sp: Span,
150         input: TokenStream,
151     ) -> Box<dyn MacResult + 'cx> {
152         if !self.valid {
153             return DummyResult::any(sp);
154         }
155         generic_extension(
156             cx, sp, self.span, self.name, self.transparency, input, &self.lhses, &self.rhses
157         )
158     }
159 }
160
161 fn trace_macros_note(cx: &mut ExtCtxt<'_>, sp: Span, message: String) {
162     let sp = sp.macro_backtrace().last().map(|trace| trace.call_site).unwrap_or(sp);
163     cx.expansions.entry(sp).or_default().push(message);
164 }
165
166 /// Given `lhses` and `rhses`, this is the new macro we create
167 fn generic_extension<'cx>(
168     cx: &'cx mut ExtCtxt<'_>,
169     sp: Span,
170     def_span: Span,
171     name: ast::Ident,
172     transparency: Transparency,
173     arg: TokenStream,
174     lhses: &[mbe::TokenTree],
175     rhses: &[mbe::TokenTree],
176 ) -> Box<dyn MacResult + 'cx> {
177     if cx.trace_macros() {
178         let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(arg.clone()));
179         trace_macros_note(cx, sp, msg);
180     }
181
182     // Which arm's failure should we report? (the one furthest along)
183     let mut best_failure: Option<(Token, &str)> = None;
184     for (i, lhs) in lhses.iter().enumerate() {
185         // try each arm's matchers
186         let lhs_tt = match *lhs {
187             mbe::TokenTree::Delimited(_, ref delim) => &delim.tts[..],
188             _ => cx.span_bug(sp, "malformed macro lhs"),
189         };
190
191         // Take a snapshot of the state of pre-expansion gating at this point.
192         // This is used so that if a matcher is not `Success(..)`ful,
193         // then the spans which became gated when parsing the unsuccessful matcher
194         // are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
195         let mut gated_spans_snaphot = mem::take(&mut *cx.parse_sess.gated_spans.spans.borrow_mut());
196
197         match parse_tt(cx, lhs_tt, arg.clone()) {
198             Success(named_matches) => {
199                 // The matcher was `Success(..)`ful.
200                 // Merge the gated spans from parsing the matcher with the pre-existing ones.
201                 cx.parse_sess.gated_spans.merge(gated_spans_snaphot);
202
203                 let rhs = match rhses[i] {
204                     // ignore delimiters
205                     mbe::TokenTree::Delimited(_, ref delimed) => delimed.tts.clone(),
206                     _ => cx.span_bug(sp, "malformed macro rhs"),
207                 };
208                 let arm_span = rhses[i].span();
209
210                 let rhs_spans = rhs.iter().map(|t| t.span()).collect::<Vec<_>>();
211                 // rhs has holes ( `$id` and `$(...)` that need filled)
212                 let mut tts = transcribe(cx, &named_matches, rhs, transparency);
213
214                 // Replace all the tokens for the corresponding positions in the macro, to maintain
215                 // proper positions in error reporting, while maintaining the macro_backtrace.
216                 if rhs_spans.len() == tts.len() {
217                     tts = tts.map_enumerated(|i, mut tt| {
218                         let mut sp = rhs_spans[i];
219                         sp = sp.with_ctxt(tt.span().ctxt());
220                         tt.set_span(sp);
221                         tt
222                     });
223                 }
224
225                 if cx.trace_macros() {
226                     let msg = format!("to `{}`", pprust::tts_to_string(tts.clone()));
227                     trace_macros_note(cx, sp, msg);
228                 }
229
230                 let directory = Directory {
231                     path: Cow::from(cx.current_expansion.module.directory.as_path()),
232                     ownership: cx.current_expansion.directory_ownership,
233                 };
234                 let mut p = Parser::new(cx.parse_sess(), tts, Some(directory), true, false, None);
235                 p.root_module_name =
236                     cx.current_expansion.module.mod_path.last().map(|id| id.to_string());
237                 p.last_type_ascription = cx.current_expansion.prior_type_ascription;
238
239                 p.process_potential_macro_variable();
240                 // Let the context choose how to interpret the result.
241                 // Weird, but useful for X-macros.
242                 return Box::new(ParserAnyMacro {
243                     parser: p,
244
245                     // Pass along the original expansion site and the name of the macro
246                     // so we can print a useful error message if the parse of the expanded
247                     // macro leaves unparsed tokens.
248                     site_span: sp,
249                     macro_ident: name,
250                     arm_span,
251                 });
252             }
253             Failure(token, msg) => match best_failure {
254                 Some((ref best_token, _)) if best_token.span.lo() >= token.span.lo() => {}
255                 _ => best_failure = Some((token, msg)),
256             },
257             Error(err_sp, ref msg) => cx.span_fatal(err_sp.substitute_dummy(sp), &msg[..]),
258         }
259
260         // The matcher was not `Success(..)`ful.
261         // Restore to the state before snapshotting and maybe try again.
262         mem::swap(&mut gated_spans_snaphot, &mut cx.parse_sess.gated_spans.spans.borrow_mut());
263     }
264
265     let (token, label) = best_failure.expect("ran no matchers");
266     let span = token.span.substitute_dummy(sp);
267     let mut err = cx.struct_span_err(span, &parse_failure_msg(&token));
268     err.span_label(span, label);
269     if !def_span.is_dummy() && cx.source_map().span_to_filename(def_span).is_real() {
270         err.span_label(cx.source_map().def_span(def_span), "when calling this macro");
271     }
272
273     // Check whether there's a missing comma in this macro call, like `println!("{}" a);`
274     if let Some((arg, comma_span)) = arg.add_comma() {
275         for lhs in lhses {
276             // try each arm's matchers
277             let lhs_tt = match *lhs {
278                 mbe::TokenTree::Delimited(_, ref delim) => &delim.tts[..],
279                 _ => continue,
280             };
281             match parse_tt(cx, lhs_tt, arg.clone()) {
282                 Success(_) => {
283                     if comma_span.is_dummy() {
284                         err.note("you might be missing a comma");
285                     } else {
286                         err.span_suggestion_short(
287                             comma_span,
288                             "missing comma here",
289                             ", ".to_string(),
290                             Applicability::MachineApplicable,
291                         );
292                     }
293                 }
294                 _ => {}
295             }
296         }
297     }
298     err.emit();
299     cx.trace_macros_diag();
300     DummyResult::any(sp)
301 }
302
303 // Note that macro-by-example's input is also matched against a token tree:
304 //                   $( $lhs:tt => $rhs:tt );+
305 //
306 // Holy self-referential!
307
308 /// Converts a macro item into a syntax extension.
309 pub fn compile_declarative_macro(
310     sess: &ParseSess,
311     features: &Features,
312     def: &ast::Item,
313     edition: Edition,
314 ) -> SyntaxExtension {
315     let diag = &sess.span_diagnostic;
316     let lhs_nm = ast::Ident::new(sym::lhs, def.span);
317     let rhs_nm = ast::Ident::new(sym::rhs, def.span);
318     let tt_spec = ast::Ident::new(sym::tt, def.span);
319
320     // Parse the macro_rules! invocation
321     let body = match def.kind {
322         ast::ItemKind::MacroDef(ref body) => body,
323         _ => unreachable!(),
324     };
325
326     // The pattern that macro_rules matches.
327     // The grammar for macro_rules! is:
328     // $( $lhs:tt => $rhs:tt );+
329     // ...quasiquoting this would be nice.
330     // These spans won't matter, anyways
331     let argument_gram = vec![
332         mbe::TokenTree::Sequence(
333             DelimSpan::dummy(),
334             Lrc::new(mbe::SequenceRepetition {
335                 tts: vec![
336                     mbe::TokenTree::MetaVarDecl(def.span, lhs_nm, tt_spec),
337                     mbe::TokenTree::token(token::FatArrow, def.span),
338                     mbe::TokenTree::MetaVarDecl(def.span, rhs_nm, tt_spec),
339                 ],
340                 separator: Some(Token::new(
341                     if body.legacy { token::Semi } else { token::Comma },
342                     def.span,
343                 )),
344                 kleene: mbe::KleeneToken::new(mbe::KleeneOp::OneOrMore, def.span),
345                 num_captures: 2,
346             }),
347         ),
348         // to phase into semicolon-termination instead of semicolon-separation
349         mbe::TokenTree::Sequence(
350             DelimSpan::dummy(),
351             Lrc::new(mbe::SequenceRepetition {
352                 tts: vec![mbe::TokenTree::token(
353                     if body.legacy { token::Semi } else { token::Comma },
354                     def.span,
355                 )],
356                 separator: None,
357                 kleene: mbe::KleeneToken::new(mbe::KleeneOp::ZeroOrMore, def.span),
358                 num_captures: 0,
359             }),
360         ),
361     ];
362
363     let argument_map = match parse(sess, body.stream(), &argument_gram, None, true) {
364         Success(m) => m,
365         Failure(token, msg) => {
366             let s = parse_failure_msg(&token);
367             let sp = token.span.substitute_dummy(def.span);
368             let mut err = sess.span_diagnostic.struct_span_fatal(sp, &s);
369             err.span_label(sp, msg);
370             err.emit();
371             FatalError.raise();
372         }
373         Error(sp, s) => {
374             sess.span_diagnostic.span_fatal(sp.substitute_dummy(def.span), &s).raise();
375         }
376     };
377
378     let mut valid = true;
379
380     // Extract the arguments:
381     let lhses = match argument_map[&lhs_nm] {
382         MatchedSeq(ref s, _) => s
383             .iter()
384             .map(|m| {
385                 if let MatchedNonterminal(ref nt) = *m {
386                     if let NtTT(ref tt) = **nt {
387                         let tt = mbe::quoted::parse(
388                             tt.clone().into(),
389                             true,
390                             sess,
391                         )
392                         .pop()
393                         .unwrap();
394                         valid &= check_lhs_nt_follows(sess, features, &def.attrs, &tt);
395                         return tt;
396                     }
397                 }
398                 sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
399             })
400             .collect::<Vec<mbe::TokenTree>>(),
401         _ => sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs"),
402     };
403
404     let rhses = match argument_map[&rhs_nm] {
405         MatchedSeq(ref s, _) => s
406             .iter()
407             .map(|m| {
408                 if let MatchedNonterminal(ref nt) = *m {
409                     if let NtTT(ref tt) = **nt {
410                         return mbe::quoted::parse(
411                             tt.clone().into(),
412                             false,
413                             sess,
414                         )
415                         .pop()
416                         .unwrap();
417                     }
418                 }
419                 sess.span_diagnostic.span_bug(def.span, "wrong-structured lhs")
420             })
421             .collect::<Vec<mbe::TokenTree>>(),
422         _ => sess.span_diagnostic.span_bug(def.span, "wrong-structured rhs"),
423     };
424
425     for rhs in &rhses {
426         valid &= check_rhs(sess, rhs);
427     }
428
429     // don't abort iteration early, so that errors for multiple lhses can be reported
430     for lhs in &lhses {
431         valid &= check_lhs_no_empty_seq(sess, slice::from_ref(lhs));
432     }
433
434     // We use CRATE_NODE_ID instead of `def.id` otherwise we may emit buffered lints for a node id
435     // that is not lint-checked and trigger the "failed to process buffered lint here" bug.
436     valid &= macro_check::check_meta_variables(sess, ast::CRATE_NODE_ID, def.span, &lhses, &rhses);
437
438     let (transparency, transparency_error) = attr::find_transparency(&def.attrs, body.legacy);
439     match transparency_error {
440         Some(TransparencyError::UnknownTransparency(value, span)) =>
441             diag.span_err(span, &format!("unknown macro transparency: `{}`", value)),
442         Some(TransparencyError::MultipleTransparencyAttrs(old_span, new_span)) =>
443             diag.span_err(vec![old_span, new_span], "multiple macro transparency attributes"),
444         None => {}
445     }
446
447     let expander: Box<_> = Box::new(MacroRulesMacroExpander {
448         name: def.ident, span: def.span, transparency, lhses, rhses, valid
449     });
450
451     SyntaxExtension::new(
452         sess,
453         SyntaxExtensionKind::LegacyBang(expander),
454         def.span,
455         Vec::new(),
456         edition,
457         def.ident.name,
458         &def.attrs,
459     )
460 }
461
462 fn check_lhs_nt_follows(
463     sess: &ParseSess,
464     features: &Features,
465     attrs: &[ast::Attribute],
466     lhs: &mbe::TokenTree,
467 ) -> bool {
468     // lhs is going to be like TokenTree::Delimited(...), where the
469     // entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
470     if let mbe::TokenTree::Delimited(_, ref tts) = *lhs {
471         check_matcher(sess, features, attrs, &tts.tts)
472     } else {
473         let msg = "invalid macro matcher; matchers must be contained in balanced delimiters";
474         sess.span_diagnostic.span_err(lhs.span(), msg);
475         false
476     }
477     // we don't abort on errors on rejection, the driver will do that for us
478     // after parsing/expansion. we can report every error in every macro this way.
479 }
480
481 /// Checks that the lhs contains no repetition which could match an empty token
482 /// tree, because then the matcher would hang indefinitely.
483 fn check_lhs_no_empty_seq(sess: &ParseSess, tts: &[mbe::TokenTree]) -> bool {
484     use mbe::TokenTree;
485     for tt in tts {
486         match *tt {
487             TokenTree::Token(..) | TokenTree::MetaVar(..) | TokenTree::MetaVarDecl(..) => (),
488             TokenTree::Delimited(_, ref del) => {
489                 if !check_lhs_no_empty_seq(sess, &del.tts) {
490                     return false;
491                 }
492             }
493             TokenTree::Sequence(span, ref seq) => {
494                 if seq.separator.is_none()
495                     && seq.tts.iter().all(|seq_tt| match *seq_tt {
496                         TokenTree::MetaVarDecl(_, _, id) => id.name == sym::vis,
497                         TokenTree::Sequence(_, ref sub_seq) => {
498                             sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
499                                 || sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne
500                         }
501                         _ => false,
502                     })
503                 {
504                     let sp = span.entire();
505                     sess.span_diagnostic.span_err(sp, "repetition matches empty token tree");
506                     return false;
507                 }
508                 if !check_lhs_no_empty_seq(sess, &seq.tts) {
509                     return false;
510                 }
511             }
512         }
513     }
514
515     true
516 }
517
518 fn check_rhs(sess: &ParseSess, rhs: &mbe::TokenTree) -> bool {
519     match *rhs {
520         mbe::TokenTree::Delimited(..) => return true,
521         _ => sess.span_diagnostic.span_err(rhs.span(), "macro rhs must be delimited"),
522     }
523     false
524 }
525
526 fn check_matcher(
527     sess: &ParseSess,
528     features: &Features,
529     attrs: &[ast::Attribute],
530     matcher: &[mbe::TokenTree],
531 ) -> bool {
532     let first_sets = FirstSets::new(matcher);
533     let empty_suffix = TokenSet::empty();
534     let err = sess.span_diagnostic.err_count();
535     check_matcher_core(sess, features, attrs, &first_sets, matcher, &empty_suffix);
536     err == sess.span_diagnostic.err_count()
537 }
538
539 // `The FirstSets` for a matcher is a mapping from subsequences in the
540 // matcher to the FIRST set for that subsequence.
541 //
542 // This mapping is partially precomputed via a backwards scan over the
543 // token trees of the matcher, which provides a mapping from each
544 // repetition sequence to its *first* set.
545 //
546 // (Hypothetically, sequences should be uniquely identifiable via their
547 // spans, though perhaps that is false, e.g., for macro-generated macros
548 // that do not try to inject artificial span information. My plan is
549 // to try to catch such cases ahead of time and not include them in
550 // the precomputed mapping.)
551 struct FirstSets {
552     // this maps each TokenTree::Sequence `$(tt ...) SEP OP` that is uniquely identified by its
553     // span in the original matcher to the First set for the inner sequence `tt ...`.
554     //
555     // If two sequences have the same span in a matcher, then map that
556     // span to None (invalidating the mapping here and forcing the code to
557     // use a slow path).
558     first: FxHashMap<Span, Option<TokenSet>>,
559 }
560
561 impl FirstSets {
562     fn new(tts: &[mbe::TokenTree]) -> FirstSets {
563         use mbe::TokenTree;
564
565         let mut sets = FirstSets { first: FxHashMap::default() };
566         build_recur(&mut sets, tts);
567         return sets;
568
569         // walks backward over `tts`, returning the FIRST for `tts`
570         // and updating `sets` at the same time for all sequence
571         // substructure we find within `tts`.
572         fn build_recur(sets: &mut FirstSets, tts: &[TokenTree]) -> TokenSet {
573             let mut first = TokenSet::empty();
574             for tt in tts.iter().rev() {
575                 match *tt {
576                     TokenTree::Token(..) | TokenTree::MetaVar(..) | TokenTree::MetaVarDecl(..) => {
577                         first.replace_with(tt.clone());
578                     }
579                     TokenTree::Delimited(span, ref delimited) => {
580                         build_recur(sets, &delimited.tts[..]);
581                         first.replace_with(delimited.open_tt(span));
582                     }
583                     TokenTree::Sequence(sp, ref seq_rep) => {
584                         let subfirst = build_recur(sets, &seq_rep.tts[..]);
585
586                         match sets.first.entry(sp.entire()) {
587                             Entry::Vacant(vac) => {
588                                 vac.insert(Some(subfirst.clone()));
589                             }
590                             Entry::Occupied(mut occ) => {
591                                 // if there is already an entry, then a span must have collided.
592                                 // This should not happen with typical macro_rules macros,
593                                 // but syntax extensions need not maintain distinct spans,
594                                 // so distinct syntax trees can be assigned the same span.
595                                 // In such a case, the map cannot be trusted; so mark this
596                                 // entry as unusable.
597                                 occ.insert(None);
598                             }
599                         }
600
601                         // If the sequence contents can be empty, then the first
602                         // token could be the separator token itself.
603
604                         if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
605                             first.add_one_maybe(TokenTree::Token(sep.clone()));
606                         }
607
608                         // Reverse scan: Sequence comes before `first`.
609                         if subfirst.maybe_empty
610                             || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
611                             || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
612                         {
613                             // If sequence is potentially empty, then
614                             // union them (preserving first emptiness).
615                             first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
616                         } else {
617                             // Otherwise, sequence guaranteed
618                             // non-empty; replace first.
619                             first = subfirst;
620                         }
621                     }
622                 }
623             }
624
625             first
626         }
627     }
628
629     // walks forward over `tts` until all potential FIRST tokens are
630     // identified.
631     fn first(&self, tts: &[mbe::TokenTree]) -> TokenSet {
632         use mbe::TokenTree;
633
634         let mut first = TokenSet::empty();
635         for tt in tts.iter() {
636             assert!(first.maybe_empty);
637             match *tt {
638                 TokenTree::Token(..) | TokenTree::MetaVar(..) | TokenTree::MetaVarDecl(..) => {
639                     first.add_one(tt.clone());
640                     return first;
641                 }
642                 TokenTree::Delimited(span, ref delimited) => {
643                     first.add_one(delimited.open_tt(span));
644                     return first;
645                 }
646                 TokenTree::Sequence(sp, ref seq_rep) => {
647                     let subfirst_owned;
648                     let subfirst = match self.first.get(&sp.entire()) {
649                         Some(&Some(ref subfirst)) => subfirst,
650                         Some(&None) => {
651                             subfirst_owned = self.first(&seq_rep.tts[..]);
652                             &subfirst_owned
653                         }
654                         None => {
655                             panic!("We missed a sequence during FirstSets construction");
656                         }
657                     };
658
659                     // If the sequence contents can be empty, then the first
660                     // token could be the separator token itself.
661                     if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
662                         first.add_one_maybe(TokenTree::Token(sep.clone()));
663                     }
664
665                     assert!(first.maybe_empty);
666                     first.add_all(subfirst);
667                     if subfirst.maybe_empty
668                         || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
669                         || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
670                     {
671                         // Continue scanning for more first
672                         // tokens, but also make sure we
673                         // restore empty-tracking state.
674                         first.maybe_empty = true;
675                         continue;
676                     } else {
677                         return first;
678                     }
679                 }
680             }
681         }
682
683         // we only exit the loop if `tts` was empty or if every
684         // element of `tts` matches the empty sequence.
685         assert!(first.maybe_empty);
686         first
687     }
688 }
689
690 // A set of `mbe::TokenTree`s, which may include `TokenTree::Match`s
691 // (for macro-by-example syntactic variables). It also carries the
692 // `maybe_empty` flag; that is true if and only if the matcher can
693 // match an empty token sequence.
694 //
695 // The First set is computed on submatchers like `$($a:expr b),* $(c)* d`,
696 // which has corresponding FIRST = {$a:expr, c, d}.
697 // Likewise, `$($a:expr b),* $(c)+ d` has FIRST = {$a:expr, c}.
698 //
699 // (Notably, we must allow for *-op to occur zero times.)
700 #[derive(Clone, Debug)]
701 struct TokenSet {
702     tokens: Vec<mbe::TokenTree>,
703     maybe_empty: bool,
704 }
705
706 impl TokenSet {
707     // Returns a set for the empty sequence.
708     fn empty() -> Self {
709         TokenSet { tokens: Vec::new(), maybe_empty: true }
710     }
711
712     // Returns the set `{ tok }` for the single-token (and thus
713     // non-empty) sequence [tok].
714     fn singleton(tok: mbe::TokenTree) -> Self {
715         TokenSet { tokens: vec![tok], maybe_empty: false }
716     }
717
718     // Changes self to be the set `{ tok }`.
719     // Since `tok` is always present, marks self as non-empty.
720     fn replace_with(&mut self, tok: mbe::TokenTree) {
721         self.tokens.clear();
722         self.tokens.push(tok);
723         self.maybe_empty = false;
724     }
725
726     // Changes self to be the empty set `{}`; meant for use when
727     // the particular token does not matter, but we want to
728     // record that it occurs.
729     fn replace_with_irrelevant(&mut self) {
730         self.tokens.clear();
731         self.maybe_empty = false;
732     }
733
734     // Adds `tok` to the set for `self`, marking sequence as non-empy.
735     fn add_one(&mut self, tok: mbe::TokenTree) {
736         if !self.tokens.contains(&tok) {
737             self.tokens.push(tok);
738         }
739         self.maybe_empty = false;
740     }
741
742     // Adds `tok` to the set for `self`. (Leaves `maybe_empty` flag alone.)
743     fn add_one_maybe(&mut self, tok: mbe::TokenTree) {
744         if !self.tokens.contains(&tok) {
745             self.tokens.push(tok);
746         }
747     }
748
749     // Adds all elements of `other` to this.
750     //
751     // (Since this is a set, we filter out duplicates.)
752     //
753     // If `other` is potentially empty, then preserves the previous
754     // setting of the empty flag of `self`. If `other` is guaranteed
755     // non-empty, then `self` is marked non-empty.
756     fn add_all(&mut self, other: &Self) {
757         for tok in &other.tokens {
758             if !self.tokens.contains(tok) {
759                 self.tokens.push(tok.clone());
760             }
761         }
762         if !other.maybe_empty {
763             self.maybe_empty = false;
764         }
765     }
766 }
767
768 // Checks that `matcher` is internally consistent and that it
769 // can legally be followed by a token `N`, for all `N` in `follow`.
770 // (If `follow` is empty, then it imposes no constraint on
771 // the `matcher`.)
772 //
773 // Returns the set of NT tokens that could possibly come last in
774 // `matcher`. (If `matcher` matches the empty sequence, then
775 // `maybe_empty` will be set to true.)
776 //
777 // Requires that `first_sets` is pre-computed for `matcher`;
778 // see `FirstSets::new`.
779 fn check_matcher_core(
780     sess: &ParseSess,
781     features: &Features,
782     attrs: &[ast::Attribute],
783     first_sets: &FirstSets,
784     matcher: &[mbe::TokenTree],
785     follow: &TokenSet,
786 ) -> TokenSet {
787     use mbe::TokenTree;
788
789     let mut last = TokenSet::empty();
790
791     // 2. For each token and suffix  [T, SUFFIX] in M:
792     // ensure that T can be followed by SUFFIX, and if SUFFIX may be empty,
793     // then ensure T can also be followed by any element of FOLLOW.
794     'each_token: for i in 0..matcher.len() {
795         let token = &matcher[i];
796         let suffix = &matcher[i + 1..];
797
798         let build_suffix_first = || {
799             let mut s = first_sets.first(suffix);
800             if s.maybe_empty {
801                 s.add_all(follow);
802             }
803             s
804         };
805
806         // (we build `suffix_first` on demand below; you can tell
807         // which cases are supposed to fall through by looking for the
808         // initialization of this variable.)
809         let suffix_first;
810
811         // First, update `last` so that it corresponds to the set
812         // of NT tokens that might end the sequence `... token`.
813         match *token {
814             TokenTree::Token(..) | TokenTree::MetaVar(..) | TokenTree::MetaVarDecl(..) => {
815                 let can_be_followed_by_any;
816                 if let Err(bad_frag) = has_legal_fragment_specifier(sess, features, attrs, token) {
817                     let msg = format!("invalid fragment specifier `{}`", bad_frag);
818                     sess.span_diagnostic
819                         .struct_span_err(token.span(), &msg)
820                         .help(VALID_FRAGMENT_NAMES_MSG)
821                         .emit();
822                     // (This eliminates false positives and duplicates
823                     // from error messages.)
824                     can_be_followed_by_any = true;
825                 } else {
826                     can_be_followed_by_any = token_can_be_followed_by_any(token);
827                 }
828
829                 if can_be_followed_by_any {
830                     // don't need to track tokens that work with any,
831                     last.replace_with_irrelevant();
832                     // ... and don't need to check tokens that can be
833                     // followed by anything against SUFFIX.
834                     continue 'each_token;
835                 } else {
836                     last.replace_with(token.clone());
837                     suffix_first = build_suffix_first();
838                 }
839             }
840             TokenTree::Delimited(span, ref d) => {
841                 let my_suffix = TokenSet::singleton(d.close_tt(span));
842                 check_matcher_core(sess, features, attrs, first_sets, &d.tts, &my_suffix);
843                 // don't track non NT tokens
844                 last.replace_with_irrelevant();
845
846                 // also, we don't need to check delimited sequences
847                 // against SUFFIX
848                 continue 'each_token;
849             }
850             TokenTree::Sequence(_, ref seq_rep) => {
851                 suffix_first = build_suffix_first();
852                 // The trick here: when we check the interior, we want
853                 // to include the separator (if any) as a potential
854                 // (but not guaranteed) element of FOLLOW. So in that
855                 // case, we make a temp copy of suffix and stuff
856                 // delimiter in there.
857                 //
858                 // FIXME: Should I first scan suffix_first to see if
859                 // delimiter is already in it before I go through the
860                 // work of cloning it? But then again, this way I may
861                 // get a "tighter" span?
862                 let mut new;
863                 let my_suffix = if let Some(sep) = &seq_rep.separator {
864                     new = suffix_first.clone();
865                     new.add_one_maybe(TokenTree::Token(sep.clone()));
866                     &new
867                 } else {
868                     &suffix_first
869                 };
870
871                 // At this point, `suffix_first` is built, and
872                 // `my_suffix` is some TokenSet that we can use
873                 // for checking the interior of `seq_rep`.
874                 let next =
875                     check_matcher_core(sess, features, attrs, first_sets, &seq_rep.tts, my_suffix);
876                 if next.maybe_empty {
877                     last.add_all(&next);
878                 } else {
879                     last = next;
880                 }
881
882                 // the recursive call to check_matcher_core already ran the 'each_last
883                 // check below, so we can just keep going forward here.
884                 continue 'each_token;
885             }
886         }
887
888         // (`suffix_first` guaranteed initialized once reaching here.)
889
890         // Now `last` holds the complete set of NT tokens that could
891         // end the sequence before SUFFIX. Check that every one works with `suffix`.
892         'each_last: for token in &last.tokens {
893             if let TokenTree::MetaVarDecl(_, name, frag_spec) = *token {
894                 for next_token in &suffix_first.tokens {
895                     match is_in_follow(next_token, frag_spec.name) {
896                         IsInFollow::Invalid(msg, help) => {
897                             sess.span_diagnostic
898                                 .struct_span_err(next_token.span(), &msg)
899                                 .help(help)
900                                 .emit();
901                             // don't bother reporting every source of
902                             // conflict for a particular element of `last`.
903                             continue 'each_last;
904                         }
905                         IsInFollow::Yes => {}
906                         IsInFollow::No(possible) => {
907                             let may_be = if last.tokens.len() == 1 && suffix_first.tokens.len() == 1
908                             {
909                                 "is"
910                             } else {
911                                 "may be"
912                             };
913
914                             let sp = next_token.span();
915                             let mut err = sess.span_diagnostic.struct_span_err(
916                                 sp,
917                                 &format!(
918                                     "`${name}:{frag}` {may_be} followed by `{next}`, which \
919                                      is not allowed for `{frag}` fragments",
920                                     name = name,
921                                     frag = frag_spec,
922                                     next = quoted_tt_to_string(next_token),
923                                     may_be = may_be
924                                 ),
925                             );
926                             err.span_label(
927                                 sp,
928                                 format!("not allowed after `{}` fragments", frag_spec),
929                             );
930                             let msg = "allowed there are: ";
931                             match possible {
932                                 &[] => {}
933                                 &[t] => {
934                                     err.note(&format!(
935                                         "only {} is allowed after `{}` fragments",
936                                         t, frag_spec,
937                                     ));
938                                 }
939                                 ts => {
940                                     err.note(&format!(
941                                         "{}{} or {}",
942                                         msg,
943                                         ts[..ts.len() - 1]
944                                             .iter()
945                                             .map(|s| *s)
946                                             .collect::<Vec<_>>()
947                                             .join(", "),
948                                         ts[ts.len() - 1],
949                                     ));
950                                 }
951                             }
952                             err.emit();
953                         }
954                     }
955                 }
956             }
957         }
958     }
959     last
960 }
961
962 fn token_can_be_followed_by_any(tok: &mbe::TokenTree) -> bool {
963     if let mbe::TokenTree::MetaVarDecl(_, _, frag_spec) = *tok {
964         frag_can_be_followed_by_any(frag_spec.name)
965     } else {
966         // (Non NT's can always be followed by anthing in matchers.)
967         true
968     }
969 }
970
971 /// Returns `true` if a fragment of type `frag` can be followed by any sort of
972 /// token. We use this (among other things) as a useful approximation
973 /// for when `frag` can be followed by a repetition like `$(...)*` or
974 /// `$(...)+`. In general, these can be a bit tricky to reason about,
975 /// so we adopt a conservative position that says that any fragment
976 /// specifier which consumes at most one token tree can be followed by
977 /// a fragment specifier (indeed, these fragments can be followed by
978 /// ANYTHING without fear of future compatibility hazards).
979 fn frag_can_be_followed_by_any(frag: Symbol) -> bool {
980     match frag {
981         sym::item     | // always terminated by `}` or `;`
982         sym::block    | // exactly one token tree
983         sym::ident    | // exactly one token tree
984         sym::literal  | // exactly one token tree
985         sym::meta     | // exactly one token tree
986         sym::lifetime | // exactly one token tree
987         sym::tt =>   // exactly one token tree
988             true,
989
990         _ =>
991             false,
992     }
993 }
994
995 enum IsInFollow {
996     Yes,
997     No(&'static [&'static str]),
998     Invalid(String, &'static str),
999 }
1000
1001 /// Returns `true` if `frag` can legally be followed by the token `tok`. For
1002 /// fragments that can consume an unbounded number of tokens, `tok`
1003 /// must be within a well-defined follow set. This is intended to
1004 /// guarantee future compatibility: for example, without this rule, if
1005 /// we expanded `expr` to include a new binary operator, we might
1006 /// break macros that were relying on that binary operator as a
1007 /// separator.
1008 // when changing this do not forget to update doc/book/macros.md!
1009 fn is_in_follow(tok: &mbe::TokenTree, frag: Symbol) -> IsInFollow {
1010     use mbe::TokenTree;
1011
1012     if let TokenTree::Token(Token { kind: token::CloseDelim(_), .. }) = *tok {
1013         // closing a token tree can never be matched by any fragment;
1014         // iow, we always require that `(` and `)` match, etc.
1015         IsInFollow::Yes
1016     } else {
1017         match frag {
1018             sym::item => {
1019                 // since items *must* be followed by either a `;` or a `}`, we can
1020                 // accept anything after them
1021                 IsInFollow::Yes
1022             }
1023             sym::block => {
1024                 // anything can follow block, the braces provide an easy boundary to
1025                 // maintain
1026                 IsInFollow::Yes
1027             }
1028             sym::stmt | sym::expr => {
1029                 const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
1030                 match tok {
1031                     TokenTree::Token(token) => match token.kind {
1032                         FatArrow | Comma | Semi => IsInFollow::Yes,
1033                         _ => IsInFollow::No(TOKENS),
1034                     },
1035                     _ => IsInFollow::No(TOKENS),
1036                 }
1037             }
1038             sym::pat => {
1039                 const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`in`"];
1040                 match tok {
1041                     TokenTree::Token(token) => match token.kind {
1042                         FatArrow | Comma | Eq | BinOp(token::Or) => IsInFollow::Yes,
1043                         Ident(name, false) if name == kw::If || name == kw::In => IsInFollow::Yes,
1044                         _ => IsInFollow::No(TOKENS),
1045                     },
1046                     _ => IsInFollow::No(TOKENS),
1047                 }
1048             }
1049             sym::path | sym::ty => {
1050                 const TOKENS: &[&str] = &[
1051                     "`{`", "`[`", "`=>`", "`,`", "`>`", "`=`", "`:`", "`;`", "`|`", "`as`",
1052                     "`where`",
1053                 ];
1054                 match tok {
1055                     TokenTree::Token(token) => match token.kind {
1056                         OpenDelim(token::DelimToken::Brace)
1057                         | OpenDelim(token::DelimToken::Bracket)
1058                         | Comma
1059                         | FatArrow
1060                         | Colon
1061                         | Eq
1062                         | Gt
1063                         | BinOp(token::Shr)
1064                         | Semi
1065                         | BinOp(token::Or) => IsInFollow::Yes,
1066                         Ident(name, false) if name == kw::As || name == kw::Where => {
1067                             IsInFollow::Yes
1068                         }
1069                         _ => IsInFollow::No(TOKENS),
1070                     },
1071                     TokenTree::MetaVarDecl(_, _, frag) if frag.name == sym::block => {
1072                         IsInFollow::Yes
1073                     }
1074                     _ => IsInFollow::No(TOKENS),
1075                 }
1076             }
1077             sym::ident | sym::lifetime => {
1078                 // being a single token, idents and lifetimes are harmless
1079                 IsInFollow::Yes
1080             }
1081             sym::literal => {
1082                 // literals may be of a single token, or two tokens (negative numbers)
1083                 IsInFollow::Yes
1084             }
1085             sym::meta | sym::tt => {
1086                 // being either a single token or a delimited sequence, tt is
1087                 // harmless
1088                 IsInFollow::Yes
1089             }
1090             sym::vis => {
1091                 // Explicitly disallow `priv`, on the off chance it comes back.
1092                 const TOKENS: &[&str] = &["`,`", "an ident", "a type"];
1093                 match tok {
1094                     TokenTree::Token(token) => match token.kind {
1095                         Comma => IsInFollow::Yes,
1096                         Ident(name, is_raw) if is_raw || name != kw::Priv => IsInFollow::Yes,
1097                         _ => {
1098                             if token.can_begin_type() {
1099                                 IsInFollow::Yes
1100                             } else {
1101                                 IsInFollow::No(TOKENS)
1102                             }
1103                         }
1104                     },
1105                     TokenTree::MetaVarDecl(_, _, frag)
1106                         if frag.name == sym::ident
1107                             || frag.name == sym::ty
1108                             || frag.name == sym::path =>
1109                     {
1110                         IsInFollow::Yes
1111                     }
1112                     _ => IsInFollow::No(TOKENS),
1113                 }
1114             }
1115             kw::Invalid => IsInFollow::Yes,
1116             _ => IsInFollow::Invalid(
1117                 format!("invalid fragment specifier `{}`", frag),
1118                 VALID_FRAGMENT_NAMES_MSG,
1119             ),
1120         }
1121     }
1122 }
1123
1124 fn has_legal_fragment_specifier(
1125     sess: &ParseSess,
1126     features: &Features,
1127     attrs: &[ast::Attribute],
1128     tok: &mbe::TokenTree,
1129 ) -> Result<(), String> {
1130     debug!("has_legal_fragment_specifier({:?})", tok);
1131     if let mbe::TokenTree::MetaVarDecl(_, _, ref frag_spec) = *tok {
1132         let frag_span = tok.span();
1133         if !is_legal_fragment_specifier(sess, features, attrs, frag_spec.name, frag_span) {
1134             return Err(frag_spec.to_string());
1135         }
1136     }
1137     Ok(())
1138 }
1139
1140 fn is_legal_fragment_specifier(
1141     _sess: &ParseSess,
1142     _features: &Features,
1143     _attrs: &[ast::Attribute],
1144     frag_name: Symbol,
1145     _frag_span: Span,
1146 ) -> bool {
1147     /*
1148      * If new fragment specifiers are invented in nightly, `_sess`,
1149      * `_features`, `_attrs`, and `_frag_span` will be useful here
1150      * for checking against feature gates. See past versions of
1151      * this function.
1152      */
1153     match frag_name {
1154         sym::item
1155         | sym::block
1156         | sym::stmt
1157         | sym::expr
1158         | sym::pat
1159         | sym::lifetime
1160         | sym::path
1161         | sym::ty
1162         | sym::ident
1163         | sym::meta
1164         | sym::tt
1165         | sym::vis
1166         | sym::literal
1167         | kw::Invalid => true,
1168         _ => false,
1169     }
1170 }
1171
1172 fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
1173     match *tt {
1174         mbe::TokenTree::Token(ref token) => pprust::token_to_string(&token),
1175         mbe::TokenTree::MetaVar(_, name) => format!("${}", name),
1176         mbe::TokenTree::MetaVarDecl(_, name, kind) => format!("${}:{}", name, kind),
1177         _ => panic!(
1178             "unexpected mbe::TokenTree::{{Sequence or Delimited}} \
1179              in follow set checker"
1180         ),
1181     }
1182 }
1183
1184 /// Use this token tree as a matcher to parse given tts.
1185 fn parse_tt(cx: &ExtCtxt<'_>, mtch: &[mbe::TokenTree], tts: TokenStream) -> NamedParseResult {
1186     // `None` is because we're not interpolating
1187     let directory = Directory {
1188         path: Cow::from(cx.current_expansion.module.directory.as_path()),
1189         ownership: cx.current_expansion.directory_ownership,
1190     };
1191     parse(cx.parse_sess(), tts, mtch, Some(directory), true)
1192 }
1193
1194 /// Generates an appropriate parsing failure message. For EOF, this is "unexpected end...". For
1195 /// other tokens, this is "unexpected token...".
1196 fn parse_failure_msg(tok: &Token) -> String {
1197     match tok.kind {
1198         token::Eof => "unexpected end of macro invocation".to_string(),
1199         _ => format!(
1200             "no rules expected the token `{}`",
1201             pprust::token_to_string(tok),
1202         ),
1203     }
1204 }