Skip to main content

rustc_expand/mbe/
macro_rules.rs

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::{self, DelimSpan, TokenStream};
11use rustc_ast::{self as ast, DUMMY_NODE_ID, NodeId, Safety};
12use rustc_ast_pretty::pprust;
13use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
14use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan};
15use rustc_feature::Features;
16use rustc_hir as hir;
17use rustc_hir::attrs::diagnostic::Directive;
18use rustc_hir::def::MacroKinds;
19use rustc_hir::find_attr;
20use rustc_lint_defs::builtin::{
21    RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
22};
23use rustc_parse::exp;
24use rustc_parse::parser::{Parser, Recovery};
25use rustc_session::Session;
26use rustc_session::parse::{ParseSess, feature_err};
27use rustc_span::edition::Edition;
28use rustc_span::hygiene::Transparency;
29use rustc_span::{Ident, Span, Symbol, kw, sym};
30use tracing::{debug, instrument, trace, trace_span};
31
32use super::diagnostics::{FailedMacro, failed_to_match_macro};
33use super::macro_parser::{NamedMatches, NamedParseResult};
34use super::{SequenceRepetition, diagnostics};
35use crate::base::{
36    AttrProcMacro, BangProcMacro, DummyResult, ExpandResult, ExtCtxt, MacResult,
37    MacroExpanderResult, SyntaxExtension, SyntaxExtensionKind, TTMacroExpander,
38};
39use crate::errors;
40use crate::expand::{AstFragment, AstFragmentKind, ensure_complete_parse, parse_ast_fragment};
41use crate::mbe::macro_check::check_meta_variables;
42use crate::mbe::macro_parser::{Error, ErrorReported, Failure, MatcherLoc, Success, TtParser};
43use crate::mbe::quoted::{RulePart, parse_one_tt};
44use crate::mbe::transcribe::transcribe;
45use crate::mbe::{self, KleeneOp};
46
47pub(crate) struct ParserAnyMacro<'a, 'b> {
48    parser: Parser<'a>,
49
50    /// Span of the expansion site of the macro this parser is for
51    site_span: Span,
52    /// The ident of the macro we're parsing
53    macro_ident: Ident,
54    lint_node_id: NodeId,
55    is_trailing_mac: bool,
56    arm_span: Span,
57    /// Whether or not this macro is defined in the current crate
58    is_local: bool,
59    bindings: &'b [MacroRule],
60    matched_rule_bindings: &'b [MatcherLoc],
61}
62
63impl<'a, 'b> ParserAnyMacro<'a, 'b> {
64    pub(crate) fn make(
65        mut self: Box<ParserAnyMacro<'a, 'b>>,
66        kind: AstFragmentKind,
67    ) -> AstFragment {
68        let ParserAnyMacro {
69            site_span,
70            macro_ident,
71            ref mut parser,
72            lint_node_id,
73            arm_span,
74            is_trailing_mac,
75            is_local,
76            bindings,
77            matched_rule_bindings,
78        } = *self;
79        let snapshot = &mut parser.create_snapshot_for_diagnostic();
80        let fragment = match parse_ast_fragment(parser, kind) {
81            Ok(f) => f,
82            Err(err) => {
83                let guar = diagnostics::emit_frag_parse_err(
84                    err,
85                    parser,
86                    snapshot,
87                    site_span,
88                    arm_span,
89                    kind,
90                    bindings,
91                    matched_rule_bindings,
92                );
93                return kind.dummy(site_span, guar);
94            }
95        };
96
97        // We allow semicolons at the end of expressions -- e.g., the semicolon in
98        // `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
99        // but `m!()` is allowed in expression positions (cf. issue #34706).
100        if kind == AstFragmentKind::Expr && parser.token == token::Semi {
101            if is_local {
102                parser.psess.buffer_lint(
103                    SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
104                    parser.token.span,
105                    lint_node_id,
106                    errors::TrailingMacro { is_trailing: is_trailing_mac, name: macro_ident },
107                );
108            }
109            parser.bump();
110        }
111
112        // Make sure we don't have any tokens left to parse so we don't silently drop anything.
113        let path = ast::Path::from_ident(macro_ident.with_span_pos(site_span));
114        ensure_complete_parse(parser, &path, kind.name(), site_span);
115        fragment
116    }
117
118    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
                ::tracing::Level::INFO <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("from_tts",
                                    "rustc_expand::mbe::macro_rules", ::tracing::Level::INFO,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                    ::tracing_core::__macro_support::Option::Some(118u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                    ::tracing_core::field::FieldSet::new(&["site_span",
                                                    "arm_span", "is_local", "macro_ident"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::INFO <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::INFO <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&site_span)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&arm_span)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&is_local as
                                                            &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&macro_ident)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Self = loop {};
            return __tracing_attr_fake_return;
        }
        {
            Self {
                parser: Parser::new(&cx.sess.psess, tts, None),
                site_span,
                macro_ident,
                lint_node_id: cx.current_expansion.lint_node_id,
                is_trailing_mac: cx.current_expansion.is_trailing_mac,
                arm_span,
                is_local,
                bindings,
                matched_rule_bindings,
            }
        }
    }
}#[instrument(skip(cx, tts, bindings, matched_rule_bindings))]
119    pub(crate) fn from_tts<'cx>(
120        cx: &'cx mut ExtCtxt<'a>,
121        tts: TokenStream,
122        site_span: Span,
123        arm_span: Span,
124        is_local: bool,
125        macro_ident: Ident,
126        // bindings and lhs is for diagnostics
127        bindings: &'b [MacroRule],
128        matched_rule_bindings: &'b [MatcherLoc],
129    ) -> Self {
130        Self {
131            parser: Parser::new(&cx.sess.psess, tts, None),
132
133            // Pass along the original expansion site and the name of the macro
134            // so we can print a useful error message if the parse of the expanded
135            // macro leaves unparsed tokens.
136            site_span,
137            macro_ident,
138            lint_node_id: cx.current_expansion.lint_node_id,
139            is_trailing_mac: cx.current_expansion.is_trailing_mac,
140            arm_span,
141            is_local,
142            bindings,
143            matched_rule_bindings,
144        }
145    }
146}
147
148pub(crate) enum MacroRule {
149    /// A function-style rule, for use with `m!()`
150    Func { lhs: Vec<MatcherLoc>, lhs_span: Span, rhs: mbe::TokenTree },
151    /// An attr rule, for use with `#[m]`
152    Attr {
153        unsafe_rule: bool,
154        args: Vec<MatcherLoc>,
155        args_span: Span,
156        body: Vec<MatcherLoc>,
157        body_span: Span,
158        rhs: mbe::TokenTree,
159    },
160    /// A derive rule, for use with `#[m]`
161    Derive { body: Vec<MatcherLoc>, body_span: Span, rhs: mbe::TokenTree },
162}
163
164pub struct MacroRulesMacroExpander {
165    node_id: NodeId,
166    name: Ident,
167    span: Span,
168    on_unmatch_args: Option<Directive>,
169    transparency: Transparency,
170    kinds: MacroKinds,
171    rules: Vec<MacroRule>,
172}
173
174impl MacroRulesMacroExpander {
175    pub fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, MultiSpan)> {
176        // If the rhs contains an invocation like `compile_error!`, don't report it as unused.
177        let (span, rhs) = match self.rules[rule_i] {
178            MacroRule::Func { lhs_span, ref rhs, .. } => (MultiSpan::from_span(lhs_span), rhs),
179            MacroRule::Attr { args_span, body_span, ref rhs, .. } => {
180                (MultiSpan::from_spans(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [args_span, body_span]))vec![args_span, body_span]), rhs)
181            }
182            MacroRule::Derive { body_span, ref rhs, .. } => (MultiSpan::from_span(body_span), rhs),
183        };
184        if has_compile_error_macro(rhs) { None } else { Some((&self.name, span)) }
185    }
186
187    pub fn kinds(&self) -> MacroKinds {
188        self.kinds
189    }
190
191    pub fn expand_derive(
192        &self,
193        cx: &mut ExtCtxt<'_>,
194        sp: Span,
195        body: &TokenStream,
196    ) -> Result<TokenStream, ErrorGuaranteed> {
197        // This is similar to `expand_macro`, but they have very different signatures, and will
198        // diverge further once derives support arguments.
199        let name = self.name;
200        let rules = &self.rules;
201        let psess = &cx.sess.psess;
202
203        if cx.trace_macros() {
204            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expanding `#[derive({1})] {0}`",
                pprust::tts_to_string(body), name))
    })format!("expanding `#[derive({name})] {}`", pprust::tts_to_string(body));
205            trace_macros_note(&mut cx.expansions, sp, msg);
206        }
207
208        match try_match_macro_derive(psess, name, body, rules, &mut NoopTracker) {
209            Ok((rule_index, rule, named_matches)) => {
210                let MacroRule::Derive { rhs, .. } = rule else {
211                    {
    ::core::panicking::panic_fmt(format_args!("try_match_macro_derive returned non-derive rule"));
};panic!("try_match_macro_derive returned non-derive rule");
212                };
213                let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
214                    cx.dcx().span_bug(sp, "malformed macro derive rhs");
215                };
216
217                let id = cx.current_expansion.id;
218                let tts = transcribe(psess, &named_matches, rhs, *rhs_span, self.transparency, id)
219                    .map_err(|e| e.emit())?;
220
221                if cx.trace_macros() {
222                    let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("to `{0}`",
                pprust::tts_to_string(&tts)))
    })format!("to `{}`", pprust::tts_to_string(&tts));
