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 site_span: Span,
53 macro_ident: Ident,
55 lint_node_id: NodeId,
56 is_trailing_mac: bool,
57 arm_span: Span,
58 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 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 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(¯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))]
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: &'b [MacroRule],
129 matched_rule_bindings: &'b [MatcherLoc],
130 ) -> Self {
131 Self {
132 parser: Parser::new(&cx.sess.psess, tts, None),
133
134 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 Func { lhs: Vec<MatcherLoc>, lhs_span: Span, rhs: mbe::TokenTree },
152 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 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 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 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 type Failure;
338
339 fn build_failure(tok: Token, position: u32, msg: &'static str) -> Self::Failure;
343
344 fn before_match_loc(&mut self, _parser: &TtParser, _matcher: &'matcher MatcherLoc) {}
346
347 fn after_arm(&mut self, _in_body: bool, _result: &NamedParseResult<Self::Failure>) {}
350
351 fn description() -> &'static str;
353
354 fn recovery() -> Recovery {
355 Recovery::Forbidden
356 }
357}
358
359pub(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#[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 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 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 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 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#[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 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 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 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 No(ErrorGuaranteed),
541}
542
543#[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 let parser = parser_from_cx(psess, arg.clone(), T::recovery());
574 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 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 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 }
603 Error(_, _) => {
604 debug!("Fatal error occurred during matching");
605 return Err(CanRetry::Yes);
607 }
608 ErrorReported(guarantee) => {
609 debug!("Fatal error occurred and was reported during matching");
610 return Err(CanRetry::No(guarantee));
612 }
613 }
614
615 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
618 }
619
620 Err(CanRetry::Yes)
621}
622
623#[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 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#[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 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
716pub 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 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 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 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 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 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 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
982fn 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
1013fn 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
1082struct FirstSets<'tt> {
1095 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 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 occ.insert(None);
1147 }
1148 }
1149
1150 if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
1154 first.add_one_maybe(TtHandle::from_token(*sep));
1155 }
1156
1157 if subfirst.maybe_empty
1159 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
1160 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
1161 {
1162 first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
1165 } else {
1166 first = subfirst;
1169 }
1170 }
1171 }
1172 }
1173
1174 first
1175 }
1176 }
1177
1178 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 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 first.maybe_empty = true;
1230 continue;
1231 } else {
1232 return first;
1233 }
1234 }
1235 }
1236 }
1237
1238 if !first.maybe_empty {
::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1241 first
1242 }
1243}
1244
1245#[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 TtRef(&'tt mbe::TokenTree),
1253
1254 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 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 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#[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 fn empty() -> Self {
1320 TokenSet { tokens: Vec::new(), maybe_empty: true }
1321 }
1322
1323 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 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 fn replace_with_irrelevant(&mut self) {
1341 self.tokens.clear();
1342 self.maybe_empty = false;
1343 }
1344
1345 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 fn add_one_maybe(&mut self, tt: TtHandle<'tt>) {
1355 if !self.tokens.contains(&tt) {
1356 self.tokens.push(tt);
1357 }
1358 }
1359
1360 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
1379fn 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 '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 let suffix_first;
1423
1424 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 last.replace_with_irrelevant();
1445 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 last.replace_with_irrelevant();
1461
1462 continue 'each_token;
1465 }
1466 TokenTree::Sequence(_, seq_rep) => {
1467 suffix_first = build_suffix_first();
1468 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 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 continue 'each_token;
1507 }
1508 }
1509
1510 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 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 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 true
1620 }
1621}
1622
1623fn 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 | NonterminalKind::Block | NonterminalKind::Ident | NonterminalKind::Literal | NonterminalKind::Meta | NonterminalKind::Lifetime | NonterminalKind::TT )
1642}
1643
1644enum IsInFollow {
1645 Yes,
1646 No(&'static [&'static str]),
1647}
1648
1649fn 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 IsInFollow::Yes
1666 } else {
1667 match kind {
1668 NonterminalKind::Item => {
1669 IsInFollow::Yes
1672 }
1673 NonterminalKind::Block => {
1674 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 IsInFollow::Yes
1747 }
1748 NonterminalKind::Literal => {
1749 IsInFollow::Yes
1751 }
1752 NonterminalKind::Meta | NonterminalKind::TT => {
1753 IsInFollow::Yes
1756 }
1757 NonterminalKind::Vis => {
1758 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 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}