Skip to main content

rustc_expand/mbe/
macro_rules.rs

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

    #[warn(clippy :: suspicious_else_formatting)]
    {

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

    #[warn(clippy :: suspicious_else_formatting)]
    {

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

    #[warn(clippy :: suspicious_else_formatting)]
    {

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