223                    trace_macros_note(&mut cx.expansions, sp, msg);
224                }
225
226                if is_defined_in_current_crate(self.node_id) {
227                    cx.resolver.record_macro_rule_usage(self.node_id, rule_index);
228                }
229
230                Ok(tts)
231            }
232            Err(CanRetry::No(guar)) => Err(guar),
233            Err(CanRetry::Yes) => {
234                let (_, guar) = failed_to_match_macro(
235                    cx.psess(),
236                    sp,
237                    self.span,
238                    name,
239                    FailedMacro::Derive,
240                    body,
241                    rules,
242                    self.on_unmatch_args.as_ref(),
243                );
244                cx.macro_error_and_trace_macros_diag();
245                Err(guar)
246            }
247        }
248    }
249}
250
251impl TTMacroExpander for MacroRulesMacroExpander {
252    fn expand<'cx, 'a: 'cx>(
253        &'a self,
254        cx: &'cx mut ExtCtxt<'_>,
255        sp: Span,
256        input: TokenStream,
257    ) -> MacroExpanderResult<'cx> {
258        ExpandResult::Ready(expand_macro(
259            cx,
260            sp,
261            self.span,
262            self.node_id,
263            self.name,
264            self.transparency,
265            input,
266            &self.rules,
267            self.on_unmatch_args.as_ref(),
268        ))
269    }
270}
271
272impl AttrProcMacro for MacroRulesMacroExpander {
273    fn expand(
274        &self,
275        _cx: &mut ExtCtxt<'_>,
276        _sp: Span,
277        _args: TokenStream,
278        _body: TokenStream,
279    ) -> Result<TokenStream, ErrorGuaranteed> {
280        {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("`expand` called on `MacroRulesMacroExpander`, expected `expand_with_safety`")));
}unreachable!("`expand` called on `MacroRulesMacroExpander`, expected `expand_with_safety`")
281    }
282
283    fn expand_with_safety(
284        &self,
285        cx: &mut ExtCtxt<'_>,
286        safety: Safety,
287        sp: Span,
288        args: TokenStream,
289        body: TokenStream,
290    ) -> Result<TokenStream, ErrorGuaranteed> {
291        expand_macro_attr(
292            cx,
293            sp,
294            self.span,
295            self.node_id,
296            self.name,
297            self.transparency,
298            safety,
299            args,
300            body,
301            &self.rules,
302            self.on_unmatch_args.as_ref(),
303        )
304    }
305}
306
307struct DummyBang(ErrorGuaranteed);
308
309impl BangProcMacro for DummyBang {
310    fn expand<'cx>(
311        &self,
312        _: &'cx mut ExtCtxt<'_>,
313        _: Span,
314        _: TokenStream,
315    ) -> Result<TokenStream, ErrorGuaranteed> {
316        Err(self.0)
317    }
318}
319
320fn trace_macros_note(cx_expansions: &mut FxIndexMap<Span, Vec<String>>, sp: Span, message: String) {
321    let sp = sp.macro_backtrace().last().map_or(sp, |trace| trace.call_site);
322    cx_expansions.entry(sp).or_default().push(message);
323}
324
325pub(super) trait Tracker<'matcher> {
326    /// The contents of `ParseResult::Failure`.
327    type Failure;
328
329    /// Arm failed to match. If the token is `token::Eof`, it indicates an unexpected
330    /// end of macro invocation. Otherwise, it indicates that no rules expected the given token.
331    /// The usize is the approximate position of the token in the input token stream.
332    fn build_failure(tok: Token, position: u32, msg: &'static str) -> Self::Failure;
333
334    /// This is called before trying to match next MatcherLoc on the current token.
335    fn before_match_loc(&mut self, _parser: &TtParser, _matcher: &'matcher MatcherLoc) {}
336
337    /// This is called after an arm has been parsed, either successfully or unsuccessfully. When
338    /// this is called, `before_match_loc` was called at least once (with a `MatcherLoc::Eof`).
339    fn after_arm(&mut self, _in_body: bool, _result: &NamedParseResult<Self::Failure>) {}
340
341    /// For tracing.
342    fn description() -> &'static str;
343
344    fn recovery() -> Recovery {
345        Recovery::Forbidden
346    }
347}
348
349/// A noop tracker that is used in the hot path of the expansion, has zero overhead thanks to
350/// monomorphization.
351pub(super) struct NoopTracker;
352
353impl<'matcher> Tracker<'matcher> for NoopTracker {
354    type Failure = ();
355
356    fn build_failure(_tok: Token, _position: u32, _msg: &'static str) -> Self::Failure {}
357
358    fn description() -> &'static str {
359        "none"
360    }
361}
362
363/// Expands the rules based macro defined by `rules` for a given input `arg`.
364#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
                ::tracing::Level::INFO <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("expand_macro",
                                    "rustc_expand::mbe::macro_rules", ::tracing::Level::INFO,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                    ::tracing_core::__macro_support::Option::Some(364u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                    ::tracing_core::field::FieldSet::new(&["sp", "def_span",
                                                    "node_id", "name"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::INFO <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::INFO <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&sp)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_span)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&node_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Box<dyn MacResult + 'cx> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let psess = &cx.sess.psess;
            if cx.trace_macros() {
                let msg =
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("expanding `{0}! {{ {1} }}`",
                                    name, pprust::tts_to_string(&arg)))
                        });
                trace_macros_note(&mut cx.expansions, sp, msg);
            }
            let try_success_result =
                try_match_macro(psess, name, &arg, rules, &mut NoopTracker);
            match try_success_result {
                Ok((rule_index, rule, named_matches)) => {
                    let MacroRule::Func { lhs, rhs, .. } =
                        rule else {
                            {
                                ::core::panicking::panic_fmt(format_args!("try_match_macro returned non-func rule"));
                            };
                        };
                    let mbe::TokenTree::Delimited(rhs_span, _, rhs) =
                        rhs else { cx.dcx().span_bug(sp, "malformed macro rhs"); };
                    let arm_span = rhs_span.entire();
                    let id = cx.current_expansion.id;
                    let tts =
                        match transcribe(psess, &named_matches, rhs, *rhs_span,
                                transparency, id) {
                            Ok(tts) => tts,
                            Err(err) => {
                                let guar = err.emit();
                                return DummyResult::any(arm_span, guar);
                            }
                        };
                    if cx.trace_macros() {
                        let msg =
                            ::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("to `{0}`",
                                            pprust::tts_to_string(&tts)))
                                });
                        trace_macros_note(&mut cx.expansions, sp, msg);
                    }
                    let is_local = is_defined_in_current_crate(node_id);
                    if is_local {
                        cx.resolver.record_macro_rule_usage(node_id, rule_index);
                    }
                    Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span,
                            is_local, name, rules, lhs))
                }
                Err(CanRetry::No(guar)) => {
                    {
                        use ::tracing::__macro_support::Callsite as _;
                        static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                            {
                                static META: ::tracing::Metadata<'static> =
                                    {
                                        ::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:420",
                                            "rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                            ::tracing_core::__macro_support::Option::Some(420u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                            ::tracing_core::field::FieldSet::new(&["message"],
                                                ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                            ::tracing::metadata::Kind::EVENT)
                                    };
                                ::tracing::callsite::DefaultCallsite::new(&META)
                            };
                        let enabled =
                            ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                    ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::LevelFilter::current() &&
                                {
                                    let interest = __CALLSITE.interest();
                                    !interest.is_never() &&
                                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                            interest)
                                };
                        if enabled {
                            (|value_set: ::tracing::field::ValueSet|
                                        {
                                            let meta = __CALLSITE.metadata();
                                            ::tracing::Event::dispatch(meta, &value_set);
                                            ;
                                        })({
                                    #[allow(unused_imports)]
                                    use ::tracing::field::{debug, display, Value};
                                    let mut iter = __CALLSITE.metadata().fields().iter();
                                    __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                        ::tracing::__macro_support::Option::Some(&format_args!("Will not retry matching as an error was emitted already")
                                                                as &dyn Value))])
                                });
                        } else { ; }
                    };
                    DummyResult::any(sp, guar)
                }
                Err(CanRetry::Yes) => {
                    let (span, guar) =
                        failed_to_match_macro(cx.psess(), sp, def_span, name,
                            FailedMacro::Func, &arg, rules, on_unmatch_args);
                    cx.macro_error_and_trace_macros_diag();
                    DummyResult::any(span, guar)
                }
            }
        }
    }
}#[instrument(skip(cx, transparency, arg, rules, on_unmatch_args))]
365fn expand_macro<'cx, 'a: 'cx>(
366    cx: &'cx mut ExtCtxt<'_>,
367    sp: Span,
368    def_span: Span,
369    node_id: NodeId,
370    name: Ident,
371    transparency: Transparency,
372    arg: TokenStream,
373    rules: &'a [MacroRule],
374    on_unmatch_args: Option<&Directive>,
375) -> Box<dyn MacResult + 'cx> {
376    let psess = &cx.sess.psess;
377
378    if cx.trace_macros() {
379        let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(&arg));
380        trace_macros_note(&mut cx.expansions, sp, msg);
381    }
382
383    // Track nothing for the best performance.
384    let try_success_result = try_match_macro(psess, name, &arg, rules, &mut NoopTracker);
385
386    match try_success_result {
387        Ok((rule_index, rule, named_matches)) => {
388            let MacroRule::Func { lhs, rhs, .. } = rule else {
389                panic!("try_match_macro returned non-func rule");
390            };
391            let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
392                cx.dcx().span_bug(sp, "malformed macro rhs");
393            };
394            let arm_span = rhs_span.entire();
395
396            // rhs has holes ( `$id` and `$(...)` that need filled)
397            let id = cx.current_expansion.id;
398            let tts = match transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id) {
399                Ok(tts) => tts,
400                Err(err) => {
401                    let guar = err.emit();
402                    return DummyResult::any(arm_span, guar);
403                }
404            };
405
406            if cx.trace_macros() {
407                let msg = format!("to `{}`", pprust::tts_to_string(&tts));
408                trace_macros_note(&mut cx.expansions, sp, msg);
409            }
410
411            let is_local = is_defined_in_current_crate(node_id);
412            if is_local {
413                cx.resolver.record_macro_rule_usage(node_id, rule_index);
414            }
415
416            // Let the context choose how to interpret the result. Weird, but useful for X-macros.
417            Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name, rules, lhs))
418        }
419        Err(CanRetry::No(guar)) => {
420            debug!("Will not retry matching as an error was emitted already");
421            DummyResult::any(sp, guar)
422        }
423        Err(CanRetry::Yes) => {
424            // Retry and emit a better error.
425            let (span, guar) = failed_to_match_macro(
426                cx.psess(),
427                sp,
428                def_span,
429                name,
430                FailedMacro::Func,
431                &arg,
432                rules,
433                on_unmatch_args,
434            );
435            cx.macro_error_and_trace_macros_diag();
436            DummyResult::any(span, guar)
437        }
438    }
439}
440
441/// Expands the rules based macro defined by `rules` for a given attribute `args` and `body`.
442#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
                ::tracing::Level::INFO <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("expand_macro_attr",
                                    "rustc_expand::mbe::macro_rules", ::tracing::Level::INFO,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                    ::tracing_core::__macro_support::Option::Some(442u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                    ::tracing_core::field::FieldSet::new(&["sp", "def_span",
                                                    "node_id", "name", "safety"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::INFO <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::INFO <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&sp)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_span)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&node_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&safety)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<TokenStream, ErrorGuaranteed> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let psess = &cx.sess.psess;
            let is_local = node_id != DUMMY_NODE_ID;
            if cx.trace_macros() {
                let msg =
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("expanding `#[{2}({0})] {1}`",
                                    pprust::tts_to_string(&args), pprust::tts_to_string(&body),
                                    name))
                        });
                trace_macros_note(&mut cx.expansions, sp, msg);
            }
            match try_match_macro_attr(psess, name, &args, &body, rules,
                    &mut NoopTracker) {
                Ok((i, rule, named_matches)) => {
                    let MacroRule::Attr { rhs, unsafe_rule, .. } =
                        rule else {
                            {
                                ::core::panicking::panic_fmt(format_args!("try_macro_match_attr returned non-attr rule"));
                            };
                        };
                    let mbe::TokenTree::Delimited(rhs_span, _, rhs) =
                        rhs else { cx.dcx().span_bug(sp, "malformed macro rhs"); };
                    match (safety, unsafe_rule) {
                        (Safety::Default, false) | (Safety::Unsafe(_), true) => {}
                        (Safety::Default, true) => {
                            cx.dcx().span_err(sp,
                                "unsafe attribute invocation requires `unsafe`");
                        }
                        (Safety::Unsafe(span), false) => {
                            cx.dcx().span_err(span,
                                "unnecessary `unsafe` on safe attribute invocation");
                        }
                        (Safety::Safe(span), _) => {
                            cx.dcx().span_bug(span, "unexpected `safe` keyword");
                        }
                    }
                    let id = cx.current_expansion.id;
                    let tts =
                        transcribe(psess, &named_matches, rhs, *rhs_span,
                                    transparency, id).map_err(|e| e.emit())?;
                    if cx.trace_macros() {
                        let msg =
                            ::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("to `{0}`",
                                            pprust::tts_to_string(&tts)))
                                });
                        trace_macros_note(&mut cx.expansions, sp, msg);
                    }
                    if is_local {
                        cx.resolver.record_macro_rule_usage(node_id, i);
                    }
                    Ok(tts)
                }
                Err(CanRetry::No(guar)) => Err(guar),
                Err(CanRetry::Yes) => {
                    let (_, guar) =
                        failed_to_match_macro(cx.psess(), sp, def_span, name,
                            FailedMacro::Attr(&args), &body, rules, on_unmatch_args);
                    cx.trace_macros_diag();
                    Err(guar)
                }
            }
        }
    }
}#[instrument(skip(cx, transparency, args, body, rules, on_unmatch_args))]
443fn expand_macro_attr(
444    cx: &mut ExtCtxt<'_>,
445    sp: Span,
446    def_span: Span,
447    node_id: NodeId,
448    name: Ident,
449    transparency: Transparency,
450    safety: Safety,
451    args: TokenStream,
452    body: TokenStream,
453    rules: &[MacroRule],
454    on_unmatch_args: Option<&Directive>,
455) -> Result<TokenStream, ErrorGuaranteed> {
456    let psess = &cx.sess.psess;
457    // Macros defined in the current crate have a real node id,
458    // whereas macros from an external crate have a dummy id.
459    let is_local = node_id != DUMMY_NODE_ID;
460
461    if cx.trace_macros() {
462        let msg = format!(
463            "expanding `#[{name}({})] {}`",
464            pprust::tts_to_string(&args),
465            pprust::tts_to_string(&body),
466        );
467        trace_macros_note(&mut cx.expansions, sp, msg);
468    }
469
470    // Track nothing for the best performance.
471    match try_match_macro_attr(psess, name, &args, &body, rules, &mut NoopTracker) {
472        Ok((i, rule, named_matches)) => {
473            let MacroRule::Attr { rhs, unsafe_rule, .. } = rule else {
474                panic!("try_macro_match_attr returned non-attr rule");
475            };
476            let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
477                cx.dcx().span_bug(sp, "malformed macro rhs");
478            };
479
480            match (safety, unsafe_rule) {
481                (Safety::Default, false) | (Safety::Unsafe(_), true) => {}
482                (Safety::Default, true) => {
483                    cx.dcx().span_err(sp, "unsafe attribute invocation requires `unsafe`");
484                }
485                (Safety::Unsafe(span), false) => {
486                    cx.dcx().span_err(span, "unnecessary `unsafe` on safe attribute invocation");
487                }
488                (Safety::Safe(span), _) => {
489                    cx.dcx().span_bug(span, "unexpected `safe` keyword");
490                }
491            }
492
493            let id = cx.current_expansion.id;
494            let tts = transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id)
495                .map_err(|e| e.emit())?;
496
497            if cx.trace_macros() {
498                let msg = format!("to `{}`", pprust::tts_to_string(&tts));
499                trace_macros_note(&mut cx.expansions, sp, msg);
500            }
501
502            if is_local {
503                cx.resolver.record_macro_rule_usage(node_id, i);
504            }
505
506            Ok(tts)
507        }
508        Err(CanRetry::No(guar)) => Err(guar),
509        Err(CanRetry::Yes) => {
510            // Retry and emit a better error.
511            let (_, guar) = failed_to_match_macro(
512                cx.psess(),
513                sp,
514                def_span,
515                name,
516                FailedMacro::Attr(&args),
517                &body,
518                rules,
519                on_unmatch_args,
520            );
521            cx.trace_macros_diag();
522            Err(guar)
523        }
524    }
525}
526
527pub(super) enum CanRetry {
528    Yes,
529    /// We are not allowed to retry macro expansion as a fatal error has been emitted already.
530    No(ErrorGuaranteed),
531}
532
533/// Try expanding the macro. Returns the index of the successful arm and its named_matches if it was successful,
534/// and nothing if it failed. On failure, it's the callers job to use `track` accordingly to record all errors
535/// correctly.
536#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("try_match_macro",
                                    "rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                    ::tracing_core::__macro_support::Option::Some(536u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                    ::tracing_core::field::FieldSet::new(&["name", "tracking"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&display(&T::description())
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<(usize, &'matcher MacroRule, NamedMatches),
                    CanRetry> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let parser = parser_from_cx(psess, arg.clone(), T::recovery());
            let mut tt_parser = TtParser::new(name);
            for (i, rule) in rules.iter().enumerate() {
                let MacroRule::Func { lhs, .. } = rule else { continue };
                let _tracing_span =
                    {
                        use ::tracing::__macro_support::Callsite as _;
                        static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                            {
                                static META: ::tracing::Metadata<'static> =
                                    {
                                        ::tracing_core::metadata::Metadata::new("Matching arm",
                                            "rustc_expand::mbe::macro_rules", ::tracing::Level::TRACE,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                            ::tracing_core::__macro_support::Option::Some(568u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                            ::tracing_core::field::FieldSet::new(&["i"],
                                                ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                            ::tracing::metadata::Kind::SPAN)
                                    };
                                ::tracing::callsite::DefaultCallsite::new(&META)
                            };
                        let mut interest = ::tracing::subscriber::Interest::never();
                        if ::tracing::Level::TRACE <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::TRACE <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    { interest = __CALLSITE.interest(); !interest.is_never() }
                                &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest) {
                            let meta = __CALLSITE.metadata();
                            ::tracing::Span::new(meta,
                                &{
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = meta.fields().iter();
                                        meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&display(&i) as
                                                                    &dyn Value))])
                                    })
                        } else {
                            let span =
                                ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                            {};
                            span
                        }
                    };
                let mut gated_spans_snapshot =
                    mem::take(&mut *psess.gated_spans.spans.borrow_mut());
                let result =
                    tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs, track);
                track.after_arm(true, &result);
                match result {
                    Success(named_matches) => {
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:582",
                                                "rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                                ::tracing_core::__macro_support::Option::Some(582u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                                ::tracing_core::field::FieldSet::new(&["message"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&format_args!("Parsed arm successfully")
                                                                    as &dyn Value))])
                                    });
                            } else { ; }
                        };
                        psess.gated_spans.merge(gated_spans_snapshot);
                        return Ok((i, rule, named_matches));
                    }
                    Failure(_) => {
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:590",
                                                "rustc_expand::mbe::macro_rules", ::tracing::Level::TRACE,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                                ::tracing_core::__macro_support::Option::Some(590u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                                ::tracing_core::field::FieldSet::new(&["message"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::TRACE <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::TRACE <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&format_args!("Failed to match arm, trying the next one")
                                                                    as &dyn Value))])
                                    });
                            } else { ; }
                        };
                    }
                    Error(_, _) => {
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:594",
                                                "rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                                ::tracing_core::__macro_support::Option::Some(594u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                                ::tracing_core::field::FieldSet::new(&["message"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&format_args!("Fatal error occurred during matching")
                                                                    as &dyn Value))])
                                    });
                            } else { ; }
                        };
                        return Err(CanRetry::Yes);
                    }
                    ErrorReported(guarantee) => {
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:599",
                                                "rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                                ::tracing_core::__macro_support::Option::Some(599u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                                ::tracing_core::field::FieldSet::new(&["message"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&format_args!("Fatal error occurred and was reported during matching")
                                                                    as &dyn Value))])
                                    });
                            } else { ; }
                        };
                        return Err(CanRetry::No(guarantee));
                    }
                }
                mem::swap(&mut gated_spans_snapshot,
                    &mut psess.gated_spans.spans.borrow_mut());
            }
            Err(CanRetry::Yes)
        }
    }
}#[instrument(level = "debug", skip(psess, arg, rules, track), fields(tracking = %T::description()))]
537pub(super) fn try_match_macro<'matcher, T: Tracker<'matcher>>(
538    psess: &ParseSess,
539    name: Ident,
540    arg: &TokenStream,
541    rules: &'matcher [MacroRule],
542    track: &mut T,
543) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
544    // We create a base parser that can be used for the "black box" parts.
545    // Every iteration needs a fresh copy of that parser. However, the parser
546    // is not mutated on many of the iterations, particularly when dealing with
547    // macros like this:
548    //
549    // macro_rules! foo {
550    //     ("a") => (A);
551    //     ("b") => (B);
552    //     ("c") => (C);
553    //     // ... etc. (maybe hundreds more)
554    // }
555    //
556    // as seen in the `html5ever` benchmark. We use a `Cow` so that the base
557    // parser is only cloned when necessary (upon mutation). Furthermore, we
558    // reinitialize the `Cow` with the base parser at the start of every
559    // iteration, so that any mutated parsers are not reused. This is all quite
560    // hacky, but speeds up the `html5ever` benchmark significantly. (Issue
561    // 68836 suggests a more comprehensive but more complex change to deal with
562    // this situation.)
563    let parser = parser_from_cx(psess, arg.clone(), T::recovery());
564    // Try each arm's matchers.
565    let mut tt_parser = TtParser::new(name);
566    for (i, rule) in rules.iter().enumerate() {
567        let MacroRule::Func { lhs, .. } = rule else { continue };
568        let _tracing_span = trace_span!("Matching arm", %i);
569
570        // Take a snapshot of the state of pre-expansion gating at this point.
571        // This is used so that if a matcher is not `Success(..)`ful,
572        // then the spans which became gated when parsing the unsuccessful matcher
573        // are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
574        let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
575
576        let result = tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs, track);
577
578        track.after_arm(true, &result);
579
580        match result {
581            Success(named_matches) => {
582                debug!("Parsed arm successfully");
583                // The matcher was `Success(..)`ful.
584                // Merge the gated spans from parsing the matcher with the preexisting ones.
585                psess.gated_spans.merge(gated_spans_snapshot);
586
587                return Ok((i, rule, named_matches));
588            }
589            Failure(_) => {
590                trace!("Failed to match arm, trying the next one");
591                // Try the next arm.
592            }
593            Error(_, _) => {
594                debug!("Fatal error occurred during matching");
595                // We haven't emitted an error yet, so we can retry.
596                return Err(CanRetry::Yes);
597            }
598            ErrorReported(guarantee) => {
599                debug!("Fatal error occurred and was reported during matching");
600                // An error has been reported already, we cannot retry as that would cause duplicate errors.
601                return Err(CanRetry::No(guarantee));
602            }
603        }
604
605        // The matcher was not `Success(..)`ful.
606        // Restore to the state before snapshotting and maybe try again.
607        mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
608    }
609
610    Err(CanRetry::Yes)
611}
612
613/// Try expanding the macro attribute. Returns the index of the successful arm and its
614/// named_matches if it was successful, and nothing if it failed. On failure, it's the caller's job
615/// to use `track` accordingly to record all errors correctly.
616#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("try_match_macro_attr",
                                    "rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                    ::tracing_core::__macro_support::Option::Some(616u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                    ::tracing_core::field::FieldSet::new(&["name", "tracking"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&display(&T::description())
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<(usize, &'matcher MacroRule, NamedMatches),
                    CanRetry> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let args_parser =
                parser_from_cx(psess, attr_args.clone(), T::recovery());
            let body_parser =
                parser_from_cx(psess, attr_body.clone(), T::recovery());
            let mut tt_parser = TtParser::new(name);
            for (i, rule) in rules.iter().enumerate() {
                let MacroRule::Attr { args, body, .. } =
                    rule else { continue };
                let mut gated_spans_snapshot =
                    mem::take(&mut *psess.gated_spans.spans.borrow_mut());
                let result =
                    tt_parser.parse_tt(&mut Cow::Borrowed(&args_parser), args,
                        track);
                track.after_arm(false, &result);
                let mut named_matches =
                    match result {
                        Success(named_matches) => named_matches,
                        Failure(_) => {
                            mem::swap(&mut gated_spans_snapshot,
                                &mut psess.gated_spans.spans.borrow_mut());
                            continue;
                        }
                        Error(_, _) => return Err(CanRetry::Yes),
                        ErrorReported(guar) => return Err(CanRetry::No(guar)),
                    };
                let result =
                    tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body,
                        track);
                track.after_arm(true, &result);
                match result {
                    Success(body_named_matches) => {
                        psess.gated_spans.merge(gated_spans_snapshot);

                        #[allow(rustc::potential_query_instability)]
                        named_matches.extend(body_named_matches);
                        return Ok((i, rule, named_matches));
                    }
                    Failure(_) => {
                        mem::swap(&mut gated_spans_snapshot,
                            &mut psess.gated_spans.spans.borrow_mut())
                    }
                    Error(_, _) => return Err(CanRetry::Yes),
                    ErrorReported(guar) => return Err(CanRetry::No(guar)),
                }
            }
            Err(CanRetry::Yes)
        }
    }
}#[instrument(level = "debug", skip(psess, attr_args, attr_body, rules, track), fields(tracking = %T::description()))]
617pub(super) fn try_match_macro_attr<'matcher, T: Tracker<'matcher>>(
618    psess: &ParseSess,
619    name: Ident,
620    attr_args: &TokenStream,
621    attr_body: &TokenStream,
622    rules: &'matcher [MacroRule],
623    track: &mut T,
624) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
625    // This uses the same strategy as `try_match_macro`
626    let args_parser = parser_from_cx(psess, attr_args.clone(), T::recovery());
627    let body_parser = parser_from_cx(psess, attr_body.clone(), T::recovery());
628    let mut tt_parser = TtParser::new(name);
629    for (i, rule) in rules.iter().enumerate() {
630        let MacroRule::Attr { args, body, .. } = rule else { continue };
631
632        let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
633
634        let result = tt_parser.parse_tt(&mut Cow::Borrowed(&args_parser), args, track);
635        track.after_arm(false, &result);
636
637        let mut named_matches = match result {
638            Success(named_matches) => named_matches,
639            Failure(_) => {
640                mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
641                continue;
642            }
643            Error(_, _) => return Err(CanRetry::Yes),
644            ErrorReported(guar) => return Err(CanRetry::No(guar)),
645        };
646
647        let result = tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body, track);
648        track.after_arm(true, &result);
649
650        match result {
651            Success(body_named_matches) => {
652                psess.gated_spans.merge(gated_spans_snapshot);
653                #[allow(rustc::potential_query_instability)]
654                named_matches.extend(body_named_matches);
655                return Ok((i, rule, named_matches));
656            }
657            Failure(_) => {
658                mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut())
659            }
660            Error(_, _) => return Err(CanRetry::Yes),
661            ErrorReported(guar) => return Err(CanRetry::No(guar)),
662        }
663    }
664
665    Err(CanRetry::Yes)
666}
667
668/// Try expanding the macro derive. Returns the index of the successful arm and its
669/// named_matches if it was successful, and nothing if it failed. On failure, it's the caller's job
670/// to use `track` accordingly to record all errors correctly.
671#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("try_match_macro_derive",
                                    "rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
                                    ::tracing_core::__macro_support::Option::Some(671u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
                                    ::tracing_core::field::FieldSet::new(&["name", "tracking"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&display(&T::description())
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<(usize, &'matcher MacroRule, NamedMatches),
                    CanRetry> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let body_parser =
                parser_from_cx(psess, body.clone(), T::recovery());
            let mut tt_parser = TtParser::new(name);
            for (i, rule) in rules.iter().enumerate() {
                let MacroRule::Derive { body, .. } = rule else { continue };
                let mut gated_spans_snapshot =
                    mem::take(&mut *psess.gated_spans.spans.borrow_mut());
                let result =
                    tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body,
                        track);
                track.after_arm(true, &result);
                match result {
                    Success(named_matches) => {
                        psess.gated_spans.merge(gated_spans_snapshot);
                        return Ok((i, rule, named_matches));
                    }
                    Failure(_) => {
                        mem::swap(&mut gated_spans_snapshot,
                            &mut psess.gated_spans.spans.borrow_mut())
                    }
                    Error(_, _) => return Err(CanRetry::Yes),
                    ErrorReported(guar) => return Err(CanRetry::No(guar)),
                }
            }
            Err(CanRetry::Yes)
        }
    }
}#[instrument(level = "debug", skip(psess, body, rules, track), fields(tracking = %T::description()))]
672pub(super) fn try_match_macro_derive<'matcher, T: Tracker<'matcher>>(
673    psess: &ParseSess,
674    name: Ident,
675    body: &TokenStream,
676    rules: &'matcher [MacroRule],
677    track: &mut T,
678) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
679    // This uses the same strategy as `try_match_macro`
680    let body_parser = parser_from_cx(psess, body.clone(), T::recovery());
681    let mut tt_parser = TtParser::new(name);
682    for (i, rule) in rules.iter().enumerate() {
683        let MacroRule::Derive { body, .. } = rule else { continue };
684
685        let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
686
687        let result = tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body, track);
688        track.after_arm(true, &result);
689
690        match result {
691            Success(named_matches) => {
692                psess.gated_spans.merge(gated_spans_snapshot);
693                return Ok((i, rule, named_matches));
694            }
695            Failure(_) => {
696                mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut())
697            }
698            Error(_, _) => return Err(CanRetry::Yes),
699            ErrorReported(guar) => return Err(CanRetry::No(guar)),
700        }
701    }
702
703    Err(CanRetry::Yes)
704}
705
706/// Converts a macro item into a syntax extension.
707pub fn compile_declarative_macro(
708    sess: &Session,
709    features: &Features,
710    macro_def: &ast::MacroDef,
711    ident: Ident,
712    attrs: &[hir::Attribute],
713    span: Span,
714    node_id: NodeId,
715    edition: Edition,
716) -> (SyntaxExtension, usize) {
717    let mk_syn_ext = |kind| {
718        let is_local = is_defined_in_current_crate(node_id);
719        SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
720    };
721    let dummy_syn_ext =
722        |guar| (mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar)))), 0);
723
724    let macro_rules = macro_def.macro_rules;
725    let exp_sep = if macro_rules { ::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Semi,
    token_type: ::rustc_parse::parser::token_type::TokenType::Semi,
}exp!(Semi) } else { ::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: ::rustc_parse::parser::token_type::TokenType::Comma,
}exp!(Comma) };
726
727    let body = macro_def.body.tokens.clone();
728    let mut p = Parser::new(&sess.psess, body, rustc_parse::MACRO_ARGUMENTS);
729
730    // Don't abort iteration early, so that multiple errors can be reported. We only abort early on
731    // parse failures we can't recover from.
732    let mut guar = None;
733    let mut check_emission = |ret: Result<(), ErrorGuaranteed>| guar = guar.or(ret.err());
734
735    let mut kinds = MacroKinds::empty();
736    let mut rules = Vec::new();
737
738    while p.token != token::Eof {
739        let unsafe_rule = p.eat_keyword_noexpect(kw::Unsafe);
740        let unsafe_keyword_span = p.prev_token.span;
741        if unsafe_rule && let Some(guar) = check_no_eof(sess, &p, "expected `attr`") {
742            return dummy_syn_ext(guar);
743        }
744        let (args, is_derive) = if p.eat_keyword_noexpect(sym::attr) {
745            kinds |= MacroKinds::ATTR;
746            if !features.macro_attr() {
747                feature_err(sess, sym::macro_attr, span, "`macro_rules!` attributes are unstable")
748                    .emit();
749            }
750            if let Some(guar) = check_no_eof(sess, &p, "expected macro attr args") {
751                return dummy_syn_ext(guar);
752            }
753            let args = p.parse_token_tree();
754            check_args_parens(sess, sym::attr, &args);
755            let args = parse_one_tt(args, RulePart::Pattern, sess, node_id, features, edition);
756            check_emission(check_lhs(sess, features, node_id, &args));
757            if let Some(guar) = check_no_eof(sess, &p, "expected macro attr body") {
758                return dummy_syn_ext(guar);
759            }
760            (Some(args), false)
761        } else if p.eat_keyword_noexpect(sym::derive) {
762            kinds |= MacroKinds::DERIVE;
763            let derive_keyword_span = p.prev_token.span;
764            if !features.macro_derive() {
765                feature_err(sess, sym::macro_derive, span, "`macro_rules!` derives are unstable")
766                    .emit();
767            }
768            if unsafe_rule {
769                sess.dcx()
770                    .span_err(unsafe_keyword_span, "`unsafe` is only supported on `attr` rules");
771            }
772            if let Some(guar) = check_no_eof(sess, &p, "expected `()` after `derive`") {
773                return dummy_syn_ext(guar);
774            }
775            let args = p.parse_token_tree();
776            check_args_parens(sess, sym::derive, &args);
777            let args_empty_result = check_args_empty(sess, &args);
778            let args_not_empty = args_empty_result.is_err();
779            check_emission(args_empty_result);
780            if let Some(guar) = check_no_eof(sess, &p, "expected macro derive body") {
781                return dummy_syn_ext(guar);
782            }
783            // If the user has `=>` right after the `()`, they might have forgotten the empty
784            // parentheses.
785            if p.token == token::FatArrow {
786                let mut err = sess
787                    .dcx()
788                    .struct_span_err(p.token.span, "expected macro derive body, got `=>`");
789                if args_not_empty {
790                    err.span_label(derive_keyword_span, "need `()` after this `derive`");
791                }
792                return dummy_syn_ext(err.emit());
793            }
794            (None, true)
795        } else {
796            kinds |= MacroKinds::BANG;
797            if unsafe_rule {
798                sess.dcx()
799                    .span_err(unsafe_keyword_span, "`unsafe` is only supported on `attr` rules");
800            }
801            (None, false)
802        };
803        let lhs_tt = p.parse_token_tree();
804        let lhs_tt = parse_one_tt(lhs_tt, RulePart::Pattern, sess, node_id, features, edition);
805        check_emission(check_lhs(sess, features, node_id, &lhs_tt));
806        if let Err(e) = p.expect(::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::FatArrow,
    token_type: ::rustc_parse::parser::token_type::TokenType::FatArrow,
}exp!(FatArrow)) {
807            return dummy_syn_ext(e.emit());
808        }
809        if let Some(guar) = check_no_eof(sess, &p, "expected right-hand side of macro rule") {
810            return dummy_syn_ext(guar);
811        }
812        let rhs = p.parse_token_tree();
813        let rhs = parse_one_tt(rhs, RulePart::Body, sess, node_id, features, edition);
814        check_emission(check_rhs(sess, &rhs));
815        check_emission(check_meta_variables(&sess.psess, node_id, args.as_ref(), &lhs_tt, &rhs));
816        let lhs_span = lhs_tt.span();
817        // Convert the lhs into `MatcherLoc` form, which is better for doing the
818        // actual matching.
819        let mbe::TokenTree::Delimited(.., delimited) = lhs_tt else {
820            return dummy_syn_ext(guar.unwrap());
821        };
822        let lhs = mbe::macro_parser::compute_locs(&delimited.tts);
823        if let Some(args) = args {
824            let args_span = args.span();
825            let mbe::TokenTree::Delimited(.., delimited) = args else {
826                return dummy_syn_ext(guar.unwrap());
827            };
828            let args = mbe::macro_parser::compute_locs(&delimited.tts);
829            let body_span = lhs_span;
830            rules.push(MacroRule::Attr { unsafe_rule, args, args_span, body: lhs, body_span, rhs });
831        } else if is_derive {
832            rules.push(MacroRule::Derive { body: lhs, body_span: lhs_span, rhs });
833        } else {
834            rules.push(MacroRule::Func { lhs, lhs_span, rhs });
835        }
836        if p.token == token::Eof {
837            break;
838        }
839        if let Err(e) = p.expect(exp_sep) {
840            return dummy_syn_ext(e.emit());
841        }
842    }
843
844    if rules.is_empty() {
845        let guar = sess.dcx().span_err(span, "macros must contain at least one rule");
846        return dummy_syn_ext(guar);
847    }
848    if !!kinds.is_empty() {
    ::core::panicking::panic("assertion failed: !kinds.is_empty()")
};assert!(!kinds.is_empty());
849
850    let transparency = {
    'done:
        {
        for i in attrs {
            #[allow(unused_imports)]
            use rustc_hir::attrs::AttributeKind::*;
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(RustcMacroTransparency(x)) => {
                    break 'done Some(*x);
                }
                rustc_hir::Attribute::Unparsed(..) =>
                    {}
                    #[deny(unreachable_patterns)]
                    _ => {}
            }
        }
        None
    }
}find_attr!(attrs, RustcMacroTransparency(x) => *x)
851        .unwrap_or(Transparency::fallback(macro_rules));
852
853    if let Some(guar) = guar {
854        // To avoid warning noise, only consider the rules of this
855        // macro for the lint, if all rules are valid.
856        return dummy_syn_ext(guar);
857    }
858
859    // Return the number of rules for unused rule linting, if this is a local macro.
860    let nrules = if is_defined_in_current_crate(node_id) { rules.len() } else { 0 };
861
862    let on_unmatch_args = {
    'done:
        {
        for i in attrs {
            #[allow(unused_imports)]
            use rustc_hir::attrs::AttributeKind::*;
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(OnUnmatchArgs { directive, .. })
                    => {
                    break 'done Some(directive.clone());
                }
                rustc_hir::Attribute::Unparsed(..) =>
                    {}
                    #[deny(unreachable_patterns)]
                    _ => {}
            }
        }
        None
    }
}find_attr!(
863        attrs,
864        OnUnmatchArgs { directive, .. } => directive.clone()
865    )
866    .flatten()
867    .map(|directive| *directive);
868
869    let exp = MacroRulesMacroExpander {
870        name: ident,
871        kinds,
872        span,
873        node_id,
874        on_unmatch_args,
875        transparency,
876        rules,
877    };
878    (mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))), nrules)
879}
880
881fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {
882    if p.token == token::Eof {
883        let err_sp = p.token.span.shrink_to_hi();
884        let guar = sess
885            .dcx()
886            .struct_span_err(err_sp, "macro definition ended unexpectedly")
887            .with_span_label(err_sp, msg)
888            .emit();
889        return Some(guar);
890    }
891    None
892}
893
894fn check_args_parens(sess: &Session, rule_kw: Symbol, args: &tokenstream::TokenTree) {
895    // This does not handle the non-delimited case; that gets handled separately by `check_lhs`.
896    if let tokenstream::TokenTree::Delimited(dspan, _, delim, _) = args
897        && *delim != Delimiter::Parenthesis
898    {
899        sess.dcx().emit_err(errors::MacroArgsBadDelim {
900            span: dspan.entire(),
901            sugg: errors::MacroArgsBadDelimSugg { open: dspan.open, close: dspan.close },
902            rule_kw,
903        });
904    }
905}
906
907fn check_args_empty(sess: &Session, args: &tokenstream::TokenTree) -> Result<(), ErrorGuaranteed> {
908    match args {
909        tokenstream::TokenTree::Delimited(.., delimited) if delimited.is_empty() => Ok(()),
910        _ => {
911            let msg = "`derive` rules do not accept arguments; `derive` must be followed by `()`";
912            Err(sess.dcx().span_err(args.span(), msg))
913        }
914    }
915}
916
917fn check_lhs(
918    sess: &Session,
919    features: &Features,
920    node_id: NodeId,
921    lhs: &mbe::TokenTree,
922) -> Result<(), ErrorGuaranteed> {
923    let e1 = check_lhs_nt_follows(sess, features, node_id, lhs);
924    let e2 = check_lhs_no_empty_seq(sess, slice::from_ref(lhs));
925    e1.and(e2)
926}
927
928fn check_lhs_nt_follows(
929    sess: &Session,
930    features: &Features,
931    node_id: NodeId,
932    lhs: &mbe::TokenTree,
933) -> Result<(), ErrorGuaranteed> {
934    // lhs is going to be like TokenTree::Delimited(...), where the
935    // entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
936    if let mbe::TokenTree::Delimited(.., delimited) = lhs {
937        check_matcher(sess, features, node_id, &delimited.tts)
938    } else {
939        let msg = "invalid macro matcher; matchers must be contained in balanced delimiters";
940        Err(sess.dcx().span_err(lhs.span(), msg))
941    }
942}
943
944fn is_empty_token_tree(sess: &Session, seq: &mbe::SequenceRepetition) -> bool {
945    if seq.separator.is_some() {
946        false
947    } else {
948        let mut is_empty = true;
949        let mut iter = seq.tts.iter().peekable();
950        while let Some(tt) = iter.next() {
951            match tt {
952                mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. } => {}
953                mbe::TokenTree::Token(t @ Token { kind: DocComment(..), .. }) => {
954                    let mut now = t;
955                    while let Some(&mbe::TokenTree::Token(
956                        next @ Token { kind: DocComment(..), .. },
957                    )) = iter.peek()
958                    {
959                        now = next;
960                        iter.next();
961                    }
962                    let span = t.span.to(now.span);
963                    sess.dcx().span_note(span, "doc comments are ignored in matcher position");
964                }
965                mbe::TokenTree::Sequence(_, sub_seq)
966                    if (sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
967                        || sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne) => {}
968                _ => is_empty = false,
969            }
970        }
971        is_empty
972    }
973}
974
975/// Checks if a `vis` nonterminal fragment is unnecessarily wrapped in an optional repetition.
976///
977/// When a `vis` fragment (which can already be empty) is wrapped in `$(...)?`,
978/// this suggests removing the redundant repetition syntax since it provides no additional benefit.
979fn check_redundant_vis_repetition(
980    err: &mut Diag<'_>,
981    sess: &Session,
982    seq: &SequenceRepetition,
983    span: &DelimSpan,
984) {
985    if seq.kleene.op == KleeneOp::ZeroOrOne
986        && #[allow(non_exhaustive_omitted_patterns)] match seq.tts.first() {
    Some(mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. }) =>
        true,
    _ => false,
}matches!(
987            seq.tts.first(),
988            Some(mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. })
989        )
990    {
991        err.note("a `vis` fragment can already be empty");
992        err.multipart_suggestion(
993            "remove the `$(` and `)?`",
994            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(sess.source_map().span_extend_to_prev_char_before(span.open, '$',
                        true), "".to_string()),
                (span.close.with_hi(seq.kleene.span.hi()), "".to_string())]))vec![
995                (
996                    sess.source_map().span_extend_to_prev_char_before(span.open, '$', true),
997                    "".to_string(),
998                ),
999                (span.close.with_hi(seq.kleene.span.hi()), "".to_string()),
1000            ],
1001            Applicability::MaybeIncorrect,
1002        );
1003    }
1004}
1005
1006/// Checks that the lhs contains no repetition which could match an empty token
1007/// tree, because then the matcher would hang indefinitely.
1008fn check_lhs_no_empty_seq(sess: &Session, tts: &[mbe::TokenTree]) -> Result<(), ErrorGuaranteed> {
1009    use mbe::TokenTree;
1010    for tt in tts {
1011        match tt {
1012            TokenTree::Token(..)
1013            | TokenTree::MetaVar(..)
1014            | TokenTree::MetaVarDecl { .. }
1015            | TokenTree::MetaVarExpr(..) => (),
1016            TokenTree::Delimited(.., del) => check_lhs_no_empty_seq(sess, &del.tts)?,
1017            TokenTree::Sequence(span, seq) => {
1018                if is_empty_token_tree(sess, seq) {
1019                    let sp = span.entire();
1020                    let mut err =
1021                        sess.dcx().struct_span_err(sp, "repetition matches empty token tree");
1022                    check_redundant_vis_repetition(&mut err, sess, seq, span);
1023                    return Err(err.emit());
1024                }
1025                check_lhs_no_empty_seq(sess, &seq.tts)?
1026            }
1027        }
1028    }
1029
1030    Ok(())
1031}
1032
1033fn check_rhs(sess: &Session, rhs: &mbe::TokenTree) -> Result<(), ErrorGuaranteed> {
1034    match *rhs {
1035        mbe::TokenTree::Delimited(..) => Ok(()),
1036        _ => Err(sess.dcx().span_err(rhs.span(), "macro rhs must be delimited")),
1037    }
1038}
1039
1040fn check_matcher(
1041    sess: &Session,
1042    features: &Features,
1043    node_id: NodeId,
1044    matcher: &[mbe::TokenTree],
1045) -> Result<(), ErrorGuaranteed> {
1046    let first_sets = FirstSets::new(matcher);
1047    let empty_suffix = TokenSet::empty();
1048    check_matcher_core(sess, features, node_id, &first_sets, matcher, &empty_suffix)?;
1049    Ok(())
1050}
1051
1052fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool {
1053    match rhs {
1054        mbe::TokenTree::Delimited(.., d) => {
1055            let has_compile_error = d.tts.array_windows::<3>().any(|[ident, bang, args]| {
1056                if let mbe::TokenTree::Token(ident) = ident
1057                    && let TokenKind::Ident(ident, _) = ident.kind
1058                    && ident == sym::compile_error
1059                    && let mbe::TokenTree::Token(bang) = bang
1060                    && let TokenKind::Bang = bang.kind
1061                    && let mbe::TokenTree::Delimited(.., del) = args
1062                    && !del.delim.skip()
1063                {
1064                    true
1065                } else {
1066                    false
1067                }
1068            });
1069            if has_compile_error { true } else { d.tts.iter().any(has_compile_error_macro) }
1070        }
1071        _ => false,
1072    }
1073}
1074
1075// `The FirstSets` for a matcher is a mapping from subsequences in the
1076// matcher to the FIRST set for that subsequence.
1077//
1078// This mapping is partially precomputed via a backwards scan over the
1079// token trees of the matcher, which provides a mapping from each
1080// repetition sequence to its *first* set.
1081//
1082// (Hypothetically, sequences should be uniquely identifiable via their
1083// spans, though perhaps that is false, e.g., for macro-generated macros
1084// that do not try to inject artificial span information. My plan is
1085// to try to catch such cases ahead of time and not include them in
1086// the precomputed mapping.)
1087struct FirstSets<'tt> {
1088    // this maps each TokenTree::Sequence `$(tt ...) SEP OP` that is uniquely identified by its
1089    // span in the original matcher to the First set for the inner sequence `tt ...`.
1090    //
1091    // If two sequences have the same span in a matcher, then map that
1092    // span to None (invalidating the mapping here and forcing the code to
1093    // use a slow path).
1094    first: FxHashMap<Span, Option<TokenSet<'tt>>>,
1095}
1096
1097impl<'tt> FirstSets<'tt> {
1098    fn new(tts: &'tt [mbe::TokenTree]) -> FirstSets<'tt> {
1099        use mbe::TokenTree;
1100
1101        let mut sets = FirstSets { first: FxHashMap::default() };
1102        build_recur(&mut sets, tts);
1103        return sets;
1104
1105        // walks backward over `tts`, returning the FIRST for `tts`
1106        // and updating `sets` at the same time for all sequence
1107        // substructure we find within `tts`.
1108        fn build_recur<'tt>(sets: &mut FirstSets<'tt>, tts: &'tt [TokenTree]) -> TokenSet<'tt> {
1109            let mut first = TokenSet::empty();
1110            for tt in tts.iter().rev() {
1111                match tt {
1112                    TokenTree::Token(..)
1113                    | TokenTree::MetaVar(..)
1114                    | TokenTree::MetaVarDecl { .. }
1115                    | TokenTree::MetaVarExpr(..) => {
1116                        first.replace_with(TtHandle::TtRef(tt));
1117                    }
1118                    TokenTree::Delimited(span, _, delimited) => {
1119                        build_recur(sets, &delimited.tts);
1120                        first.replace_with(TtHandle::from_token_kind(
1121                            delimited.delim.as_open_token_kind(),
1122                            span.open,
1123                        ));
1124                    }
1125                    TokenTree::Sequence(sp, seq_rep) => {
1126                        let subfirst = build_recur(sets, &seq_rep.tts);
1127
1128                        match sets.first.entry(sp.entire()) {
1129                            Entry::Vacant(vac) => {
1130                                vac.insert(Some(subfirst.clone()));
1131                            }
1132                            Entry::Occupied(mut occ) => {
1133                                // if there is already an entry, then a span must have collided.
1134                                // This should not happen with typical macro_rules macros,
1135                                // but syntax extensions need not maintain distinct spans,
1136                                // so distinct syntax trees can be assigned the same span.
1137                                // In such a case, the map cannot be trusted; so mark this
1138                                // entry as unusable.
1139                                occ.insert(None);
1140                            }
1141                        }
1142
1143                        // If the sequence contents can be empty, then the first
1144                        // token could be the separator token itself.
1145
1146                        if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
1147                            first.add_one_maybe(TtHandle::from_token(*sep));
1148                        }
1149
1150                        // Reverse scan: Sequence comes before `first`.
1151                        if subfirst.maybe_empty
1152                            || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
1153                            || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
1154                        {
1155                            // If sequence is potentially empty, then
1156                            // union them (preserving first emptiness).
1157                            first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
1158                        } else {
1159                            // Otherwise, sequence guaranteed
1160                            // non-empty; replace first.
1161                            first = subfirst;
1162                        }
1163                    }
1164                }
1165            }
1166
1167            first
1168        }
1169    }
1170
1171    // walks forward over `tts` until all potential FIRST tokens are
1172    // identified.
1173    fn first(&self, tts: &'tt [mbe::TokenTree]) -> TokenSet<'tt> {
1174        use mbe::TokenTree;
1175
1176        let mut first = TokenSet::empty();
1177        for tt in tts.iter() {
1178            if !first.maybe_empty {
    ::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1179            match tt {
1180                TokenTree::Token(..)
1181                | TokenTree::MetaVar(..)
1182                | TokenTree::MetaVarDecl { .. }
1183                | TokenTree::MetaVarExpr(..) => {
1184                    first.add_one(TtHandle::TtRef(tt));
1185                    return first;
1186                }
1187                TokenTree::Delimited(span, _, delimited) => {
1188                    first.add_one(TtHandle::from_token_kind(
1189                        delimited.delim.as_open_token_kind(),
1190                        span.open,
1191                    ));
1192                    return first;
1193                }
1194                TokenTree::Sequence(sp, seq_rep) => {
1195                    let subfirst_owned;
1196                    let subfirst = match self.first.get(&sp.entire()) {
1197                        Some(Some(subfirst)) => subfirst,
1198                        Some(&None) => {
1199                            subfirst_owned = self.first(&seq_rep.tts);
1200                            &subfirst_owned
1201                        }
1202                        None => {
1203                            {
    ::core::panicking::panic_fmt(format_args!("We missed a sequence during FirstSets construction"));
};panic!("We missed a sequence during FirstSets construction");
1204                        }
1205                    };
1206
1207                    // If the sequence contents can be empty, then the first
1208                    // token could be the separator token itself.
1209                    if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
1210                        first.add_one_maybe(TtHandle::from_token(*sep));
1211                    }
1212
1213                    if !first.maybe_empty {
    ::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1214                    first.add_all(subfirst);
1215                    if subfirst.maybe_empty
1216                        || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
1217                        || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
1218                    {
1219                        // Continue scanning for more first
1220                        // tokens, but also make sure we
1221                        // restore empty-tracking state.
1222                        first.maybe_empty = true;
1223                        continue;
1224                    } else {
1225                        return first;
1226                    }
1227                }
1228            }
1229        }
1230
1231        // we only exit the loop if `tts` was empty or if every
1232        // element of `tts` matches the empty sequence.
1233        if !first.maybe_empty {
    ::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1234        first
1235    }
1236}
1237
1238// Most `mbe::TokenTree`s are preexisting in the matcher, but some are defined
1239// implicitly, such as opening/closing delimiters and sequence repetition ops.
1240// This type encapsulates both kinds. It implements `Clone` while avoiding the
1241// need for `mbe::TokenTree` to implement `Clone`.
1242#[derive(#[automatically_derived]
impl<'tt> ::core::fmt::Debug for TtHandle<'tt> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            TtHandle::TtRef(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "TtRef",
                    &__self_0),
            TtHandle::Token(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Token",
                    &__self_0),
        }
    }
}Debug)]
1243enum TtHandle<'tt> {
1244    /// This is used in most cases.
1245    TtRef(&'tt mbe::TokenTree),
1246
1247    /// This is only used for implicit token trees. The `mbe::TokenTree` *must*
1248    /// be `mbe::TokenTree::Token`. No other variants are allowed. We store an
1249    /// `mbe::TokenTree` rather than a `Token` so that `get()` can return a
1250    /// `&mbe::TokenTree`.
1251    Token(mbe::TokenTree),
1252}
1253
1254impl<'tt> TtHandle<'tt> {
1255    fn from_token(tok: Token) -> Self {
1256        TtHandle::Token(mbe::TokenTree::Token(tok))
1257    }
1258
1259    fn from_token_kind(kind: TokenKind, span: Span) -> Self {
1260        TtHandle::from_token(Token::new(kind, span))
1261    }
1262
1263    // Get a reference to a token tree.
1264    fn get(&'tt self) -> &'tt mbe::TokenTree {
1265        match self {
1266            TtHandle::TtRef(tt) => tt,
1267            TtHandle::Token(token_tt) => token_tt,
1268        }
1269    }
1270}
1271
1272impl<'tt> PartialEq for TtHandle<'tt> {
1273    fn eq(&self, other: &TtHandle<'tt>) -> bool {
1274        self.get() == other.get()
1275    }
1276}
1277
1278impl<'tt> Clone for TtHandle<'tt> {
1279    fn clone(&self) -> Self {
1280        match self {
1281            TtHandle::TtRef(tt) => TtHandle::TtRef(tt),
1282
1283            // This variant *must* contain a `mbe::TokenTree::Token`, and not
1284            // any other variant of `mbe::TokenTree`.
1285            TtHandle::Token(mbe::TokenTree::Token(tok)) => {
1286                TtHandle::Token(mbe::TokenTree::Token(*tok))
1287            }
1288
1289            _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1290        }
1291    }
1292}
1293
1294// A set of `mbe::TokenTree`s, which may include `TokenTree::Match`s
1295// (for macro-by-example syntactic variables). It also carries the
1296// `maybe_empty` flag; that is true if and only if the matcher can
1297// match an empty token sequence.
1298//
1299// The First set is computed on submatchers like `$($a:expr b),* $(c)* d`,
1300// which has corresponding FIRST = {$a:expr, c, d}.
1301// Likewise, `$($a:expr b),* $(c)+ d` has FIRST = {$a:expr, c}.
1302//
1303// (Notably, we must allow for *-op to occur zero times.)
1304#[derive(#[automatically_derived]
impl<'tt> ::core::clone::Clone for TokenSet<'tt> {
    #[inline]
    fn clone(&self) -> TokenSet<'tt> {
        TokenSet {
            tokens: ::core::clone::Clone::clone(&self.tokens),
            maybe_empty: ::core::clone::Clone::clone(&self.maybe_empty),
        }
    }
}Clone, #[automatically_derived]
impl<'tt> ::core::fmt::Debug for TokenSet<'tt> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "TokenSet",
            "tokens", &self.tokens, "maybe_empty", &&self.maybe_empty)
    }
}Debug)]
1305struct TokenSet<'tt> {
1306    tokens: Vec<TtHandle<'tt>>,
1307    maybe_empty: bool,
1308}
1309
1310impl<'tt> TokenSet<'tt> {
1311    // Returns a set for the empty sequence.
1312    fn empty() -> Self {
1313        TokenSet { tokens: Vec::new(), maybe_empty: true }
1314    }
1315
1316    // Returns the set `{ tok }` for the single-token (and thus
1317    // non-empty) sequence [tok].
1318    fn singleton(tt: TtHandle<'tt>) -> Self {
1319        TokenSet { tokens: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [tt]))vec![tt], maybe_empty: false }
1320    }
1321
1322    // Changes self to be the set `{ tok }`.
1323    // Since `tok` is always present, marks self as non-empty.
1324    fn replace_with(&mut self, tt: TtHandle<'tt>) {
1325        self.tokens.clear();
1326        self.tokens.push(tt);
1327        self.maybe_empty = false;
1328    }
1329
1330    // Changes self to be the empty set `{}`; meant for use when
1331    // the particular token does not matter, but we want to
1332    // record that it occurs.
1333    fn replace_with_irrelevant(&mut self) {
1334        self.tokens.clear();
1335        self.maybe_empty = false;
1336    }
1337
1338    // Adds `tok` to the set for `self`, marking sequence as non-empty.
1339    fn add_one(&mut self, tt: TtHandle<'tt>) {
1340        if !self.tokens.contains(&tt) {
1341            self.tokens.push(tt);
1342        }
1343        self.maybe_empty = false;
1344    }
1345
1346    // Adds `tok` to the set for `self`. (Leaves `maybe_empty` flag alone.)
1347    fn add_one_maybe(&mut self, tt: TtHandle<'tt>) {
1348        if !self.tokens.contains(&tt) {
1349            self.tokens.push(tt);
1350        }
1351    }
1352
1353    // Adds all elements of `other` to this.
1354    //
1355    // (Since this is a set, we filter out duplicates.)
1356    //
1357    // If `other` is potentially empty, then preserves the previous
1358    // setting of the empty flag of `self`. If `other` is guaranteed
1359    // non-empty, then `self` is marked non-empty.
1360    fn add_all(&mut self, other: &Self) {
1361        for tt in &other.tokens {
1362            if !self.tokens.contains(tt) {
1363                self.tokens.push(tt.clone());
1364            }
1365        }
1366        if !other.maybe_empty {
1367            self.maybe_empty = false;
1368        }
1369    }
1370}
1371
1372// Checks that `matcher` is internally consistent and that it
1373// can legally be followed by a token `N`, for all `N` in `follow`.
1374// (If `follow` is empty, then it imposes no constraint on
1375// the `matcher`.)
1376//
1377// Returns the set of NT tokens that could possibly come last in
1378// `matcher`. (If `matcher` matches the empty sequence, then
1379// `maybe_empty` will be set to true.)
1380//
1381// Requires that `first_sets` is pre-computed for `matcher`;
1382// see `FirstSets::new`.
1383fn check_matcher_core<'tt>(
1384    sess: &Session,
1385    features: &Features,
1386    node_id: NodeId,
1387    first_sets: &FirstSets<'tt>,
1388    matcher: &'tt [mbe::TokenTree],
1389    follow: &TokenSet<'tt>,
1390) -> Result<TokenSet<'tt>, ErrorGuaranteed> {
1391    use mbe::TokenTree;
1392
1393    let mut last = TokenSet::empty();
1394
1395    let mut errored = Ok(());
1396
1397    // 2. For each token and suffix  [T, SUFFIX] in M:
1398    // ensure that T can be followed by SUFFIX, and if SUFFIX may be empty,
1399    // then ensure T can also be followed by any element of FOLLOW.
1400    'each_token: for i in 0..matcher.len() {
1401        let token = &matcher[i];
1402        let suffix = &matcher[i + 1..];
1403
1404        let build_suffix_first = || {
1405            let mut s = first_sets.first(suffix);
1406            if s.maybe_empty {
1407                s.add_all(follow);
1408            }
1409            s
1410        };
1411
1412        // (we build `suffix_first` on demand below; you can tell
1413        // which cases are supposed to fall through by looking for the
1414        // initialization of this variable.)
1415        let suffix_first;
1416
1417        // First, update `last` so that it corresponds to the set
1418        // of NT tokens that might end the sequence `... token`.
1419        match token {
1420            TokenTree::Token(..)
1421            | TokenTree::MetaVar(..)
1422            | TokenTree::MetaVarDecl { .. }
1423            | TokenTree::MetaVarExpr(..) => {
1424                if let TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } = token
1425                    && !features.macro_guard_matcher()
1426                {
1427                    feature_err(
1428                        sess,
1429                        sym::macro_guard_matcher,
1430                        token.span(),
1431                        "`guard` fragments in macro are unstable",
1432                    )
1433                    .emit();
1434                }
1435                if token_can_be_followed_by_any(token) {
1436                    // don't need to track tokens that work with any,
1437                    last.replace_with_irrelevant();
1438                    // ... and don't need to check tokens that can be
1439                    // followed by anything against SUFFIX.
1440                    continue 'each_token;
1441                } else {
1442                    last.replace_with(TtHandle::TtRef(token));
1443                    suffix_first = build_suffix_first();
1444                }
1445            }
1446            TokenTree::Delimited(span, _, d) => {
1447                let my_suffix = TokenSet::singleton(TtHandle::from_token_kind(
1448                    d.delim.as_close_token_kind(),
1449                    span.close,
1450                ));
1451                check_matcher_core(sess, features, node_id, first_sets, &d.tts, &my_suffix)?;
1452                // don't track non NT tokens
1453                last.replace_with_irrelevant();
1454
1455                // also, we don't need to check delimited sequences
1456                // against SUFFIX
1457                continue 'each_token;
1458            }
1459            TokenTree::Sequence(_, seq_rep) => {
1460                suffix_first = build_suffix_first();
1461                // The trick here: when we check the interior, we want
1462                // to include the separator (if any) as a potential
1463                // (but not guaranteed) element of FOLLOW. So in that
1464                // case, we make a temp copy of suffix and stuff
1465                // delimiter in there.
1466                //
1467                // FIXME: Should I first scan suffix_first to see if
1468                // delimiter is already in it before I go through the
1469                // work of cloning it? But then again, this way I may
1470                // get a "tighter" span?
1471                let mut new;
1472                let my_suffix = if let Some(sep) = &seq_rep.separator {
1473                    new = suffix_first.clone();
1474                    new.add_one_maybe(TtHandle::from_token(*sep));
1475                    &new
1476                } else {
1477                    &suffix_first
1478                };
1479
1480                // At this point, `suffix_first` is built, and
1481                // `my_suffix` is some TokenSet that we can use
1482                // for checking the interior of `seq_rep`.
1483                let next = check_matcher_core(
1484                    sess,
1485                    features,
1486                    node_id,
1487                    first_sets,
1488                    &seq_rep.tts,
1489                    my_suffix,
1490                )?;
1491                if next.maybe_empty {
1492                    last.add_all(&next);
1493                } else {
1494                    last = next;
1495                }
1496
1497                // the recursive call to check_matcher_core already ran the 'each_last
1498                // check below, so we can just keep going forward here.
1499                continue 'each_token;
1500            }
1501        }
1502
1503        // (`suffix_first` guaranteed initialized once reaching here.)
1504
1505        // Now `last` holds the complete set of NT tokens that could
1506        // end the sequence before SUFFIX. Check that every one works with `suffix`.
1507        for tt in &last.tokens {
1508            if let &TokenTree::MetaVarDecl { span, name, kind } = tt.get() {
1509                for next_token in &suffix_first.tokens {
1510                    let next_token = next_token.get();
1511
1512                    // Check if the old pat is used and the next token is `|`
1513                    // to warn about incompatibility with Rust 2021.
1514                    // We only emit this lint if we're parsing the original
1515                    // definition of this macro_rules, not while (re)parsing
1516                    // the macro when compiling another crate that is using the
1517                    // macro. (See #86567.)
1518                    if is_defined_in_current_crate(node_id)
1519                        && #[allow(non_exhaustive_omitted_patterns)] match kind {
    NonterminalKind::Pat(PatParam { inferred: true }) => true,
    _ => false,
}matches!(kind, NonterminalKind::Pat(PatParam { inferred: true }))
1520                        && #[allow(non_exhaustive_omitted_patterns)] match next_token {
    TokenTree::Token(token) if *token == token::Or => true,
    _ => false,
}matches!(
1521                            next_token,
1522                            TokenTree::Token(token) if *token == token::Or
1523                        )
1524                    {
1525                        // It is suggestion to use pat_param, for example: $x:pat -> $x:pat_param.
1526                        let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl {
1527                            span,
1528                            name,
1529                            kind: NonterminalKind::Pat(PatParam { inferred: false }),
1530                        });
1531                        sess.psess.buffer_lint(
1532                            RUST_2021_INCOMPATIBLE_OR_PATTERNS,
1533                            span,
1534                            ast::CRATE_NODE_ID,
1535                            errors::OrPatternsBackCompat { span, suggestion },
1536                        );
1537                    }
1538                    match is_in_follow(next_token, kind) {
1539                        IsInFollow::Yes => {}
1540                        IsInFollow::No(possible) => {
1541                            let may_be = if last.tokens.len() == 1 && suffix_first.tokens.len() == 1
1542                            {
1543                                "is"
1544                            } else {
1545                                "may be"
1546                            };
1547
1548                            let sp = next_token.span();
1549                            let mut err = sess.dcx().struct_span_err(
1550                                sp,
1551                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`${0}:{1}` {3} followed by `{2}`, which is not allowed for `{1}` fragments",
                name, kind, quoted_tt_to_string(next_token), may_be))
    })format!(
1552                                    "`${name}:{frag}` {may_be} followed by `{next}`, which \
1553                                     is not allowed for `{frag}` fragments",
1554                                    name = name,
1555                                    frag = kind,
1556                                    next = quoted_tt_to_string(next_token),
1557                                    may_be = may_be
1558                                ),
1559                            );
1560                            err.span_label(sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("not allowed after `{0}` fragments",
                kind))
    })format!("not allowed after `{kind}` fragments"));
