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::def::MacroKinds;
18use rustc_hir::find_attr;
19use rustc_lint_defs::builtin::{
20    RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
21};
22use rustc_parse::exp;
23use rustc_parse::parser::{Parser, Recovery};
24use rustc_session::Session;
25use rustc_session::parse::{ParseSess, feature_err};
26use rustc_span::edition::Edition;
27use rustc_span::hygiene::Transparency;
28use rustc_span::{Ident, Span, Symbol, kw, sym};
29use tracing::{debug, instrument, trace, trace_span};
30
31use super::diagnostics::{FailedMacro, failed_to_match_macro};
32use super::macro_parser::{NamedMatches, NamedParseResult};
33use super::{SequenceRepetition, diagnostics};
34use crate::base::{
35    AttrProcMacro, BangProcMacro, DummyResult, ExpandResult, ExtCtxt, MacResult,
36    MacroExpanderResult, SyntaxExtension, SyntaxExtensionKind, TTMacroExpander,
37};
38use crate::errors;
39use crate::expand::{AstFragment, AstFragmentKind, ensure_complete_parse, parse_ast_fragment};
40use crate::mbe::macro_check::check_meta_variables;
41use crate::mbe::macro_parser::{Error, ErrorReported, Failure, MatcherLoc, Success, TtParser};
42use crate::mbe::quoted::{RulePart, parse_one_tt};
43use crate::mbe::transcribe::transcribe;
44use crate::mbe::{self, KleeneOp};
45
46pub(crate) struct ParserAnyMacro<'a, 'b> {
47    parser: Parser<'a>,
48
49    /// Span of the expansion site of the macro this parser is for
50    site_span: Span,
51    /// The ident of the macro we're parsing
52    macro_ident: Ident,
53    lint_node_id: NodeId,
54    is_trailing_mac: bool,
55    arm_span: Span,
56    /// Whether or not this macro is defined in the current crate
57    is_local: bool,
58    bindings: &'b [MacroRule],
59    matched_rule_bindings: &'b [MatcherLoc],
60}
61
62impl<'a, 'b> ParserAnyMacro<'a, 'b> {
63    pub(crate) fn make(
64        mut self: Box<ParserAnyMacro<'a, 'b>>,
65        kind: AstFragmentKind,
66    ) -> AstFragment {
67        let ParserAnyMacro {
68            site_span,
69            macro_ident,
70            ref mut parser,
71            lint_node_id,
72            arm_span,
73            is_trailing_mac,
74            is_local,
75            bindings,
76            matched_rule_bindings,
77        } = *self;
78        let snapshot = &mut parser.create_snapshot_for_diagnostic();
79        let fragment = match parse_ast_fragment(parser, kind) {
80            Ok(f) => f,
81            Err(err) => {
82                let guar = diagnostics::emit_frag_parse_err(
83                    err,
84                    parser,
85                    snapshot,
86                    site_span,
87                    arm_span,
88                    kind,
89                    bindings,
90                    matched_rule_bindings,
91                );
92                return kind.dummy(site_span, guar);
93            }
94        };
95
96        // We allow semicolons at the end of expressions -- e.g., the semicolon in
97        // `macro_rules! m { () => { panic!(); } }` isn't parsed by `.parse_expr()`,
98        // but `m!()` is allowed in expression positions (cf. issue #34706).
99        if kind == AstFragmentKind::Expr && parser.token == token::Semi {
100            if is_local {
101                parser.psess.buffer_lint(
102                    SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
103                    parser.token.span,
104                    lint_node_id,
105                    errors::TrailingMacro { is_trailing: is_trailing_mac, name: macro_ident },
106                );
107            }
108            parser.bump();
109        }
110
111        // Make sure we don't have any tokens left to parse so we don't silently drop anything.
112        let path = ast::Path::from_ident(macro_ident.with_span_pos(site_span));
113        ensure_complete_parse(parser, &path, kind.name(), site_span);
114        fragment
115    }
116
117    #[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(117u32),
                                    ::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))]
