1use std::borrow::Cow;
2use std::collections::hash_map::Entry;
3use std::sync::Arc;
4use std::{mem, slice};
5
6use ast::token::IdentIsRaw;
7use rustc_ast::token::NtPatKind::*;
8use rustc_ast::token::TokenKind::*;
9use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind};
10use rustc_ast::tokenstream::{self, DelimSpan, TokenStream};
11use rustc_ast::{self as ast, DUMMY_NODE_ID, NodeId, Safety};
12use rustc_ast_pretty::pprust;
13use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
14use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan};
15use rustc_feature::Features;
16use rustc_hir as hir;
17use rustc_hir::def::MacroKinds;
18use rustc_hir::find_attr;
19use rustc_lint_defs::builtin::{
20 RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
21};
22use rustc_parse::exp;
23use rustc_parse::parser::{Parser, Recovery};
24use rustc_session::Session;
25use rustc_session::parse::{ParseSess, feature_err};
26use rustc_span::edition::Edition;
27use rustc_span::hygiene::Transparency;
28use rustc_span::{Ident, Span, Symbol, kw, sym};
29use tracing::{debug, instrument, trace, trace_span};
30
31use super::diagnostics::{FailedMacro, failed_to_match_macro};
32use super::macro_parser::{NamedMatches, NamedParseResult};
33use super::{SequenceRepetition, diagnostics};
34use crate::base::{
35 AttrProcMacro, BangProcMacro, DummyResult, ExpandResult, ExtCtxt, MacResult,
36 MacroExpanderResult, SyntaxExtension, SyntaxExtensionKind, TTMacroExpander,
37};
38use crate::errors;
39use crate::expand::{AstFragment, AstFragmentKind, ensure_complete_parse, parse_ast_fragment};
40use crate::mbe::macro_check::check_meta_variables;
41use crate::mbe::macro_parser::{Error, ErrorReported, Failure, MatcherLoc, Success, TtParser};
42use crate::mbe::quoted::{RulePart, parse_one_tt};
43use crate::mbe::transcribe::transcribe;
44use crate::mbe::{self, KleeneOp};
45
46pub(crate) struct ParserAnyMacro<'a, 'b> {
47 parser: Parser<'a>,
48
49 site_span: Span,
51 macro_ident: Ident,
53 lint_node_id: NodeId,
54 is_trailing_mac: bool,
55 arm_span: Span,
56 is_local: bool,
58 bindings: &'b [MacroRule],
59 matched_rule_bindings: &'b [MatcherLoc],
60}
61
62impl<'a, 'b> ParserAnyMacro<'a, 'b> {
63 pub(crate) fn make(
64 mut self: Box<ParserAnyMacro<'a, 'b>>,
65 kind: AstFragmentKind,
66 ) -> AstFragment {
67 let ParserAnyMacro {
68 site_span,
69 macro_ident,
70 ref mut parser,
71 lint_node_id,
72 arm_span,
73 is_trailing_mac,
74 is_local,
75 bindings,
76 matched_rule_bindings,
77 } = *self;
78 let snapshot = &mut parser.create_snapshot_for_diagnostic();
79 let fragment = match parse_ast_fragment(parser, kind) {
80 Ok(f) => f,
81 Err(err) => {
82 let guar = diagnostics::emit_frag_parse_err(
83 err,
84 parser,
85 snapshot,
86 site_span,
87 arm_span,
88 kind,
89 bindings,
90 matched_rule_bindings,
91 );
92 return kind.dummy(site_span, guar);
93 }
94 };
95
96 if kind == AstFragmentKind::Expr && parser.token == token::Semi {
100 if is_local {
101 parser.psess.buffer_lint(
102 SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
103 parser.token.span,
104 lint_node_id,
105 errors::TrailingMacro { is_trailing: is_trailing_mac, name: macro_ident },
106 );
107 }
108 parser.bump();
109 }
110
111 let path = ast::Path::from_ident(macro_ident.with_span_pos(site_span));
113 ensure_complete_parse(parser, &path, kind.name(), site_span);
114 fragment
115 }
116
117 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("from_tts",
"rustc_expand::mbe::macro_rules", ::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(117u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["site_span",
"arm_span", "is_local", "macro_ident"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::INFO <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&site_span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&arm_span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&is_local as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(¯o_ident)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Self = loop {};
return __tracing_attr_fake_return;
}
{
Self {
parser: Parser::new(&cx.sess.psess, tts, None),
site_span,
macro_ident,
lint_node_id: cx.current_expansion.lint_node_id,
is_trailing_mac: cx.current_expansion.is_trailing_mac,
arm_span,
is_local,
bindings,
matched_rule_bindings,
}
}
}
}#[instrument(skip(cx, tts, bindings, matched_rule_bindings))]
118 pub(crate) fn from_tts<'cx>(
119 cx: &'cx mut ExtCtxt<'a>,
120 tts: TokenStream,
121 site_span: Span,
122 arm_span: Span,
123 is_local: bool,
124 macro_ident: Ident,
125 bindings: &'b [MacroRule],
127 matched_rule_bindings: &'b [MatcherLoc],
128 ) -> Self {
129 Self {
130 parser: Parser::new(&cx.sess.psess, tts, None),
131
132 site_span,
136 macro_ident,
137 lint_node_id: cx.current_expansion.lint_node_id,
138 is_trailing_mac: cx.current_expansion.is_trailing_mac,
139 arm_span,
140 is_local,
141 bindings,
142 matched_rule_bindings,
143 }
144 }
145}
146
147pub(crate) enum MacroRule {
148 Func { lhs: Vec<MatcherLoc>, lhs_span: Span, rhs: mbe::TokenTree },
150 Attr {
152 unsafe_rule: bool,
153 args: Vec<MatcherLoc>,
154 args_span: Span,
155 body: Vec<MatcherLoc>,
156 body_span: Span,
157 rhs: mbe::TokenTree,
158 },
159 Derive { body: Vec<MatcherLoc>, body_span: Span, rhs: mbe::TokenTree },
161}
162
163pub struct MacroRulesMacroExpander {
164 node_id: NodeId,
165 name: Ident,
166 span: Span,
167 transparency: Transparency,
168 kinds: MacroKinds,
169 rules: Vec<MacroRule>,
170}
171
172impl MacroRulesMacroExpander {
173 pub fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, MultiSpan)> {
174 let (span, rhs) = match self.rules[rule_i] {
176 MacroRule::Func { lhs_span, ref rhs, .. } => (MultiSpan::from_span(lhs_span), rhs),
177 MacroRule::Attr { args_span, body_span, ref rhs, .. } => {
178 (MultiSpan::from_spans(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[args_span, body_span]))vec![args_span, body_span]), rhs)
179 }
180 MacroRule::Derive { body_span, ref rhs, .. } => (MultiSpan::from_span(body_span), rhs),
181 };
182 if has_compile_error_macro(rhs) { None } else { Some((&self.name, span)) }
183 }
184
185 pub fn kinds(&self) -> MacroKinds {
186 self.kinds
187 }
188
189 pub fn expand_derive(
190 &self,
191 cx: &mut ExtCtxt<'_>,
192 sp: Span,
193 body: &TokenStream,
194 ) -> Result<TokenStream, ErrorGuaranteed> {
195 let Self { name, ref rules, node_id, .. } = *self;
198 let psess = &cx.sess.psess;
199
200 if cx.trace_macros() {
201 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expanding `#[derive({1})] {0}`",
pprust::tts_to_string(body), name))
})format!("expanding `#[derive({name})] {}`", pprust::tts_to_string(body));
202 trace_macros_note(&mut cx.expansions, sp, msg);
203 }
204
205 match try_match_macro_derive(psess, name, body, rules, &mut NoopTracker) {
206 Ok((rule_index, rule, named_matches)) => {
207 let MacroRule::Derive { rhs, .. } = rule else {
208 {
::core::panicking::panic_fmt(format_args!("try_match_macro_derive returned non-derive rule"));
};panic!("try_match_macro_derive returned non-derive rule");
209 };
210 let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
211 cx.dcx().span_bug(sp, "malformed macro derive rhs");
212 };
213
214 let id = cx.current_expansion.id;
215 let tts = transcribe(psess, &named_matches, rhs, *rhs_span, self.transparency, id)
216 .map_err(|e| e.emit())?;
217
218 if cx.trace_macros() {
219 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to `{0}`",
pprust::tts_to_string(&tts)))
})format!("to `{}`", pprust::tts_to_string(&tts));
220 trace_macros_note(&mut cx.expansions, sp, msg);
221 }
222
223 if is_defined_in_current_crate(node_id) {
224 cx.resolver.record_macro_rule_usage(node_id, rule_index);
225 }
226
227 Ok(tts)
228 }
229 Err(CanRetry::No(guar)) => Err(guar),
230 Err(CanRetry::Yes) => {
231 let (_, guar) = failed_to_match_macro(
232 cx.psess(),
233 sp,
234 self.span,
235 name,
236 FailedMacro::Derive,
237 body,
238 rules,
239 );
240 cx.macro_error_and_trace_macros_diag();
241 Err(guar)
242 }
243 }
244 }
245}
246
247impl TTMacroExpander for MacroRulesMacroExpander {
248 fn expand<'cx, 'a: 'cx>(
249 &'a self,
250 cx: &'cx mut ExtCtxt<'_>,
251 sp: Span,
252 input: TokenStream,
253 ) -> MacroExpanderResult<'cx> {
254 ExpandResult::Ready(expand_macro(
255 cx,
256 sp,
257 self.span,
258 self.node_id,
259 self.name,
260 self.transparency,
261 input,
262 &self.rules,
263 ))
264 }
265}
266
267impl AttrProcMacro for MacroRulesMacroExpander {
268 fn expand(
269 &self,
270 _cx: &mut ExtCtxt<'_>,
271 _sp: Span,
272 _args: TokenStream,
273 _body: TokenStream,
274 ) -> Result<TokenStream, ErrorGuaranteed> {
275 {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("`expand` called on `MacroRulesMacroExpander`, expected `expand_with_safety`")));
}unreachable!("`expand` called on `MacroRulesMacroExpander`, expected `expand_with_safety`")
276 }
277
278 fn expand_with_safety(
279 &self,
280 cx: &mut ExtCtxt<'_>,
281 safety: Safety,
282 sp: Span,
283 args: TokenStream,
284 body: TokenStream,
285 ) -> Result<TokenStream, ErrorGuaranteed> {
286 expand_macro_attr(
287 cx,
288 sp,
289 self.span,
290 self.node_id,
291 self.name,
292 self.transparency,
293 safety,
294 args,
295 body,
296 &self.rules,
297 )
298 }
299}
300
301struct DummyBang(ErrorGuaranteed);
302
303impl BangProcMacro for DummyBang {
304 fn expand<'cx>(
305 &self,
306 _: &'cx mut ExtCtxt<'_>,
307 _: Span,
308 _: TokenStream,
309 ) -> Result<TokenStream, ErrorGuaranteed> {
310 Err(self.0)
311 }
312}
313
314fn trace_macros_note(cx_expansions: &mut FxIndexMap<Span, Vec<String>>, sp: Span, message: String) {
315 let sp = sp.macro_backtrace().last().map_or(sp, |trace| trace.call_site);
316 cx_expansions.entry(sp).or_default().push(message);
317}
318
319pub(super) trait Tracker<'matcher> {
320 type Failure;
322
323 fn build_failure(tok: Token, position: u32, msg: &'static str) -> Self::Failure;
327
328 fn before_match_loc(&mut self, _parser: &TtParser, _matcher: &'matcher MatcherLoc) {}
330
331 fn after_arm(&mut self, _in_body: bool, _result: &NamedParseResult<Self::Failure>) {}
334
335 fn description() -> &'static str;
337
338 fn recovery() -> Recovery {
339 Recovery::Forbidden
340 }
341}
342
343pub(super) struct NoopTracker;
346
347impl<'matcher> Tracker<'matcher> for NoopTracker {
348 type Failure = ();
349
350 fn build_failure(_tok: Token, _position: u32, _msg: &'static str) -> Self::Failure {}
351
352 fn description() -> &'static str {
353 "none"
354 }
355}
356
357#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("expand_macro",
"rustc_expand::mbe::macro_rules", ::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(358u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["sp", "def_span",
"node_id", "name"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::INFO <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&sp)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&node_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Box<dyn MacResult + 'cx> =
loop {};
return __tracing_attr_fake_return;
}
{
let psess = &cx.sess.psess;
if cx.trace_macros() {
let msg =
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expanding `{0}! {{ {1} }}`",
name, pprust::tts_to_string(&arg)))
});
trace_macros_note(&mut cx.expansions, sp, msg);
}
let try_success_result =
try_match_macro(psess, name, &arg, rules, &mut NoopTracker);
match try_success_result {
Ok((rule_index, rule, named_matches)) => {
let MacroRule::Func { lhs, rhs, .. } =
rule else {
{
::core::panicking::panic_fmt(format_args!("try_match_macro returned non-func rule"));
};
};
let mbe::TokenTree::Delimited(rhs_span, _, rhs) =
rhs else { cx.dcx().span_bug(sp, "malformed macro rhs"); };
let arm_span = rhs_span.entire();
let id = cx.current_expansion.id;
let tts =
match transcribe(psess, &named_matches, rhs, *rhs_span,
transparency, id) {
Ok(tts) => tts,
Err(err) => {
let guar = err.emit();
return DummyResult::any(arm_span, guar);
}
};
if cx.trace_macros() {
let msg =
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to `{0}`",
pprust::tts_to_string(&tts)))
});
trace_macros_note(&mut cx.expansions, sp, msg);
}
let is_local = is_defined_in_current_crate(node_id);
if is_local {
cx.resolver.record_macro_rule_usage(node_id, rule_index);
}
Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span,
is_local, name, rules, lhs))
}
Err(CanRetry::No(guar)) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:413",
"rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(413u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Will not retry matching as an error was emitted already")
as &dyn Value))])
});
} else { ; }
};
DummyResult::any(sp, guar)
}
Err(CanRetry::Yes) => {
let (span, guar) =
failed_to_match_macro(cx.psess(), sp, def_span, name,
FailedMacro::Func, &arg, rules);
cx.macro_error_and_trace_macros_diag();
DummyResult::any(span, guar)
}
}
}
}
}#[instrument(skip(cx, transparency, arg, rules))]
359fn expand_macro<'cx, 'a: 'cx>(
360 cx: &'cx mut ExtCtxt<'_>,
361 sp: Span,
362 def_span: Span,
363 node_id: NodeId,
364 name: Ident,
365 transparency: Transparency,
366 arg: TokenStream,
367 rules: &'a [MacroRule],
368) -> Box<dyn MacResult + 'cx> {
369 let psess = &cx.sess.psess;
370
371 if cx.trace_macros() {
372 let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(&arg));
373 trace_macros_note(&mut cx.expansions, sp, msg);
374 }
375
376 let try_success_result = try_match_macro(psess, name, &arg, rules, &mut NoopTracker);
378
379 match try_success_result {
380 Ok((rule_index, rule, named_matches)) => {
381 let MacroRule::Func { lhs, rhs, .. } = rule else {
382 panic!("try_match_macro returned non-func rule");
383 };
384 let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
385 cx.dcx().span_bug(sp, "malformed macro rhs");
386 };
387 let arm_span = rhs_span.entire();
388
389 let id = cx.current_expansion.id;
391 let tts = match transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id) {
392 Ok(tts) => tts,
393 Err(err) => {
394 let guar = err.emit();
395 return DummyResult::any(arm_span, guar);
396 }
397 };
398
399 if cx.trace_macros() {
400 let msg = format!("to `{}`", pprust::tts_to_string(&tts));
401 trace_macros_note(&mut cx.expansions, sp, msg);
402 }
403
404 let is_local = is_defined_in_current_crate(node_id);
405 if is_local {
406 cx.resolver.record_macro_rule_usage(node_id, rule_index);
407 }
408
409 Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name, rules, lhs))
411 }
412 Err(CanRetry::No(guar)) => {
413 debug!("Will not retry matching as an error was emitted already");
414 DummyResult::any(sp, guar)
415 }
416 Err(CanRetry::Yes) => {
417 let (span, guar) = failed_to_match_macro(
419 cx.psess(),
420 sp,
421 def_span,
422 name,
423 FailedMacro::Func,
424 &arg,
425 rules,
426 );
427 cx.macro_error_and_trace_macros_diag();
428 DummyResult::any(span, guar)
429 }
430 }
431}
432
433#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("expand_macro_attr",
"rustc_expand::mbe::macro_rules", ::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(434u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["sp", "def_span",
"node_id", "name", "safety"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::INFO <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&sp)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&node_id)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&safety)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
Result<TokenStream, ErrorGuaranteed> = loop {};
return __tracing_attr_fake_return;
}
{
let psess = &cx.sess.psess;
let is_local = node_id != DUMMY_NODE_ID;
if cx.trace_macros() {
let msg =
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expanding `#[{2}({0})] {1}`",
pprust::tts_to_string(&args), pprust::tts_to_string(&body),
name))
});
trace_macros_note(&mut cx.expansions, sp, msg);
}
match try_match_macro_attr(psess, name, &args, &body, rules,
&mut NoopTracker) {
Ok((i, rule, named_matches)) => {
let MacroRule::Attr { rhs, unsafe_rule, .. } =
rule else {
{
::core::panicking::panic_fmt(format_args!("try_macro_match_attr returned non-attr rule"));
};
};
let mbe::TokenTree::Delimited(rhs_span, _, rhs) =
rhs else { cx.dcx().span_bug(sp, "malformed macro rhs"); };
match (safety, unsafe_rule) {
(Safety::Default, false) | (Safety::Unsafe(_), true) => {}
(Safety::Default, true) => {
cx.dcx().span_err(sp,
"unsafe attribute invocation requires `unsafe`");
}
(Safety::Unsafe(span), false) => {
cx.dcx().span_err(span,
"unnecessary `unsafe` on safe attribute invocation");
}
(Safety::Safe(span), _) => {
cx.dcx().span_bug(span, "unexpected `safe` keyword");
}
}
let id = cx.current_expansion.id;
let tts =
transcribe(psess, &named_matches, rhs, *rhs_span,
transparency, id).map_err(|e| e.emit())?;
if cx.trace_macros() {
let msg =
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to `{0}`",
pprust::tts_to_string(&tts)))
});
trace_macros_note(&mut cx.expansions, sp, msg);
}
if is_local {
cx.resolver.record_macro_rule_usage(node_id, i);
}
Ok(tts)
}
Err(CanRetry::No(guar)) => Err(guar),
Err(CanRetry::Yes) => {
let (_, guar) =
failed_to_match_macro(cx.psess(), sp, def_span, name,
FailedMacro::Attr(&args), &body, rules);
cx.trace_macros_diag();
Err(guar)
}
}
}
}
}#[instrument(skip(cx, transparency, args, body, rules))]
435fn expand_macro_attr(
436 cx: &mut ExtCtxt<'_>,
437 sp: Span,
438 def_span: Span,
439 node_id: NodeId,
440 name: Ident,
441 transparency: Transparency,
442 safety: Safety,
443 args: TokenStream,
444 body: TokenStream,
445 rules: &[MacroRule],
446) -> Result<TokenStream, ErrorGuaranteed> {
447 let psess = &cx.sess.psess;
448 let is_local = node_id != DUMMY_NODE_ID;
451
452 if cx.trace_macros() {
453 let msg = format!(
454 "expanding `#[{name}({})] {}`",
455 pprust::tts_to_string(&args),
456 pprust::tts_to_string(&body),
457 );
458 trace_macros_note(&mut cx.expansions, sp, msg);
459 }
460
461 match try_match_macro_attr(psess, name, &args, &body, rules, &mut NoopTracker) {
463 Ok((i, rule, named_matches)) => {
464 let MacroRule::Attr { rhs, unsafe_rule, .. } = rule else {
465 panic!("try_macro_match_attr returned non-attr rule");
466 };
467 let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
468 cx.dcx().span_bug(sp, "malformed macro rhs");
469 };
470
471 match (safety, unsafe_rule) {
472 (Safety::Default, false) | (Safety::Unsafe(_), true) => {}
473 (Safety::Default, true) => {
474 cx.dcx().span_err(sp, "unsafe attribute invocation requires `unsafe`");
475 }
476 (Safety::Unsafe(span), false) => {
477 cx.dcx().span_err(span, "unnecessary `unsafe` on safe attribute invocation");
478 }
479 (Safety::Safe(span), _) => {
480 cx.dcx().span_bug(span, "unexpected `safe` keyword");
481 }
482 }
483
484 let id = cx.current_expansion.id;
485 let tts = transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id)
486 .map_err(|e| e.emit())?;
487
488 if cx.trace_macros() {
489 let msg = format!("to `{}`", pprust::tts_to_string(&tts));
490 trace_macros_note(&mut cx.expansions, sp, msg);
491 }
492
493 if is_local {
494 cx.resolver.record_macro_rule_usage(node_id, i);
495 }
496
497 Ok(tts)
498 }
499 Err(CanRetry::No(guar)) => Err(guar),
500 Err(CanRetry::Yes) => {
501 let (_, guar) = failed_to_match_macro(
503 cx.psess(),
504 sp,
505 def_span,
506 name,
507 FailedMacro::Attr(&args),
508 &body,
509 rules,
510 );
511 cx.trace_macros_diag();
512 Err(guar)
513 }
514 }
515}
516
517pub(super) enum CanRetry {
518 Yes,
519 No(ErrorGuaranteed),
521}
522
523#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("try_match_macro",
"rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(526u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["name", "tracking"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&display(&T::description())
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
Result<(usize, &'matcher MacroRule, NamedMatches),
CanRetry> = loop {};
return __tracing_attr_fake_return;
}
{
let parser = parser_from_cx(psess, arg.clone(), T::recovery());
let mut tt_parser = TtParser::new(name);
for (i, rule) in rules.iter().enumerate() {
let MacroRule::Func { lhs, .. } = rule else { continue };
let _tracing_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("Matching arm",
"rustc_expand::mbe::macro_rules", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(558u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["i"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&display(&i) as
&dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
let mut gated_spans_snapshot =
mem::take(&mut *psess.gated_spans.spans.borrow_mut());
let result =
tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs, track);
track.after_arm(true, &result);
match result {
Success(named_matches) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:572",
"rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(572u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Parsed arm successfully")
as &dyn Value))])
});
} else { ; }
};
psess.gated_spans.merge(gated_spans_snapshot);
return Ok((i, rule, named_matches));
}
Failure(_) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:580",
"rustc_expand::mbe::macro_rules", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(580u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Failed to match arm, trying the next one")
as &dyn Value))])
});
} else { ; }
};
}
Error(_, _) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:584",
"rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(584u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Fatal error occurred during matching")
as &dyn Value))])
});
} else { ; }
};
return Err(CanRetry::Yes);
}
ErrorReported(guarantee) => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_expand/src/mbe/macro_rules.rs:589",
"rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(589u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Fatal error occurred and was reported during matching")
as &dyn Value))])
});
} else { ; }
};
return Err(CanRetry::No(guarantee));
}
}
mem::swap(&mut gated_spans_snapshot,
&mut psess.gated_spans.spans.borrow_mut());
}
Err(CanRetry::Yes)
}
}
}#[instrument(level = "debug", skip(psess, arg, rules, track), fields(tracking = %T::description()))]
527pub(super) fn try_match_macro<'matcher, T: Tracker<'matcher>>(
528 psess: &ParseSess,
529 name: Ident,
530 arg: &TokenStream,
531 rules: &'matcher [MacroRule],
532 track: &mut T,
533) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
534 let parser = parser_from_cx(psess, arg.clone(), T::recovery());
554 let mut tt_parser = TtParser::new(name);
556 for (i, rule) in rules.iter().enumerate() {
557 let MacroRule::Func { lhs, .. } = rule else { continue };
558 let _tracing_span = trace_span!("Matching arm", %i);
559
560 let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
565
566 let result = tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs, track);
567
568 track.after_arm(true, &result);
569
570 match result {
571 Success(named_matches) => {
572 debug!("Parsed arm successfully");
573 psess.gated_spans.merge(gated_spans_snapshot);
576
577 return Ok((i, rule, named_matches));
578 }
579 Failure(_) => {
580 trace!("Failed to match arm, trying the next one");
581 }
583 Error(_, _) => {
584 debug!("Fatal error occurred during matching");
585 return Err(CanRetry::Yes);
587 }
588 ErrorReported(guarantee) => {
589 debug!("Fatal error occurred and was reported during matching");
590 return Err(CanRetry::No(guarantee));
592 }
593 }
594
595 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
598 }
599
600 Err(CanRetry::Yes)
601}
602
603#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("try_match_macro_attr",
"rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(606u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["name", "tracking"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&display(&T::description())
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
Result<(usize, &'matcher MacroRule, NamedMatches),
CanRetry> = loop {};
return __tracing_attr_fake_return;
}
{
let args_parser =
parser_from_cx(psess, attr_args.clone(), T::recovery());
let body_parser =
parser_from_cx(psess, attr_body.clone(), T::recovery());
let mut tt_parser = TtParser::new(name);
for (i, rule) in rules.iter().enumerate() {
let MacroRule::Attr { args, body, .. } =
rule else { continue };
let mut gated_spans_snapshot =
mem::take(&mut *psess.gated_spans.spans.borrow_mut());
let result =
tt_parser.parse_tt(&mut Cow::Borrowed(&args_parser), args,
track);
track.after_arm(false, &result);
let mut named_matches =
match result {
Success(named_matches) => named_matches,
Failure(_) => {
mem::swap(&mut gated_spans_snapshot,
&mut psess.gated_spans.spans.borrow_mut());
continue;
}
Error(_, _) => return Err(CanRetry::Yes),
ErrorReported(guar) => return Err(CanRetry::No(guar)),
};
let result =
tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body,
track);
track.after_arm(true, &result);
match result {
Success(body_named_matches) => {
psess.gated_spans.merge(gated_spans_snapshot);
#[allow(rustc::potential_query_instability)]
named_matches.extend(body_named_matches);
return Ok((i, rule, named_matches));
}
Failure(_) => {
mem::swap(&mut gated_spans_snapshot,
&mut psess.gated_spans.spans.borrow_mut())
}
Error(_, _) => return Err(CanRetry::Yes),
ErrorReported(guar) => return Err(CanRetry::No(guar)),
}
}
Err(CanRetry::Yes)
}
}
}#[instrument(level = "debug", skip(psess, attr_args, attr_body, rules, track), fields(tracking = %T::description()))]
607pub(super) fn try_match_macro_attr<'matcher, T: Tracker<'matcher>>(
608 psess: &ParseSess,
609 name: Ident,
610 attr_args: &TokenStream,
611 attr_body: &TokenStream,
612 rules: &'matcher [MacroRule],
613 track: &mut T,
614) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
615 let args_parser = parser_from_cx(psess, attr_args.clone(), T::recovery());
617 let body_parser = parser_from_cx(psess, attr_body.clone(), T::recovery());
618 let mut tt_parser = TtParser::new(name);
619 for (i, rule) in rules.iter().enumerate() {
620 let MacroRule::Attr { args, body, .. } = rule else { continue };
621
622 let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
623
624 let result = tt_parser.parse_tt(&mut Cow::Borrowed(&args_parser), args, track);
625 track.after_arm(false, &result);
626
627 let mut named_matches = match result {
628 Success(named_matches) => named_matches,
629 Failure(_) => {
630 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
631 continue;
632 }
633 Error(_, _) => return Err(CanRetry::Yes),
634 ErrorReported(guar) => return Err(CanRetry::No(guar)),
635 };
636
637 let result = tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body, track);
638 track.after_arm(true, &result);
639
640 match result {
641 Success(body_named_matches) => {
642 psess.gated_spans.merge(gated_spans_snapshot);
643 #[allow(rustc::potential_query_instability)]
644 named_matches.extend(body_named_matches);
645 return Ok((i, rule, named_matches));
646 }
647 Failure(_) => {
648 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut())
649 }
650 Error(_, _) => return Err(CanRetry::Yes),
651 ErrorReported(guar) => return Err(CanRetry::No(guar)),
652 }
653 }
654
655 Err(CanRetry::Yes)
656}
657
658#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("try_match_macro_derive",
"rustc_expand::mbe::macro_rules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/mbe/macro_rules.rs"),
::tracing_core::__macro_support::Option::Some(661u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::mbe::macro_rules"),
::tracing_core::field::FieldSet::new(&["name", "tracking"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&name)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&display(&T::description())
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return:
Result<(usize, &'matcher MacroRule, NamedMatches),
CanRetry> = loop {};
return __tracing_attr_fake_return;
}
{
let body_parser =
parser_from_cx(psess, body.clone(), T::recovery());
let mut tt_parser = TtParser::new(name);
for (i, rule) in rules.iter().enumerate() {
let MacroRule::Derive { body, .. } = rule else { continue };
let mut gated_spans_snapshot =
mem::take(&mut *psess.gated_spans.spans.borrow_mut());
let result =
tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body,
track);
track.after_arm(true, &result);
match result {
Success(named_matches) => {
psess.gated_spans.merge(gated_spans_snapshot);
return Ok((i, rule, named_matches));
}
Failure(_) => {
mem::swap(&mut gated_spans_snapshot,
&mut psess.gated_spans.spans.borrow_mut())
}
Error(_, _) => return Err(CanRetry::Yes),
ErrorReported(guar) => return Err(CanRetry::No(guar)),
}
}
Err(CanRetry::Yes)
}
}
}#[instrument(level = "debug", skip(psess, body, rules, track), fields(tracking = %T::description()))]
662pub(super) fn try_match_macro_derive<'matcher, T: Tracker<'matcher>>(
663 psess: &ParseSess,
664 name: Ident,
665 body: &TokenStream,
666 rules: &'matcher [MacroRule],
667 track: &mut T,
668) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
669 let body_parser = parser_from_cx(psess, body.clone(), T::recovery());
671 let mut tt_parser = TtParser::new(name);
672 for (i, rule) in rules.iter().enumerate() {
673 let MacroRule::Derive { body, .. } = rule else { continue };
674
675 let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
676
677 let result = tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body, track);
678 track.after_arm(true, &result);
679
680 match result {
681 Success(named_matches) => {
682 psess.gated_spans.merge(gated_spans_snapshot);
683 return Ok((i, rule, named_matches));
684 }
685 Failure(_) => {
686 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut())
687 }
688 Error(_, _) => return Err(CanRetry::Yes),
689 ErrorReported(guar) => return Err(CanRetry::No(guar)),
690 }
691 }
692
693 Err(CanRetry::Yes)
694}
695
696pub fn compile_declarative_macro(
698 sess: &Session,
699 features: &Features,
700 macro_def: &ast::MacroDef,
701 ident: Ident,
702 attrs: &[hir::Attribute],
703 span: Span,
704 node_id: NodeId,
705 edition: Edition,
706) -> (SyntaxExtension, usize) {
707 let mk_syn_ext = |kind| {
708 let is_local = is_defined_in_current_crate(node_id);
709 SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
710 };
711 let dummy_syn_ext =
712 |guar| (mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar)))), 0);
713
714 let macro_rules = macro_def.macro_rules;
715 let exp_sep = if macro_rules { ::rustc_parse::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Semi,
token_type: ::rustc_parse::parser::token_type::TokenType::Semi,
}exp!(Semi) } else { ::rustc_parse::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Comma,
token_type: ::rustc_parse::parser::token_type::TokenType::Comma,
}exp!(Comma) };
716
717 let body = macro_def.body.tokens.clone();
718 let mut p = Parser::new(&sess.psess, body, rustc_parse::MACRO_ARGUMENTS);
719
720 let mut guar = None;
723 let mut check_emission = |ret: Result<(), ErrorGuaranteed>| guar = guar.or(ret.err());
724
725 let mut kinds = MacroKinds::empty();
726 let mut rules = Vec::new();
727
728 while p.token != token::Eof {
729 let unsafe_rule = p.eat_keyword_noexpect(kw::Unsafe);
730 let unsafe_keyword_span = p.prev_token.span;
731 if unsafe_rule && let Some(guar) = check_no_eof(sess, &p, "expected `attr`") {
732 return dummy_syn_ext(guar);
733 }
734 let (args, is_derive) = if p.eat_keyword_noexpect(sym::attr) {
735 kinds |= MacroKinds::ATTR;
736 if !features.macro_attr() {
737 feature_err(sess, sym::macro_attr, span, "`macro_rules!` attributes are unstable")
738 .emit();
739 }
740 if let Some(guar) = check_no_eof(sess, &p, "expected macro attr args") {
741 return dummy_syn_ext(guar);
742 }
743 let args = p.parse_token_tree();
744 check_args_parens(sess, sym::attr, &args);
745 let args = parse_one_tt(args, RulePart::Pattern, sess, node_id, features, edition);
746 check_emission(check_lhs(sess, features, node_id, &args));
747 if let Some(guar) = check_no_eof(sess, &p, "expected macro attr body") {
748 return dummy_syn_ext(guar);
749 }
750 (Some(args), false)
751 } else if p.eat_keyword_noexpect(sym::derive) {
752 kinds |= MacroKinds::DERIVE;
753 let derive_keyword_span = p.prev_token.span;
754 if !features.macro_derive() {
755 feature_err(sess, sym::macro_derive, span, "`macro_rules!` derives are unstable")
756 .emit();
757 }
758 if unsafe_rule {
759 sess.dcx()
760 .span_err(unsafe_keyword_span, "`unsafe` is only supported on `attr` rules");
761 }
762 if let Some(guar) = check_no_eof(sess, &p, "expected `()` after `derive`") {
763 return dummy_syn_ext(guar);
764 }
765 let args = p.parse_token_tree();
766 check_args_parens(sess, sym::derive, &args);
767 let args_empty_result = check_args_empty(sess, &args);
768 let args_not_empty = args_empty_result.is_err();
769 check_emission(args_empty_result);
770 if let Some(guar) = check_no_eof(sess, &p, "expected macro derive body") {
771 return dummy_syn_ext(guar);
772 }
773 if p.token == token::FatArrow {
776 let mut err = sess
777 .dcx()
778 .struct_span_err(p.token.span, "expected macro derive body, got `=>`");
779 if args_not_empty {
780 err.span_label(derive_keyword_span, "need `()` after this `derive`");
781 }
782 return dummy_syn_ext(err.emit());
783 }
784 (None, true)
785 } else {
786 kinds |= MacroKinds::BANG;
787 if unsafe_rule {
788 sess.dcx()
789 .span_err(unsafe_keyword_span, "`unsafe` is only supported on `attr` rules");
790 }
791 (None, false)
792 };
793 let lhs_tt = p.parse_token_tree();
794 let lhs_tt = parse_one_tt(lhs_tt, RulePart::Pattern, sess, node_id, features, edition);
795 check_emission(check_lhs(sess, features, node_id, &lhs_tt));
796 if let Err(e) = p.expect(::rustc_parse::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::FatArrow,
token_type: ::rustc_parse::parser::token_type::TokenType::FatArrow,
}exp!(FatArrow)) {
797 return dummy_syn_ext(e.emit());
798 }
799 if let Some(guar) = check_no_eof(sess, &p, "expected right-hand side of macro rule") {
800 return dummy_syn_ext(guar);
801 }
802 let rhs = p.parse_token_tree();
803 let rhs = parse_one_tt(rhs, RulePart::Body, sess, node_id, features, edition);
804 check_emission(check_rhs(sess, &rhs));
805 check_emission(check_meta_variables(&sess.psess, node_id, args.as_ref(), &lhs_tt, &rhs));
806 let lhs_span = lhs_tt.span();
807 let mbe::TokenTree::Delimited(.., delimited) = lhs_tt else {
810 return dummy_syn_ext(guar.unwrap());
811 };
812 let lhs = mbe::macro_parser::compute_locs(&delimited.tts);
813 if let Some(args) = args {
814 let args_span = args.span();
815 let mbe::TokenTree::Delimited(.., delimited) = args else {
816 return dummy_syn_ext(guar.unwrap());
817 };
818 let args = mbe::macro_parser::compute_locs(&delimited.tts);
819 let body_span = lhs_span;
820 rules.push(MacroRule::Attr { unsafe_rule, args, args_span, body: lhs, body_span, rhs });
821 } else if is_derive {
822 rules.push(MacroRule::Derive { body: lhs, body_span: lhs_span, rhs });
823 } else {
824 rules.push(MacroRule::Func { lhs, lhs_span, rhs });
825 }
826 if p.token == token::Eof {
827 break;
828 }
829 if let Err(e) = p.expect(exp_sep) {
830 return dummy_syn_ext(e.emit());
831 }
832 }
833
834 if rules.is_empty() {
835 let guar = sess.dcx().span_err(span, "macros must contain at least one rule");
836 return dummy_syn_ext(guar);
837 }
838 if !!kinds.is_empty() {
::core::panicking::panic("assertion failed: !kinds.is_empty()")
};assert!(!kinds.is_empty());
839
840 let transparency = {
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcMacroTransparency(x)) => {
break 'done Some(*x);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, RustcMacroTransparency(x) => *x)
841 .unwrap_or(Transparency::fallback(macro_rules));
842
843 if let Some(guar) = guar {
844 return dummy_syn_ext(guar);
847 }
848
849 let nrules = if is_defined_in_current_crate(node_id) { rules.len() } else { 0 };
851
852 let exp = MacroRulesMacroExpander { name: ident, kinds, span, node_id, transparency, rules };
853 (mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))), nrules)
854}
855
856fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {
857 if p.token == token::Eof {
858 let err_sp = p.token.span.shrink_to_hi();
859 let guar = sess
860 .dcx()
861 .struct_span_err(err_sp, "macro definition ended unexpectedly")
862 .with_span_label(err_sp, msg)
863 .emit();
864 return Some(guar);
865 }
866 None
867}
868
869fn check_args_parens(sess: &Session, rule_kw: Symbol, args: &tokenstream::TokenTree) {
870 if let tokenstream::TokenTree::Delimited(dspan, _, delim, _) = args
872 && *delim != Delimiter::Parenthesis
873 {
874 sess.dcx().emit_err(errors::MacroArgsBadDelim {
875 span: dspan.entire(),
876 sugg: errors::MacroArgsBadDelimSugg { open: dspan.open, close: dspan.close },
877 rule_kw,
878 });
879 }
880}
881
882fn check_args_empty(sess: &Session, args: &tokenstream::TokenTree) -> Result<(), ErrorGuaranteed> {
883 match args {
884 tokenstream::TokenTree::Delimited(.., delimited) if delimited.is_empty() => Ok(()),
885 _ => {
886 let msg = "`derive` rules do not accept arguments; `derive` must be followed by `()`";
887 Err(sess.dcx().span_err(args.span(), msg))
888 }
889 }
890}
891
892fn check_lhs(
893 sess: &Session,
894 features: &Features,
895 node_id: NodeId,
896 lhs: &mbe::TokenTree,
897) -> Result<(), ErrorGuaranteed> {
898 let e1 = check_lhs_nt_follows(sess, features, node_id, lhs);
899 let e2 = check_lhs_no_empty_seq(sess, slice::from_ref(lhs));
900 e1.and(e2)
901}
902
903fn check_lhs_nt_follows(
904 sess: &Session,
905 features: &Features,
906 node_id: NodeId,
907 lhs: &mbe::TokenTree,
908) -> Result<(), ErrorGuaranteed> {
909 if let mbe::TokenTree::Delimited(.., delimited) = lhs {
912 check_matcher(sess, features, node_id, &delimited.tts)
913 } else {
914 let msg = "invalid macro matcher; matchers must be contained in balanced delimiters";
915 Err(sess.dcx().span_err(lhs.span(), msg))
916 }
917}
918
919fn is_empty_token_tree(sess: &Session, seq: &mbe::SequenceRepetition) -> bool {
920 if seq.separator.is_some() {
921 false
922 } else {
923 let mut is_empty = true;
924 let mut iter = seq.tts.iter().peekable();
925 while let Some(tt) = iter.next() {
926 match tt {
927 mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. } => {}
928 mbe::TokenTree::Token(t @ Token { kind: DocComment(..), .. }) => {
929 let mut now = t;
930 while let Some(&mbe::TokenTree::Token(
931 next @ Token { kind: DocComment(..), .. },
932 )) = iter.peek()
933 {
934 now = next;
935 iter.next();
936 }
937 let span = t.span.to(now.span);
938 sess.dcx().span_note(span, "doc comments are ignored in matcher position");
939 }
940 mbe::TokenTree::Sequence(_, sub_seq)
941 if (sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
942 || sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne) => {}
943 _ => is_empty = false,
944 }
945 }
946 is_empty
947 }
948}
949
950fn check_redundant_vis_repetition(
955 err: &mut Diag<'_>,
956 sess: &Session,
957 seq: &SequenceRepetition,
958 span: &DelimSpan,
959) {
960 if seq.kleene.op == KleeneOp::ZeroOrOne
961 && #[allow(non_exhaustive_omitted_patterns)] match seq.tts.first() {
Some(mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. }) =>
true,
_ => false,
}matches!(
962 seq.tts.first(),
963 Some(mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. })
964 )
965 {
966 err.note("a `vis` fragment can already be empty");
967 err.multipart_suggestion(
968 "remove the `$(` and `)?`",
969 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(sess.source_map().span_extend_to_prev_char_before(span.open, '$',
true), "".to_string()),
(span.close.with_hi(seq.kleene.span.hi()), "".to_string())]))vec![
970 (
971 sess.source_map().span_extend_to_prev_char_before(span.open, '$', true),
972 "".to_string(),
973 ),
974 (span.close.with_hi(seq.kleene.span.hi()), "".to_string()),
975 ],
976 Applicability::MaybeIncorrect,
977 );
978 }
979}
980
981fn check_lhs_no_empty_seq(sess: &Session, tts: &[mbe::TokenTree]) -> Result<(), ErrorGuaranteed> {
984 use mbe::TokenTree;
985 for tt in tts {
986 match tt {
987 TokenTree::Token(..)
988 | TokenTree::MetaVar(..)
989 | TokenTree::MetaVarDecl { .. }
990 | TokenTree::MetaVarExpr(..) => (),
991 TokenTree::Delimited(.., del) => check_lhs_no_empty_seq(sess, &del.tts)?,
992 TokenTree::Sequence(span, seq) => {
993 if is_empty_token_tree(sess, seq) {
994 let sp = span.entire();
995 let mut err =
996 sess.dcx().struct_span_err(sp, "repetition matches empty token tree");
997 check_redundant_vis_repetition(&mut err, sess, seq, span);
998 return Err(err.emit());
999 }
1000 check_lhs_no_empty_seq(sess, &seq.tts)?
1001 }
1002 }
1003 }
1004
1005 Ok(())
1006}
1007
1008fn check_rhs(sess: &Session, rhs: &mbe::TokenTree) -> Result<(), ErrorGuaranteed> {
1009 match *rhs {
1010 mbe::TokenTree::Delimited(..) => Ok(()),
1011 _ => Err(sess.dcx().span_err(rhs.span(), "macro rhs must be delimited")),
1012 }
1013}
1014
1015fn check_matcher(
1016 sess: &Session,
1017 features: &Features,
1018 node_id: NodeId,
1019 matcher: &[mbe::TokenTree],
1020) -> Result<(), ErrorGuaranteed> {
1021 let first_sets = FirstSets::new(matcher);
1022 let empty_suffix = TokenSet::empty();
1023 check_matcher_core(sess, features, node_id, &first_sets, matcher, &empty_suffix)?;
1024 Ok(())
1025}
1026
1027fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool {
1028 match rhs {
1029 mbe::TokenTree::Delimited(.., d) => {
1030 let has_compile_error = d.tts.array_windows::<3>().any(|[ident, bang, args]| {
1031 if let mbe::TokenTree::Token(ident) = ident
1032 && let TokenKind::Ident(ident, _) = ident.kind
1033 && ident == sym::compile_error
1034 && let mbe::TokenTree::Token(bang) = bang
1035 && let TokenKind::Bang = bang.kind
1036 && let mbe::TokenTree::Delimited(.., del) = args
1037 && !del.delim.skip()
1038 {
1039 true
1040 } else {
1041 false
1042 }
1043 });
1044 if has_compile_error { true } else { d.tts.iter().any(has_compile_error_macro) }
1045 }
1046 _ => false,
1047 }
1048}
1049
1050struct FirstSets<'tt> {
1063 first: FxHashMap<Span, Option<TokenSet<'tt>>>,
1070}
1071
1072impl<'tt> FirstSets<'tt> {
1073 fn new(tts: &'tt [mbe::TokenTree]) -> FirstSets<'tt> {
1074 use mbe::TokenTree;
1075
1076 let mut sets = FirstSets { first: FxHashMap::default() };
1077 build_recur(&mut sets, tts);
1078 return sets;
1079
1080 fn build_recur<'tt>(sets: &mut FirstSets<'tt>, tts: &'tt [TokenTree]) -> TokenSet<'tt> {
1084 let mut first = TokenSet::empty();
1085 for tt in tts.iter().rev() {
1086 match tt {
1087 TokenTree::Token(..)
1088 | TokenTree::MetaVar(..)
1089 | TokenTree::MetaVarDecl { .. }
1090 | TokenTree::MetaVarExpr(..) => {
1091 first.replace_with(TtHandle::TtRef(tt));
1092 }
1093 TokenTree::Delimited(span, _, delimited) => {
1094 build_recur(sets, &delimited.tts);
1095 first.replace_with(TtHandle::from_token_kind(
1096 delimited.delim.as_open_token_kind(),
1097 span.open,
1098 ));
1099 }
1100 TokenTree::Sequence(sp, seq_rep) => {
1101 let subfirst = build_recur(sets, &seq_rep.tts);
1102
1103 match sets.first.entry(sp.entire()) {
1104 Entry::Vacant(vac) => {
1105 vac.insert(Some(subfirst.clone()));
1106 }
1107 Entry::Occupied(mut occ) => {
1108 occ.insert(None);
1115 }
1116 }
1117
1118 if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
1122 first.add_one_maybe(TtHandle::from_token(*sep));
1123 }
1124
1125 if subfirst.maybe_empty
1127 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
1128 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
1129 {
1130 first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
1133 } else {
1134 first = subfirst;
1137 }
1138 }
1139 }
1140 }
1141
1142 first
1143 }
1144 }
1145
1146 fn first(&self, tts: &'tt [mbe::TokenTree]) -> TokenSet<'tt> {
1149 use mbe::TokenTree;
1150
1151 let mut first = TokenSet::empty();
1152 for tt in tts.iter() {
1153 if !first.maybe_empty {
::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1154 match tt {
1155 TokenTree::Token(..)
1156 | TokenTree::MetaVar(..)
1157 | TokenTree::MetaVarDecl { .. }
1158 | TokenTree::MetaVarExpr(..) => {
1159 first.add_one(TtHandle::TtRef(tt));
1160 return first;
1161 }
1162 TokenTree::Delimited(span, _, delimited) => {
1163 first.add_one(TtHandle::from_token_kind(
1164 delimited.delim.as_open_token_kind(),
1165 span.open,
1166 ));
1167 return first;
1168 }
1169 TokenTree::Sequence(sp, seq_rep) => {
1170 let subfirst_owned;
1171 let subfirst = match self.first.get(&sp.entire()) {
1172 Some(Some(subfirst)) => subfirst,
1173 Some(&None) => {
1174 subfirst_owned = self.first(&seq_rep.tts);
1175 &subfirst_owned
1176 }
1177 None => {
1178 {
::core::panicking::panic_fmt(format_args!("We missed a sequence during FirstSets construction"));
};panic!("We missed a sequence during FirstSets construction");
1179 }
1180 };
1181
1182 if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
1185 first.add_one_maybe(TtHandle::from_token(*sep));
1186 }
1187
1188 if !first.maybe_empty {
::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1189 first.add_all(subfirst);
1190 if subfirst.maybe_empty
1191 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
1192 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
1193 {
1194 first.maybe_empty = true;
1198 continue;
1199 } else {
1200 return first;
1201 }
1202 }
1203 }
1204 }
1205
1206 if !first.maybe_empty {
::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1209 first
1210 }
1211}
1212
1213#[derive(#[automatically_derived]
impl<'tt> ::core::fmt::Debug for TtHandle<'tt> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
TtHandle::TtRef(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "TtRef",
&__self_0),
TtHandle::Token(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Token",
&__self_0),
}
}
}Debug)]
1218enum TtHandle<'tt> {
1219 TtRef(&'tt mbe::TokenTree),
1221
1222 Token(mbe::TokenTree),
1227}
1228
1229impl<'tt> TtHandle<'tt> {
1230 fn from_token(tok: Token) -> Self {
1231 TtHandle::Token(mbe::TokenTree::Token(tok))
1232 }
1233
1234 fn from_token_kind(kind: TokenKind, span: Span) -> Self {
1235 TtHandle::from_token(Token::new(kind, span))
1236 }
1237
1238 fn get(&'tt self) -> &'tt mbe::TokenTree {
1240 match self {
1241 TtHandle::TtRef(tt) => tt,
1242 TtHandle::Token(token_tt) => token_tt,
1243 }
1244 }
1245}
1246
1247impl<'tt> PartialEq for TtHandle<'tt> {
1248 fn eq(&self, other: &TtHandle<'tt>) -> bool {
1249 self.get() == other.get()
1250 }
1251}
1252
1253impl<'tt> Clone for TtHandle<'tt> {
1254 fn clone(&self) -> Self {
1255 match self {
1256 TtHandle::TtRef(tt) => TtHandle::TtRef(tt),
1257
1258 TtHandle::Token(mbe::TokenTree::Token(tok)) => {
1261 TtHandle::Token(mbe::TokenTree::Token(*tok))
1262 }
1263
1264 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1265 }
1266 }
1267}
1268
1269#[derive(#[automatically_derived]
impl<'tt> ::core::clone::Clone for TokenSet<'tt> {
#[inline]
fn clone(&self) -> TokenSet<'tt> {
TokenSet {
tokens: ::core::clone::Clone::clone(&self.tokens),
maybe_empty: ::core::clone::Clone::clone(&self.maybe_empty),
}
}
}Clone, #[automatically_derived]
impl<'tt> ::core::fmt::Debug for TokenSet<'tt> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "TokenSet",
"tokens", &self.tokens, "maybe_empty", &&self.maybe_empty)
}
}Debug)]
1280struct TokenSet<'tt> {
1281 tokens: Vec<TtHandle<'tt>>,
1282 maybe_empty: bool,
1283}
1284
1285impl<'tt> TokenSet<'tt> {
1286 fn empty() -> Self {
1288 TokenSet { tokens: Vec::new(), maybe_empty: true }
1289 }
1290
1291 fn singleton(tt: TtHandle<'tt>) -> Self {
1294 TokenSet { tokens: ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[tt]))vec![tt], maybe_empty: false }
1295 }
1296
1297 fn replace_with(&mut self, tt: TtHandle<'tt>) {
1300 self.tokens.clear();
1301 self.tokens.push(tt);
1302 self.maybe_empty = false;
1303 }
1304
1305 fn replace_with_irrelevant(&mut self) {
1309 self.tokens.clear();
1310 self.maybe_empty = false;
1311 }
1312
1313 fn add_one(&mut self, tt: TtHandle<'tt>) {
1315 if !self.tokens.contains(&tt) {
1316 self.tokens.push(tt);
1317 }
1318 self.maybe_empty = false;
1319 }
1320
1321 fn add_one_maybe(&mut self, tt: TtHandle<'tt>) {
1323 if !self.tokens.contains(&tt) {
1324 self.tokens.push(tt);
1325 }
1326 }
1327
1328 fn add_all(&mut self, other: &Self) {
1336 for tt in &other.tokens {
1337 if !self.tokens.contains(tt) {
1338 self.tokens.push(tt.clone());
1339 }
1340 }
1341 if !other.maybe_empty {
1342 self.maybe_empty = false;
1343 }
1344 }
1345}
1346
1347fn check_matcher_core<'tt>(
1359 sess: &Session,
1360 features: &Features,
1361 node_id: NodeId,
1362 first_sets: &FirstSets<'tt>,
1363 matcher: &'tt [mbe::TokenTree],
1364 follow: &TokenSet<'tt>,
1365) -> Result<TokenSet<'tt>, ErrorGuaranteed> {
1366 use mbe::TokenTree;
1367
1368 let mut last = TokenSet::empty();
1369
1370 let mut errored = Ok(());
1371
1372 'each_token: for i in 0..matcher.len() {
1376 let token = &matcher[i];
1377 let suffix = &matcher[i + 1..];
1378
1379 let build_suffix_first = || {
1380 let mut s = first_sets.first(suffix);
1381 if s.maybe_empty {
1382 s.add_all(follow);
1383 }
1384 s
1385 };
1386
1387 let suffix_first;
1391
1392 match token {
1395 TokenTree::Token(..)
1396 | TokenTree::MetaVar(..)
1397 | TokenTree::MetaVarDecl { .. }
1398 | TokenTree::MetaVarExpr(..) => {
1399 if let TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } = token
1400 && !features.macro_guard_matcher()
1401 {
1402 feature_err(
1403 sess,
1404 sym::macro_guard_matcher,
1405 token.span(),
1406 "`guard` fragments in macro are unstable",
1407 )
1408 .emit();
1409 }
1410 if token_can_be_followed_by_any(token) {
1411 last.replace_with_irrelevant();
1413 continue 'each_token;
1416 } else {
1417 last.replace_with(TtHandle::TtRef(token));
1418 suffix_first = build_suffix_first();
1419 }
1420 }
1421 TokenTree::Delimited(span, _, d) => {
1422 let my_suffix = TokenSet::singleton(TtHandle::from_token_kind(
1423 d.delim.as_close_token_kind(),
1424 span.close,
1425 ));
1426 check_matcher_core(sess, features, node_id, first_sets, &d.tts, &my_suffix)?;
1427 last.replace_with_irrelevant();
1429
1430 continue 'each_token;
1433 }
1434 TokenTree::Sequence(_, seq_rep) => {
1435 suffix_first = build_suffix_first();
1436 let mut new;
1447 let my_suffix = if let Some(sep) = &seq_rep.separator {
1448 new = suffix_first.clone();
1449 new.add_one_maybe(TtHandle::from_token(*sep));
1450 &new
1451 } else {
1452 &suffix_first
1453 };
1454
1455 let next = check_matcher_core(
1459 sess,
1460 features,
1461 node_id,
1462 first_sets,
1463 &seq_rep.tts,
1464 my_suffix,
1465 )?;
1466 if next.maybe_empty {
1467 last.add_all(&next);
1468 } else {
1469 last = next;
1470 }
1471
1472 continue 'each_token;
1475 }
1476 }
1477
1478 for tt in &last.tokens {
1483 if let &TokenTree::MetaVarDecl { span, name, kind } = tt.get() {
1484 for next_token in &suffix_first.tokens {
1485 let next_token = next_token.get();
1486
1487 if is_defined_in_current_crate(node_id)
1494 && #[allow(non_exhaustive_omitted_patterns)] match kind {
NonterminalKind::Pat(PatParam { inferred: true }) => true,
_ => false,
}matches!(kind, NonterminalKind::Pat(PatParam { inferred: true }))
1495 && #[allow(non_exhaustive_omitted_patterns)] match next_token {
TokenTree::Token(token) if *token == token::Or => true,
_ => false,
}matches!(
1496 next_token,
1497 TokenTree::Token(token) if *token == token::Or
1498 )
1499 {
1500 let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl {
1502 span,
1503 name,
1504 kind: NonterminalKind::Pat(PatParam { inferred: false }),
1505 });
1506 sess.psess.buffer_lint(
1507 RUST_2021_INCOMPATIBLE_OR_PATTERNS,
1508 span,
1509 ast::CRATE_NODE_ID,
1510 errors::OrPatternsBackCompat { span, suggestion },
1511 );
1512 }
1513 match is_in_follow(next_token, kind) {
1514 IsInFollow::Yes => {}
1515 IsInFollow::No(possible) => {
1516 let may_be = if last.tokens.len() == 1 && suffix_first.tokens.len() == 1
1517 {
1518 "is"
1519 } else {
1520 "may be"
1521 };
1522
1523 let sp = next_token.span();
1524 let mut err = sess.dcx().struct_span_err(
1525 sp,
1526 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`${0}:{1}` {3} followed by `{2}`, which is not allowed for `{1}` fragments",
name, kind, quoted_tt_to_string(next_token), may_be))
})format!(
1527 "`${name}:{frag}` {may_be} followed by `{next}`, which \
1528 is not allowed for `{frag}` fragments",
1529 name = name,
1530 frag = kind,
1531 next = quoted_tt_to_string(next_token),
1532 may_be = may_be
1533 ),
1534 );
1535 err.span_label(sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("not allowed after `{0}` fragments",
kind))
})format!("not allowed after `{kind}` fragments"));
1536
1537 if kind == NonterminalKind::Pat(PatWithOr)
1538 && sess.psess.edition.at_least_rust_2021()
1539 && next_token.is_token(&token::Or)
1540 {
1541 let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl {
1542 span,
1543 name,
1544 kind: NonterminalKind::Pat(PatParam { inferred: false }),
1545 });
1546 err.span_suggestion(
1547 span,
1548 "try a `pat_param` fragment specifier instead",
1549 suggestion,
1550 Applicability::MaybeIncorrect,
1551 );
1552 }
1553
1554 let msg = "allowed there are: ";
1555 match possible {
1556 &[] => {}
1557 &[t] => {
1558 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("only {0} is allowed after `{1}` fragments",
t, kind))
})format!(
1559 "only {t} is allowed after `{kind}` fragments",
1560 ));
1561 }
1562 ts => {
1563 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1} or {2}", msg,
ts[..ts.len() - 1].to_vec().join(", "), ts[ts.len() - 1]))
})format!(
1564 "{}{} or {}",
1565 msg,
1566 ts[..ts.len() - 1].to_vec().join(", "),
1567 ts[ts.len() - 1],
1568 ));
1569 }
1570 }
1571 errored = Err(err.emit());
1572 }
1573 }
1574 }
1575 }
1576 }
1577 }
1578 errored?;
1579 Ok(last)
1580}
1581
1582fn token_can_be_followed_by_any(tok: &mbe::TokenTree) -> bool {
1583 if let mbe::TokenTree::MetaVarDecl { kind, .. } = *tok {
1584 frag_can_be_followed_by_any(kind)
1585 } else {
1586 true
1588 }
1589}
1590
1591fn frag_can_be_followed_by_any(kind: NonterminalKind) -> bool {
1600 #[allow(non_exhaustive_omitted_patterns)] match kind {
NonterminalKind::Item | NonterminalKind::Block | NonterminalKind::Ident |
NonterminalKind::Literal | NonterminalKind::Meta |
NonterminalKind::Lifetime | NonterminalKind::TT => true,
_ => false,
}matches!(
1601 kind,
1602 NonterminalKind::Item | NonterminalKind::Block | NonterminalKind::Ident | NonterminalKind::Literal | NonterminalKind::Meta | NonterminalKind::Lifetime | NonterminalKind::TT )
1610}
1611
1612enum IsInFollow {
1613 Yes,
1614 No(&'static [&'static str]),
1615}
1616
1617fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
1626 use mbe::TokenTree;
1627
1628 if let TokenTree::Token(Token { kind, .. }) = tok
1629 && kind.close_delim().is_some()
1630 {
1631 IsInFollow::Yes
1634 } else {
1635 match kind {
1636 NonterminalKind::Item => {
1637 IsInFollow::Yes
1640 }
1641 NonterminalKind::Block => {
1642 IsInFollow::Yes
1645 }
1646 NonterminalKind::Stmt | NonterminalKind::Expr(_) => {
1647 const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
1648 match tok {
1649 TokenTree::Token(token) => match token.kind {
1650 FatArrow | Comma | Semi => IsInFollow::Yes,
1651 _ => IsInFollow::No(TOKENS),
1652 },
1653 _ => IsInFollow::No(TOKENS),
1654 }
1655 }
1656 NonterminalKind::Pat(PatParam { .. }) => {
1657 const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`if let`", "`in`"];
1658 match tok {
1659 TokenTree::Token(token) => match token.kind {
1660 FatArrow | Comma | Eq | Or => IsInFollow::Yes,
1661 Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1662 IsInFollow::Yes
1663 }
1664 _ => IsInFollow::No(TOKENS),
1665 },
1666 TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } => IsInFollow::Yes,
1667 _ => IsInFollow::No(TOKENS),
1668 }
1669 }
1670 NonterminalKind::Pat(PatWithOr) => {
1671 const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`if`", "`if let`", "`in`"];
1672 match tok {
1673 TokenTree::Token(token) => match token.kind {
1674 FatArrow | Comma | Eq => IsInFollow::Yes,
1675 Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1676 IsInFollow::Yes
1677 }
1678 _ => IsInFollow::No(TOKENS),
1679 },
1680 TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } => IsInFollow::Yes,
1681 _ => IsInFollow::No(TOKENS),
1682 }
1683 }
1684 NonterminalKind::Guard => {
1685 const TOKENS: &[&str] = &["`=>`", "`,`", "`{`"];
1686 match tok {
1687 TokenTree::Token(token) => match token.kind {
1688 FatArrow | Comma | OpenBrace => IsInFollow::Yes,
1689 _ => IsInFollow::No(TOKENS),
1690 },
1691 _ => IsInFollow::No(TOKENS),
1692 }
1693 }
1694 NonterminalKind::Path | NonterminalKind::Ty => {
1695 const TOKENS: &[&str] = &[
1696 "`{`", "`[`", "`=>`", "`,`", "`>`", "`=`", "`:`", "`;`", "`|`", "`as`",
1697 "`where`",
1698 ];
1699 match tok {
1700 TokenTree::Token(token) => match token.kind {
1701 OpenBrace | OpenBracket | Comma | FatArrow | Colon | Eq | Gt | Shr
1702 | Semi | Or => IsInFollow::Yes,
1703 Ident(name, IdentIsRaw::No) if name == kw::As || name == kw::Where => {
1704 IsInFollow::Yes
1705 }
1706 _ => IsInFollow::No(TOKENS),
1707 },
1708 TokenTree::MetaVarDecl { kind: NonterminalKind::Block, .. } => IsInFollow::Yes,
1709 _ => IsInFollow::No(TOKENS),
1710 }
1711 }
1712 NonterminalKind::Ident | NonterminalKind::Lifetime => {
1713 IsInFollow::Yes
1715 }
1716 NonterminalKind::Literal => {
1717 IsInFollow::Yes
1719 }
1720 NonterminalKind::Meta | NonterminalKind::TT => {
1721 IsInFollow::Yes
1724 }
1725 NonterminalKind::Vis => {
1726 const TOKENS: &[&str] = &["`,`", "an ident", "a type"];
1728 match tok {
1729 TokenTree::Token(token) => match token.kind {
1730 Comma => IsInFollow::Yes,
1731 Ident(_, IdentIsRaw::Yes) => IsInFollow::Yes,
1732 Ident(name, _) if name != kw::Priv => IsInFollow::Yes,
1733 _ => {
1734 if token.can_begin_type() {
1735 IsInFollow::Yes
1736 } else {
1737 IsInFollow::No(TOKENS)
1738 }
1739 }
1740 },
1741 TokenTree::MetaVarDecl {
1742 kind: NonterminalKind::Ident | NonterminalKind::Ty | NonterminalKind::Path,
1743 ..
1744 } => IsInFollow::Yes,
1745 _ => IsInFollow::No(TOKENS),
1746 }
1747 }
1748 }
1749 }
1750}
1751
1752fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
1753 match tt {
1754 mbe::TokenTree::Token(token) => pprust::token_to_string(token).into(),
1755 mbe::TokenTree::MetaVar(_, name) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("${0}", name))
})format!("${name}"),
1756 mbe::TokenTree::MetaVarDecl { name, kind, .. } => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("${0}:{1}", name, kind))
})format!("${name}:{kind}"),
1757 _ => {
::core::panicking::panic_display(&"unexpected mbe::TokenTree::{Sequence or Delimited} \
in follow set checker");
}panic!(
1758 "{}",
1759 "unexpected mbe::TokenTree::{Sequence or Delimited} \
1760 in follow set checker"
1761 ),
1762 }
1763}
1764
1765fn is_defined_in_current_crate(node_id: NodeId) -> bool {
1766 node_id != DUMMY_NODE_ID
1769}
1770
1771pub(super) fn parser_from_cx(
1772 psess: &ParseSess,
1773 mut tts: TokenStream,
1774 recovery: Recovery,
1775) -> Parser<'_> {
1776 tts.desugar_doc_comments();
1777 Parser::new(psess, tts, rustc_parse::MACRO_ARGUMENTS).recovery(recovery)
1778}