1561
1562                            if kind == NonterminalKind::Pat(PatWithOr)
1563                                && sess.psess.edition.at_least_rust_2021()
1564                                && next_token.is_token(&token::Or)
1565                            {
1566                                let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl {
1567                                    span,
1568                                    name,
1569                                    kind: NonterminalKind::Pat(PatParam { inferred: false }),
1570                                });
1571                                err.span_suggestion(
1572                                    span,
1573                                    "try a `pat_param` fragment specifier instead",
1574                                    suggestion,
1575                                    Applicability::MaybeIncorrect,
1576                                );
1577                            }
1578
1579                            let msg = "allowed there are: ";
1580                            match possible {
1581                                &[] => {}
1582                                &[t] => {
1583                                    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("only {0} is allowed after `{1}` fragments",
                t, kind))
    })format!(
1584                                        "only {t} is allowed after `{kind}` fragments",
1585                                    ));
1586                                }
1587                                ts => {
1588                                    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1} or {2}", msg,
                ts[..ts.len() - 1].to_vec().join(", "), ts[ts.len() - 1]))
    })format!(
1589                                        "{}{} or {}",
1590                                        msg,
1591                                        ts[..ts.len() - 1].to_vec().join(", "),
1592                                        ts[ts.len() - 1],
1593                                    ));
1594                                }
1595                            }
1596                            errored = Err(err.emit());
1597                        }
1598                    }
1599                }
1600            }
1601        }
1602    }
1603    errored?;
1604    Ok(last)
1605}
1606
1607fn token_can_be_followed_by_any(tok: &mbe::TokenTree) -> bool {
1608    if let mbe::TokenTree::MetaVarDecl { kind, .. } = *tok {
1609        frag_can_be_followed_by_any(kind)
1610    } else {
1611        // (Non NT's can always be followed by anything in matchers.)
1612        true
1613    }
1614}
1615
1616/// Returns `true` if a fragment of type `frag` can be followed by any sort of
1617/// token. We use this (among other things) as a useful approximation
1618/// for when `frag` can be followed by a repetition like `$(...)*` or
1619/// `$(...)+`. In general, these can be a bit tricky to reason about,
1620/// so we adopt a conservative position that says that any fragment
1621/// specifier which consumes at most one token tree can be followed by
1622/// a fragment specifier (indeed, these fragments can be followed by
1623/// ANYTHING without fear of future compatibility hazards).
1624fn frag_can_be_followed_by_any(kind: NonterminalKind) -> bool {
1625    #[allow(non_exhaustive_omitted_patterns)] match kind {
    NonterminalKind::Item | NonterminalKind::Block | NonterminalKind::Ident |
        NonterminalKind::Literal | NonterminalKind::Meta |
        NonterminalKind::Lifetime | NonterminalKind::TT => true,
    _ => false,
}matches!(
1626        kind,
1627        NonterminalKind::Item           // always terminated by `}` or `;`
1628        | NonterminalKind::Block        // exactly one token tree
1629        | NonterminalKind::Ident        // exactly one token tree
1630        | NonterminalKind::Literal      // exactly one token tree
1631        | NonterminalKind::Meta         // exactly one token tree
1632        | NonterminalKind::Lifetime     // exactly one token tree
1633        | NonterminalKind::TT // exactly one token tree
1634    )
1635}
1636
1637enum IsInFollow {
1638    Yes,
1639    No(&'static [&'static str]),
1640}
1641
1642/// Returns `true` if `frag` can legally be followed by the token `tok`. For
1643/// fragments that can consume an unbounded number of tokens, `tok`
1644/// must be within a well-defined follow set. This is intended to
1645/// guarantee future compatibility: for example, without this rule, if
1646/// we expanded `expr` to include a new binary operator, we might
1647/// break macros that were relying on that binary operator as a
1648/// separator.
1649// when changing this do not forget to update doc/book/macros.md!
1650fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
1651    use mbe::TokenTree;
1652
1653    if let TokenTree::Token(Token { kind, .. }) = tok
1654        && kind.close_delim().is_some()
1655    {
1656        // closing a token tree can never be matched by any fragment;
1657        // iow, we always require that `(` and `)` match, etc.
1658        IsInFollow::Yes
1659    } else {
1660        match kind {
1661            NonterminalKind::Item => {
1662                // since items *must* be followed by either a `;` or a `}`, we can
1663                // accept anything after them
1664                IsInFollow::Yes
1665            }
1666            NonterminalKind::Block => {
1667                // anything can follow block, the braces provide an easy boundary to
1668                // maintain
1669                IsInFollow::Yes
1670            }
1671            NonterminalKind::Stmt | NonterminalKind::Expr(_) => {
1672                const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
1673                match tok {
1674                    TokenTree::Token(token) => match token.kind {
1675                        FatArrow | Comma | Semi => IsInFollow::Yes,
1676                        _ => IsInFollow::No(TOKENS),
1677                    },
1678                    _ => IsInFollow::No(TOKENS),
1679                }
1680            }
1681            NonterminalKind::Pat(PatParam { .. }) => {
1682                const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`if let`", "`in`"];
1683                match tok {
1684                    TokenTree::Token(token) => match token.kind {
1685                        FatArrow | Comma | Eq | Or => IsInFollow::Yes,
1686                        Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1687                            IsInFollow::Yes
1688                        }
1689                        _ => IsInFollow::No(TOKENS),
1690                    },
1691                    TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } => IsInFollow::Yes,
1692                    _ => IsInFollow::No(TOKENS),
1693                }
1694            }
1695            NonterminalKind::Pat(PatWithOr) => {
1696                const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`if`", "`if let`", "`in`"];
1697                match tok {
1698                    TokenTree::Token(token) => match token.kind {
1699                        FatArrow | Comma | Eq => IsInFollow::Yes,
1700                        Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1701                            IsInFollow::Yes
1702                        }
1703                        _ => IsInFollow::No(TOKENS),
1704                    },
1705                    TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } => IsInFollow::Yes,
1706                    _ => IsInFollow::No(TOKENS),
1707                }
1708            }
1709            NonterminalKind::Guard => {
1710                const TOKENS: &[&str] = &["`=>`", "`,`", "`{`"];
1711                match tok {
1712                    TokenTree::Token(token) => match token.kind {
1713                        FatArrow | Comma | OpenBrace => IsInFollow::Yes,
1714                        _ => IsInFollow::No(TOKENS),
1715                    },
1716                    _ => IsInFollow::No(TOKENS),
1717                }
1718            }
1719            NonterminalKind::Path | NonterminalKind::Ty => {
1720                const TOKENS: &[&str] = &[
1721                    "`{`", "`[`", "`=>`", "`,`", "`>`", "`=`", "`:`", "`;`", "`|`", "`as`",
1722                    "`where`",
1723                ];
1724                match tok {
1725                    TokenTree::Token(token) => match token.kind {
1726                        OpenBrace | OpenBracket | Comma | FatArrow | Colon | Eq | Gt | Shr
1727                        | Semi | Or => IsInFollow::Yes,
1728                        Ident(name, IdentIsRaw::No) if name == kw::As || name == kw::Where => {
1729                            IsInFollow::Yes
1730                        }
1731                        _ => IsInFollow::No(TOKENS),
1732                    },
1733                    TokenTree::MetaVarDecl { kind: NonterminalKind::Block, .. } => IsInFollow::Yes,
1734                    _ => IsInFollow::No(TOKENS),
1735                }
1736            }
1737            NonterminalKind::Ident | NonterminalKind::Lifetime => {
1738                // being a single token, idents and lifetimes are harmless
1739                IsInFollow::Yes
1740            }
1741            NonterminalKind::Literal => {
1742                // literals may be of a single token, or two tokens (negative numbers)
1743                IsInFollow::Yes
1744            }
1745            NonterminalKind::Meta | NonterminalKind::TT => {
1746                // being either a single token or a delimited sequence, tt is
1747                // harmless
1748                IsInFollow::Yes
1749            }
1750            NonterminalKind::Vis => {
1751                // Explicitly disallow `priv`, on the off chance it comes back.
1752                const TOKENS: &[&str] = &["`,`", "an ident", "a type"];
1753                match tok {
1754                    TokenTree::Token(token) => match token.kind {
1755                        Comma => IsInFollow::Yes,
1756                        Ident(_, IdentIsRaw::Yes) => IsInFollow::Yes,
1757                        Ident(name, _) if name != kw::Priv => IsInFollow::Yes,
1758                        _ => {
1759                            if token.can_begin_type() {
1760                                IsInFollow::Yes
1761                            } else {
1762                                IsInFollow::No(TOKENS)
1763                            }
1764                        }
1765                    },
1766                    TokenTree::MetaVarDecl {
1767                        kind: NonterminalKind::Ident | NonterminalKind::Ty | NonterminalKind::Path,
1768                        ..
1769                    } => IsInFollow::Yes,
1770                    _ => IsInFollow::No(TOKENS),
1771                }
1772            }
1773        }
1774    }
1775}
1776
1777fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
1778    match tt {
1779        mbe::TokenTree::Token(token) => pprust::token_to_string(token).into(),
1780        mbe::TokenTree::MetaVar(_, name) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("${0}", name))
    })format!("${name}"),
1781        mbe::TokenTree::MetaVarDecl { name, kind, .. } => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("${0}:{1}", name, kind))
    })format!("${name}:{kind}"),
1782        _ => {
    ::core::panicking::panic_display(&"unexpected mbe::TokenTree::{Sequence or Delimited} \
             in follow set checker");
}panic!(
1783            "{}",
1784            "unexpected mbe::TokenTree::{Sequence or Delimited} \
1785             in follow set checker"
1786        ),
1787    }
1788}
1789
1790fn is_defined_in_current_crate(node_id: NodeId) -> bool {
1791    // Macros defined in the current crate have a real node id,
1792    // whereas macros from an external crate have a dummy id.
1793    node_id != DUMMY_NODE_ID
1794}
1795
1796pub(super) fn parser_from_cx(
1797    psess: &ParseSess,
1798    mut tts: TokenStream,
1799    recovery: Recovery,
1800) -> Parser<'_> {
1801    tts.desugar_doc_comments();
1802    Parser::new(psess, tts, rustc_parse::MACRO_ARGUMENTS).recovery(recovery)
1803}