1use std::borrow::Cow;
2use std::collections::hash_map::Entry;
3use std::sync::Arc;
4use std::{mem, slice};
5
6use ast::token::IdentIsRaw;
7use rustc_ast::token::NtPatKind::*;
8use rustc_ast::token::TokenKind::*;
9use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind};
10use rustc_ast::tokenstream::{DelimSpan, TokenStream};
11use rustc_ast::{self as ast, DUMMY_NODE_ID, NodeId};
12use rustc_ast_pretty::pprust;
13use rustc_attr_parsing::{AttributeKind, find_attr};
14use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
15use rustc_errors::{Applicability, ErrorGuaranteed};
16use rustc_feature::Features;
17use rustc_hir as hir;
18use rustc_lint_defs::BuiltinLintDiag;
19use rustc_lint_defs::builtin::{
20 RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
21};
22use rustc_parse::parser::{ParseNtResult, Parser, Recovery};
23use rustc_session::Session;
24use rustc_session::parse::ParseSess;
25use rustc_span::edition::Edition;
26use rustc_span::hygiene::Transparency;
27use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, kw, sym};
28use tracing::{debug, instrument, trace, trace_span};
29
30use super::diagnostics;
31use super::macro_parser::{NamedMatches, NamedParseResult};
32use crate::base::{
33 DummyResult, ExpandResult, ExtCtxt, MacResult, MacroExpanderResult, SyntaxExtension,
34 SyntaxExtensionKind, TTMacroExpander,
35};
36use crate::expand::{AstFragment, AstFragmentKind, ensure_complete_parse, parse_ast_fragment};
37use crate::mbe;
38use crate::mbe::diagnostics::{annotate_doc_comment, parse_failure_msg};
39use crate::mbe::macro_check;
40use crate::mbe::macro_parser::NamedMatch::*;
41use crate::mbe::macro_parser::{Error, ErrorReported, Failure, MatcherLoc, Success, TtParser};
42use crate::mbe::transcribe::transcribe;
43
44pub(crate) struct ParserAnyMacro<'a> {
45 parser: Parser<'a>,
46
47 site_span: Span,
49 macro_ident: Ident,
51 lint_node_id: NodeId,
52 is_trailing_mac: bool,
53 arm_span: Span,
54 is_local: bool,
56}
57
58impl<'a> ParserAnyMacro<'a> {
59 pub(crate) fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
60 let ParserAnyMacro {
61 site_span,
62 macro_ident,
63 ref mut parser,
64 lint_node_id,
65 arm_span,
66 is_trailing_mac,
67 is_local,
68 } = *self;
69 let snapshot = &mut parser.create_snapshot_for_diagnostic();
70 let fragment = match parse_ast_fragment(parser, kind) {
71 Ok(f) => f,
72 Err(err) => {
73 let guar = diagnostics::emit_frag_parse_err(
74 err, parser, snapshot, site_span, arm_span, kind,
75 );
76 return kind.dummy(site_span, guar);
77 }
78 };
79
80 if kind == AstFragmentKind::Expr && parser.token == token::Semi {
84 if is_local {
85 parser.psess.buffer_lint(
86 SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
87 parser.token.span,
88 lint_node_id,
89 BuiltinLintDiag::TrailingMacro(is_trailing_mac, macro_ident),
90 );
91 }
92 parser.bump();
93 }
94
95 let path = ast::Path::from_ident(macro_ident.with_span_pos(site_span));
97 ensure_complete_parse(parser, &path, kind.name(), site_span);
98 fragment
99 }
100}
101
102struct MacroRulesMacroExpander {
103 node_id: NodeId,
104 name: Ident,
105 span: Span,
106 transparency: Transparency,
107 lhses: Vec<Vec<MatcherLoc>>,
108 rhses: Vec<mbe::TokenTree>,
109}
110
111impl TTMacroExpander for MacroRulesMacroExpander {
112 fn expand<'cx>(
113 &self,
114 cx: &'cx mut ExtCtxt<'_>,
115 sp: Span,
116 input: TokenStream,
117 ) -> MacroExpanderResult<'cx> {
118 ExpandResult::Ready(expand_macro(
119 cx,
120 sp,
121 self.span,
122 self.node_id,
123 self.name,
124 self.transparency,
125 input,
126 &self.lhses,
127 &self.rhses,
128 ))
129 }
130}
131
132struct DummyExpander(ErrorGuaranteed);
133
134impl TTMacroExpander for DummyExpander {
135 fn expand<'cx>(
136 &self,
137 _: &'cx mut ExtCtxt<'_>,
138 span: Span,
139 _: TokenStream,
140 ) -> ExpandResult<Box<dyn MacResult + 'cx>, ()> {
141 ExpandResult::Ready(DummyResult::any(span, self.0))
142 }
143}
144
145fn trace_macros_note(cx_expansions: &mut FxIndexMap<Span, Vec<String>>, sp: Span, message: String) {
146 let sp = sp.macro_backtrace().last().map_or(sp, |trace| trace.call_site);
147 cx_expansions.entry(sp).or_default().push(message);
148}
149
150pub(super) trait Tracker<'matcher> {
151 type Failure;
153
154 fn build_failure(tok: Token, position: u32, msg: &'static str) -> Self::Failure;
158
159 fn before_match_loc(&mut self, _parser: &TtParser, _matcher: &'matcher MatcherLoc) {}
161
162 fn after_arm(&mut self, _result: &NamedParseResult<Self::Failure>) {}
165
166 fn description() -> &'static str;
168
169 fn recovery() -> Recovery {
170 Recovery::Forbidden
171 }
172
173 fn set_expected_token(&mut self, _tok: &'matcher Token) {}
174 fn get_expected_token(&self) -> Option<&'matcher Token> {
175 None
176 }
177}
178
179pub(super) struct NoopTracker;
182
183impl<'matcher> Tracker<'matcher> for NoopTracker {
184 type Failure = ();
185
186 fn build_failure(_tok: Token, _position: u32, _msg: &'static str) -> Self::Failure {}
187
188 fn description() -> &'static str {
189 "none"
190 }
191}
192
193#[instrument(skip(cx, transparency, arg, lhses, rhses))]
196fn expand_macro<'cx>(
197 cx: &'cx mut ExtCtxt<'_>,
198 sp: Span,
199 def_span: Span,
200 node_id: NodeId,
201 name: Ident,
202 transparency: Transparency,
203 arg: TokenStream,
204 lhses: &[Vec<MatcherLoc>],
205 rhses: &[mbe::TokenTree],
206) -> Box<dyn MacResult + 'cx> {
207 let psess = &cx.sess.psess;
208 let is_local = node_id != DUMMY_NODE_ID;
211
212 if cx.trace_macros() {
213 let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(&arg));
214 trace_macros_note(&mut cx.expansions, sp, msg);
215 }
216
217 let try_success_result = try_match_macro(psess, name, &arg, lhses, &mut NoopTracker);
219
220 match try_success_result {
221 Ok((i, named_matches)) => {
222 let (rhs, rhs_span): (&mbe::Delimited, DelimSpan) = match &rhses[i] {
223 mbe::TokenTree::Delimited(span, _, delimited) => (&delimited, *span),
224 _ => cx.dcx().span_bug(sp, "malformed macro rhs"),
225 };
226 let arm_span = rhses[i].span();
227
228 let id = cx.current_expansion.id;
230 let tts = match transcribe(psess, &named_matches, rhs, rhs_span, transparency, id) {
231 Ok(tts) => tts,
232 Err(err) => {
233 let guar = err.emit();
234 return DummyResult::any(arm_span, guar);
235 }
236 };
237
238 if cx.trace_macros() {
239 let msg = format!("to `{}`", pprust::tts_to_string(&tts));
240 trace_macros_note(&mut cx.expansions, sp, msg);
241 }
242
243 let p = Parser::new(psess, tts, None);
244
245 if is_local {
246 cx.resolver.record_macro_rule_usage(node_id, i);
247 }
248
249 Box::new(ParserAnyMacro {
252 parser: p,
253
254 site_span: sp,
258 macro_ident: name,
259 lint_node_id: cx.current_expansion.lint_node_id,
260 is_trailing_mac: cx.current_expansion.is_trailing_mac,
261 arm_span,
262 is_local,
263 })
264 }
265 Err(CanRetry::No(guar)) => {
266 debug!("Will not retry matching as an error was emitted already");
267 DummyResult::any(sp, guar)
268 }
269 Err(CanRetry::Yes) => {
270 let (span, guar) =
272 diagnostics::failed_to_match_macro(cx.psess(), sp, def_span, name, arg, lhses);
273 cx.trace_macros_diag();
274 DummyResult::any(span, guar)
275 }
276 }
277}
278
279pub(super) enum CanRetry {
280 Yes,
281 No(ErrorGuaranteed),
283}
284
285#[instrument(level = "debug", skip(psess, arg, lhses, track), fields(tracking = %T::description()))]
289pub(super) fn try_match_macro<'matcher, T: Tracker<'matcher>>(
290 psess: &ParseSess,
291 name: Ident,
292 arg: &TokenStream,
293 lhses: &'matcher [Vec<MatcherLoc>],
294 track: &mut T,
295) -> Result<(usize, NamedMatches), CanRetry> {
296 let parser = parser_from_cx(psess, arg.clone(), T::recovery());
316 let mut tt_parser = TtParser::new(name);
318 for (i, lhs) in lhses.iter().enumerate() {
319 let _tracing_span = trace_span!("Matching arm", %i);
320
321 let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
326
327 let result = tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs, track);
328
329 track.after_arm(&result);
330
331 match result {
332 Success(named_matches) => {
333 debug!("Parsed arm successfully");
334 psess.gated_spans.merge(gated_spans_snapshot);
337
338 return Ok((i, named_matches));
339 }
340 Failure(_) => {
341 trace!("Failed to match arm, trying the next one");
342 }
344 Error(_, _) => {
345 debug!("Fatal error occurred during matching");
346 return Err(CanRetry::Yes);
348 }
349 ErrorReported(guarantee) => {
350 debug!("Fatal error occurred and was reported during matching");
351 return Err(CanRetry::No(guarantee));
353 }
354 }
355
356 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
359 }
360
361 Err(CanRetry::Yes)
362}
363
364pub fn compile_declarative_macro(
371 sess: &Session,
372 features: &Features,
373 macro_def: &ast::MacroDef,
374 ident: Ident,
375 attrs: &[hir::Attribute],
376 span: Span,
377 node_id: NodeId,
378 edition: Edition,
379) -> (SyntaxExtension, Vec<(usize, Span)>) {
380 let mk_syn_ext = |expander| {
381 SyntaxExtension::new(
382 sess,
383 SyntaxExtensionKind::LegacyBang(expander),
384 span,
385 Vec::new(),
386 edition,
387 ident.name,
388 attrs,
389 node_id != DUMMY_NODE_ID,
390 )
391 };
392 let dummy_syn_ext = |guar| (mk_syn_ext(Arc::new(DummyExpander(guar))), Vec::new());
393
394 let lhs_nm = Ident::new(sym::lhs, span);
395 let rhs_nm = Ident::new(sym::rhs, span);
396 let tt_spec = Some(NonterminalKind::TT);
397 let macro_rules = macro_def.macro_rules;
398
399 let argument_gram = vec![
407 mbe::TokenTree::Sequence(
408 DelimSpan::dummy(),
409 mbe::SequenceRepetition {
410 tts: vec![
411 mbe::TokenTree::MetaVarDecl(span, lhs_nm, tt_spec),
412 mbe::TokenTree::token(token::FatArrow, span),
413 mbe::TokenTree::MetaVarDecl(span, rhs_nm, tt_spec),
414 ],
415 separator: Some(Token::new(
416 if macro_rules { token::Semi } else { token::Comma },
417 span,
418 )),
419 kleene: mbe::KleeneToken::new(mbe::KleeneOp::OneOrMore, span),
420 num_captures: 2,
421 },
422 ),
423 mbe::TokenTree::Sequence(
425 DelimSpan::dummy(),
426 mbe::SequenceRepetition {
427 tts: vec![mbe::TokenTree::token(
428 if macro_rules { token::Semi } else { token::Comma },
429 span,
430 )],
431 separator: None,
432 kleene: mbe::KleeneToken::new(mbe::KleeneOp::ZeroOrMore, span),
433 num_captures: 0,
434 },
435 ),
436 ];
437 let argument_gram = mbe::macro_parser::compute_locs(&argument_gram);
439
440 let create_parser = || {
441 let body = macro_def.body.tokens.clone();
442 Parser::new(&sess.psess, body, rustc_parse::MACRO_ARGUMENTS)
443 };
444
445 let parser = create_parser();
446 let mut tt_parser =
447 TtParser::new(Ident::with_dummy_span(if macro_rules { kw::MacroRules } else { kw::Macro }));
448 let argument_map =
449 match tt_parser.parse_tt(&mut Cow::Owned(parser), &argument_gram, &mut NoopTracker) {
450 Success(m) => m,
451 Failure(()) => {
452 let retry_parser = create_parser();
456
457 let mut track = diagnostics::FailureForwarder::new();
458 let parse_result =
459 tt_parser.parse_tt(&mut Cow::Owned(retry_parser), &argument_gram, &mut track);
460 let Failure((token, _, msg)) = parse_result else {
461 unreachable!("matcher returned something other than Failure after retry");
462 };
463
464 let s = parse_failure_msg(&token, track.get_expected_token());
465 let sp = token.span.substitute_dummy(span);
466 let mut err = sess.dcx().struct_span_err(sp, s);
467 err.span_label(sp, msg);
468 annotate_doc_comment(&mut err, sess.source_map(), sp);
469 let guar = err.emit();
470 return dummy_syn_ext(guar);
471 }
472 Error(sp, msg) => {
473 let guar = sess.dcx().span_err(sp.substitute_dummy(span), msg);
474 return dummy_syn_ext(guar);
475 }
476 ErrorReported(guar) => {
477 return dummy_syn_ext(guar);
478 }
479 };
480
481 let mut guar = None;
482 let mut check_emission = |ret: Result<(), ErrorGuaranteed>| guar = guar.or(ret.err());
483
484 let lhses = match &argument_map[&MacroRulesNormalizedIdent::new(lhs_nm)] {
486 MatchedSeq(s) => s
487 .iter()
488 .map(|m| {
489 if let MatchedSingle(ParseNtResult::Tt(tt)) = m {
490 let tt = mbe::quoted::parse(
491 &TokenStream::new(vec![tt.clone()]),
492 true,
493 sess,
494 node_id,
495 features,
496 edition,
497 )
498 .pop()
499 .unwrap();
500 check_emission(check_lhs_nt_follows(sess, node_id, &tt));
503 return tt;
504 }
505 sess.dcx().span_bug(span, "wrong-structured lhs")
506 })
507 .collect::<Vec<mbe::TokenTree>>(),
508 _ => sess.dcx().span_bug(span, "wrong-structured lhs"),
509 };
510
511 let rhses = match &argument_map[&MacroRulesNormalizedIdent::new(rhs_nm)] {
512 MatchedSeq(s) => s
513 .iter()
514 .map(|m| {
515 if let MatchedSingle(ParseNtResult::Tt(tt)) = m {
516 return mbe::quoted::parse(
517 &TokenStream::new(vec![tt.clone()]),
518 false,
519 sess,
520 node_id,
521 features,
522 edition,
523 )
524 .pop()
525 .unwrap();
526 }
527 sess.dcx().span_bug(span, "wrong-structured rhs")
528 })
529 .collect::<Vec<mbe::TokenTree>>(),
530 _ => sess.dcx().span_bug(span, "wrong-structured rhs"),
531 };
532
533 for rhs in &rhses {
534 check_emission(check_rhs(sess, rhs));
535 }
536
537 for lhs in &lhses {
539 check_emission(check_lhs_no_empty_seq(sess, slice::from_ref(lhs)));
540 }
541
542 check_emission(macro_check::check_meta_variables(&sess.psess, node_id, span, &lhses, &rhses));
543
544 let transparency = find_attr!(attrs, AttributeKind::MacroTransparency(x) => *x)
545 .unwrap_or(Transparency::fallback(macro_rules));
546
547 if let Some(guar) = guar {
548 return dummy_syn_ext(guar);
551 }
552
553 let rule_spans = if node_id != DUMMY_NODE_ID {
556 lhses
557 .iter()
558 .zip(rhses.iter())
559 .enumerate()
560 .filter(|(_idx, (_lhs, rhs))| !has_compile_error_macro(rhs))
563 .map(|(idx, (lhs, _rhs))| (idx, lhs.span()))
566 .collect::<Vec<_>>()
567 } else {
568 Vec::new()
569 };
570
571 let lhses = lhses
574 .iter()
575 .map(|lhs| {
576 match lhs {
578 mbe::TokenTree::Delimited(.., delimited) => {
579 mbe::macro_parser::compute_locs(&delimited.tts)
580 }
581 _ => sess.dcx().span_bug(span, "malformed macro lhs"),
582 }
583 })
584 .collect();
585
586 let expander = Arc::new(MacroRulesMacroExpander {
587 name: ident,
588 span,
589 node_id,
590 transparency,
591 lhses,
592 rhses,
593 });
594 (mk_syn_ext(expander), rule_spans)
595}
596
597fn check_lhs_nt_follows(
598 sess: &Session,
599 node_id: NodeId,
600 lhs: &mbe::TokenTree,
601) -> Result<(), ErrorGuaranteed> {
602 if let mbe::TokenTree::Delimited(.., delimited) = lhs {
605 check_matcher(sess, node_id, &delimited.tts)
606 } else {
607 let msg = "invalid macro matcher; matchers must be contained in balanced delimiters";
608 Err(sess.dcx().span_err(lhs.span(), msg))
609 }
610}
611
612fn is_empty_token_tree(sess: &Session, seq: &mbe::SequenceRepetition) -> bool {
613 if seq.separator.is_some() {
614 false
615 } else {
616 let mut is_empty = true;
617 let mut iter = seq.tts.iter().peekable();
618 while let Some(tt) = iter.next() {
619 match tt {
620 mbe::TokenTree::MetaVarDecl(_, _, Some(NonterminalKind::Vis)) => {}
621 mbe::TokenTree::Token(t @ Token { kind: DocComment(..), .. }) => {
622 let mut now = t;
623 while let Some(&mbe::TokenTree::Token(
624 next @ Token { kind: DocComment(..), .. },
625 )) = iter.peek()
626 {
627 now = next;
628 iter.next();
629 }
630 let span = t.span.to(now.span);
631 sess.dcx().span_note(span, "doc comments are ignored in matcher position");
632 }
633 mbe::TokenTree::Sequence(_, sub_seq)
634 if (sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
635 || sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne) => {}
636 _ => is_empty = false,
637 }
638 }
639 is_empty
640 }
641}
642
643fn check_lhs_no_empty_seq(sess: &Session, tts: &[mbe::TokenTree]) -> Result<(), ErrorGuaranteed> {
646 use mbe::TokenTree;
647 for tt in tts {
648 match tt {
649 TokenTree::Token(..)
650 | TokenTree::MetaVar(..)
651 | TokenTree::MetaVarDecl(..)
652 | TokenTree::MetaVarExpr(..) => (),
653 TokenTree::Delimited(.., del) => check_lhs_no_empty_seq(sess, &del.tts)?,
654 TokenTree::Sequence(span, seq) => {
655 if is_empty_token_tree(sess, seq) {
656 let sp = span.entire();
657 let guar = sess.dcx().span_err(sp, "repetition matches empty token tree");
658 return Err(guar);
659 }
660 check_lhs_no_empty_seq(sess, &seq.tts)?
661 }
662 }
663 }
664
665 Ok(())
666}
667
668fn check_rhs(sess: &Session, rhs: &mbe::TokenTree) -> Result<(), ErrorGuaranteed> {
669 match *rhs {
670 mbe::TokenTree::Delimited(..) => Ok(()),
671 _ => Err(sess.dcx().span_err(rhs.span(), "macro rhs must be delimited")),
672 }
673}
674
675fn check_matcher(
676 sess: &Session,
677 node_id: NodeId,
678 matcher: &[mbe::TokenTree],
679) -> Result<(), ErrorGuaranteed> {
680 let first_sets = FirstSets::new(matcher);
681 let empty_suffix = TokenSet::empty();
682 check_matcher_core(sess, node_id, &first_sets, matcher, &empty_suffix)?;
683 Ok(())
684}
685
686fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool {
687 match rhs {
688 mbe::TokenTree::Delimited(.., d) => {
689 let has_compile_error = d.tts.array_windows::<3>().any(|[ident, bang, args]| {
690 if let mbe::TokenTree::Token(ident) = ident
691 && let TokenKind::Ident(ident, _) = ident.kind
692 && ident == sym::compile_error
693 && let mbe::TokenTree::Token(bang) = bang
694 && let TokenKind::Bang = bang.kind
695 && let mbe::TokenTree::Delimited(.., del) = args
696 && !del.delim.skip()
697 {
698 true
699 } else {
700 false
701 }
702 });
703 if has_compile_error { true } else { d.tts.iter().any(has_compile_error_macro) }
704 }
705 _ => false,
706 }
707}
708
709struct FirstSets<'tt> {
722 first: FxHashMap<Span, Option<TokenSet<'tt>>>,
729}
730
731impl<'tt> FirstSets<'tt> {
732 fn new(tts: &'tt [mbe::TokenTree]) -> FirstSets<'tt> {
733 use mbe::TokenTree;
734
735 let mut sets = FirstSets { first: FxHashMap::default() };
736 build_recur(&mut sets, tts);
737 return sets;
738
739 fn build_recur<'tt>(sets: &mut FirstSets<'tt>, tts: &'tt [TokenTree]) -> TokenSet<'tt> {
743 let mut first = TokenSet::empty();
744 for tt in tts.iter().rev() {
745 match tt {
746 TokenTree::Token(..)
747 | TokenTree::MetaVar(..)
748 | TokenTree::MetaVarDecl(..)
749 | TokenTree::MetaVarExpr(..) => {
750 first.replace_with(TtHandle::TtRef(tt));
751 }
752 TokenTree::Delimited(span, _, delimited) => {
753 build_recur(sets, &delimited.tts);
754 first.replace_with(TtHandle::from_token_kind(
755 token::OpenDelim(delimited.delim),
756 span.open,
757 ));
758 }
759 TokenTree::Sequence(sp, seq_rep) => {
760 let subfirst = build_recur(sets, &seq_rep.tts);
761
762 match sets.first.entry(sp.entire()) {
763 Entry::Vacant(vac) => {
764 vac.insert(Some(subfirst.clone()));
765 }
766 Entry::Occupied(mut occ) => {
767 occ.insert(None);
774 }
775 }
776
777 if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
781 first.add_one_maybe(TtHandle::from_token(sep.clone()));
782 }
783
784 if subfirst.maybe_empty
786 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
787 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
788 {
789 first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
792 } else {
793 first = subfirst;
796 }
797 }
798 }
799 }
800
801 first
802 }
803 }
804
805 fn first(&self, tts: &'tt [mbe::TokenTree]) -> TokenSet<'tt> {
808 use mbe::TokenTree;
809
810 let mut first = TokenSet::empty();
811 for tt in tts.iter() {
812 assert!(first.maybe_empty);
813 match tt {
814 TokenTree::Token(..)
815 | TokenTree::MetaVar(..)
816 | TokenTree::MetaVarDecl(..)
817 | TokenTree::MetaVarExpr(..) => {
818 first.add_one(TtHandle::TtRef(tt));
819 return first;
820 }
821 TokenTree::Delimited(span, _, delimited) => {
822 first.add_one(TtHandle::from_token_kind(
823 token::OpenDelim(delimited.delim),
824 span.open,
825 ));
826 return first;
827 }
828 TokenTree::Sequence(sp, seq_rep) => {
829 let subfirst_owned;
830 let subfirst = match self.first.get(&sp.entire()) {
831 Some(Some(subfirst)) => subfirst,
832 Some(&None) => {
833 subfirst_owned = self.first(&seq_rep.tts);
834 &subfirst_owned
835 }
836 None => {
837 panic!("We missed a sequence during FirstSets construction");
838 }
839 };
840
841 if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
844 first.add_one_maybe(TtHandle::from_token(sep.clone()));
845 }
846
847 assert!(first.maybe_empty);
848 first.add_all(subfirst);
849 if subfirst.maybe_empty
850 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
851 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
852 {
853 first.maybe_empty = true;
857 continue;
858 } else {
859 return first;
860 }
861 }
862 }
863 }
864
865 assert!(first.maybe_empty);
868 first
869 }
870}
871
872#[derive(Debug)]
877enum TtHandle<'tt> {
878 TtRef(&'tt mbe::TokenTree),
880
881 Token(mbe::TokenTree),
886}
887
888impl<'tt> TtHandle<'tt> {
889 fn from_token(tok: Token) -> Self {
890 TtHandle::Token(mbe::TokenTree::Token(tok))
891 }
892
893 fn from_token_kind(kind: TokenKind, span: Span) -> Self {
894 TtHandle::from_token(Token::new(kind, span))
895 }
896
897 fn get(&'tt self) -> &'tt mbe::TokenTree {
899 match self {
900 TtHandle::TtRef(tt) => tt,
901 TtHandle::Token(token_tt) => token_tt,
902 }
903 }
904}
905
906impl<'tt> PartialEq for TtHandle<'tt> {
907 fn eq(&self, other: &TtHandle<'tt>) -> bool {
908 self.get() == other.get()
909 }
910}
911
912impl<'tt> Clone for TtHandle<'tt> {
913 fn clone(&self) -> Self {
914 match self {
915 TtHandle::TtRef(tt) => TtHandle::TtRef(tt),
916
917 TtHandle::Token(mbe::TokenTree::Token(tok)) => {
920 TtHandle::Token(mbe::TokenTree::Token(tok.clone()))
921 }
922
923 _ => unreachable!(),
924 }
925 }
926}
927
928#[derive(Clone, Debug)]
939struct TokenSet<'tt> {
940 tokens: Vec<TtHandle<'tt>>,
941 maybe_empty: bool,
942}
943
944impl<'tt> TokenSet<'tt> {
945 fn empty() -> Self {
947 TokenSet { tokens: Vec::new(), maybe_empty: true }
948 }
949
950 fn singleton(tt: TtHandle<'tt>) -> Self {
953 TokenSet { tokens: vec![tt], maybe_empty: false }
954 }
955
956 fn replace_with(&mut self, tt: TtHandle<'tt>) {
959 self.tokens.clear();
960 self.tokens.push(tt);
961 self.maybe_empty = false;
962 }
963
964 fn replace_with_irrelevant(&mut self) {
968 self.tokens.clear();
969 self.maybe_empty = false;
970 }
971
972 fn add_one(&mut self, tt: TtHandle<'tt>) {
974 if !self.tokens.contains(&tt) {
975 self.tokens.push(tt);
976 }
977 self.maybe_empty = false;
978 }
979
980 fn add_one_maybe(&mut self, tt: TtHandle<'tt>) {
982 if !self.tokens.contains(&tt) {
983 self.tokens.push(tt);
984 }
985 }
986
987 fn add_all(&mut self, other: &Self) {
995 for tt in &other.tokens {
996 if !self.tokens.contains(tt) {
997 self.tokens.push(tt.clone());
998 }
999 }
1000 if !other.maybe_empty {
1001 self.maybe_empty = false;
1002 }
1003 }
1004}
1005
1006fn check_matcher_core<'tt>(
1018 sess: &Session,
1019 node_id: NodeId,
1020 first_sets: &FirstSets<'tt>,
1021 matcher: &'tt [mbe::TokenTree],
1022 follow: &TokenSet<'tt>,
1023) -> Result<TokenSet<'tt>, ErrorGuaranteed> {
1024 use mbe::TokenTree;
1025
1026 let mut last = TokenSet::empty();
1027
1028 let mut errored = Ok(());
1029
1030 'each_token: for i in 0..matcher.len() {
1034 let token = &matcher[i];
1035 let suffix = &matcher[i + 1..];
1036
1037 let build_suffix_first = || {
1038 let mut s = first_sets.first(suffix);
1039 if s.maybe_empty {
1040 s.add_all(follow);
1041 }
1042 s
1043 };
1044
1045 let suffix_first;
1049
1050 match token {
1053 TokenTree::Token(..)
1054 | TokenTree::MetaVar(..)
1055 | TokenTree::MetaVarDecl(..)
1056 | TokenTree::MetaVarExpr(..) => {
1057 if token_can_be_followed_by_any(token) {
1058 last.replace_with_irrelevant();
1060 continue 'each_token;
1063 } else {
1064 last.replace_with(TtHandle::TtRef(token));
1065 suffix_first = build_suffix_first();
1066 }
1067 }
1068 TokenTree::Delimited(span, _, d) => {
1069 let my_suffix = TokenSet::singleton(TtHandle::from_token_kind(
1070 token::CloseDelim(d.delim),
1071 span.close,
1072 ));
1073 check_matcher_core(sess, node_id, first_sets, &d.tts, &my_suffix)?;
1074 last.replace_with_irrelevant();
1076
1077 continue 'each_token;
1080 }
1081 TokenTree::Sequence(_, seq_rep) => {
1082 suffix_first = build_suffix_first();
1083 let mut new;
1094 let my_suffix = if let Some(sep) = &seq_rep.separator {
1095 new = suffix_first.clone();
1096 new.add_one_maybe(TtHandle::from_token(sep.clone()));
1097 &new
1098 } else {
1099 &suffix_first
1100 };
1101
1102 let next = check_matcher_core(sess, node_id, first_sets, &seq_rep.tts, my_suffix)?;
1106 if next.maybe_empty {
1107 last.add_all(&next);
1108 } else {
1109 last = next;
1110 }
1111
1112 continue 'each_token;
1115 }
1116 }
1117
1118 for tt in &last.tokens {
1123 if let &TokenTree::MetaVarDecl(span, name, Some(kind)) = tt.get() {
1124 for next_token in &suffix_first.tokens {
1125 let next_token = next_token.get();
1126
1127 if node_id != DUMMY_NODE_ID
1136 && matches!(kind, NonterminalKind::Pat(PatParam { inferred: true }))
1137 && matches!(
1138 next_token,
1139 TokenTree::Token(token) if *token == token::Or
1140 )
1141 {
1142 let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl(
1144 span,
1145 name,
1146 Some(NonterminalKind::Pat(PatParam { inferred: false })),
1147 ));
1148 sess.psess.buffer_lint(
1149 RUST_2021_INCOMPATIBLE_OR_PATTERNS,
1150 span,
1151 ast::CRATE_NODE_ID,
1152 BuiltinLintDiag::OrPatternsBackCompat(span, suggestion),
1153 );
1154 }
1155 match is_in_follow(next_token, kind) {
1156 IsInFollow::Yes => {}
1157 IsInFollow::No(possible) => {
1158 let may_be = if last.tokens.len() == 1 && suffix_first.tokens.len() == 1
1159 {
1160 "is"
1161 } else {
1162 "may be"
1163 };
1164
1165 let sp = next_token.span();
1166 let mut err = sess.dcx().struct_span_err(
1167 sp,
1168 format!(
1169 "`${name}:{frag}` {may_be} followed by `{next}`, which \
1170 is not allowed for `{frag}` fragments",
1171 name = name,
1172 frag = kind,
1173 next = quoted_tt_to_string(next_token),
1174 may_be = may_be
1175 ),
1176 );
1177 err.span_label(sp, format!("not allowed after `{kind}` fragments"));
1178
1179 if kind == NonterminalKind::Pat(PatWithOr)
1180 && sess.psess.edition.at_least_rust_2021()
1181 && next_token.is_token(&token::Or)
1182 {
1183 let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl(
1184 span,
1185 name,
1186 Some(NonterminalKind::Pat(PatParam { inferred: false })),
1187 ));
1188 err.span_suggestion(
1189 span,
1190 "try a `pat_param` fragment specifier instead",
1191 suggestion,
1192 Applicability::MaybeIncorrect,
1193 );
1194 }
1195
1196 let msg = "allowed there are: ";
1197 match possible {
1198 &[] => {}
1199 &[t] => {
1200 err.note(format!(
1201 "only {t} is allowed after `{kind}` fragments",
1202 ));
1203 }
1204 ts => {
1205 err.note(format!(
1206 "{}{} or {}",
1207 msg,
1208 ts[..ts.len() - 1].to_vec().join(", "),
1209 ts[ts.len() - 1],
1210 ));
1211 }
1212 }
1213 errored = Err(err.emit());
1214 }
1215 }
1216 }
1217 }
1218 }
1219 }
1220 errored?;
1221 Ok(last)
1222}
1223
1224fn token_can_be_followed_by_any(tok: &mbe::TokenTree) -> bool {
1225 if let mbe::TokenTree::MetaVarDecl(_, _, Some(kind)) = *tok {
1226 frag_can_be_followed_by_any(kind)
1227 } else {
1228 true
1230 }
1231}
1232
1233fn frag_can_be_followed_by_any(kind: NonterminalKind) -> bool {
1242 matches!(
1243 kind,
1244 NonterminalKind::Item | NonterminalKind::Block | NonterminalKind::Ident | NonterminalKind::Literal | NonterminalKind::Meta | NonterminalKind::Lifetime | NonterminalKind::TT )
1252}
1253
1254enum IsInFollow {
1255 Yes,
1256 No(&'static [&'static str]),
1257}
1258
1259fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
1268 use mbe::TokenTree;
1269
1270 if let TokenTree::Token(Token { kind: token::CloseDelim(_), .. }) = *tok {
1271 IsInFollow::Yes
1274 } else {
1275 match kind {
1276 NonterminalKind::Item => {
1277 IsInFollow::Yes
1280 }
1281 NonterminalKind::Block => {
1282 IsInFollow::Yes
1285 }
1286 NonterminalKind::Stmt | NonterminalKind::Expr(_) => {
1287 const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
1288 match tok {
1289 TokenTree::Token(token) => match token.kind {
1290 FatArrow | Comma | Semi => IsInFollow::Yes,
1291 _ => IsInFollow::No(TOKENS),
1292 },
1293 _ => IsInFollow::No(TOKENS),
1294 }
1295 }
1296 NonterminalKind::Pat(PatParam { .. }) => {
1297 const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`in`"];
1298 match tok {
1299 TokenTree::Token(token) => match token.kind {
1300 FatArrow | Comma | Eq | Or => IsInFollow::Yes,
1301 Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1302 IsInFollow::Yes
1303 }
1304 _ => IsInFollow::No(TOKENS),
1305 },
1306 _ => IsInFollow::No(TOKENS),
1307 }
1308 }
1309 NonterminalKind::Pat(PatWithOr) => {
1310 const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`if`", "`in`"];
1311 match tok {
1312 TokenTree::Token(token) => match token.kind {
1313 FatArrow | Comma | Eq => IsInFollow::Yes,
1314 Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1315 IsInFollow::Yes
1316 }
1317 _ => IsInFollow::No(TOKENS),
1318 },
1319 _ => IsInFollow::No(TOKENS),
1320 }
1321 }
1322 NonterminalKind::Path | NonterminalKind::Ty => {
1323 const TOKENS: &[&str] = &[
1324 "`{`", "`[`", "`=>`", "`,`", "`>`", "`=`", "`:`", "`;`", "`|`", "`as`",
1325 "`where`",
1326 ];
1327 match tok {
1328 TokenTree::Token(token) => match token.kind {
1329 OpenDelim(Delimiter::Brace)
1330 | OpenDelim(Delimiter::Bracket)
1331 | Comma
1332 | FatArrow
1333 | Colon
1334 | Eq
1335 | Gt
1336 | Shr
1337 | Semi
1338 | Or => IsInFollow::Yes,
1339 Ident(name, IdentIsRaw::No) if name == kw::As || name == kw::Where => {
1340 IsInFollow::Yes
1341 }
1342 _ => IsInFollow::No(TOKENS),
1343 },
1344 TokenTree::MetaVarDecl(_, _, Some(NonterminalKind::Block)) => IsInFollow::Yes,
1345 _ => IsInFollow::No(TOKENS),
1346 }
1347 }
1348 NonterminalKind::Ident | NonterminalKind::Lifetime => {
1349 IsInFollow::Yes
1351 }
1352 NonterminalKind::Literal => {
1353 IsInFollow::Yes
1355 }
1356 NonterminalKind::Meta | NonterminalKind::TT => {
1357 IsInFollow::Yes
1360 }
1361 NonterminalKind::Vis => {
1362 const TOKENS: &[&str] = &["`,`", "an ident", "a type"];
1364 match tok {
1365 TokenTree::Token(token) => match token.kind {
1366 Comma => IsInFollow::Yes,
1367 Ident(_, IdentIsRaw::Yes) => IsInFollow::Yes,
1368 Ident(name, _) if name != kw::Priv => IsInFollow::Yes,
1369 _ => {
1370 if token.can_begin_type() {
1371 IsInFollow::Yes
1372 } else {
1373 IsInFollow::No(TOKENS)
1374 }
1375 }
1376 },
1377 TokenTree::MetaVarDecl(
1378 _,
1379 _,
1380 Some(NonterminalKind::Ident | NonterminalKind::Ty | NonterminalKind::Path),
1381 ) => IsInFollow::Yes,
1382 _ => IsInFollow::No(TOKENS),
1383 }
1384 }
1385 }
1386 }
1387}
1388
1389fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
1390 match tt {
1391 mbe::TokenTree::Token(token) => pprust::token_to_string(token).into(),
1392 mbe::TokenTree::MetaVar(_, name) => format!("${name}"),
1393 mbe::TokenTree::MetaVarDecl(_, name, Some(kind)) => format!("${name}:{kind}"),
1394 mbe::TokenTree::MetaVarDecl(_, name, None) => format!("${name}:"),
1395 _ => panic!(
1396 "{}",
1397 "unexpected mbe::TokenTree::{Sequence or Delimited} \
1398 in follow set checker"
1399 ),
1400 }
1401}
1402
1403pub(super) fn parser_from_cx(
1404 psess: &ParseSess,
1405 mut tts: TokenStream,
1406 recovery: Recovery,
1407) -> Parser<'_> {
1408 tts.desugar_doc_comments();
1409 Parser::new(psess, tts, rustc_parse::MACRO_ARGUMENTS).recovery(recovery)
1410}