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