118    pub(crate) fn from_tts<'cx>(
119        cx: &'cx mut ExtCtxt<'a>,
120        tts: TokenStream,
121        site_span: Span,
122        arm_span: Span,
123        is_local: bool,
124        macro_ident: Ident,
125        // bindings and lhs is for diagnostics
126        bindings: &'b [MacroRule],
127        matched_rule_bindings: &'b [MatcherLoc],
128    ) -> Self {
129        Self {
130            parser: Parser::new(&cx.sess.psess, tts, None),
131
132            // Pass along the original expansion site and the name of the macro
133            // so we can print a useful error message if the parse of the expanded
134            // macro leaves unparsed tokens.
135            site_span,
136            macro_ident,
137            lint_node_id: cx.current_expansion.lint_node_id,
138            is_trailing_mac: cx.current_expansion.is_trailing_mac,
139            arm_span,
140            is_local,
141            bindings,
142            matched_rule_bindings,
143        }
144    }
145}
146
147pub(crate) enum MacroRule {
148    /// A function-style rule, for use with `m!()`
149    Func { lhs: Vec<MatcherLoc>, lhs_span: Span, rhs: mbe::TokenTree },
150    /// An attr rule, for use with `#[m]`
151    Attr {
152        unsafe_rule: bool,
153        args: Vec<MatcherLoc>,
154        args_span: Span,
155        body: Vec<MatcherLoc>,
156        body_span: Span,
157        rhs: mbe::TokenTree,
158    },
159    /// A derive rule, for use with `#[m]`
160    Derive { body: Vec<MatcherLoc>, body_span: Span, rhs: mbe::TokenTree },
161}
162
163pub struct MacroRulesMacroExpander {
164    node_id: NodeId,
165    name: Ident,
166    span: Span,
167    transparency: Transparency,
168    kinds: MacroKinds,
169    rules: Vec<MacroRule>,
170}
171
172impl MacroRulesMacroExpander {
173    pub fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, MultiSpan)> {
174        // If the rhs contains an invocation like `compile_error!`, don't report it as unused.
175        let (span, rhs) = match self.rules[rule_i] {
176            MacroRule::Func { lhs_span, ref rhs, .. } => (MultiSpan::from_span(lhs_span), rhs),
177            MacroRule::Attr { args_span, body_span, ref rhs, .. } => {
178                (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)
179            }
180            MacroRule::Derive { body_span, ref rhs, .. } => (MultiSpan::from_span(body_span), rhs),
181        };
182        if has_compile_error_macro(rhs) { None } else { Some((&self.name, span)) }
183    }
184
185    pub fn kinds(&self) -> MacroKinds {
186        self.kinds
187    }
188
189    pub fn expand_derive(
190        &self,
191        cx: &mut ExtCtxt<'_>,
192        sp: Span,
193        body: &TokenStream,
194    ) -> Result<TokenStream, ErrorGuaranteed> {
195        // This is similar to `expand_macro`, but they have very different signatures, and will
196        // diverge further once derives support arguments.
197        let Self { name, ref rules, node_id, .. } = *self;
198        let psess = &cx.sess.psess;
199
200        if cx.trace_macros() {
201            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));
202            trace_macros_note(&mut cx.expansions, sp, msg);
203        }
204
205        match try_match_macro_derive(psess, name, body, rules, &mut NoopTracker) {
206            Ok((rule_index, rule, named_matches)) => {
207                let MacroRule::Derive { rhs, .. } = rule else {
208                    {
    ::core::panicking::panic_fmt(format_args!("try_match_macro_derive returned non-derive rule"));
};panic!("try_match_macro_derive returned non-derive rule");
209                };
210                let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
211                    cx.dcx().span_bug(sp, "malformed macro derive rhs");
212                };
213
214                let id = cx.current_expansion.id;
215                let tts = transcribe(psess, &named_matches, rhs, *rhs_span, self.transparency, id)
216                    .map_err(|e| e.emit())?;
217
218                if cx.trace_macros() {
219                    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));
220                    trace_macros_note(&mut cx.expansions, sp, msg);
221                }
222
223                if is_defined_in_current_crate(node_id) {
224                    cx.resolver.record_macro_rule_usage(node_id, rule_index);
225                }
226
227                Ok(tts)
228            }
229            Err(CanRetry::No(guar)) => Err(guar),
230            Err(CanRetry::Yes) => {
231                let (_, guar) = failed_to_match_macro(
232                    cx.psess(),
233                    sp,
234                    self.span,
235                    name,
236                    FailedMacro::Derive,
237                    body,
238                    rules,
239                );
240                cx.macro_error_and_trace_macros_diag();
241                Err(guar)
242            }
243        }
244    }
245}
246
247impl TTMacroExpander for MacroRulesMacroExpander {
248    fn expand<'cx, 'a: 'cx>(
249        &'a self,
250        cx: &'cx mut ExtCtxt<'_>,
251        sp: Span,
252        input: TokenStream,
253    ) -> MacroExpanderResult<'cx> {
254        ExpandResult::Ready(expand_macro(
255            cx,
256            sp,
257            self.span,
258            self.node_id,
259            self.name,
260            self.transparency,
261            input,
262            &self.rules,
263        ))
264    }
265}
266
267impl AttrProcMacro for MacroRulesMacroExpander {
268    fn expand(
269        &self,
270        _cx: &mut ExtCtxt<'_>,
271        _sp: Span,
272        _args: TokenStream,
273        _body: TokenStream,
274    ) -> Result<TokenStream, ErrorGuaranteed> {
275        {
    ::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`")
276    }
277
278    fn expand_with_safety(
279        &self,
280        cx: &mut ExtCtxt<'_>,
281        safety: Safety,
282        sp: Span,
283        args: TokenStream,
284        body: TokenStream,
285    ) -> Result<TokenStream, ErrorGuaranteed> {
286        expand_macro_attr(
287            cx,
288            sp,
289            self.span,
290            self.node_id,
291            self.name,
292            self.transparency,
293            safety,
294            args,
295            body,
296            &self.rules,
297        )
298    }
299}
300
301struct DummyBang(ErrorGuaranteed);
302
303impl BangProcMacro for DummyBang {
304    fn expand<'cx>(
305        &self,
306        _: &'cx mut ExtCtxt<'_>,
307        _: Span,
308        _: TokenStream,
309    ) -> Result<TokenStream, ErrorGuaranteed> {
310        Err(self.0)
311    }
312}
313
314fn trace_macros_note(cx_expansions: &mut FxIndexMap<Span, Vec<String>>, sp: Span, message: String) {
315    let sp = sp.macro_backtrace().last().map_or(sp, |trace| trace.call_site);
316    cx_expansions.entry(sp).or_default().push(message);
317}
318
319pub(super) trait Tracker<'matcher> {
320    /// The contents of `ParseResult::Failure`.
321    type Failure;
322
323    /// Arm failed to match. If the token is `token::Eof`, it indicates an unexpected
324    /// end of macro invocation. Otherwise, it indicates that no rules expected the given token.
325    /// The usize is the approximate position of the token in the input token stream.
326    fn build_failure(tok: Token, position: u32, msg: &'static str) -> Self::Failure;
327
328    /// This is called before trying to match next MatcherLoc on the current token.
329    fn before_match_loc(&mut self, _parser: &TtParser, _matcher: &'matcher MatcherLoc) {}
330
331    /// This is called after an arm has been parsed, either successfully or unsuccessfully. When
332    /// this is called, `before_match_loc` was called at least once (with a `MatcherLoc::Eof`).
333    fn after_arm(&mut self, _in_body: bool, _result: &NamedParseResult<Self::Failure>) {}
334
335    /// For tracing.
336    fn description() -> &'static str;
337
338    fn recovery() -> Recovery {
339        Recovery::Forbidden
340    }
341}
342
343/// A noop tracker that is used in the hot path of the expansion, has zero overhead thanks to
344/// monomorphization.
345pub(super) struct NoopTracker;
346
347impl<'matcher> Tracker<'matcher> for NoopTracker {
348    type Failure = ();
349
350    fn build_failure(_tok: Token, _position: u32, _msg: &'static str) -> Self::Failure {}
351
352    fn description() -> &'static str {
353        "none"
354    }
355}
356
357/// Expands the rules based macro defined by `rules` for a given input `arg`.
358#[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(358u32),
                                    ::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:413",
                                            "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(413u32),
                                            ::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);
                    cx.macro_error_and_trace_macros_diag();
                    DummyResult::any(span, guar)
                }
            }
        }
    }
}#[instrument(skip(cx, transparency, arg, rules))]
359fn expand_macro<'cx, 'a: 'cx>(
360    cx: &'cx mut ExtCtxt<'_>,
361    sp: Span,
362    def_span: Span,
363    node_id: NodeId,
364    name: Ident,
365    transparency: Transparency,
366    arg: TokenStream,
367    rules: &'a [MacroRule],
368) -> Box<dyn MacResult + 'cx> {
369    let psess = &cx.sess.psess;
370
371    if cx.trace_macros() {
372        let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(&arg));
373        trace_macros_note(&mut cx.expansions, sp, msg);
374    }
375
376    // Track nothing for the best performance.
377    let try_success_result = try_match_macro(psess, name, &arg, rules, &mut NoopTracker);
378
379    match try_success_result {
380        Ok((rule_index, rule, named_matches)) => {
381            let MacroRule::Func { lhs, rhs, .. } = rule else {
382                panic!("try_match_macro returned non-func rule");
383            };
384            let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
385                cx.dcx().span_bug(sp, "malformed macro rhs");
386            };
387            let arm_span = rhs_span.entire();
388
389            // rhs has holes ( `$id` and `$(...)` that need filled)
390            let id = cx.current_expansion.id;
391            let tts = match transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id) {
392                Ok(tts) => tts,
393                Err(err) => {
394                    let guar = err.emit();
395                    return DummyResult::any(arm_span, guar);
396                }
397            };
398
399            if cx.trace_macros() {
400                let msg = format!("to `{}`", pprust::tts_to_string(&tts));
401                trace_macros_note(&mut cx.expansions, sp, msg);
402            }
403
404            let is_local = is_defined_in_current_crate(node_id);
405            if is_local {
406                cx.resolver.record_macro_rule_usage(node_id, rule_index);
407            }
408
409            // Let the context choose how to interpret the result. Weird, but useful for X-macros.
410            Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name, rules, lhs))
411        }
412        Err(CanRetry::No(guar)) => {
413            debug!("Will not retry matching as an error was emitted already");
414            DummyResult::any(sp, guar)
415        }
416        Err(CanRetry::Yes) => {
417            // Retry and emit a better error.
418            let (span, guar) = failed_to_match_macro(
419                cx.psess(),
420                sp,
421                def_span,
422                name,
423                FailedMacro::Func,
424                &arg,
425                rules,
426            );
427            cx.macro_error_and_trace_macros_diag();
428            DummyResult::any(span, guar)
429        }
430    }
431}
432
433/// Expands the rules based macro defined by `rules` for a given attribute `args` and `body`.
434#[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(434u32),
                                    ::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);
                    cx.trace_macros_diag();
                    Err(guar)
                }
            }
        }
    }
}#[instrument(skip(cx, transparency, args, body, rules))]
435fn expand_macro_attr(
436    cx: &mut ExtCtxt<'_>,
437    sp: Span,
438    def_span: Span,
439    node_id: NodeId,
440    name: Ident,
441    transparency: Transparency,
442    safety: Safety,
443    args: TokenStream,
444    body: TokenStream,
445    rules: &[MacroRule],
446) -> Result<TokenStream, ErrorGuaranteed> {
447    let psess = &cx.sess.psess;
448    // Macros defined in the current crate have a real node id,
449    // whereas macros from an external crate have a dummy id.
450    let is_local = node_id != DUMMY_NODE_ID;
451
452    if cx.trace_macros() {
453        let msg = format!(
454            "expanding `#[{name}({})] {}`",
455            pprust::tts_to_string(&args),
456            pprust::tts_to_string(&body),
457        );
458        trace_macros_note(&mut cx.expansions, sp, msg);
459    }
460
461    // Track nothing for the best performance.
462    match try_match_macro_attr(psess, name, &args, &body, rules, &mut NoopTracker) {
463        Ok((i, rule, named_matches)) => {
464            let MacroRule::Attr { rhs, unsafe_rule, .. } = rule else {
465                panic!("try_macro_match_attr returned non-attr rule");
466            };
467            let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
468                cx.dcx().span_bug(sp, "malformed macro rhs");
469            };
470
471            match (safety, unsafe_rule) {
472                (Safety::Default, false) | (Safety::Unsafe(_), true) => {}
473                (Safety::Default, true) => {
474                    cx.dcx().span_err(sp, "unsafe attribute invocation requires `unsafe`");
475                }
476                (Safety::Unsafe(span), false) => {
477                    cx.dcx().span_err(span, "unnecessary `unsafe` on safe attribute invocation");
478                }
479                (Safety::Safe(span), _) => {
480                    cx.dcx().span_bug(span, "unexpected `safe` keyword");
481                }
482            }
483
484            let id = cx.current_expansion.id;
485            let tts = transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id)
486                .map_err(|e| e.emit())?;
487
488            if cx.trace_macros() {
489                let msg = format!("to `{}`", pprust::tts_to_string(&tts));
490                trace_macros_note(&mut cx.expansions, sp, msg);
491            }
492
493            if is_local {
494                cx.resolver.record_macro_rule_usage(node_id, i);
495            }
496
497            Ok(tts)
498        }
499        Err(CanRetry::No(guar)) => Err(guar),
500        Err(CanRetry::Yes) => {
501            // Retry and emit a better error.
502            let (_, guar) = failed_to_match_macro(
503                cx.psess(),
504                sp,
505                def_span,
506                name,
507                FailedMacro::Attr(&args),
508                &body,
509                rules,
510            );
511            cx.trace_macros_diag();
512            Err(guar)
513        }
514    }
515}
516
517pub(super) enum CanRetry {
518    Yes,
519    /// We are not allowed to retry macro expansion as a fatal error has been emitted already.
520    No(ErrorGuaranteed),
521}
522
523/// Try expanding the macro. Returns the index of the successful arm and its named_matches if it was successful,
524/// and nothing if it failed. On failure, it's the callers job to use `track` accordingly to record all errors
525/// correctly.
526#[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(526u32),
                                    ::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(558u32),
                                            ::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:572",
                                                "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(572u32),
                                                ::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:580",
                                                "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(580u32),
                                                ::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:584",
                                                "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(584u32),
                                                ::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:589",
                                                "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(589u32),
                                                ::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()))]
