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