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}
174
175impl MacroRulesMacroExpander {
176 pub fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, MultiSpan)> {
177 let (span, rhs) = match self.rules[rule_i] {
179 MacroRule::Func { lhs_span, ref rhs, .. } => (MultiSpan::from_span(lhs_span), rhs),
180 MacroRule::Attr { args_span, body_span, ref rhs, .. } => {
181 (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)
182 }
183 MacroRule::Derive { body_span, ref rhs, .. } => (MultiSpan::from_span(body_span), rhs),
184 };
185 if has_compile_error_macro(rhs) { None } else { Some((&self.name, span)) }
186 }
187
188 pub fn kinds(&self) -> MacroKinds {
189 self.kinds
190 }
191
192 pub fn expand_derive(
193 &self,
194 cx: &mut ExtCtxt<'_>,
195 sp: Span,
196 body: &TokenStream,
197 ) -> Result<TokenStream, ErrorGuaranteed> {
198 let name = self.name;
201 let rules = &self.rules;
202 let psess = &cx.sess.psess;
203
204 if cx.trace_macros() {
205 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));
206 trace_macros_note(&mut cx.expansions, sp, msg);
207 }
208
209 match try_match_macro_derive(psess, name, body, rules, &mut NoopTracker) {
210 Ok((rule_index, rule, named_matches)) => {
211 let MacroRule::Derive { rhs, .. } = rule else {
212 {
::core::panicking::panic_fmt(format_args!("try_match_macro_derive returned non-derive rule"));
};panic!("try_match_macro_derive returned non-derive rule");
213 };
214 let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
215 cx.dcx().span_bug(sp, "malformed macro derive rhs");
216 };
217
218 let id = cx.current_expansion.id;
219 let tts = transcribe(psess, &named_matches, rhs, *rhs_span, self.transparency, id)
220 .map_err(|e| e.emit())?;
221
222 if cx.trace_macros() {
223 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));
224 trace_macros_note(&mut cx.expansions, sp, msg);
225 }
226
227 if is_defined_in_current_crate(self.node_id) {
228 cx.resolver.record_macro_rule_usage(self.node_id, rule_index);
229 }
230
231 Ok(tts)
232 }
233 Err(CanRetry::No(guar)) => Err(guar),
234 Err(CanRetry::Yes) => {
235 let (_, guar) = failed_to_match_macro(
236 cx.psess(),
237 sp,
238 self.span,
239 name,
240 FailedMacro::Derive,
241 body,
242 rules,
243 self.on_unmatch_args.as_ref(),
244 );
245 cx.macro_error_and_trace_macros_diag();
246 Err(guar)
247 }
248 }
249 }
250}
251
252impl TTMacroExpander for MacroRulesMacroExpander {
253 fn expand<'cx, 'a: 'cx>(
254 &'a self,
255 cx: &'cx mut ExtCtxt<'_>,
256 sp: Span,
257 input: TokenStream,
258 ) -> MacroExpanderResult<'cx> {
259 ExpandResult::Ready(expand_macro(
260 cx,
261 sp,
262 self.span,
263 self.node_id,
264 self.name,
265 self.transparency,
266 input,
267 &self.rules,
268 self.on_unmatch_args.as_ref(),
269 ))
270 }
271}
272
273impl AttrProcMacro for MacroRulesMacroExpander {
274 fn expand(
275 &self,
276 _cx: &mut ExtCtxt<'_>,
277 _sp: Span,
278 _args: TokenStream,
279 _body: TokenStream,
280 ) -> Result<TokenStream, ErrorGuaranteed> {
281 {
::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`")
282 }
283
284 fn expand_with_safety(
285 &self,
286 cx: &mut ExtCtxt<'_>,
287 safety: Safety,
288 sp: Span,
289 args: TokenStream,
290 body: TokenStream,
291 ) -> Result<TokenStream, ErrorGuaranteed> {
292 expand_macro_attr(
293 cx,
294 sp,
295 self.span,
296 self.node_id,
297 self.name,
298 self.transparency,
299 safety,
300 args,
301 body,
302 &self.rules,
303 self.on_unmatch_args.as_ref(),
304 )
305 }
306}
307
308struct DummyBang(ErrorGuaranteed);
309
310impl BangProcMacro for DummyBang {
311 fn expand<'cx>(
312 &self,
313 _: &'cx mut ExtCtxt<'_>,
314 _: Span,
315 _: TokenStream,
316 ) -> Result<TokenStream, ErrorGuaranteed> {
317 Err(self.0)
318 }
319}
320
321fn trace_macros_note(cx_expansions: &mut FxIndexMap<Span, Vec<String>>, sp: Span, message: String) {
322 let sp = sp.macro_backtrace().last().map_or(sp, |trace| trace.call_site);
323 cx_expansions.entry(sp).or_default().push(message);
324}
325
326pub(super) trait Tracker<'matcher> {
327 type Failure;
329
330 fn build_failure(tok: Token, position: u32, msg: &'static str) -> Self::Failure;
334
335 fn before_match_loc(&mut self, _parser: &TtParser, _matcher: &'matcher MatcherLoc) {}
337
338 fn after_arm(&mut self, _in_body: bool, _result: &NamedParseResult<Self::Failure>) {}
341
342 fn description() -> &'static str;
344
345 fn recovery() -> Recovery {
346 Recovery::Forbidden
347 }
348}
349
350pub(super) struct NoopTracker;
353
354impl<'matcher> Tracker<'matcher> for NoopTracker {
355 type Failure = ();
356
357 fn build_failure(_tok: Token, _position: u32, _msg: &'static str) -> Self::Failure {}
358
359 fn description() -> &'static str {
360 "none"
361 }
362}
363
364#[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(365u32),
::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:421",
"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(421u32),
::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))]
366fn expand_macro<'cx, 'a: 'cx>(
367 cx: &'cx mut ExtCtxt<'_>,
368 sp: Span,
369 def_span: Span,
370 node_id: NodeId,
371 name: Ident,
372 transparency: Transparency,
373 arg: TokenStream,
374 rules: &'a [MacroRule],
375 on_unmatch_args: Option<&Directive>,
376) -> Box<dyn MacResult + 'cx> {
377 let psess = &cx.sess.psess;
378
379 if cx.trace_macros() {
380 let msg = format!("expanding `{}! {{ {} }}`", name, pprust::tts_to_string(&arg));
381 trace_macros_note(&mut cx.expansions, sp, msg);
382 }
383
384 let try_success_result = try_match_macro(psess, name, &arg, rules, &mut NoopTracker);
386
387 match try_success_result {
388 Ok((rule_index, rule, named_matches)) => {
389 let MacroRule::Func { lhs, rhs, .. } = rule else {
390 panic!("try_match_macro returned non-func rule");
391 };
392 let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
393 cx.dcx().span_bug(sp, "malformed macro rhs");
394 };
395 let arm_span = rhs_span.entire();
396
397 let id = cx.current_expansion.id;
399 let tts = match transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id) {
400 Ok(tts) => tts,
401 Err(err) => {
402 let guar = err.emit();
403 return DummyResult::any(arm_span, guar);
404 }
405 };
406
407 if cx.trace_macros() {
408 let msg = format!("to `{}`", pprust::tts_to_string(&tts));
409 trace_macros_note(&mut cx.expansions, sp, msg);
410 }
411
412 let is_local = is_defined_in_current_crate(node_id);
413 if is_local {
414 cx.resolver.record_macro_rule_usage(node_id, rule_index);
415 }
416
417 Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name, rules, lhs))
419 }
420 Err(CanRetry::No(guar)) => {
421 debug!("Will not retry matching as an error was emitted already");
422 DummyResult::any(sp, guar)
423 }
424 Err(CanRetry::Yes) => {
425 let (span, guar) = failed_to_match_macro(
427 cx.psess(),
428 sp,
429 def_span,
430 name,
431 FailedMacro::Func,
432 &arg,
433 rules,
434 on_unmatch_args,
435 );
436 cx.macro_error_and_trace_macros_diag();
437 DummyResult::any(span, guar)
438 }
439 }
440}
441
442#[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(443u32),
::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))]
444fn expand_macro_attr(
445 cx: &mut ExtCtxt<'_>,
446 sp: Span,
447 def_span: Span,
448 node_id: NodeId,
449 name: Ident,
450 transparency: Transparency,
451 safety: Safety,
452 args: TokenStream,
453 body: TokenStream,
454 rules: &[MacroRule],
455 on_unmatch_args: Option<&Directive>,
456) -> Result<TokenStream, ErrorGuaranteed> {
457 let psess = &cx.sess.psess;
458 let is_local = node_id != DUMMY_NODE_ID;
461
462 if cx.trace_macros() {
463 let msg = format!(
464 "expanding `#[{name}({})] {}`",
465 pprust::tts_to_string(&args),
466 pprust::tts_to_string(&body),
467 );
468 trace_macros_note(&mut cx.expansions, sp, msg);
469 }
470
471 match try_match_macro_attr(psess, name, &args, &body, rules, &mut NoopTracker) {
473 Ok((i, rule, named_matches)) => {
474 let MacroRule::Attr { rhs, unsafe_rule, .. } = rule else {
475 panic!("try_macro_match_attr returned non-attr rule");
476 };
477 let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
478 cx.dcx().span_bug(sp, "malformed macro rhs");
479 };
480
481 match (safety, unsafe_rule) {
482 (Safety::Default, false) | (Safety::Unsafe(_), true) => {}
483 (Safety::Default, true) => {
484 cx.dcx().span_err(sp, "unsafe attribute invocation requires `unsafe`");
485 }
486 (Safety::Unsafe(span), false) => {
487 cx.dcx().span_err(span, "unnecessary `unsafe` on safe attribute invocation");
488 }
489 (Safety::Safe(span), _) => {
490 cx.dcx().span_bug(span, "unexpected `safe` keyword");
491 }
492 }
493
494 let id = cx.current_expansion.id;
495 let tts = transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id)
496 .map_err(|e| e.emit())?;
497
498 if cx.trace_macros() {
499 let msg = format!("to `{}`", pprust::tts_to_string(&tts));
500 trace_macros_note(&mut cx.expansions, sp, msg);
501 }
502
503 if is_local {
504 cx.resolver.record_macro_rule_usage(node_id, i);
505 }
506
507 Ok(tts)
508 }
509 Err(CanRetry::No(guar)) => Err(guar),
510 Err(CanRetry::Yes) => {
511 let (_, guar) = failed_to_match_macro(
513 cx.psess(),
514 sp,
515 def_span,
516 name,
517 FailedMacro::Attr(&args),
518 &body,
519 rules,
520 on_unmatch_args,
521 );
522 cx.trace_macros_diag();
523 Err(guar)
524 }
525 }
526}
527
528pub(super) enum CanRetry {
529 Yes,
530 No(ErrorGuaranteed),
532}
533
534#[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(537u32),
::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(569u32),
::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:583",
"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(583u32),
::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:591",
"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(591u32),
::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:595",
"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(595u32),
::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:600",
"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(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::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()))]
538pub(super) fn try_match_macro<'matcher, T: Tracker<'matcher>>(
539 psess: &ParseSess,
540 name: Ident,
541 arg: &TokenStream,
542 rules: &'matcher [MacroRule],
543 track: &mut T,
544) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
545 let parser = parser_from_cx(psess, arg.clone(), T::recovery());
565 let mut tt_parser = TtParser::new(name);
567 for (i, rule) in rules.iter().enumerate() {
568 let MacroRule::Func { lhs, .. } = rule else { continue };
569 let _tracing_span = trace_span!("Matching arm", %i);
570
571 let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
576
577 let result = tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs, track);
578
579 track.after_arm(true, &result);
580
581 match result {
582 Success(named_matches) => {
583 debug!("Parsed arm successfully");
584 psess.gated_spans.merge(gated_spans_snapshot);
587
588 return Ok((i, rule, named_matches));
589 }
590 Failure(_) => {
591 trace!("Failed to match arm, trying the next one");
592 }
594 Error(_, _) => {
595 debug!("Fatal error occurred during matching");
596 return Err(CanRetry::Yes);
598 }
599 ErrorReported(guarantee) => {
600 debug!("Fatal error occurred and was reported during matching");
601 return Err(CanRetry::No(guarantee));
603 }
604 }
605
606 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
609 }
610
611 Err(CanRetry::Yes)
612}
613
614#[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(617u32),
::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()))]
618pub(super) fn try_match_macro_attr<'matcher, T: Tracker<'matcher>>(
619 psess: &ParseSess,
620 name: Ident,
621 attr_args: &TokenStream,
622 attr_body: &TokenStream,
623 rules: &'matcher [MacroRule],
624 track: &mut T,
625) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
626 let args_parser = parser_from_cx(psess, attr_args.clone(), T::recovery());
628 let body_parser = parser_from_cx(psess, attr_body.clone(), T::recovery());
629 let mut tt_parser = TtParser::new(name);
630 for (i, rule) in rules.iter().enumerate() {
631 let MacroRule::Attr { args, body, .. } = rule else { continue };
632
633 let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
634
635 let result = tt_parser.parse_tt(&mut Cow::Borrowed(&args_parser), args, track);
636 track.after_arm(false, &result);
637
638 let mut named_matches = match result {
639 Success(named_matches) => named_matches,
640 Failure(_) => {
641 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut());
642 continue;
643 }
644 Error(_, _) => return Err(CanRetry::Yes),
645 ErrorReported(guar) => return Err(CanRetry::No(guar)),
646 };
647
648 let result = tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body, track);
649 track.after_arm(true, &result);
650
651 match result {
652 Success(body_named_matches) => {
653 psess.gated_spans.merge(gated_spans_snapshot);
654 #[allow(rustc::potential_query_instability)]
655 named_matches.extend(body_named_matches);
656 return Ok((i, rule, named_matches));
657 }
658 Failure(_) => {
659 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut())
660 }
661 Error(_, _) => return Err(CanRetry::Yes),
662 ErrorReported(guar) => return Err(CanRetry::No(guar)),
663 }
664 }
665
666 Err(CanRetry::Yes)
667}
668
669#[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(672u32),
::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()))]
673pub(super) fn try_match_macro_derive<'matcher, T: Tracker<'matcher>>(
674 psess: &ParseSess,
675 name: Ident,
676 body: &TokenStream,
677 rules: &'matcher [MacroRule],
678 track: &mut T,
679) -> Result<(usize, &'matcher MacroRule, NamedMatches), CanRetry> {
680 let body_parser = parser_from_cx(psess, body.clone(), T::recovery());
682 let mut tt_parser = TtParser::new(name);
683 for (i, rule) in rules.iter().enumerate() {
684 let MacroRule::Derive { body, .. } = rule else { continue };
685
686 let mut gated_spans_snapshot = mem::take(&mut *psess.gated_spans.spans.borrow_mut());
687
688 let result = tt_parser.parse_tt(&mut Cow::Borrowed(&body_parser), body, track);
689 track.after_arm(true, &result);
690
691 match result {
692 Success(named_matches) => {
693 psess.gated_spans.merge(gated_spans_snapshot);
694 return Ok((i, rule, named_matches));
695 }
696 Failure(_) => {
697 mem::swap(&mut gated_spans_snapshot, &mut psess.gated_spans.spans.borrow_mut())
698 }
699 Error(_, _) => return Err(CanRetry::Yes),
700 ErrorReported(guar) => return Err(CanRetry::No(guar)),
701 }
702 }
703
704 Err(CanRetry::Yes)
705}
706
707pub fn compile_declarative_macro(
709 sess: &Session,
710 features: &Features,
711 macro_def: &ast::MacroDef,
712 ident: Ident,
713 attrs: &[hir::Attribute],
714 span: Span,
715 node_id: NodeId,
716 edition: Edition,
717) -> (SyntaxExtension, usize) {
718 let mk_syn_ext = |kind| {
719 let is_local = is_defined_in_current_crate(node_id);
720 SyntaxExtension::new(sess, kind, span, Vec::new(), edition, ident.name, attrs, is_local)
721 };
722 let dummy_syn_ext =
723 |guar| (mk_syn_ext(SyntaxExtensionKind::Bang(Arc::new(DummyBang(guar)))), 0);
724
725 let macro_rules = macro_def.macro_rules;
726 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) };
727
728 let body = macro_def.body.tokens.clone();
729 let mut p = Parser::new(&sess.psess, body, rustc_parse::MACRO_ARGUMENTS);
730
731 let mut guar = None;
734 let mut check_emission = |ret: Result<(), ErrorGuaranteed>| guar = guar.or(ret.err());
735
736 let mut kinds = MacroKinds::empty();
737 let mut rules = Vec::new();
738
739 while p.token != token::Eof {
740 let unsafe_rule = p.eat_keyword_noexpect(kw::Unsafe);
741 let unsafe_keyword_span = p.prev_token.span;
742 if unsafe_rule && let Some(guar) = check_no_eof(sess, &p, "expected `attr`") {
743 return dummy_syn_ext(guar);
744 }
745 let (args, is_derive) = if p.eat_keyword_noexpect(sym::attr) {
746 kinds |= MacroKinds::ATTR;
747 if !features.macro_attr() {
748 feature_err(sess, sym::macro_attr, span, "`macro_rules!` attributes are unstable")
749 .emit();
750 }
751 if let Some(guar) = check_no_eof(sess, &p, "expected macro attr args") {
752 return dummy_syn_ext(guar);
753 }
754 let args = p.parse_token_tree();
755 check_args_parens(sess, sym::attr, &args);
756 let args = parse_one_tt(args, RulePart::Pattern, sess, node_id, features, edition);
757 check_emission(check_lhs(sess, features, node_id, &args));
758 if let Some(guar) = check_no_eof(sess, &p, "expected macro attr body") {
759 return dummy_syn_ext(guar);
760 }
761 (Some(args), false)
762 } else if p.eat_keyword_noexpect(sym::derive) {
763 kinds |= MacroKinds::DERIVE;
764 let derive_keyword_span = p.prev_token.span;
765 if !features.macro_derive() {
766 feature_err(sess, sym::macro_derive, span, "`macro_rules!` derives are unstable")
767 .emit();
768 }
769 if unsafe_rule {
770 sess.dcx()
771 .span_err(unsafe_keyword_span, "`unsafe` is only supported on `attr` rules");
772 }
773 if let Some(guar) = check_no_eof(sess, &p, "expected `()` after `derive`") {
774 return dummy_syn_ext(guar);
775 }
776 let args = p.parse_token_tree();
777 check_args_parens(sess, sym::derive, &args);
778 let args_empty_result = check_args_empty(sess, &args);
779 let args_not_empty = args_empty_result.is_err();
780 check_emission(args_empty_result);
781 if let Some(guar) = check_no_eof(sess, &p, "expected macro derive body") {
782 return dummy_syn_ext(guar);
783 }
784 if p.token == token::FatArrow {
787 let mut err = sess
788 .dcx()
789 .struct_span_err(p.token.span, "expected macro derive body, got `=>`");
790 if args_not_empty {
791 err.span_label(derive_keyword_span, "need `()` after this `derive`");
792 }
793 return dummy_syn_ext(err.emit());
794 }
795 (None, true)
796 } else {
797 kinds |= MacroKinds::BANG;
798 if unsafe_rule {
799 sess.dcx()
800 .span_err(unsafe_keyword_span, "`unsafe` is only supported on `attr` rules");
801 }
802 (None, false)
803 };
804 let lhs_tt = p.parse_token_tree();
805 let lhs_tt = parse_one_tt(lhs_tt, RulePart::Pattern, sess, node_id, features, edition);
806 check_emission(check_lhs(sess, features, node_id, &lhs_tt));
807 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)) {
808 return dummy_syn_ext(e.emit());
809 }
810 if let Some(guar) = check_no_eof(sess, &p, "expected right-hand side of macro rule") {
811 return dummy_syn_ext(guar);
812 }
813 let rhs = p.parse_token_tree();
814 let rhs = parse_one_tt(rhs, RulePart::Body, sess, node_id, features, edition);
815 check_emission(check_rhs(sess, &rhs));
816 check_emission(check_meta_variables(&sess.psess, node_id, args.as_ref(), &lhs_tt, &rhs));
817 let lhs_span = lhs_tt.span();
818 let mbe::TokenTree::Delimited(.., delimited) = lhs_tt else {
821 return dummy_syn_ext(guar.unwrap());
822 };
823 let lhs = mbe::macro_parser::compute_locs(&delimited.tts);
824 if let Some(args) = args {
825 let args_span = args.span();
826 let mbe::TokenTree::Delimited(.., delimited) = args else {
827 return dummy_syn_ext(guar.unwrap());
828 };
829 let args = mbe::macro_parser::compute_locs(&delimited.tts);
830 let body_span = lhs_span;
831 rules.push(MacroRule::Attr { unsafe_rule, args, args_span, body: lhs, body_span, rhs });
832 } else if is_derive {
833 rules.push(MacroRule::Derive { body: lhs, body_span: lhs_span, rhs });
834 } else {
835 rules.push(MacroRule::Func { lhs, lhs_span, rhs });
836 }
837 if p.token == token::Eof {
838 break;
839 }
840 if let Err(e) = p.expect(exp_sep) {
841 return dummy_syn_ext(e.emit());
842 }
843 }
844
845 if rules.is_empty() {
846 let guar = sess.dcx().span_err(span, "macros must contain at least one rule");
847 return dummy_syn_ext(guar);
848 }
849 if !!kinds.is_empty() {
::core::panicking::panic("assertion failed: !kinds.is_empty()")
};assert!(!kinds.is_empty());
850
851 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)
852 .unwrap_or(Transparency::fallback(macro_rules));
853
854 if let Some(guar) = guar {
855 return dummy_syn_ext(guar);
858 }
859
860 let nrules = if is_defined_in_current_crate(node_id) { rules.len() } else { 0 };
862
863 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!(
864 attrs,
865 OnUnmatchArgs { directive, .. } => directive.clone()
866 )
867 .flatten()
868 .map(|directive| *directive);
869
870 let exp = MacroRulesMacroExpander {
871 name: ident,
872 kinds,
873 span,
874 node_id,
875 on_unmatch_args,
876 transparency,
877 rules,
878 };
879 (mk_syn_ext(SyntaxExtensionKind::MacroRules(Arc::new(exp))), nrules)
880}
881
882fn check_no_eof(sess: &Session, p: &Parser<'_>, msg: &'static str) -> Option<ErrorGuaranteed> {
883 if p.token == token::Eof {
884 let err_sp = p.token.span.shrink_to_hi();
885 let guar = sess
886 .dcx()
887 .struct_span_err(err_sp, "macro definition ended unexpectedly")
888 .with_span_label(err_sp, msg)
889 .emit();
890 return Some(guar);
891 }
892 None
893}
894
895fn check_args_parens(sess: &Session, rule_kw: Symbol, args: &tokenstream::TokenTree) {
896 if let tokenstream::TokenTree::Delimited(dspan, _, delim, _) = args
898 && *delim != Delimiter::Parenthesis
899 {
900 sess.dcx().emit_err(errors::MacroArgsBadDelim {
901 span: dspan.entire(),
902 sugg: errors::MacroArgsBadDelimSugg { open: dspan.open, close: dspan.close },
903 rule_kw,
904 });
905 }
906}
907
908fn check_args_empty(sess: &Session, args: &tokenstream::TokenTree) -> Result<(), ErrorGuaranteed> {
909 match args {
910 tokenstream::TokenTree::Delimited(.., delimited) if delimited.is_empty() => Ok(()),
911 _ => {
912 let msg = "`derive` rules do not accept arguments; `derive` must be followed by `()`";
913 Err(sess.dcx().span_err(args.span(), msg))
914 }
915 }
916}
917
918fn check_lhs(
919 sess: &Session,
920 features: &Features,
921 node_id: NodeId,
922 lhs: &mbe::TokenTree,
923) -> Result<(), ErrorGuaranteed> {
924 let e1 = check_lhs_nt_follows(sess, features, node_id, lhs);
925 let e2 = check_lhs_no_empty_seq(sess, slice::from_ref(lhs));
926 e1.and(e2)
927}
928
929fn check_lhs_nt_follows(
930 sess: &Session,
931 features: &Features,
932 node_id: NodeId,
933 lhs: &mbe::TokenTree,
934) -> Result<(), ErrorGuaranteed> {
935 if let mbe::TokenTree::Delimited(.., delimited) = lhs {
938 check_matcher(sess, features, node_id, &delimited.tts)
939 } else {
940 let msg = "invalid macro matcher; matchers must be contained in balanced delimiters";
941 Err(sess.dcx().span_err(lhs.span(), msg))
942 }
943}
944
945fn is_empty_token_tree(sess: &Session, seq: &mbe::SequenceRepetition) -> bool {
946 if seq.separator.is_some() {
947 false
948 } else {
949 let mut is_empty = true;
950 let mut iter = seq.tts.iter().peekable();
951 while let Some(tt) = iter.next() {
952 match tt {
953 mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. } => {}
954 mbe::TokenTree::Token(t @ Token { kind: DocComment(..), .. }) => {
955 let mut now = t;
956 while let Some(&mbe::TokenTree::Token(
957 next @ Token { kind: DocComment(..), .. },
958 )) = iter.peek()
959 {
960 now = next;
961 iter.next();
962 }
963 let span = t.span.to(now.span);
964 sess.dcx().span_note(span, "doc comments are ignored in matcher position");
965 }
966 mbe::TokenTree::Sequence(_, sub_seq)
967 if (sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
968 || sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne) => {}
969 _ => is_empty = false,
970 }
971 }
972 is_empty
973 }
974}
975
976fn check_redundant_vis_repetition(
981 err: &mut Diag<'_>,
982 sess: &Session,
983 seq: &SequenceRepetition,
984 span: &DelimSpan,
985) {
986 if seq.kleene.op == KleeneOp::ZeroOrOne
987 && #[allow(non_exhaustive_omitted_patterns)] match seq.tts.first() {
Some(mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. }) =>
true,
_ => false,
}matches!(
988 seq.tts.first(),
989 Some(mbe::TokenTree::MetaVarDecl { kind: NonterminalKind::Vis, .. })
990 )
991 {
992 err.note("a `vis` fragment can already be empty");
993 err.multipart_suggestion(
994 "remove the `$(` and `)?`",
995 ::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![
996 (
997 sess.source_map().span_extend_to_prev_char_before(span.open, '$', true),
998 "".to_string(),
999 ),
1000 (span.close.with_hi(seq.kleene.span.hi()), "".to_string()),
1001 ],
1002 Applicability::MaybeIncorrect,
1003 );
1004 }
1005}
1006
1007fn check_lhs_no_empty_seq(sess: &Session, tts: &[mbe::TokenTree]) -> Result<(), ErrorGuaranteed> {
1010 use mbe::TokenTree;
1011 for tt in tts {
1012 match tt {
1013 TokenTree::Token(..)
1014 | TokenTree::MetaVar(..)
1015 | TokenTree::MetaVarDecl { .. }
1016 | TokenTree::MetaVarExpr(..) => (),
1017 TokenTree::Delimited(.., del) => check_lhs_no_empty_seq(sess, &del.tts)?,
1018 TokenTree::Sequence(span, seq) => {
1019 if is_empty_token_tree(sess, seq) {
1020 let sp = span.entire();
1021 let mut err =
1022 sess.dcx().struct_span_err(sp, "repetition matches empty token tree");
1023 check_redundant_vis_repetition(&mut err, sess, seq, span);
1024 return Err(err.emit());
1025 }
1026 check_lhs_no_empty_seq(sess, &seq.tts)?
1027 }
1028 }
1029 }
1030
1031 Ok(())
1032}
1033
1034fn check_rhs(sess: &Session, rhs: &mbe::TokenTree) -> Result<(), ErrorGuaranteed> {
1035 match *rhs {
1036 mbe::TokenTree::Delimited(..) => Ok(()),
1037 _ => Err(sess.dcx().span_err(rhs.span(), "macro rhs must be delimited")),
1038 }
1039}
1040
1041fn check_matcher(
1042 sess: &Session,
1043 features: &Features,
1044 node_id: NodeId,
1045 matcher: &[mbe::TokenTree],
1046) -> Result<(), ErrorGuaranteed> {
1047 let first_sets = FirstSets::new(matcher);
1048 let empty_suffix = TokenSet::empty();
1049 check_matcher_core(sess, features, node_id, &first_sets, matcher, &empty_suffix)?;
1050 Ok(())
1051}
1052
1053fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool {
1054 match rhs {
1055 mbe::TokenTree::Delimited(.., d) => {
1056 let has_compile_error = d.tts.array_windows::<3>().any(|[ident, bang, args]| {
1057 if let mbe::TokenTree::Token(ident) = ident
1058 && let TokenKind::Ident(ident, _) = ident.kind
1059 && ident == sym::compile_error
1060 && let mbe::TokenTree::Token(bang) = bang
1061 && let TokenKind::Bang = bang.kind
1062 && let mbe::TokenTree::Delimited(.., del) = args
1063 && !del.delim.skip()
1064 {
1065 true
1066 } else {
1067 false
1068 }
1069 });
1070 if has_compile_error { true } else { d.tts.iter().any(has_compile_error_macro) }
1071 }
1072 _ => false,
1073 }
1074}
1075
1076struct FirstSets<'tt> {
1089 first: FxHashMap<Span, Option<TokenSet<'tt>>>,
1096}
1097
1098impl<'tt> FirstSets<'tt> {
1099 fn new(tts: &'tt [mbe::TokenTree]) -> FirstSets<'tt> {
1100 use mbe::TokenTree;
1101
1102 let mut sets = FirstSets { first: FxHashMap::default() };
1103 build_recur(&mut sets, tts);
1104 return sets;
1105
1106 fn build_recur<'tt>(sets: &mut FirstSets<'tt>, tts: &'tt [TokenTree]) -> TokenSet<'tt> {
1110 let mut first = TokenSet::empty();
1111 for tt in tts.iter().rev() {
1112 match tt {
1113 TokenTree::Token(..)
1114 | TokenTree::MetaVar(..)
1115 | TokenTree::MetaVarDecl { .. }
1116 | TokenTree::MetaVarExpr(..) => {
1117 first.replace_with(TtHandle::TtRef(tt));
1118 }
1119 TokenTree::Delimited(span, _, delimited) => {
1120 build_recur(sets, &delimited.tts);
1121 first.replace_with(TtHandle::from_token_kind(
1122 delimited.delim.as_open_token_kind(),
1123 span.open,
1124 ));
1125 }
1126 TokenTree::Sequence(sp, seq_rep) => {
1127 let subfirst = build_recur(sets, &seq_rep.tts);
1128
1129 match sets.first.entry(sp.entire()) {
1130 Entry::Vacant(vac) => {
1131 vac.insert(Some(subfirst.clone()));
1132 }
1133 Entry::Occupied(mut occ) => {
1134 occ.insert(None);
1141 }
1142 }
1143
1144 if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
1148 first.add_one_maybe(TtHandle::from_token(*sep));
1149 }
1150
1151 if subfirst.maybe_empty
1153 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
1154 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
1155 {
1156 first.add_all(&TokenSet { maybe_empty: true, ..subfirst });
1159 } else {
1160 first = subfirst;
1163 }
1164 }
1165 }
1166 }
1167
1168 first
1169 }
1170 }
1171
1172 fn first(&self, tts: &'tt [mbe::TokenTree]) -> TokenSet<'tt> {
1175 use mbe::TokenTree;
1176
1177 let mut first = TokenSet::empty();
1178 for tt in tts.iter() {
1179 if !first.maybe_empty {
::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1180 match tt {
1181 TokenTree::Token(..)
1182 | TokenTree::MetaVar(..)
1183 | TokenTree::MetaVarDecl { .. }
1184 | TokenTree::MetaVarExpr(..) => {
1185 first.add_one(TtHandle::TtRef(tt));
1186 return first;
1187 }
1188 TokenTree::Delimited(span, _, delimited) => {
1189 first.add_one(TtHandle::from_token_kind(
1190 delimited.delim.as_open_token_kind(),
1191 span.open,
1192 ));
1193 return first;
1194 }
1195 TokenTree::Sequence(sp, seq_rep) => {
1196 let subfirst_owned;
1197 let subfirst = match self.first.get(&sp.entire()) {
1198 Some(Some(subfirst)) => subfirst,
1199 Some(&None) => {
1200 subfirst_owned = self.first(&seq_rep.tts);
1201 &subfirst_owned
1202 }
1203 None => {
1204 {
::core::panicking::panic_fmt(format_args!("We missed a sequence during FirstSets construction"));
};panic!("We missed a sequence during FirstSets construction");
1205 }
1206 };
1207
1208 if let (Some(sep), true) = (&seq_rep.separator, subfirst.maybe_empty) {
1211 first.add_one_maybe(TtHandle::from_token(*sep));
1212 }
1213
1214 if !first.maybe_empty {
::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1215 first.add_all(subfirst);
1216 if subfirst.maybe_empty
1217 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrMore
1218 || seq_rep.kleene.op == mbe::KleeneOp::ZeroOrOne
1219 {
1220 first.maybe_empty = true;
1224 continue;
1225 } else {
1226 return first;
1227 }
1228 }
1229 }
1230 }
1231
1232 if !first.maybe_empty {
::core::panicking::panic("assertion failed: first.maybe_empty")
};assert!(first.maybe_empty);
1235 first
1236 }
1237}
1238
1239#[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)]
1244enum TtHandle<'tt> {
1245 TtRef(&'tt mbe::TokenTree),
1247
1248 Token(mbe::TokenTree),
1253}
1254
1255impl<'tt> TtHandle<'tt> {
1256 fn from_token(tok: Token) -> Self {
1257 TtHandle::Token(mbe::TokenTree::Token(tok))
1258 }
1259
1260 fn from_token_kind(kind: TokenKind, span: Span) -> Self {
1261 TtHandle::from_token(Token::new(kind, span))
1262 }
1263
1264 fn get(&'tt self) -> &'tt mbe::TokenTree {
1266 match self {
1267 TtHandle::TtRef(tt) => tt,
1268 TtHandle::Token(token_tt) => token_tt,
1269 }
1270 }
1271}
1272
1273impl<'tt> PartialEq for TtHandle<'tt> {
1274 fn eq(&self, other: &TtHandle<'tt>) -> bool {
1275 self.get() == other.get()
1276 }
1277}
1278
1279impl<'tt> Clone for TtHandle<'tt> {
1280 fn clone(&self) -> Self {
1281 match self {
1282 TtHandle::TtRef(tt) => TtHandle::TtRef(tt),
1283
1284 TtHandle::Token(mbe::TokenTree::Token(tok)) => {
1287 TtHandle::Token(mbe::TokenTree::Token(*tok))
1288 }
1289
1290 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1291 }
1292 }
1293}
1294
1295#[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)]
1306struct TokenSet<'tt> {
1307 tokens: Vec<TtHandle<'tt>>,
1308 maybe_empty: bool,
1309}
1310
1311impl<'tt> TokenSet<'tt> {
1312 fn empty() -> Self {
1314 TokenSet { tokens: Vec::new(), maybe_empty: true }
1315 }
1316
1317 fn singleton(tt: TtHandle<'tt>) -> Self {
1320 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 }
1321 }
1322
1323 fn replace_with(&mut self, tt: TtHandle<'tt>) {
1326 self.tokens.clear();
1327 self.tokens.push(tt);
1328 self.maybe_empty = false;
1329 }
1330
1331 fn replace_with_irrelevant(&mut self) {
1335 self.tokens.clear();
1336 self.maybe_empty = false;
1337 }
1338
1339 fn add_one(&mut self, tt: TtHandle<'tt>) {
1341 if !self.tokens.contains(&tt) {
1342 self.tokens.push(tt);
1343 }
1344 self.maybe_empty = false;
1345 }
1346
1347 fn add_one_maybe(&mut self, tt: TtHandle<'tt>) {
1349 if !self.tokens.contains(&tt) {
1350 self.tokens.push(tt);
1351 }
1352 }
1353
1354 fn add_all(&mut self, other: &Self) {
1362 for tt in &other.tokens {
1363 if !self.tokens.contains(tt) {
1364 self.tokens.push(tt.clone());
1365 }
1366 }
1367 if !other.maybe_empty {
1368 self.maybe_empty = false;
1369 }
1370 }
1371}
1372
1373fn check_matcher_core<'tt>(
1385 sess: &Session,
1386 features: &Features,
1387 node_id: NodeId,
1388 first_sets: &FirstSets<'tt>,
1389 matcher: &'tt [mbe::TokenTree],
1390 follow: &TokenSet<'tt>,
1391) -> Result<TokenSet<'tt>, ErrorGuaranteed> {
1392 use mbe::TokenTree;
1393
1394 let mut last = TokenSet::empty();
1395
1396 let mut errored = Ok(());
1397
1398 'each_token: for i in 0..matcher.len() {
1402 let token = &matcher[i];
1403 let suffix = &matcher[i + 1..];
1404
1405 let build_suffix_first = || {
1406 let mut s = first_sets.first(suffix);
1407 if s.maybe_empty {
1408 s.add_all(follow);
1409 }
1410 s
1411 };
1412
1413 let suffix_first;
1417
1418 match token {
1421 TokenTree::Token(..)
1422 | TokenTree::MetaVar(..)
1423 | TokenTree::MetaVarDecl { .. }
1424 | TokenTree::MetaVarExpr(..) => {
1425 if let TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } = token
1426 && !features.macro_guard_matcher()
1427 {
1428 feature_err(
1429 sess,
1430 sym::macro_guard_matcher,
1431 token.span(),
1432 "`guard` fragments in macro are unstable",
1433 )
1434 .emit();
1435 }
1436 if token_can_be_followed_by_any(token) {
1437 last.replace_with_irrelevant();
1439 continue 'each_token;
1442 } else {
1443 last.replace_with(TtHandle::TtRef(token));
1444 suffix_first = build_suffix_first();
1445 }
1446 }
1447 TokenTree::Delimited(span, _, d) => {
1448 let my_suffix = TokenSet::singleton(TtHandle::from_token_kind(
1449 d.delim.as_close_token_kind(),
1450 span.close,
1451 ));
1452 check_matcher_core(sess, features, node_id, first_sets, &d.tts, &my_suffix)?;
1453 last.replace_with_irrelevant();
1455
1456 continue 'each_token;
1459 }
1460 TokenTree::Sequence(_, seq_rep) => {
1461 suffix_first = build_suffix_first();
1462 let mut new;
1473 let my_suffix = if let Some(sep) = &seq_rep.separator {
1474 new = suffix_first.clone();
1475 new.add_one_maybe(TtHandle::from_token(*sep));
1476 &new
1477 } else {
1478 &suffix_first
1479 };
1480
1481 let next = check_matcher_core(
1485 sess,
1486 features,
1487 node_id,
1488 first_sets,
1489 &seq_rep.tts,
1490 my_suffix,
1491 )?;
1492 if next.maybe_empty {
1493 last.add_all(&next);
1494 } else {
1495 last = next;
1496 }
1497
1498 continue 'each_token;
1501 }
1502 }
1503
1504 for tt in &last.tokens {
1509 if let &TokenTree::MetaVarDecl { span, name, kind } = tt.get() {
1510 for next_token in &suffix_first.tokens {
1511 let next_token = next_token.get();
1512
1513 if is_defined_in_current_crate(node_id)
1520 && #[allow(non_exhaustive_omitted_patterns)] match kind {
NonterminalKind::Pat(PatParam { inferred: true }) => true,
_ => false,
}matches!(kind, NonterminalKind::Pat(PatParam { inferred: true }))
1521 && #[allow(non_exhaustive_omitted_patterns)] match next_token {
TokenTree::Token(token) if *token == token::Or => true,
_ => false,
}matches!(
1522 next_token,
1523 TokenTree::Token(token) if *token == token::Or
1524 )
1525 {
1526 let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl {
1528 span,
1529 name,
1530 kind: NonterminalKind::Pat(PatParam { inferred: false }),
1531 });
1532 sess.psess.buffer_lint(
1533 RUST_2021_INCOMPATIBLE_OR_PATTERNS,
1534 span,
1535 ast::CRATE_NODE_ID,
1536 errors::OrPatternsBackCompat { span, suggestion },
1537 );
1538 }
1539 match is_in_follow(next_token, kind) {
1540 IsInFollow::Yes => {}
1541 IsInFollow::No(possible) => {
1542 let may_be = if last.tokens.len() == 1 && suffix_first.tokens.len() == 1
1543 {
1544 "is"
1545 } else {
1546 "may be"
1547 };
1548
1549 let sp = next_token.span();
1550 let mut err = sess.dcx().struct_span_err(
1551 sp,
1552 ::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!(
1553 "`${name}:{frag}` {may_be} followed by `{next}`, which \
1554 is not allowed for `{frag}` fragments",
1555 name = name,
1556 frag = kind,
1557 next = quoted_tt_to_string(next_token),
1558 may_be = may_be
1559 ),
1560 );
1561 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"));
1562
1563 if kind == NonterminalKind::Pat(PatWithOr)
1564 && sess.psess.edition.at_least_rust_2021()
1565 && next_token.is_token(&token::Or)
1566 {
1567 let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl {
1568 span,
1569 name,
1570 kind: NonterminalKind::Pat(PatParam { inferred: false }),
1571 });
1572 err.span_suggestion(
1573 span,
1574 "try a `pat_param` fragment specifier instead",
1575 suggestion,
1576 Applicability::MaybeIncorrect,
1577 );
1578 }
1579
1580 let msg = "allowed there are: ";
1581 match possible {
1582 &[] => {}
1583 &[t] => {
1584 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("only {0} is allowed after `{1}` fragments",
t, kind))
})format!(
1585 "only {t} is allowed after `{kind}` fragments",
1586 ));
1587 }
1588 ts => {
1589 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!(
1590 "{}{} or {}",
1591 msg,
1592 ts[..ts.len() - 1].to_vec().join(", "),
1593 ts[ts.len() - 1],
1594 ));
1595 }
1596 }
1597 errored = Err(err.emit());
1598 }
1599 }
1600 }
1601 }
1602 }
1603 }
1604 errored?;
1605 Ok(last)
1606}
1607
1608fn token_can_be_followed_by_any(tok: &mbe::TokenTree) -> bool {
1609 if let mbe::TokenTree::MetaVarDecl { kind, .. } = *tok {
1610 frag_can_be_followed_by_any(kind)
1611 } else {
1612 true
1614 }
1615}
1616
1617fn frag_can_be_followed_by_any(kind: NonterminalKind) -> bool {
1626 #[allow(non_exhaustive_omitted_patterns)] match kind {
NonterminalKind::Item | NonterminalKind::Block | NonterminalKind::Ident |
NonterminalKind::Literal | NonterminalKind::Meta |
NonterminalKind::Lifetime | NonterminalKind::TT => true,
_ => false,
}matches!(
1627 kind,
1628 NonterminalKind::Item | NonterminalKind::Block | NonterminalKind::Ident | NonterminalKind::Literal | NonterminalKind::Meta | NonterminalKind::Lifetime | NonterminalKind::TT )
1636}
1637
1638enum IsInFollow {
1639 Yes,
1640 No(&'static [&'static str]),
1641}
1642
1643fn is_in_follow(tok: &mbe::TokenTree, kind: NonterminalKind) -> IsInFollow {
1652 use mbe::TokenTree;
1653
1654 if let TokenTree::Token(Token { kind, .. }) = tok
1655 && kind.close_delim().is_some()
1656 {
1657 IsInFollow::Yes
1660 } else {
1661 match kind {
1662 NonterminalKind::Item => {
1663 IsInFollow::Yes
1666 }
1667 NonterminalKind::Block => {
1668 IsInFollow::Yes
1671 }
1672 NonterminalKind::Stmt | NonterminalKind::Expr(_) => {
1673 const TOKENS: &[&str] = &["`=>`", "`,`", "`;`"];
1674 match tok {
1675 TokenTree::Token(token) => match token.kind {
1676 FatArrow | Comma | Semi => IsInFollow::Yes,
1677 _ => IsInFollow::No(TOKENS),
1678 },
1679 _ => IsInFollow::No(TOKENS),
1680 }
1681 }
1682 NonterminalKind::Pat(PatParam { .. }) => {
1683 const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`|`", "`if`", "`if let`", "`in`"];
1684 match tok {
1685 TokenTree::Token(token) => match token.kind {
1686 FatArrow | Comma | Eq | Or => IsInFollow::Yes,
1687 Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1688 IsInFollow::Yes
1689 }
1690 _ => IsInFollow::No(TOKENS),
1691 },
1692 TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } => IsInFollow::Yes,
1693 _ => IsInFollow::No(TOKENS),
1694 }
1695 }
1696 NonterminalKind::Pat(PatWithOr) => {
1697 const TOKENS: &[&str] = &["`=>`", "`,`", "`=`", "`if`", "`if let`", "`in`"];
1698 match tok {
1699 TokenTree::Token(token) => match token.kind {
1700 FatArrow | Comma | Eq => IsInFollow::Yes,
1701 Ident(name, IdentIsRaw::No) if name == kw::If || name == kw::In => {
1702 IsInFollow::Yes
1703 }
1704 _ => IsInFollow::No(TOKENS),
1705 },
1706 TokenTree::MetaVarDecl { kind: NonterminalKind::Guard, .. } => IsInFollow::Yes,
1707 _ => IsInFollow::No(TOKENS),
1708 }
1709 }
1710 NonterminalKind::Guard => {
1711 const TOKENS: &[&str] = &["`=>`", "`,`", "`{`"];
1712 match tok {
1713 TokenTree::Token(token) => match token.kind {
1714 FatArrow | Comma | OpenBrace => IsInFollow::Yes,
1715 _ => IsInFollow::No(TOKENS),
1716 },
1717 _ => IsInFollow::No(TOKENS),
1718 }
1719 }
1720 NonterminalKind::Path | NonterminalKind::Ty => {
1721 const TOKENS: &[&str] = &[
1722 "`{`", "`[`", "`=>`", "`,`", "`>`", "`=`", "`:`", "`;`", "`|`", "`as`",
1723 "`where`",
1724 ];
1725 match tok {
1726 TokenTree::Token(token) => match token.kind {
1727 OpenBrace | OpenBracket | Comma | FatArrow | Colon | Eq | Gt | Shr
1728 | Semi | Or => IsInFollow::Yes,
1729 Ident(name, IdentIsRaw::No) if name == kw::As || name == kw::Where => {
1730 IsInFollow::Yes
1731 }
1732 _ => IsInFollow::No(TOKENS),
1733 },
1734 TokenTree::MetaVarDecl { kind: NonterminalKind::Block, .. } => IsInFollow::Yes,
1735 _ => IsInFollow::No(TOKENS),
1736 }
1737 }
1738 NonterminalKind::Ident | NonterminalKind::Lifetime => {
1739 IsInFollow::Yes
1741 }
1742 NonterminalKind::Literal => {
1743 IsInFollow::Yes
1745 }
1746 NonterminalKind::Meta | NonterminalKind::TT => {
1747 IsInFollow::Yes
1750 }
1751 NonterminalKind::Vis => {
1752 const TOKENS: &[&str] = &["`,`", "an ident", "a type"];
1754 match tok {
1755 TokenTree::Token(token) => match token.kind {
1756 Comma => IsInFollow::Yes,
1757 Ident(_, IdentIsRaw::Yes) => IsInFollow::Yes,
1758 Ident(name, _) if name != kw::Priv => IsInFollow::Yes,
1759 _ => {
1760 if token.can_begin_type() {
1761 IsInFollow::Yes
1762 } else {
1763 IsInFollow::No(TOKENS)
1764 }
1765 }
1766 },
1767 TokenTree::MetaVarDecl {
1768 kind: NonterminalKind::Ident | NonterminalKind::Ty | NonterminalKind::Path,
1769 ..
1770 } => IsInFollow::Yes,
1771 _ => IsInFollow::No(TOKENS),
1772 }
1773 }
1774 }
1775 }
1776}
1777
1778fn quoted_tt_to_string(tt: &mbe::TokenTree) -> String {
1779 match tt {
1780 mbe::TokenTree::Token(token) => pprust::token_to_string(token).into(),
1781 mbe::TokenTree::MetaVar(_, name) => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("${0}", name))
})format!("${name}"),
1782 mbe::TokenTree::MetaVarDecl { name, kind, .. } => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("${0}:{1}", name, kind))
})format!("${name}:{kind}"),
1783 _ => {
::core::panicking::panic_display(&"unexpected mbe::TokenTree::{Sequence or Delimited} \
in follow set checker");
}panic!(
1784 "{}",
1785 "unexpected mbe::TokenTree::{Sequence or Delimited} \
1786 in follow set checker"
1787 ),
1788 }
1789}
1790
1791fn is_defined_in_current_crate(node_id: NodeId) -> bool {
1792 node_id != DUMMY_NODE_ID
1795}
1796
1797pub(super) fn parser_from_cx(
1798 psess: &ParseSess,
1799 mut tts: TokenStream,
1800 recovery: Recovery,
1801) -> Parser<'_> {
1802 tts.desugar_doc_comments();
1803 Parser::new(psess, tts, rustc_parse::MACRO_ARGUMENTS).recovery(recovery)
1804}