527pub(super) fn try_match_macro<'matcher, T: Tracker<'matcher>>(
528    psess: &ParseSess,
529    name: Ident,
530    arg: &TokenStream,
531    rules: &'matcher [MacroRule],
532    track: &mut T,
533) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
534    // We create a base parser that can be used for the "black box" parts.
535    // Every iteration needs a fresh copy of that parser. However, the parser
536    // is not mutated on many of the iterations, particularly when dealing with
537    // macros like this:
538    //
539    // macro_rules! foo {
540    //     ("a") => (A);
541    //     ("b") => (B);
542    //     ("c") => (C);
543    //     // ... etc. (maybe hundreds more)
544    // }
545    //
546    // as seen in the `html5ever` benchmark. We use a `Cow` so that the base
547    // parser is only cloned when necessary (upon mutation). Furthermore, we
548    // reinitialize the `Cow` with the base parser at the start of every
549    // iteration, so that any mutated parsers are not reused. This is all quite
550    // hacky, but speeds up the `html5ever` benchmark significantly. (Issue
551    // 68836 suggests a more comprehensive but more complex change to deal with
552    // this situation.)
553    let parser = parser_from_cx(psess, arg.clone(), T::recovery());
554    // Try each arm's matchers.
555    let mut tt_parser = TtParser::new(name);
556    for (i, rule) in rules.iter().enumerate() {
557        let MacroRule::Func { lhs, .. } = rule else { continue };
558        let _tracing_span = trace_span!("Matching arm", %i);
559
560        // Take a snapshot of the state of pre-expansion gating at this point.
561        // This is used so that if a matcher is not `Success(..)`ful,
562        // then the spans which became gated when parsing the unsuccessful matcher
563        // are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
564        let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
565
566        let result = tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs, track);
567
568        track.after_arm(true, &result);
569
570        match result {
571            Success(named_matches) => {
572                debug!("Parsed arm successfully");
573                // The matcher was `Success(..)`ful.
574                // Merge the gated spans from parsing the matcher with the preexisting ones.
575                psess.gated_spans.merge(gated_spans_snapshot);
576
577                return Ok((i, rule, named_matches));
578            }
579            Failure(_) => {
580                trace!("Failed to match arm, trying the next one");
581                // Try the next arm.
582            }
583            Error(_, _) => {
584                debug!("Fatal error occurred during matching");
585                // We haven't emitted an error yet, so we can retry.
586                return Err(CanRetry::Yes);
587            }
588            ErrorReported(guarantee) => {
589                debug!("Fatal error occurred and was reported during matching");
590                // An error has been reported already, we cannot retry as that would cause duplicate errors.
591                return Err(CanRetry::No(guarantee));
592            }
593        }
594
595        // The matcher was not `Success(..)`ful.
596        // Restore to the state before snapshotting and maybe try again.
597        mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
598    }
599
600    Err(CanRetry::Yes)
601}
602
603/// Try expanding the macro attribute. Returns the index of the successful arm and its
604/// named_matches if it was successful, and nothing if it failed. On failure, it's the caller's job
605/// to use `track` accordingly to record all errors correctly.
606#[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(606u32),
                                    ::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()))]
607pub(super) fn try_match_macro_attr<'matcher, T: Tracker<'matcher>>(
608    psess: &ParseSess,
609    name: Ident,
610    attr_args: &TokenStream,
611    attr_body: &TokenStream,
612    rules: &'matcher [MacroRule],
613    track: &mut T,
614) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
615    // This uses the same strategy as `try_match_macro`
616    let args_parser = parser_from_cx(psess, attr_args.clone(), T::recovery());
617    let body_parser = parser_from_cx(psess, attr_body.clone(), T::recovery());
618    let mut tt_parser = TtParser::new(name);
619    for (i, rule) in rules.iter().enumerate() {
620        let MacroRule::Attr { args, body, .. } = rule else { continue };
621
622        let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
623
624        let result = tt_parser.parse_tt(&mut Cow::Borrowed(&args_parser), args, track);
625        track.after_arm(false, &result);
626
627        let mut named_matches = match result {
628            Success(named_matches) => named_matches,
629            Failure(_) => {
630                mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
631                continue;
632            }
633            Error(_, _) => return Err(CanRetry::Yes),
634            ErrorReported(guar) => return Err(CanRetry::No(guar)),
635        };
636
637        let result = tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body, track);
638        track.after_arm(true, &result);
639
640        match result {
641            Success(body_named_matches) => {
642                psess.gated_spans.merge(gated_spans_snapshot);
643                #[allow(rustc::potential_query_instability)]
644                named_matches.extend(body_named_matches);
645                return Ok((i, rule, named_matches));
646            }
647            Failure(_) => {
648                mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut())
649            }
650            Error(_, _) => return Err(CanRetry::Yes),
651            ErrorReported(guar) => return Err(CanRetry::No(guar)),
652        }
653    }
654
655    Err(CanRetry::Yes)
656}
657
658/// Try expanding the macro derive. Returns the index of the successful arm and its
659/// named_matches if it was successful, and nothing if it failed. On failure, it's the caller's job
660/// to use `track` accordingly to record all errors correctly.
661#[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(661u32),
                                    ::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()))]
662pub(super) fn try_match_macro_derive<'matcher, T: Tracker<'matcher>>(
663    psess: &ParseSess,
664    name: Ident,
665    body: &TokenStream,
666    rules: &'matcher [MacroRule],
667    track: &mut T,
668) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
669    // This uses the same strategy as `try_match_macro`
670    let body_parser = parser_from_cx(psess, body.clone(), T::recovery());
671    let mut tt_parser = TtParser::new(name);
672    for (i, rule) in rules.iter().enumerate() {
673        let MacroRule::Derive { body, .. } = rule else { continue };
674
675        let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
676
677        let result = tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body, track);
678        track.after_arm(true, &result);
679
680        match result {
681            Success(named_matches) => {
682                psess.gated_spans.merge(gated_spans_snapshot);
683                return Ok((i, rule, named_matches));
684            }
685            Failure(_) => {
686                mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut())
687            }
688            Error(_, _) => return Err(CanRetry::Yes),
689            ErrorReported(guar) => return Err(CanRetry::No(guar)),
690        }
691    }
692
693    Err(CanRetry::Yes)
694}
695
696/// Converts a macro item into a syntax extension.
697pub fn compile_declarative_macro(
698    sess: &Session,
699    features: &Features,
700    macro_def: &ast::MacroDef,
701    ident: Ident,
702    attrs: &[hir::Attribute],
703    span: Span,
704    node_id: NodeId,
705    edition: Edition,
706) -> (SyntaxExtension, usize) {
707    let mk_syn_ext = |kind| {
708        let is_local = is_defined_in_current_crate(node_id);
709        SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
710    };
711    let dummy_syn_ext =
712        |guar| (mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar)))), 0);
713
714    let macro_rules = macro_def.macro_rules;
715    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) };
716
717    let body = macro_def.body.tokens.clone();
718    let mut p = Parser::new(&sess.psess, body, rustc_parse::MACRO_ARGUMENTS);
719
720    // Don't abort iteration early, so that multiple errors can be reported. We only abort early on
721    // parse failures we can't recover from.
722    let mut guar = None;
723    let mut check_emission = |ret: Result<(), ErrorGuaranteed>| guar = guar.or(ret.err());
724
725    let mut kinds = MacroKinds::empty();
726    let mut rules = Vec::new();
727
728    while p.token != token::Eof {
729        let unsafe_rule = p.eat_keyword_noexpect(kw::Unsafe);
730        let unsafe_keyword_span = p.prev_token.span;
731        if unsafe_rule && let Some(guar) = check_no_eof(sess, &p, "expected `attr`") {
732            return dummy_syn_ext(guar);
733        }
734        let (args, is_derive) = if p.eat_keyword_noexpect(sym::attr) {
735            kinds |= MacroKinds::ATTR;
736            if !features.macro_attr() {
737                feature_err(sess, sym::macro_attr, span, "`macro_rules!` attributes are unstable")
738                    .emit();
739            }
740            if let Some(guar) = check_no_eof(sess, &p, "expected macro attr args") {
741                return dummy_syn_ext(guar);
742            }
743            let args = p.parse_token_tree();
744            check_args_parens(sess, sym::attr, &args);
745            let args = parse_one_tt(args, RulePart::Pattern, sess, node_id, features, edition);
746            check_emission(check_lhs(sess, features, node_id, &args));
747            if let Some(guar) = check_no_eof(sess, &p, "expected macro attr body") {
748                return dummy_syn_ext(guar);
749            }
750            (Some(args), false)
751        } else if p.eat_keyword_noexpect(sym::derive) {
752            kinds |= MacroKinds::DERIVE;
753            let derive_keyword_span = p.prev_token.span;
754            if !features.macro_derive() {
755                feature_err(sess, sym::macro_derive, span, "`macro_rules!` derives are unstable")
756                    .emit();
757            }
758            if unsafe_rule {
759                sess.dcx()
760                    .span_err(unsafe_keyword_span, "`unsafe` is only supported on `attr` rules");
761            }
762            if let Some(guar) = check_no_eof(sess, &p, "expected `()` after `derive`") {
763                return dummy_syn_ext(guar);
764            }
765            let args = p.parse_token_tree();
766            check_args_parens(sess, sym::derive, &args);
767            let args_empty_result = check_args_empty(sess, &args);
768            let args_not_empty = args_empty_result.is_err();
769            check_emission(args_empty_result);
770            if let Some(guar) = check_no_eof(sess, &p, "expected macro derive body") {
771                return dummy_syn_ext(guar);
772            }
773            // If the user has `=>` right after the `()`, they might have forgotten the empty
774            // parentheses.
775            if p.token == token::FatArrow {
776                let mut err = sess
777                    .dcx()
778                    .struct_span_err(p.token.span, "expected macro derive body, got `=>`");
779                if args_not_empty {
780                    err.span_label(derive_keyword_span, "need `()` after this `derive`");
781                }
782                return dummy_syn_ext(err.emit());
783            }
784            (None, true)
785        } else {
786            kinds |= MacroKinds::BANG;
787            if unsafe_rule {
788                sess.dcx()
789                    .span_err(unsafe_keyword_span, "`unsafe` is only supported on `attr` rules");
790            }
791            (None, false)
792        };
793        let lhs_tt = p.parse_token_tree();
794        let lhs_tt = parse_one_tt(lhs_tt, RulePart::Pattern, sess, node_id, features, edition);
795        check_emission(check_lhs(sess, features, node_id, &lhs_tt));
796        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)) {
797            return dummy_syn_ext(e.emit());
798        }
799        if let Some(guar) = check_no_eof(sess, &p, "expected right-hand side of macro rule") {
800            return dummy_syn_ext(guar);
801        }
802        let rhs = p.parse_token_tree();
803        let rhs = parse_one_tt(rhs, RulePart::Body, sess, node_id, features, edition);
804        check_emission(check_rhs(sess, &rhs));
805        check_emission(check_meta_variables(&sess.psess, node_id, args.as_ref(), &lhs_tt, &rhs));
806        let lhs_span = lhs_tt.span();
807        // Convert the lhs into `MatcherLoc` form, which is better for doing the
808        // actual matching.
809        let mbe::TokenTree::Delimited(.., delimited) = lhs_tt else {
810            return dummy_syn_ext(guar.unwrap());
811        };
812        let lhs = mbe::macro_parser::compute_locs(&delimited.tts);
813        if let Some(args) = args {
814            let args_span = args.span();
815            let mbe::TokenTree::Delimited(.., delimited) = args else {
816                return dummy_syn_ext(guar.unwrap());
817            };
818            let args = mbe::macro_parser::compute_locs(&delimited.tts);
819            let body_span = lhs_span;
820            rules.push(MacroRule::Attr { unsafe_rule, args, args_span, body: lhs, body_span, rhs });
821        } else if is_derive {
822            rules.push(MacroRule::Derive { body: lhs, body_span: lhs_span, rhs });
823        } else {
824            rules.push(MacroRule::Func { lhs, lhs_span, rhs });
825        }
826        if p.token == token::Eof {
827            break;
828        }
829        if let Err(e) = p.expect(exp_sep) {
830            return dummy_syn_ext(e.emit());
831        }
832    }
833
834    if rules.is_empty() {
835        let guar = sess.dcx().span_err(span, "macros must contain at least one rule");
836        return dummy_syn_ext(guar);
837    }
838    if !!kinds.is_empty() {
    ::core::panicking::panic("assertion failed: !kinds.is_empty()")
};assert!(!kinds.is_empty());
839
840    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)
841        .unwrap_or(Transparency::fallback(macro_rules));
842
843    if let Some(guar) = guar {
844        // To avoid warning noise, only consider the rules of this
845        // macro for the lint, if all rules are valid.
846        return dummy_syn_ext(guar);
847    }
848
849    // Return the number of rules for unused rule linting, if this is a local macro.
850    let nrules = if is_defined_in_current_crate(node_id) { rules.len() } else { 0 };
851
852    let exp = MacroRulesMacroExpander { name: ident, kinds, span, node_id, transparency, rules };
853    (mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))), nrules)
854}
855
856fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {
857    if p.token == token::Eof {
858        let err_sp = p.token.span.shrink_to_hi();
859        let guar = sess
860            .dcx()
861            .struct_span_err(err_sp, "macro definition ended unexpectedly")
862            .with_span_label(err_sp, msg)
863            .emit();
864        return Some(guar);
865    }
866    None
867}
868
869fn check_args_parens(sess: &Session, rule_kw: Symbol, args: &tokenstream::TokenTree) {
870    // This does not handle the non-delimited case; that gets handled separately by `check_lhs`.
871    if let tokenstream::TokenTree::Delimited(dspan, _, delim, _) = args
872        && *delim != Delimiter::Parenthesis
873    {
874        sess.dcx().emit_err(errors::MacroArgsBadDelim {
875            span: dspan.entire(),
876            sugg: errors::MacroArgsBadDelimSugg { open: dspan.open, close: dspan.close },
877            rule_kw,
878        });
879    }
880}
881
882fn check_args_empty(sess: &Session, args: &tokenstream::TokenTree) -> Result<(), ErrorGuaranteed> {
883    match args {
884        tokenstream::TokenTree::Delimited(.., delimited) if delimited.is_empty() => Ok(()),
885        _ => {
886            let msg = "`derive` rules do not accept arguments; `derive` must be followed by `()`";
887            Err(sess.dcx().span_err(args.span(), msg))
888        }
889    }
890}
891
892fn check_lhs(
893    sess: &Session,
894    features: &Features,
895    node_id: NodeId,
896    lhs: &mbe::TokenTree,
897) -> Result<(), ErrorGuaranteed> {
898    let e1 = check_lhs_nt_follows(sess, features, node_id, lhs);
899    let e2 = check_lhs_no_empty_seq(sess, slice::from_ref(lhs));
900    e1.and(e2)
901}
902
903fn check_lhs_nt_follows(
904    sess: &Session,
905    features: &Features,
906    node_id: NodeId,
907    lhs: &mbe::TokenTree,
908) -> Result<(), ErrorGuaranteed> {
909    // lhs is going to be like TokenTree::Delimited(...), where the
910    // entire lhs is those tts. Or, it can be a "bare sequence", not wrapped in parens.
911    if let mbe::TokenTree::Delimited(.., delimited) = lhs {
912        check_matcher(sess, features, node_id, &delimited.tts)
913    } else {
914        let msg = "invalid macro matcher; matchers must be contained in balanced delimiters";
915        Err(sess.dcx().span_err(lhs.span(), msg))
916    }
917}
918
919fn is_empty_token_tree(sess: &Session, seq: &mbe::SequenceRepetition) -> bool {
920    if seq.separator.is_some() {
921        false
922    } else {
923        let mut is_empty = true;
924        let mut iter = seq.tts.iter().peekable();
925        while let Some(tt) = iter.next() {
926            match tt {
927                mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. } => {}
928                mbe::TokenTree::Token(t @ Token { kind: DocComment(..), .. }) => {
929                    let mut now = t;
930                    while let Some(&mbe::TokenTree::Token(
931                        next @ Token { kind: DocComment(..), .. },
932                    )) = iter.peek()
933                    {
934                        now = next;
935                        iter.next();
936                    }
937                    let span = t.span.to(now.span);
938                    sess.dcx().span_note(span, "doc comments are ignored in matcher position");
939                }
940                mbe::TokenTree::Sequence(_, sub_seq)
941                    if (sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
942                        || sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne) => {}
943                _ => is_empty = false,
944            }
945        }
946        is_empty
947    }
948}
949
950/// Checks if a `vis` nonterminal fragment is unnecessarily wrapped in an optional repetition.
951///
952/// When a `vis` fragment (which can already be empty) is wrapped in `$(...)?`,
953/// this suggests removing the redundant repetition syntax since it provides no additional benefit.
954fn check_redundant_vis_repetition(
955    err: &mut Diag<'_>,
956    sess: &Session,
957    seq: &SequenceRepetition,
958    span: &DelimSpan,
959) {
960    if seq.kleene.op == KleeneOp::ZeroOrOne
961        && #[allow(non_exhaustive_omitted_patterns)] match seq.tts.first() {
    Some(mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. }) =>
        true,
    _ => false,
}matches!(
962            seq.tts.first(),
963            Some(mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. })
964        )
965    {
966        err.note("a `vis` fragment can already be empty");
967        err.multipart_suggestion(
968            "remove the `$(` and `)?`",
969            ::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![
970                (
971                    sess.source_map().span_extend_to_prev_char_before(span.open, '$', true),
972                    "".to_string(),
973                ),
974                (span.close.with_hi(seq.kleene.span.hi()), "".to_string()),
975            ],
976            Applicability::MaybeIncorrect,
977        );
978    }
979}
980
981/// Checks that the lhs contains no repetition which could match an empty token
982/// tree, because then the matcher would hang indefinitely.
983fn check_lhs_no_empty_seq(sess: &Session, tts: &[mbe::TokenTree]) -> Result<(), ErrorGuaranteed> {
984    use mbe::TokenTree;
985    for tt in tts {
986        match tt {
987            TokenTree::Token(..)
988            | TokenTree::MetaVar(..)
989            | TokenTree::MetaVarDecl { .. }
990            | TokenTree::MetaVarExpr(..) => (),
991            TokenTree::Delimited(.., del) => check_lhs_no_empty_seq(sess, &del.tts)?,
992            TokenTree::Sequence(span, seq) => {
993                if is_empty_token_tree(sess, seq) {
994                    let sp = span.entire();
995                    let mut err =
996                        sess.dcx().struct_span_err(sp, "repetition matches empty token tree");
997                    check_redundant_vis_repetition(&mut err, sess, seq, span);
998                    return Err(err.emit());
999                }
1000                check_lhs_no_empty_seq(sess, &seq.tts)?
1001            }
1002        }
1003    }
1004
1005    Ok(())
1006}
1007
1008fn check_rhs(sess: &Session, rhs: &mbe::TokenTree) -> Result<(), ErrorGuaranteed> {
1009    match *rhs {
1010        mbe::TokenTree::Delimited(..) => Ok(()),
1011        _ => Err(sess.dcx().span_err(rhs.span(), "macro rhs must be delimited")),
1012    }
1013}
1014
1015fn check_matcher(
1016    sess: &Session,
1017    features: &Features,
1018    node_id: NodeId,
1019    matcher: &[mbe::TokenTree],
1020) -> Result<(), ErrorGuaranteed> {
1021    let first_sets = FirstSets::new(matcher);
1022    let empty_suffix = TokenSet::empty();
1023    check_matcher_core(sess, features, node_id, &first_sets, matcher, &empty_suffix)?;
1024    Ok(())
1025}
1026
1027fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool {
1028    match rhs {
1029        mbe::TokenTree::Delimited(.., d) => {
1030            let has_compile_error = d.tts.array_windows::<3>().any(|[ident, bang, args]| {
1031                if let mbe::TokenTree::Token(ident) = ident
1032                    && let TokenKind::Ident(ident, _) = ident.kind
1033                    && ident == sym::compile_error
1034                    && let mbe::TokenTree::Token(bang) = bang
1035                    && let TokenKind::Bang = bang.kind
1036                    && let mbe::TokenTree::Delimited(.., del) = args
1037                    && !del.delim.skip()
1038                {
1039                    true
1040                } else {
1041                    false
1042                }
1043            });
1044            if has_compile_error { true } else { d.tts.iter().any(has_compile_error_macro) }
1045        }
1046        _ => false,
1047    }
1048}
1049
1050// `The FirstSets` for a matcher is a mapping from subsequences in the
1051// matcher to the FIRST set for that subsequence.
1052//
1053// This mapping is partially precomputed via a backwards scan over the
1054// token trees of the matcher, which provides a mapping from each
1055// repetition sequence to its *first* set.
1056//
1057// (Hypothetically, sequences should be uniquely identifiable via their
1058// spans, though perhaps that is false, e.g., for macro-generated macros
1059// that do not try to inject artificial span information. My plan is
1060// to try to catch such cases ahead of time and not include them in
1061// the precomputed mapping.)
1062struct FirstSets<'tt> {
1063    // this maps each TokenTree::Sequence `$(tt ...) SEP OP` that is uniquely identified by its
1064    // span in the original matcher to the First set for the inner sequence `tt ...`.
1065    //
1066    // If two sequences have the same span in a matcher, then map that
1067    // span to None (invalidating the mapping here and forcing the code to
1068    // use a slow path).
1069    first: FxHashMap<Span, Option<TokenSet<'tt>>>,
1070}
1071
1072impl<'tt> FirstSets<'tt> {
1073    fn new(tts: &'tt [mbe::TokenTree]) -> FirstSets<'tt> {
1074        use mbe::TokenTree;
1075
1076        let mut sets = FirstSets { first: FxHashMap::default() };
1077        build_recur(&mut sets, tts);
1078        return sets;
1079
1080        // walks backward over `tts`, returning the FIRST for `tts`
1081        // and updating `sets` at the same time for all sequence
1082        // substructure we find within `tts`.
1083        fn build_recur<'tt>(sets: &mut FirstSets<'tt>, tts: &'tt [TokenTree]) -> TokenSet<'tt> {
1084            let mut first = TokenSet::empty();
1085            for tt in tts.iter().rev() {
1086                match tt {
1087                    TokenTree::Token(..)
1088                    | TokenTree::MetaVar(..)
1089                    | TokenTree::MetaVarDecl { .. }
1090                    | TokenTree::MetaVarExpr(..) => {
1091                        first.replace_with(TtHandle::TtRef(tt));
1092                    }
1093                    TokenTree::Delimited(span, _, delimited) => {
1094                        build_recur(sets, &delimited.tts);
1095                        first.replace_with(TtHandle::from_token_kind(
1096                            delimited.delim.as_open_token_kind(),
1097                            span.open,
1098                        ));
1099                    }
1100                    TokenTree::Sequence(sp, seq_rep) => {
1101                        let subfirst = build_recur(sets, &seq_rep.tts);
1102
1103                        match sets.first.entry(sp.entire()) {
1104                            Entry::Vacant(vac) => {
1105                                vac.insert(Some(subfirst.clone()));
1106                            }
1107                            Entry::Occupied(mut occ) => {
1108                                // if there is already an entry, then a span must have collided.
1109                                // This should not happen with typical macro_rules macros,
1110                                // but syntax extensions need not maintain distinct spans,
1111                                // so distinct syntax trees can be assigned the same span.
1112                                // In such a case, the map cannot be trusted; so mark this
1113                                // entry as unusable.
1114                                occ.insert(None);
1115                            }
1116                        }
1117
1118                        // If the sequence contents can be empty, then the first
1119                        // token could be the separator token itself.
1120
1121                        if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
1122                            first.add_one_maybe(TtHandle::from_token(*sep));
1123                        }
1124
1125                        // Reverse scan: Sequence comes before `first`.
1126                        if subfirst.maybe_empty
1127                            || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
1128                            || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
1129                        {
1130                            // If sequence is potentially empty, then
1131                            // union them (preserving first emptiness).
1132                            first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
1133                        } else {
1134                            // Otherwise, sequence guaranteed
1135                            // non-empty; replace first.
1136                            first = subfirst;
1137                        }
1138                    }
1139                }
1140            }
1141
1142            first
1143        }
1144    }
1145
1146    // walks forward over `tts` until all potential FIRST tokens are
1147    // identified.
1148    fn first(&self, tts: &'tt [mbe::TokenTree]) -> TokenSet<'tt> {
1149        use mbe::TokenTree;
1150
1151        let mut first = TokenSet::empty();
1152        for tt in tts.iter() {
1153            if !first.maybe_empty {
    ::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1154            match tt {
1155                TokenTree::Token(..)
1156                | TokenTree::MetaVar(..)
1157                | TokenTree::MetaVarDecl { .. }
1158                | TokenTree::MetaVarExpr(..) => {
1159                    first.add_one(TtHandle::TtRef(tt));
1160                    return first;
1161                }
1162                TokenTree::Delimited(span, _, delimited) => {
1163                    first.add_one(TtHandle::from_token_kind(
1164                        delimited.delim.as_open_token_kind(),
1165                        span.open,
1166                    ));
1167                    return first;
1168                }
1169                TokenTree::Sequence(sp, seq_rep) => {
1170                    let subfirst_owned;
1171                    let subfirst = match self.first.get(&sp.entire()) {
1172                        Some(Some(subfirst)) => subfirst,
1173                        Some(&None) => {
1174                            subfirst_owned = self.first(&seq_rep.tts);
1175                            &subfirst_owned
1176                        }
1177                        None => {
1178                            {
    ::core::panicking::panic_fmt(format_args!("We missed a sequence during FirstSets construction"));
};panic!("We missed a sequence during FirstSets construction");
1179                        }
1180                    };
1181
1182                    // If the sequence contents can be empty, then the first
1183                    // token could be the separator token itself.
1184                    if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
1185                        first.add_one_maybe(TtHandle::from_token(*sep));
1186                    }
1187
1188                    if !first.maybe_empty {
    ::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1189                    first.add_all(subfirst);
1190                    if subfirst.maybe_empty
1191                        || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
1192                        || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
1193                    {
1194                        // Continue scanning for more first
1195                        // tokens, but also make sure we
1196                        // restore empty-tracking state.
1197                        first.maybe_empty = true;
1198                        continue;
1199                    } else {
1200                        return first;
1201                    }
1202                }
1203            }
1204        }
1205
1206        // we only exit the loop if `tts` was empty or if every
1207        // element of `tts` matches the empty sequence.
1208        if !first.maybe_empty {
    ::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1209        first
1210    }
1211}
1212
1213// Most `mbe::TokenTree`s are preexisting in the matcher, but some are defined
1214// implicitly, such as opening/closing delimiters and sequence repetition ops.
1215// This type encapsulates both kinds. It implements `Clone` while avoiding the
1216// need for `mbe::TokenTree` to implement `Clone`.
1217#[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)]
1218enum TtHandle<'tt> {
1219    /// This is used in most cases.
1220    TtRef(&'tt mbe::TokenTree),
1221
1222    /// This is only used for implicit token trees. The `mbe::TokenTree` *must*
1223    /// be `mbe::TokenTree::Token`. No other variants are allowed. We store an
1224    /// `mbe::TokenTree` rather than a `Token` so that `get()` can return a
1225    /// `&mbe::TokenTree`.
1226    Token(mbe::TokenTree),
1227}
1228
1229impl<'tt> TtHandle<'tt> {
1230    fn from_token(tok: Token) -> Self {
1231        TtHandle::Token(mbe::TokenTree::Token(tok))
1232    }
1233
1234    fn from_token_kind(kind: TokenKind, span: Span) -> Self {
1235        TtHandle::from_token(Token::new(kind, span))
1236    }
1237
1238    // Get a reference to a token tree.
1239    fn get(&'tt self) -> &'tt mbe::TokenTree {
1240        match self {
1241            TtHandle::TtRef(tt) => tt,
1242            TtHandle::Token(token_tt) => token_tt,
1243        }
1244    }
1245}
1246
1247impl<'tt> PartialEq for TtHandle<'tt> {
1248    fn eq(&self, other: &TtHandle<'tt>) -> bool {
1249        self.get() == other.get()
1250    }
1251}
1252
1253impl<'tt> Clone for TtHandle<'tt> {
1254    fn clone(&self) -> Self {
1255        match self {
1256            TtHandle::TtRef(tt) => TtHandle::TtRef(tt),
1257
1258            // This variant *must* contain a `mbe::TokenTree::Token`, and not
1259            // any other variant of `mbe::TokenTree`.
1260            TtHandle::Token(mbe::TokenTree::Token(tok)) => {
1261                TtHandle::Token(mbe::TokenTree::Token(*tok))
1262            }
1263
1264            _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1265        }
1266    }
1267}
1268
1269// A set of `mbe::TokenTree`s, which may include `TokenTree::Match`s
1270// (for macro-by-example syntactic variables). It also carries the
1271// `maybe_empty` flag; that is true if and only if the matcher can
1272// match an empty token sequence.
1273//
1274// The First set is computed on submatchers like `$($a:expr b),* $(c)* d`,
1275// which has corresponding FIRST = {$a:expr, c, d}.
1276// Likewise, `$($a:expr b),* $(c)+ d` has FIRST = {$a:expr, c}.
1277//
1278// (Notably, we must allow for *-op to occur zero times.)
1279#[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)]
1280struct TokenSet<'tt> {
1281    tokens: Vec<TtHandle<'tt>>,
1282    maybe_empty: bool,
1283}
1284
1285impl<'tt> TokenSet<'tt> {
1286    // Returns a set for the empty sequence.
1287    fn empty() -> Self {
1288        TokenSet { tokens: Vec::new(), maybe_empty: true }
1289    }
1290
1291    // Returns the set `{ tok }` for the single-token (and thus
1292    // non-empty) sequence [tok].
1293    fn singleton(tt: TtHandle<'tt>) -> Self {
1294        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 }
1295    }
1296
1297    // Changes self to be the set `{ tok }`.
1298    // Since `tok` is always present, marks self as non-empty.
1299    fn replace_with(&mut self, tt: TtHandle<'tt>) {
1300        self.tokens.clear();
1301        self.tokens.push(tt);
1302        self.maybe_empty = false;
1303    }
1304
1305    // Changes self to be the empty set `{}`; meant for use when
1306    // the particular token does not matter, but we want to
1307    // record that it occurs.
1308    fn replace_with_irrelevant(&mut self) {
1309        self.tokens.clear();
1310        self.maybe_empty = false;
1311    }
1312
1313    // Adds `tok` to the set for `self`, marking sequence as non-empty.
1314    fn add_one(&mut self, tt: TtHandle<'tt>) {
1315        if !self.tokens.contains(&tt) {
1316            self.tokens.push(tt);
1317        }
1318        self.maybe_empty = false;
1319    }
1320
1321    // Adds `tok` to the set for `self`. (Leaves `maybe_empty` flag alone.)
1322    fn add_one_maybe(&mut self, tt: TtHandle<'tt>) {
1323        if !self.tokens.contains(&tt) {
1324            self.tokens.push(tt);
1325        }
1326    }
1327
1328    // Adds all elements of `other` to this.
1329    //
1330    // (Since this is a set, we filter out duplicates.)
1331    //
1332    // If `other` is potentially empty, then preserves the previous
1333    // setting of the empty flag of `self`. If `other` is guaranteed
1334    // non-empty, then `self` is marked non-empty.
1335    fn add_all(&mut self, other: &Self) {
1336        for tt in &other.tokens {
1337            if !self.tokens.contains(tt) {
1338                self.tokens.push(tt.clone());
1339            }
1340        }
1341        if !other.maybe_empty {
1342            self.maybe_empty = false;
1343        }
1344    }
1345}
1346
1347// Checks that `matcher` is internally consistent and that it
1348// can legally be followed by a token `N`, for all `N` in `follow`.
1349// (If `follow` is empty, then it imposes no constraint on
1350// the `matcher`.)
1351//
1352// Returns the set of NT tokens that could possibly come last in
1353// `matcher`. (If `matcher` matches the empty sequence, then
1354// `maybe_empty` will be set to true.)
1355//
1356// Requires that `first_sets` is pre-computed for `matcher`;
1357// see `FirstSets::new`.
1358fn check_matcher_core<'tt>(
1359    sess: &Session,
1360    features: &Features,
1361    node_id: NodeId,
1362    first_sets: &FirstSets<'tt>,
1363    matcher: &'tt [mbe::TokenTree],
1364    follow: &TokenSet<'tt>,
1365) -> Result<TokenSet<'tt>, ErrorGuaranteed> {
1366    use mbe::TokenTree;
1367
1368    let mut last = TokenSet::empty();
1369
1370    let mut errored = Ok(());
1371
1372    // 2. For each token and suffix  [T, SUFFIX] in M:
1373    // ensure that T can be followed by SUFFIX, and if SUFFIX may be empty,
1374    // then ensure T can also be followed by any element of FOLLOW.
1375    'each_token: for i in 0..matcher.len() {
1376        let token = &matcher[i];
1377        let suffix = &matcher[i + 1..];
1378
1379        let build_suffix_first = || {
1380            let mut s = first_sets.first(suffix);
1381            if s.maybe_empty {
1382                s.add_all(follow);
1383            }
1384            s
1385        };
1386
1387        // (we build `suffix_first` on demand below; you can tell
1388        // which cases are supposed to fall through by looking for the
1389        // initialization of this variable.)
1390        let suffix_first;
1391
1392        // First, update `last` so that it corresponds to the set
1393        // of NT tokens that might end the sequence `... token`.
1394        match token {
1395            TokenTree::Token(..)
1396            | TokenTree::MetaVar(..)
1397            | TokenTree::MetaVarDecl { .. }
1398            | TokenTree::MetaVarExpr(..) => {
1399                if let TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } = token
1400                    && !features.macro_guard_matcher()
1401                {
1402                    feature_err(
1403                        sess,
1404                        sym::macro_guard_matcher,
1405                        token.span(),
1406                        "`guard` fragments in macro are unstable",
1407                    )
1408                    .emit();
1409                }
1410                if token_can_be_followed_by_any(token) {
1411                    // don't need to track tokens that work with any,
1412                    last.replace_with_irrelevant();
1413                    // ... and don't need to check tokens that can be
1414                    // followed by anything against SUFFIX.
1415                    continue 'each_token;
1416                } else {
1417                    last.replace_with(TtHandle::TtRef(token));
1418                    suffix_first = build_suffix_first();
1419                }
1420            }
1421            TokenTree::Delimited(span, _, d) => {
1422                let my_suffix = TokenSet::singleton(TtHandle::from_token_kind(
1423                    d.delim.as_close_token_kind(),
1424                    span.close,
1425                ));
1426                check_matcher_core(sess, features, node_id, first_sets, &d.tts, &my_suffix)?;
1427                // don't track non NT tokens
1428                last.replace_with_irrelevant();
1429
1430                // also, we don't need to check delimited sequences
1431                // against SUFFIX
1432                continue 'each_token;
1433            }
1434            TokenTree::Sequence(_, seq_rep) => {
1435                suffix_first = build_suffix_first();
1436                // The trick here: when we check the interior, we want
1437                // to include the separator (if any) as a potential
1438                // (but not guaranteed) element of FOLLOW. So in that
1439                // case, we make a temp copy of suffix and stuff
1440                // delimiter in there.
1441                //
1442                // FIXME: Should I first scan suffix_first to see if
1443                // delimiter is already in it before I go through the
1444                // work of cloning it? But then again, this way I may
1445                // get a "tighter" span?
1446                let mut new;
1447                let my_suffix = if let Some(sep) = &seq_rep.separator {
1448                    new = suffix_first.clone();
1449                    new.add_one_maybe(TtHandle::from_token(*sep));
1450                    &new
1451                } else {
1452                    &suffix_first
1453                };
1454
1455                // At this point, `suffix_first` is built, and
1456                // `my_suffix` is some TokenSet that we can use
1457                // for checking the interior of `seq_rep`.
1458                let next = check_matcher_core(
1459                    sess,
1460                    features,
1461                    node_id,
1462                    first_sets,
1463                    &seq_rep.tts,
1464                    my_suffix,
1465                )?;
1466                if next.maybe_empty {
1467                    last.add_all(&next);
1468                } else {
1469                    last = next;
1470                }
1471
1472                // the recursive call to check_matcher_core already ran the 'each_last
1473                // check below, so we can just keep going forward here.
1474                continue 'each_token;
1475            }
1476        }
1477
1478        // (`suffix_first` guaranteed initialized once reaching here.)
1479
1480        // Now `last` holds the complete set of NT tokens that could
1481        // end the sequence before SUFFIX. Check that every one works with `suffix`.
1482        for tt in &last.tokens {
1483            if let &TokenTree::MetaVarDecl { span, name, kind } = tt.get() {
1484                for next_token in &suffix_first.tokens {
1485                    let next_token = next_token.get();
1486
1487                    // Check if the old pat is used and the next token is `|`
1488                    // to warn about incompatibility with Rust 2021.
1489                    // We only emit this lint if we're parsing the original
1490                    // definition of this macro_rules, not while (re)parsing
1491                    // the macro when compiling another crate that is using the
1492                    // macro. (See #86567.)
1493                    if is_defined_in_current_crate(node_id)
1494                        && #[allow(non_exhaustive_omitted_patterns)] match kind {
    NonterminalKind::Pat(PatParam { inferred: true }) => true,
    _ => false,
}matches!(kind, NonterminalKind::Pat(PatParam { inferred: true }))
1495                        && #[allow(non_exhaustive_omitted_patterns)] match next_token {
    TokenTree::Token(token) if *token == token::Or => true,
    _ => false,
}matches!(
1496                            next_token,
1497                            TokenTree::Token(token) if *token == token::Or
1498                        )
1499                    {
1500                        // It is suggestion to use pat_param, for example: $x:pat -> $x:pat_param.
1501                        let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl {
1502                            span,
1503                            name,
1504                            kind: NonterminalKind::Pat(PatParam { inferred: false }),
1505                        });
1506                        sess.psess.buffer_lint(
1507                            RUST_2021_INCOMPATIBLE_OR_PATTERNS,
1508                            span,
1509                            ast::CRATE_NODE_ID,
1510                            errors::OrPatternsBackCompat { span, suggestion },
1511                        );
1512                    }
1513                    match is_in_follow(next_token, kind) {
1514                        IsInFollow::Yes => {}
1515                        IsInFollow::No(possible) => {
1516                            let may_be = if last.tokens.len() == 1 && suffix_first.tokens.len() == 1
1517                            {
1518                                "is"
1519                            } else {
1520                                "may be"
1521                            };
1522
1523                            let sp = next_token.span();
1524                            let mut err = sess.dcx().struct_span_err(
1525                                sp,
1526                                ::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!(
1527                                    "`${name}:{frag}` {may_be} followed by `{next}`, which \
1528                                     is not allowed for `{frag}` fragments",
1529                                    name = name,
1530                                    frag = kind,
1531                                    next = quoted_tt_to_string(next_token),
1532                                    may_be = may_be
1533                                ),
1534                            );
1535                            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"));
1536
1537                            if kind == NonterminalKind::Pat(PatWithOr)
1538                                && sess.psess.edition.at_least_rust_2021()
1539                                && next_token.is_token(&token::Or)
1540                            {
1541                                let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl {
1542                                    span,
1543                                    name,
1544                                    kind: NonterminalKind::Pat(PatParam { inferred: false }),
1545                                });
1546                                err.span_suggestion(
1547                                    span,
1548                                    "try a `pat_param` fragment specifier instead",
1549                                    suggestion,
1550                                    Applicability::MaybeIncorrect,
1551                                );
1552                            }
1553
1554                            let msg = "allowed there are: ";
1555                            match possible {
1556                                &[] => {}
1557                                &[t] => {
1558                                    err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("only {0} is allowed after `{1}` fragments",
                t, kind))
    })format!(
1559                                        "only {t} is allowed after `{kind}` fragments",
1560                                    ));
1561                                }
1562                                ts => {
1563                                    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!(
1564                                        "{}{} or {}",
1565                                        msg,
1566                                        ts[..ts.len() - 1].to_vec().join(", "),
1567                                        ts[ts.len() - 1],
1568                                    ));
1569                                }
1570                            }
1571                            errored = Err(err.emit());
1572                        }
1573                    }
1574                }
1575            }
1576        }
1577    }
1578    errored?;
1579    Ok(last)
1580}
1581
1582fn token_can_be_followed_by_any(tok: &mbe::TokenTree) -> bool {
1583    if let mbe::TokenTree::MetaVarDecl { kind, .. } = *tok {
1584        frag_can_be_followed_by_any(kind)
1585    } else {
1586        // (Non NT's can always be followed by anything in matchers.)
1587        true
1588    }
1589}
1590
1591/// Returns `true` if a fragment of type `frag` can be followed by any sort of
1592/// token. We use this (among other things) as a useful approximation
1593/// for when `frag` can be followed by a repetition like `$(...)*` or
1594/// `$(...)+`. In general, these can be a bit tricky to reason about,
1595/// so we adopt a conservative position that says that any fragment
1596/// specifier which consumes at most one token tree can be followed by
1597/// a fragment specifier (indeed, these fragments can be followed by
1598/// ANYTHING without fear of future compatibility hazards).
1599fn frag_can_be_followed_by_any(kind: NonterminalKind) -> bool {
1600    #[allow(non_exhaustive_omitted_patterns)] match kind {
    NonterminalKind::Item | NonterminalKind::Block | NonterminalKind::Ident |
        NonterminalKind::Literal | NonterminalKind::Meta |
        NonterminalKind::Lifetime | NonterminalKind::TT => true,
    _ => false,
}matches!(
1601        kind,
1602        NonterminalKind::Item           // always terminated by `}` or `;`
1603        | NonterminalKind::Block        // exactly one token tree
1604        | NonterminalKind::Ident        // exactly one token tree
1605        | NonterminalKind::Literal      // exactly one token tree
1606        | NonterminalKind::Meta         // exactly one token tree
1607        | NonterminalKind::Lifetime     // exactly one token tree
1608        | NonterminalKind::TT // exactly one token tree
1609    )
1610}
1611
1612enum IsInFollow {
1613    Yes,
1614    No(&'static [&'static str]),
1615}
1616
1617/// Returns `true` if `frag` can legally be followed by the token `tok`. For
1618/// fragments that can consume an unbounded number of tokens, `tok`
1619/// must be within a well-defined follow set. This is intended to
1620/// guarantee future compatibility: for example, without this rule, if
1621/// we expanded `expr` to include a new binary operator, we might
1622/// break macros that were relying on that binary operator as a
1623/// separator.
1624// when changing this do not forget to update doc/book/macros.md!
1625fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
1626    use mbe::TokenTree;
1627
1628    if let TokenTree::Token(Token { kind, .. }) = tok
1629        && kind.close_delim().is_some()
1630    {
1631        // closing a token tree can never be matched by any fragment;
1632        // iow, we always require that `(` and `)` match, etc.
1633        IsInFollow::Yes
1634    } else {
1635        match kind {
1636            NonterminalKind::Item => {
1637                // since items *must* be followed by either a `;` or a `}`, we can
1638                // accept anything after them
1639                IsInFollow::Yes
1640            }
1641            NonterminalKind::Block => {
1642                // anything can follow block, the braces provide an easy boundary to
1643                // maintain
1644                IsInFollow::Yes
1645            }
1646            NonterminalKind::Stmt | NonterminalKind::Expr(_) => {
1647                const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
1648                match tok {
1649                    TokenTree::Token(token) => match token.kind {
1650                        FatArrow | Comma | Semi => IsInFollow::Yes,
1651                        _ => IsInFollow::No(TOKENS),
1652                    },
1653                    _ => IsInFollow::No(TOKENS),
1654                }
1655            }
1656            NonterminalKind::Pat(PatParam { .. }) => {
1657                const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`if let`", "`in`"];
1658                match tok {
1659                    TokenTree::Token(token) => match token.kind {
1660                        FatArrow | Comma | Eq | Or => IsInFollow::Yes,
1661                        Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1662                            IsInFollow::Yes
1663                        }
1664                        _ => IsInFollow::No(TOKENS),
1665                    },
1666                    TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } => IsInFollow::Yes,
1667                    _ => IsInFollow::No(TOKENS),
1668                }
1669            }
1670            NonterminalKind::Pat(PatWithOr) => {
1671                const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`if`", "`if let`", "`in`"];
1672                match tok {
1673                    TokenTree::Token(token) => match token.kind {
1674                        FatArrow | Comma | Eq => IsInFollow::Yes,
1675                        Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1676                            IsInFollow::Yes
1677                        }
1678                        _ => IsInFollow::No(TOKENS),
1679                    },
1680                    TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } => IsInFollow::Yes,
1681                    _ => IsInFollow::No(TOKENS),
1682                }
1683            }
1684            NonterminalKind::Guard => {
1685                const TOKENS: &[&str] = &["`=>`", "`,`", "`{`"];
1686                match tok {
1687                    TokenTree::Token(token) => match token.kind {
1688                        FatArrow | Comma | OpenBrace => IsInFollow::Yes,
1689                        _ => IsInFollow::No(TOKENS),
1690                    },
1691                    _ => IsInFollow::No(TOKENS),
1692                }
1693            }
1694            NonterminalKind::Path | NonterminalKind::Ty => {
1695                const TOKENS: &[&str] = &[
1696                    "`{`", "`[`", "`=>`", "`,`", "`>`", "`=`", "`:`", "`;`", "`|`", "`as`",
1697                    "`where`",
1698                ];
1699                match tok {
1700                    TokenTree::Token(token) => match token.kind {
1701                        OpenBrace | OpenBracket | Comma | FatArrow | Colon | Eq | Gt | Shr
1702                        | Semi | Or => IsInFollow::Yes,
1703                        Ident(name, IdentIsRaw::No) if name == kw::As || name == kw::Where => {
1704                            IsInFollow::Yes
1705                        }
1706                        _ => IsInFollow::No(TOKENS),
1707                    },
1708                    TokenTree::MetaVarDecl { kind: NonterminalKind::Block, .. } => IsInFollow::Yes,
1709                    _ => IsInFollow::No(TOKENS),
1710                }
1711            }
1712            NonterminalKind::Ident | NonterminalKind::Lifetime => {
1713                // being a single token, idents and lifetimes are harmless
1714                IsInFollow::Yes
1715            }
1716            NonterminalKind::Literal => {
1717                // literals may be of a single token, or two tokens (negative numbers)
1718                IsInFollow::Yes
1719            }
1720            NonterminalKind::Meta | NonterminalKind::TT => {
1721                // being either a single token or a delimited sequence, tt is
1722                // harmless
1723                IsInFollow::Yes
1724            }
1725            NonterminalKind::Vis => {
1726                // Explicitly disallow `priv`, on the off chance it comes back.
1727                const TOKENS: &[&str] = &["`,`", "an ident", "a type"];
1728                match tok {
1729                    TokenTree::Token(token) => match token.kind {
1730                        Comma => IsInFollow::Yes,
1731                        Ident(_, IdentIsRaw::Yes) => IsInFollow::Yes,
1732                        Ident(name, _) if name != kw::Priv => IsInFollow::Yes,
1733                        _ => {
1734                            if token.can_begin_type() {
1735                                IsInFollow::Yes
1736                            } else {
1737                                IsInFollow::No(TOKENS)
1738                            }
1739                        }
1740                    },
1741                    TokenTree::MetaVarDecl {
1742                        kind: NonterminalKind::Ident | NonterminalKind::Ty | NonterminalKind::Path,
1743                        ..
1744                    } => IsInFollow::Yes,
1745                    _ => IsInFollow::No(TOKENS),
1746                }
1747            }
1748        }
1749    }
1750}
1751
1752fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
1753    match tt {
1754        mbe::TokenTree::Token(token) => pprust::token_to_string(token).into(),
1755        mbe::TokenTree::MetaVar(_, name) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("${0}", name))
    })format!("${name}"),
1756        mbe::TokenTree::MetaVarDecl { name, kind, .. } => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("${0}:{1}", name, kind))
    })format!("${name}:{kind}"),
1757        _ => {
    ::core::panicking::panic_display(&"unexpected mbe::TokenTree::{Sequence or Delimited} \
             in follow set checker");
}panic!(
1758            "{}",
1759            "unexpected mbe::TokenTree::{Sequence or Delimited} \
1760             in follow set checker"
1761        ),
1762    }
1763}
1764
1765fn is_defined_in_current_crate(node_id: NodeId) -> bool {
1766    // Macros defined in the current crate have a real node id,
1767    // whereas macros from an external crate have a dummy id.
1768    node_id != DUMMY_NODE_ID
1769}
1770
1771pub(super) fn parser_from_cx(
1772    psess: &ParseSess,
1773    mut tts: TokenStream,
1774    recovery: Recovery,
1775) -> Parser<'_> {
1776    tts.desugar_doc_comments();
1777    Parser::new(psess, tts, rustc_parse::MACRO_ARGUMENTS).recovery(recovery)
1778}