1use std::any::Any;
2use std::default::Default;
3use std::iter;
4use std::path::Component::Prefix;
5use std::path::PathBuf;
6use std::rc::Rc;
7use std::sync::Arc;
8
9use rustc_ast::attr::MarkedAttrs;
10use rustc_ast::tokenstream::TokenStream;
11use rustc_ast::visit::{AssocCtxt, Visitor};
12use rustc_ast::{self as ast, AttrVec, Attribute, HasAttrs, Item, NodeId, PatKind, Safety};
13use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
14use rustc_data_structures::sync;
15use rustc_errors::{BufferedEarlyLint, DiagCtxtHandle, ErrorGuaranteed, PResult};
16use rustc_feature::Features;
17use rustc_hir as hir;
18use rustc_hir::attrs::{CfgEntry, CollapseMacroDebuginfo, Deprecation};
19use rustc_hir::def::MacroKinds;
20use rustc_hir::limit::Limit;
21use rustc_hir::{Stability, find_attr};
22use rustc_lint_defs::RegisteredTools;
23use rustc_parse::MACRO_ARGUMENTS;
24use rustc_parse::parser::Parser;
25use rustc_session::Session;
26use rustc_session::parse::ParseSess;
27use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
28use rustc_span::edition::Edition;
29use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId, MacroKind};
30use rustc_span::source_map::SourceMap;
31use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw};
32use smallvec::{SmallVec, smallvec};
33use thin_vec::ThinVec;
34
35use crate::errors;
36use crate::expand::{self, AstFragment, Invocation};
37use crate::mbe::macro_rules::ParserAnyMacro;
38use crate::module::DirOwnership;
39use crate::stats::MacroStat;
40
41#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Annotatable {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Annotatable::Item(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Item",
&__self_0),
Annotatable::AssocItem(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"AssocItem", __self_0, &__self_1),
Annotatable::ForeignItem(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ForeignItem", &__self_0),
Annotatable::Stmt(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Stmt",
&__self_0),
Annotatable::Expr(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Expr",
&__self_0),
Annotatable::Arm(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Arm",
&__self_0),
Annotatable::ExprField(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"ExprField", &__self_0),
Annotatable::PatField(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"PatField", &__self_0),
Annotatable::GenericParam(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"GenericParam", &__self_0),
Annotatable::Param(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Param",
&__self_0),
Annotatable::FieldDef(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"FieldDef", &__self_0),
Annotatable::Variant(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Variant", &__self_0),
Annotatable::WherePredicate(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WherePredicate", &__self_0),
Annotatable::Crate(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Crate",
&__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for Annotatable {
#[inline]
fn clone(&self) -> Annotatable {
match self {
Annotatable::Item(__self_0) =>
Annotatable::Item(::core::clone::Clone::clone(__self_0)),
Annotatable::AssocItem(__self_0, __self_1) =>
Annotatable::AssocItem(::core::clone::Clone::clone(__self_0),
::core::clone::Clone::clone(__self_1)),
Annotatable::ForeignItem(__self_0) =>
Annotatable::ForeignItem(::core::clone::Clone::clone(__self_0)),
Annotatable::Stmt(__self_0) =>
Annotatable::Stmt(::core::clone::Clone::clone(__self_0)),
Annotatable::Expr(__self_0) =>
Annotatable::Expr(::core::clone::Clone::clone(__self_0)),
Annotatable::Arm(__self_0) =>
Annotatable::Arm(::core::clone::Clone::clone(__self_0)),
Annotatable::ExprField(__self_0) =>
Annotatable::ExprField(::core::clone::Clone::clone(__self_0)),
Annotatable::PatField(__self_0) =>
Annotatable::PatField(::core::clone::Clone::clone(__self_0)),
Annotatable::GenericParam(__self_0) =>
Annotatable::GenericParam(::core::clone::Clone::clone(__self_0)),
Annotatable::Param(__self_0) =>
Annotatable::Param(::core::clone::Clone::clone(__self_0)),
Annotatable::FieldDef(__self_0) =>
Annotatable::FieldDef(::core::clone::Clone::clone(__self_0)),
Annotatable::Variant(__self_0) =>
Annotatable::Variant(::core::clone::Clone::clone(__self_0)),
Annotatable::WherePredicate(__self_0) =>
Annotatable::WherePredicate(::core::clone::Clone::clone(__self_0)),
Annotatable::Crate(__self_0) =>
Annotatable::Crate(::core::clone::Clone::clone(__self_0)),
}
}
}Clone)]
45pub enum Annotatable {
46 Item(Box<ast::Item>),
47 AssocItem(Box<ast::AssocItem>, AssocCtxt),
48 ForeignItem(Box<ast::ForeignItem>),
49 Stmt(Box<ast::Stmt>),
50 Expr(Box<ast::Expr>),
51 Arm(ast::Arm),
52 ExprField(ast::ExprField),
53 PatField(ast::PatField),
54 GenericParam(ast::GenericParam),
55 Param(ast::Param),
56 FieldDef(ast::FieldDef),
57 Variant(ast::Variant),
58 WherePredicate(ast::WherePredicate),
59 Crate(ast::Crate),
60}
61
62impl Annotatable {
63 pub fn span(&self) -> Span {
64 match self {
65 Annotatable::Item(item) => item.span,
66 Annotatable::AssocItem(assoc_item, _) => assoc_item.span,
67 Annotatable::ForeignItem(foreign_item) => foreign_item.span,
68 Annotatable::Stmt(stmt) => stmt.span,
69 Annotatable::Expr(expr) => expr.span,
70 Annotatable::Arm(arm) => arm.span,
71 Annotatable::ExprField(field) => field.span,
72 Annotatable::PatField(fp) => fp.pat.span,
73 Annotatable::GenericParam(gp) => gp.ident.span,
74 Annotatable::Param(p) => p.span,
75 Annotatable::FieldDef(sf) => sf.span,
76 Annotatable::Variant(v) => v.span,
77 Annotatable::WherePredicate(wp) => wp.span,
78 Annotatable::Crate(c) => c.spans.inner_span,
79 }
80 }
81
82 pub fn visit_attrs(&mut self, f: impl FnOnce(&mut AttrVec)) {
83 match self {
84 Annotatable::Item(item) => item.visit_attrs(f),
85 Annotatable::AssocItem(assoc_item, _) => assoc_item.visit_attrs(f),
86 Annotatable::ForeignItem(foreign_item) => foreign_item.visit_attrs(f),
87 Annotatable::Stmt(stmt) => stmt.visit_attrs(f),
88 Annotatable::Expr(expr) => expr.visit_attrs(f),
89 Annotatable::Arm(arm) => arm.visit_attrs(f),
90 Annotatable::ExprField(field) => field.visit_attrs(f),
91 Annotatable::PatField(fp) => fp.visit_attrs(f),
92 Annotatable::GenericParam(gp) => gp.visit_attrs(f),
93 Annotatable::Param(p) => p.visit_attrs(f),
94 Annotatable::FieldDef(sf) => sf.visit_attrs(f),
95 Annotatable::Variant(v) => v.visit_attrs(f),
96 Annotatable::WherePredicate(wp) => wp.visit_attrs(f),
97 Annotatable::Crate(c) => c.visit_attrs(f),
98 }
99 }
100
101 pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) -> V::Result {
102 match self {
103 Annotatable::Item(item) => visitor.visit_item(item),
104 Annotatable::AssocItem(item, ctxt) => visitor.visit_assoc_item(item, *ctxt),
105 Annotatable::ForeignItem(foreign_item) => visitor.visit_foreign_item(foreign_item),
106 Annotatable::Stmt(stmt) => visitor.visit_stmt(stmt),
107 Annotatable::Expr(expr) => visitor.visit_expr(expr),
108 Annotatable::Arm(arm) => visitor.visit_arm(arm),
109 Annotatable::ExprField(field) => visitor.visit_expr_field(field),
110 Annotatable::PatField(fp) => visitor.visit_pat_field(fp),
111 Annotatable::GenericParam(gp) => visitor.visit_generic_param(gp),
112 Annotatable::Param(p) => visitor.visit_param(p),
113 Annotatable::FieldDef(sf) => visitor.visit_field_def(sf),
114 Annotatable::Variant(v) => visitor.visit_variant(v),
115 Annotatable::WherePredicate(wp) => visitor.visit_where_predicate(wp),
116 Annotatable::Crate(c) => visitor.visit_crate(c),
117 }
118 }
119
120 pub fn to_tokens(&self) -> TokenStream {
121 match self {
122 Annotatable::Item(node) => TokenStream::from_ast(node),
123 Annotatable::AssocItem(node, _) => TokenStream::from_ast(node),
124 Annotatable::ForeignItem(node) => TokenStream::from_ast(node),
125 Annotatable::Stmt(node) => {
126 if !!#[allow(non_exhaustive_omitted_patterns)] match node.kind {
ast::StmtKind::Empty => true,
_ => false,
} {
::core::panicking::panic("assertion failed: !matches!(node.kind, ast::StmtKind::Empty)")
};assert!(!matches!(node.kind, ast::StmtKind::Empty));
127 TokenStream::from_ast(node)
128 }
129 Annotatable::Expr(node) => TokenStream::from_ast(node),
130 Annotatable::Arm(..)
131 | Annotatable::ExprField(..)
132 | Annotatable::PatField(..)
133 | Annotatable::GenericParam(..)
134 | Annotatable::Param(..)
135 | Annotatable::FieldDef(..)
136 | Annotatable::Variant(..)
137 | Annotatable::WherePredicate(..)
138 | Annotatable::Crate(..) => { ::core::panicking::panic_fmt(format_args!("unexpected annotatable")); }panic!("unexpected annotatable"),
139 }
140 }
141
142 pub fn expect_item(self) -> Box<ast::Item> {
143 match self {
144 Annotatable::Item(i) => i,
145 _ => { ::core::panicking::panic_fmt(format_args!("expected Item")); }panic!("expected Item"),
146 }
147 }
148
149 pub fn expect_trait_item(self) -> Box<ast::AssocItem> {
150 match self {
151 Annotatable::AssocItem(i, AssocCtxt::Trait) => i,
152 _ => { ::core::panicking::panic_fmt(format_args!("expected trait item")); }panic!("expected trait item"),
153 }
154 }
155
156 pub fn expect_impl_item(self) -> Box<ast::AssocItem> {
157 match self {
158 Annotatable::AssocItem(i, AssocCtxt::Impl { .. }) => i,
159 _ => { ::core::panicking::panic_fmt(format_args!("expected impl item")); }panic!("expected impl item"),
160 }
161 }
162
163 pub fn expect_foreign_item(self) -> Box<ast::ForeignItem> {
164 match self {
165 Annotatable::ForeignItem(i) => i,
166 _ => { ::core::panicking::panic_fmt(format_args!("expected foreign item")); }panic!("expected foreign item"),
167 }
168 }
169
170 pub fn expect_stmt(self) -> ast::Stmt {
171 match self {
172 Annotatable::Stmt(stmt) => *stmt,
173 _ => { ::core::panicking::panic_fmt(format_args!("expected statement")); }panic!("expected statement"),
174 }
175 }
176
177 pub fn expect_expr(self) -> Box<ast::Expr> {
178 match self {
179 Annotatable::Expr(expr) => expr,
180 _ => { ::core::panicking::panic_fmt(format_args!("expected expression")); }panic!("expected expression"),
181 }
182 }
183
184 pub fn expect_arm(self) -> ast::Arm {
185 match self {
186 Annotatable::Arm(arm) => arm,
187 _ => { ::core::panicking::panic_fmt(format_args!("expected match arm")); }panic!("expected match arm"),
188 }
189 }
190
191 pub fn expect_expr_field(self) -> ast::ExprField {
192 match self {
193 Annotatable::ExprField(field) => field,
194 _ => { ::core::panicking::panic_fmt(format_args!("expected field")); }panic!("expected field"),
195 }
196 }
197
198 pub fn expect_pat_field(self) -> ast::PatField {
199 match self {
200 Annotatable::PatField(fp) => fp,
201 _ => { ::core::panicking::panic_fmt(format_args!("expected field pattern")); }panic!("expected field pattern"),
202 }
203 }
204
205 pub fn expect_generic_param(self) -> ast::GenericParam {
206 match self {
207 Annotatable::GenericParam(gp) => gp,
208 _ => { ::core::panicking::panic_fmt(format_args!("expected generic parameter")); }panic!("expected generic parameter"),
209 }
210 }
211
212 pub fn expect_param(self) -> ast::Param {
213 match self {
214 Annotatable::Param(param) => param,
215 _ => { ::core::panicking::panic_fmt(format_args!("expected parameter")); }panic!("expected parameter"),
216 }
217 }
218
219 pub fn expect_field_def(self) -> ast::FieldDef {
220 match self {
221 Annotatable::FieldDef(sf) => sf,
222 _ => { ::core::panicking::panic_fmt(format_args!("expected struct field")); }panic!("expected struct field"),
223 }
224 }
225
226 pub fn expect_variant(self) -> ast::Variant {
227 match self {
228 Annotatable::Variant(v) => v,
229 _ => { ::core::panicking::panic_fmt(format_args!("expected variant")); }panic!("expected variant"),
230 }
231 }
232
233 pub fn expect_where_predicate(self) -> ast::WherePredicate {
234 match self {
235 Annotatable::WherePredicate(wp) => wp,
236 _ => { ::core::panicking::panic_fmt(format_args!("expected where predicate")); }panic!("expected where predicate"),
237 }
238 }
239
240 pub fn expect_crate(self) -> ast::Crate {
241 match self {
242 Annotatable::Crate(krate) => krate,
243 _ => { ::core::panicking::panic_fmt(format_args!("expected krate")); }panic!("expected krate"),
244 }
245 }
246}
247
248pub enum ExpandResult<T, U> {
251 Ready(T),
253 Retry(U),
255}
256
257impl<T, U> ExpandResult<T, U> {
258 pub fn map<E, F: FnOnce(T) -> E>(self, f: F) -> ExpandResult<E, U> {
259 match self {
260 ExpandResult::Ready(t) => ExpandResult::Ready(f(t)),
261 ExpandResult::Retry(u) => ExpandResult::Retry(u),
262 }
263 }
264}
265
266impl<'cx> MacroExpanderResult<'cx> {
267 pub fn from_tts(
271 cx: &'cx mut ExtCtxt<'_>,
272 tts: TokenStream,
273 site_span: Span,
274 arm_span: Span,
275 macro_ident: Ident,
276 ) -> Self {
277 let is_local = true;
279
280 let parser =
281 ParserAnyMacro::from_tts(cx, tts, site_span, arm_span, is_local, macro_ident, &[], &[]);
282 ExpandResult::Ready(Box::new(parser))
283 }
284}
285
286pub trait MultiItemModifier {
287 fn expand(
289 &self,
290 ecx: &mut ExtCtxt<'_>,
291 span: Span,
292 meta_item: &ast::MetaItem,
293 item: Annotatable,
294 is_derive_const: bool,
295 ) -> ExpandResult<Vec<Annotatable>, Annotatable>;
296}
297
298impl<F> MultiItemModifier for F
299where
300 F: Fn(&mut ExtCtxt<'_>, Span, &ast::MetaItem, Annotatable) -> Vec<Annotatable>,
301{
302 fn expand(
303 &self,
304 ecx: &mut ExtCtxt<'_>,
305 span: Span,
306 meta_item: &ast::MetaItem,
307 item: Annotatable,
308 _is_derive_const: bool,
309 ) -> ExpandResult<Vec<Annotatable>, Annotatable> {
310 ExpandResult::Ready(self(ecx, span, meta_item, item))
311 }
312}
313
314pub trait BangProcMacro {
315 fn expand<'cx>(
316 &self,
317 ecx: &'cx mut ExtCtxt<'_>,
318 span: Span,
319 ts: TokenStream,
320 ) -> Result<TokenStream, ErrorGuaranteed>;
321}
322
323impl<F> BangProcMacro for F
324where
325 F: Fn(&mut ExtCtxt<'_>, Span, TokenStream) -> Result<TokenStream, ErrorGuaranteed>,
326{
327 fn expand<'cx>(
328 &self,
329 ecx: &'cx mut ExtCtxt<'_>,
330 span: Span,
331 ts: TokenStream,
332 ) -> Result<TokenStream, ErrorGuaranteed> {
333 self(ecx, span, ts)
335 }
336}
337
338pub trait AttrProcMacro {
339 fn expand<'cx>(
340 &self,
341 ecx: &'cx mut ExtCtxt<'_>,
342 span: Span,
343 annotation: TokenStream,
344 annotated: TokenStream,
345 ) -> Result<TokenStream, ErrorGuaranteed>;
346
347 fn expand_with_safety<'cx>(
349 &self,
350 ecx: &'cx mut ExtCtxt<'_>,
351 safety: Safety,
352 span: Span,
353 annotation: TokenStream,
354 annotated: TokenStream,
355 ) -> Result<TokenStream, ErrorGuaranteed> {
356 if let Safety::Unsafe(span) = safety {
357 ecx.dcx().span_err(span, "unnecessary `unsafe` on safe attribute");
358 }
359 self.expand(ecx, span, annotation, annotated)
360 }
361}
362
363impl<F> AttrProcMacro for F
364where
365 F: Fn(TokenStream, TokenStream) -> TokenStream,
366{
367 fn expand<'cx>(
368 &self,
369 _ecx: &'cx mut ExtCtxt<'_>,
370 _span: Span,
371 annotation: TokenStream,
372 annotated: TokenStream,
373 ) -> Result<TokenStream, ErrorGuaranteed> {
374 Ok(self(annotation, annotated))
376 }
377}
378
379pub trait TTMacroExpander: Any {
381 fn expand<'cx, 'a: 'cx>(
382 &'a self,
383 ecx: &'cx mut ExtCtxt<'_>,
384 span: Span,
385 input: TokenStream,
386 ) -> MacroExpanderResult<'cx>;
387}
388
389pub type MacroExpanderResult<'cx> = ExpandResult<Box<dyn MacResult + 'cx>, ()>;
390
391pub type MacroExpanderFn =
392 for<'cx> fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> MacroExpanderResult<'cx>;
393
394impl<F: 'static> TTMacroExpander for F
395where
396 F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> MacroExpanderResult<'cx>,
397{
398 fn expand<'cx, 'a: 'cx>(
399 &'a self,
400 ecx: &'cx mut ExtCtxt<'_>,
401 span: Span,
402 input: TokenStream,
403 ) -> MacroExpanderResult<'cx> {
404 self(ecx, span, input)
405 }
406}
407
408pub trait GlobDelegationExpander {
409 fn expand(&self, ecx: &mut ExtCtxt<'_>) -> ExpandResult<Vec<(Ident, Option<Ident>)>, ()>;
410}
411
412fn make_stmts_default(expr: Option<Box<ast::Expr>>) -> Option<SmallVec<[ast::Stmt; 1]>> {
413 expr.map(|e| {
414 {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(ast::Stmt {
id: ast::DUMMY_NODE_ID,
span: e.span,
kind: ast::StmtKind::Expr(e),
});
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[ast::Stmt {
id: ast::DUMMY_NODE_ID,
span: e.span,
kind: ast::StmtKind::Expr(e),
}])))
}
}smallvec![ast::Stmt { id: ast::DUMMY_NODE_ID, span: e.span, kind: ast::StmtKind::Expr(e) }]
415 })
416}
417
418pub trait MacResult {
421 fn make_expr(self: Box<Self>) -> Option<Box<ast::Expr>> {
423 None
424 }
425
426 fn make_method_receiver_expr(self: Box<Self>) -> Option<Box<ast::Expr>> {
427 self.make_expr()
428 }
429
430 fn make_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::Item>; 1]>> {
432 None
433 }
434
435 fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
437 None
438 }
439
440 fn make_trait_impl_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
442 None
443 }
444
445 fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
447 None
448 }
449
450 fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::ForeignItem>; 1]>> {
452 None
453 }
454
455 fn make_pat(self: Box<Self>) -> Option<Box<ast::Pat>> {
457 None
458 }
459
460 fn make_stmts(self: Box<Self>) -> Option<SmallVec<[ast::Stmt; 1]>> {
465 make_stmts_default(self.make_expr())
466 }
467
468 fn make_ty(self: Box<Self>) -> Option<Box<ast::Ty>> {
469 None
470 }
471
472 fn make_arms(self: Box<Self>) -> Option<SmallVec<[ast::Arm; 1]>> {
473 None
474 }
475
476 fn make_expr_fields(self: Box<Self>) -> Option<SmallVec<[ast::ExprField; 1]>> {
477 None
478 }
479
480 fn make_pat_fields(self: Box<Self>) -> Option<SmallVec<[ast::PatField; 1]>> {
481 None
482 }
483
484 fn make_generic_params(self: Box<Self>) -> Option<SmallVec<[ast::GenericParam; 1]>> {
485 None
486 }
487
488 fn make_params(self: Box<Self>) -> Option<SmallVec<[ast::Param; 1]>> {
489 None
490 }
491
492 fn make_field_defs(self: Box<Self>) -> Option<SmallVec<[ast::FieldDef; 1]>> {
493 None
494 }
495
496 fn make_variants(self: Box<Self>) -> Option<SmallVec<[ast::Variant; 1]>> {
497 None
498 }
499
500 fn make_where_predicates(self: Box<Self>) -> Option<SmallVec<[ast::WherePredicate; 1]>> {
501 None
502 }
503
504 fn make_crate(self: Box<Self>) -> Option<ast::Crate> {
505 ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
507 }
508}
509
510macro_rules! make_MacEager {
511 ( $( $fld:ident: $t:ty, )* ) => {
512 #[derive(Default)]
515 pub struct MacEager {
516 $(
517 pub $fld: Option<$t>,
518 )*
519 }
520
521 impl MacEager {
522 $(
523 pub fn $fld(v: $t) -> Box<dyn MacResult> {
524 Box::new(MacEager {
525 $fld: Some(v),
526 ..Default::default()
527 })
528 }
529 )*
530 }
531 }
532}
533
534pub struct MacEager {
pub expr: Option<Box<ast::Expr>>,
pub pat: Option<Box<ast::Pat>>,
pub items: Option<SmallVec<[Box<ast::Item>; 1]>>,
pub impl_items: Option<SmallVec<[Box<ast::AssocItem>; 1]>>,
pub trait_items: Option<SmallVec<[Box<ast::AssocItem>; 1]>>,
pub foreign_items: Option<SmallVec<[Box<ast::ForeignItem>; 1]>>,
pub stmts: Option<SmallVec<[ast::Stmt; 1]>>,
pub ty: Option<Box<ast::Ty>>,
}
#[automatically_derived]
impl ::core::default::Default for MacEager {
#[inline]
fn default() -> MacEager {
MacEager {
expr: ::core::default::Default::default(),
pat: ::core::default::Default::default(),
items: ::core::default::Default::default(),
impl_items: ::core::default::Default::default(),
trait_items: ::core::default::Default::default(),
foreign_items: ::core::default::Default::default(),
stmts: ::core::default::Default::default(),
ty: ::core::default::Default::default(),
}
}
}
impl MacEager {
pub fn expr(v: Box<ast::Expr>) -> Box<dyn MacResult> {
Box::new(MacEager { expr: Some(v), ..Default::default() })
}
pub fn pat(v: Box<ast::Pat>) -> Box<dyn MacResult> {
Box::new(MacEager { pat: Some(v), ..Default::default() })
}
pub fn items(v: SmallVec<[Box<ast::Item>; 1]>) -> Box<dyn MacResult> {
Box::new(MacEager { items: Some(v), ..Default::default() })
}
pub fn impl_items(v: SmallVec<[Box<ast::AssocItem>; 1]>)
-> Box<dyn MacResult> {
Box::new(MacEager { impl_items: Some(v), ..Default::default() })
}
pub fn trait_items(v: SmallVec<[Box<ast::AssocItem>; 1]>)
-> Box<dyn MacResult> {
Box::new(MacEager { trait_items: Some(v), ..Default::default() })
}
pub fn foreign_items(v: SmallVec<[Box<ast::ForeignItem>; 1]>)
-> Box<dyn MacResult> {
Box::new(MacEager { foreign_items: Some(v), ..Default::default() })
}
pub fn stmts(v: SmallVec<[ast::Stmt; 1]>) -> Box<dyn MacResult> {
Box::new(MacEager { stmts: Some(v), ..Default::default() })
}
pub fn ty(v: Box<ast::Ty>) -> Box<dyn MacResult> {
Box::new(MacEager { ty: Some(v), ..Default::default() })
}
}make_MacEager! {
535 expr: Box<ast::Expr>,
536 pat: Box<ast::Pat>,
537 items: SmallVec<[Box<ast::Item>; 1]>,
538 impl_items: SmallVec<[Box<ast::AssocItem>; 1]>,
539 trait_items: SmallVec<[Box<ast::AssocItem>; 1]>,
540 foreign_items: SmallVec<[Box<ast::ForeignItem>; 1]>,
541 stmts: SmallVec<[ast::Stmt; 1]>,
542 ty: Box<ast::Ty>,
543}
544
545impl MacResult for MacEager {
546 fn make_expr(self: Box<Self>) -> Option<Box<ast::Expr>> {
547 self.expr
548 }
549
550 fn make_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::Item>; 1]>> {
551 self.items
552 }
553
554 fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
555 self.impl_items
556 }
557
558 fn make_trait_impl_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
559 self.impl_items
560 }
561
562 fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
563 self.trait_items
564 }
565
566 fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::ForeignItem>; 1]>> {
567 self.foreign_items
568 }
569
570 fn make_stmts(self: Box<Self>) -> Option<SmallVec<[ast::Stmt; 1]>> {
571 if self.stmts.as_ref().is_none_or(|s| s.is_empty()) {
572 make_stmts_default(self.make_expr())
573 } else {
574 self.stmts
575 }
576 }
577
578 fn make_pat(self: Box<Self>) -> Option<Box<ast::Pat>> {
579 if let Some(p) = self.pat {
580 return Some(p);
581 }
582 if let Some(e) = self.expr {
583 if #[allow(non_exhaustive_omitted_patterns)] match e.kind {
ast::ExprKind::Lit(_) | ast::ExprKind::IncludedBytes(_) => true,
_ => false,
}matches!(e.kind, ast::ExprKind::Lit(_) | ast::ExprKind::IncludedBytes(_)) {
584 return Some(Box::new(ast::Pat {
585 id: ast::DUMMY_NODE_ID,
586 span: e.span,
587 kind: PatKind::Expr(e),
588 tokens: None,
589 }));
590 }
591 }
592 None
593 }
594
595 fn make_ty(self: Box<Self>) -> Option<Box<ast::Ty>> {
596 self.ty
597 }
598}
599
600#[derive(#[automatically_derived]
impl ::core::marker::Copy for DummyResult { }Copy, #[automatically_derived]
impl ::core::clone::Clone for DummyResult {
#[inline]
fn clone(&self) -> DummyResult {
let _: ::core::clone::AssertParamIsClone<Option<ErrorGuaranteed>>;
let _: ::core::clone::AssertParamIsClone<Span>;
*self
}
}Clone)]
603pub struct DummyResult {
604 guar: Option<ErrorGuaranteed>,
605 span: Span,
606}
607
608impl DummyResult {
609 pub fn any(span: Span, guar: ErrorGuaranteed) -> Box<dyn MacResult + 'static> {
614 Box::new(DummyResult { guar: Some(guar), span })
615 }
616
617 pub fn any_valid(span: Span) -> Box<dyn MacResult + 'static> {
619 Box::new(DummyResult { guar: None, span })
620 }
621
622 pub fn raw_expr(sp: Span, guar: Option<ErrorGuaranteed>) -> Box<ast::Expr> {
624 Box::new(ast::Expr {
625 id: ast::DUMMY_NODE_ID,
626 kind: if let Some(guar) = guar {
627 ast::ExprKind::Err(guar)
628 } else {
629 ast::ExprKind::Tup(ThinVec::new())
630 },
631 span: sp,
632 attrs: ast::AttrVec::new(),
633 tokens: None,
634 })
635 }
636}
637
638impl MacResult for DummyResult {
639 fn make_expr(self: Box<DummyResult>) -> Option<Box<ast::Expr>> {
640 Some(DummyResult::raw_expr(self.span, self.guar))
641 }
642
643 fn make_pat(self: Box<DummyResult>) -> Option<Box<ast::Pat>> {
644 Some(Box::new(ast::Pat {
645 id: ast::DUMMY_NODE_ID,
646 kind: PatKind::Wild,
647 span: self.span,
648 tokens: None,
649 }))
650 }
651
652 fn make_items(self: Box<DummyResult>) -> Option<SmallVec<[Box<ast::Item>; 1]>> {
653 Some(SmallVec::new())
654 }
655
656 fn make_impl_items(self: Box<DummyResult>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
657 Some(SmallVec::new())
658 }
659
660 fn make_trait_impl_items(self: Box<DummyResult>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
661 Some(SmallVec::new())
662 }
663
664 fn make_trait_items(self: Box<DummyResult>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
665 Some(SmallVec::new())
666 }
667
668 fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::ForeignItem>; 1]>> {
669 Some(SmallVec::new())
670 }
671
672 fn make_stmts(self: Box<DummyResult>) -> Option<SmallVec<[ast::Stmt; 1]>> {
673 Some({
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(ast::Stmt {
id: ast::DUMMY_NODE_ID,
kind: ast::StmtKind::Expr(DummyResult::raw_expr(self.span,
self.guar)),
span: self.span,
});
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[ast::Stmt {
id: ast::DUMMY_NODE_ID,
kind: ast::StmtKind::Expr(DummyResult::raw_expr(self.span,
self.guar)),
span: self.span,
}])))
}
}smallvec![ast::Stmt {
674 id: ast::DUMMY_NODE_ID,
675 kind: ast::StmtKind::Expr(DummyResult::raw_expr(self.span, self.guar)),
676 span: self.span,
677 }])
678 }
679
680 fn make_ty(self: Box<DummyResult>) -> Option<Box<ast::Ty>> {
681 Some(Box::new(ast::Ty {
685 id: ast::DUMMY_NODE_ID,
686 kind: ast::TyKind::Tup(ThinVec::new()),
687 span: self.span,
688 tokens: None,
689 }))
690 }
691
692 fn make_arms(self: Box<DummyResult>) -> Option<SmallVec<[ast::Arm; 1]>> {
693 Some(SmallVec::new())
694 }
695
696 fn make_expr_fields(self: Box<DummyResult>) -> Option<SmallVec<[ast::ExprField; 1]>> {
697 Some(SmallVec::new())
698 }
699
700 fn make_pat_fields(self: Box<DummyResult>) -> Option<SmallVec<[ast::PatField; 1]>> {
701 Some(SmallVec::new())
702 }
703
704 fn make_generic_params(self: Box<DummyResult>) -> Option<SmallVec<[ast::GenericParam; 1]>> {
705 Some(SmallVec::new())
706 }
707
708 fn make_params(self: Box<DummyResult>) -> Option<SmallVec<[ast::Param; 1]>> {
709 Some(SmallVec::new())
710 }
711
712 fn make_field_defs(self: Box<DummyResult>) -> Option<SmallVec<[ast::FieldDef; 1]>> {
713 Some(SmallVec::new())
714 }
715
716 fn make_variants(self: Box<DummyResult>) -> Option<SmallVec<[ast::Variant; 1]>> {
717 Some(SmallVec::new())
718 }
719
720 fn make_crate(self: Box<DummyResult>) -> Option<ast::Crate> {
721 Some(ast::Crate {
722 attrs: Default::default(),
723 items: Default::default(),
724 spans: Default::default(),
725 id: ast::DUMMY_NODE_ID,
726 is_placeholder: Default::default(),
727 })
728 }
729}
730
731#[derive(#[automatically_derived]
impl ::core::clone::Clone for SyntaxExtensionKind {
#[inline]
fn clone(&self) -> SyntaxExtensionKind {
match self {
SyntaxExtensionKind::MacroRules(__self_0) =>
SyntaxExtensionKind::MacroRules(::core::clone::Clone::clone(__self_0)),
SyntaxExtensionKind::Bang(__self_0) =>
SyntaxExtensionKind::Bang(::core::clone::Clone::clone(__self_0)),
SyntaxExtensionKind::LegacyBang(__self_0) =>
SyntaxExtensionKind::LegacyBang(::core::clone::Clone::clone(__self_0)),
SyntaxExtensionKind::Attr(__self_0) =>
SyntaxExtensionKind::Attr(::core::clone::Clone::clone(__self_0)),
SyntaxExtensionKind::LegacyAttr(__self_0) =>
SyntaxExtensionKind::LegacyAttr(::core::clone::Clone::clone(__self_0)),
SyntaxExtensionKind::NonMacroAttr =>
SyntaxExtensionKind::NonMacroAttr,
SyntaxExtensionKind::Derive(__self_0) =>
SyntaxExtensionKind::Derive(::core::clone::Clone::clone(__self_0)),
SyntaxExtensionKind::LegacyDerive(__self_0) =>
SyntaxExtensionKind::LegacyDerive(::core::clone::Clone::clone(__self_0)),
SyntaxExtensionKind::GlobDelegation(__self_0) =>
SyntaxExtensionKind::GlobDelegation(::core::clone::Clone::clone(__self_0)),
}
}
}Clone)]
733pub enum SyntaxExtensionKind {
734 MacroRules(Arc<crate::MacroRulesMacroExpander>),
736
737 Bang(
739 Arc<dyn BangProcMacro + sync::DynSync + sync::DynSend>,
741 ),
742
743 LegacyBang(
745 Arc<dyn TTMacroExpander + sync::DynSync + sync::DynSend>,
747 ),
748
749 Attr(
751 Arc<dyn AttrProcMacro + sync::DynSync + sync::DynSend>,
755 ),
756
757 LegacyAttr(
759 Arc<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
763 ),
764
765 NonMacroAttr,
770
771 Derive(
773 Arc<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
781 ),
782
783 LegacyDerive(
785 Arc<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
788 ),
789
790 GlobDelegation(Arc<dyn GlobDelegationExpander + sync::DynSync + sync::DynSend>),
794}
795
796impl SyntaxExtensionKind {
797 pub fn as_legacy_bang(&self) -> Option<&(dyn TTMacroExpander + sync::DynSync + sync::DynSend)> {
801 match self {
802 SyntaxExtensionKind::LegacyBang(exp) => Some(exp.as_ref()),
803 SyntaxExtensionKind::MacroRules(exp) if exp.kinds().contains(MacroKinds::BANG) => {
804 Some(exp.as_ref())
805 }
806 _ => None,
807 }
808 }
809
810 pub fn as_attr(&self) -> Option<&(dyn AttrProcMacro + sync::DynSync + sync::DynSend)> {
814 match self {
815 SyntaxExtensionKind::Attr(exp) => Some(exp.as_ref()),
816 SyntaxExtensionKind::MacroRules(exp) if exp.kinds().contains(MacroKinds::ATTR) => {
817 Some(exp.as_ref())
818 }
819 _ => None,
820 }
821 }
822}
823
824pub struct SyntaxExtension {
826 pub kind: SyntaxExtensionKind,
828 pub span: Span,
830 pub allow_internal_unstable: Option<Arc<[Symbol]>>,
832 pub stability: Option<Stability>,
834 pub deprecation: Option<Deprecation>,
836 pub helper_attrs: Vec<Symbol>,
838 pub edition: Edition,
840 pub builtin_name: Option<Symbol>,
843 pub allow_internal_unsafe: bool,
845 pub local_inner_macros: bool,
847 pub collapse_debuginfo: bool,
850 pub hide_backtrace: bool,
853}
854
855impl SyntaxExtension {
856 pub fn macro_kinds(&self) -> MacroKinds {
858 match self.kind {
859 SyntaxExtensionKind::Bang(..)
860 | SyntaxExtensionKind::LegacyBang(..)
861 | SyntaxExtensionKind::GlobDelegation(..) => MacroKinds::BANG,
862 SyntaxExtensionKind::Attr(..)
863 | SyntaxExtensionKind::LegacyAttr(..)
864 | SyntaxExtensionKind::NonMacroAttr => MacroKinds::ATTR,
865 SyntaxExtensionKind::Derive(..) | SyntaxExtensionKind::LegacyDerive(..) => {
866 MacroKinds::DERIVE
867 }
868 SyntaxExtensionKind::MacroRules(ref m) => m.kinds(),
869 }
870 }
871
872 pub fn default(kind: SyntaxExtensionKind, edition: Edition) -> SyntaxExtension {
874 SyntaxExtension {
875 span: DUMMY_SP,
876 allow_internal_unstable: None,
877 stability: None,
878 deprecation: None,
879 helper_attrs: Vec::new(),
880 edition,
881 builtin_name: None,
882 kind,
883 allow_internal_unsafe: false,
884 local_inner_macros: false,
885 collapse_debuginfo: false,
886 hide_backtrace: false,
887 }
888 }
889
890 fn get_collapse_debuginfo(sess: &Session, attrs: &[hir::Attribute], ext: bool) -> bool {
897 let flag = sess.opts.cg.collapse_macro_debuginfo;
898 let attr = if let Some(info) = {
'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(CollapseDebugInfo(info)) => {
break 'done Some(info);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, CollapseDebugInfo(info) => info) {
899 *info
900 } else if {
{
'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(RustcBuiltinMacro { .. }) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, RustcBuiltinMacro { .. }) {
901 CollapseMacroDebuginfo::Yes
902 } else {
903 CollapseMacroDebuginfo::Unspecified
904 };
905
906 #[rustfmt::skip]
907 let collapse_table = [
908 [false, false, false, false],
909 [false, ext, ext, true],
910 [false, ext, ext, true],
911 [true, true, true, true],
912 ];
913 collapse_table[flag as usize][attr as usize]
914 }
915
916 fn get_hide_backtrace(attrs: &[hir::Attribute]) -> bool {
917 {
{
'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(RustcDiagnosticItem(..)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, RustcDiagnosticItem(..))
920 }
921
922 pub fn new(
925 sess: &Session,
926 kind: SyntaxExtensionKind,
927 span: Span,
928 helper_attrs: Vec<Symbol>,
929 edition: Edition,
930 name: Symbol,
931 attrs: &[hir::Attribute],
932 is_local: bool,
933 ) -> SyntaxExtension {
934 let allow_internal_unstable = {
'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(AllowInternalUnstable(i, _)) => {
break 'done Some(i);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, AllowInternalUnstable(i, _) => i)
935 .map(|i| i.as_slice())
936 .unwrap_or_default();
937 let allow_internal_unsafe = {
{
'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(AllowInternalUnsafe(_)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, AllowInternalUnsafe(_));
938
939 let local_inner_macros =
940 *{
'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(MacroExport {
local_inner_macros: l, .. }) => {
break 'done Some(l);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, MacroExport {local_inner_macros: l, ..} => l).unwrap_or(&false);
941 let collapse_debuginfo = Self::get_collapse_debuginfo(sess, attrs, !is_local);
942 {
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/base.rs:942",
"rustc_expand::base", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/base.rs"),
::tracing_core::__macro_support::Option::Some(942u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::base"),
::tracing_core::field::FieldSet::new(&["name",
"local_inner_macros", "collapse_debuginfo",
"allow_internal_unsafe"],
::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(&debug(&name) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&local_inner_macros)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&collapse_debuginfo)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&allow_internal_unsafe)
as &dyn Value))])
});
} else { ; }
};tracing::debug!(?name, ?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);
943
944 let (builtin_name, helper_attrs) = match {
'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(RustcBuiltinMacro {
builtin_name, helper_attrs, .. }) => {
break 'done Some((builtin_name, helper_attrs));
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, RustcBuiltinMacro { builtin_name, helper_attrs, .. } => (builtin_name, helper_attrs))
945 {
946 Some((Some(name), helper_attrs)) => {
949 (Some(*name), helper_attrs.iter().copied().collect())
950 }
951 Some((None, _)) => (Some(name), Vec::new()),
952
953 None => (None, helper_attrs),
955 };
956 let hide_backtrace = builtin_name.is_some() || Self::get_hide_backtrace(attrs);
957
958 let stability = {
'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(Stability { stability, .. }) => {
break 'done Some(*stability);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, Stability { stability, .. } => *stability);
959
960 if let Some(sp) = {
'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(RustcBodyStability { span, .. })
=> {
break 'done Some(*span);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, RustcBodyStability{ span, .. } => *span) {
961 sess.dcx().emit_err(errors::MacroBodyStability {
962 span: sp,
963 head_span: sess.source_map().guess_head_span(span),
964 });
965 }
966
967 SyntaxExtension {
968 kind,
969 span,
970 allow_internal_unstable: (!allow_internal_unstable.is_empty())
971 .then(|| allow_internal_unstable.iter().map(|i| i.0).collect::<Vec<_>>().into()),
973 stability,
974 deprecation: {
'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(Deprecated { deprecation, .. })
=> {
break 'done Some(*deprecation);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(
975 attrs,
976 Deprecated { deprecation, .. } => *deprecation
977 ),
978 helper_attrs,
979 edition,
980 builtin_name,
981 allow_internal_unsafe,
982 local_inner_macros,
983 collapse_debuginfo,
984 hide_backtrace,
985 }
986 }
987
988 pub fn dummy_bang(edition: Edition) -> SyntaxExtension {
990 fn expand(
991 ecx: &mut ExtCtxt<'_>,
992 span: Span,
993 _ts: TokenStream,
994 ) -> Result<TokenStream, ErrorGuaranteed> {
995 Err(ecx.dcx().span_delayed_bug(span, "expanded a dummy bang macro"))
996 }
997 SyntaxExtension::default(SyntaxExtensionKind::Bang(Arc::new(expand)), edition)
998 }
999
1000 pub fn dummy_derive(edition: Edition) -> SyntaxExtension {
1002 fn expander(
1003 _: &mut ExtCtxt<'_>,
1004 _: Span,
1005 _: &ast::MetaItem,
1006 _: Annotatable,
1007 ) -> Vec<Annotatable> {
1008 Vec::new()
1009 }
1010 SyntaxExtension::default(SyntaxExtensionKind::Derive(Arc::new(expander)), edition)
1011 }
1012
1013 pub fn non_macro_attr(edition: Edition) -> SyntaxExtension {
1014 SyntaxExtension::default(SyntaxExtensionKind::NonMacroAttr, edition)
1015 }
1016
1017 pub fn glob_delegation(
1018 trait_def_id: DefId,
1019 impl_def_id: LocalDefId,
1020 star_span: Span,
1021 edition: Edition,
1022 ) -> SyntaxExtension {
1023 struct GlobDelegationExpanderImpl {
1024 trait_def_id: DefId,
1025 impl_def_id: LocalDefId,
1026 star_span: Span,
1027 }
1028 impl GlobDelegationExpander for GlobDelegationExpanderImpl {
1029 fn expand(
1030 &self,
1031 ecx: &mut ExtCtxt<'_>,
1032 ) -> ExpandResult<Vec<(Ident, Option<Ident>)>, ()> {
1033 match ecx.resolver.glob_delegation_suffixes(
1034 self.trait_def_id,
1035 self.impl_def_id,
1036 self.star_span,
1037 ) {
1038 Ok(suffixes) => ExpandResult::Ready(suffixes),
1039 Err(Indeterminate) if ecx.force_mode => ExpandResult::Ready(Vec::new()),
1040 Err(Indeterminate) => ExpandResult::Retry(()),
1041 }
1042 }
1043 }
1044
1045 let expander = GlobDelegationExpanderImpl { trait_def_id, impl_def_id, star_span };
1046 SyntaxExtension::default(SyntaxExtensionKind::GlobDelegation(Arc::new(expander)), edition)
1047 }
1048
1049 pub fn expn_data(
1050 &self,
1051 parent: LocalExpnId,
1052 call_site: Span,
1053 descr: Symbol,
1054 kind: MacroKind,
1055 macro_def_id: Option<DefId>,
1056 parent_module: Option<DefId>,
1057 ) -> ExpnData {
1058 ExpnData::new(
1059 ExpnKind::Macro(kind, descr),
1060 parent.to_expn_id(),
1061 call_site,
1062 self.span,
1063 self.allow_internal_unstable.clone(),
1064 self.edition,
1065 macro_def_id,
1066 parent_module,
1067 self.allow_internal_unsafe,
1068 self.local_inner_macros,
1069 self.collapse_debuginfo,
1070 self.hide_backtrace,
1071 )
1072 }
1073}
1074
1075pub struct Indeterminate;
1077
1078pub struct DeriveResolution {
1079 pub path: ast::Path,
1080 pub item: Annotatable,
1081 pub exts: Option<Arc<SyntaxExtension>>,
1085 pub is_const: bool,
1086}
1087
1088pub trait ResolverExpand {
1089 fn next_node_id(&mut self) -> NodeId;
1090 fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId;
1091
1092 fn resolve_dollar_crates(&self);
1093 fn visit_ast_fragment_with_placeholders(
1094 &mut self,
1095 expn_id: LocalExpnId,
1096 fragment: &AstFragment,
1097 );
1098 fn register_builtin_macro(&mut self, name: Symbol, ext: SyntaxExtensionKind);
1099
1100 fn expansion_for_ast_pass(
1101 &mut self,
1102 call_site: Span,
1103 pass: AstPass,
1104 features: &[Symbol],
1105 parent_module_id: Option<NodeId>,
1106 ) -> LocalExpnId;
1107
1108 fn resolve_imports(&mut self);
1109
1110 fn resolve_macro_invocation(
1111 &mut self,
1112 invoc: &Invocation,
1113 eager_expansion_root: LocalExpnId,
1114 force: bool,
1115 ) -> Result<Arc<SyntaxExtension>, Indeterminate>;
1116
1117 fn record_macro_rule_usage(&mut self, mac_id: NodeId, rule_index: usize);
1118
1119 fn check_unused_macros(&mut self);
1120
1121 fn has_derive_copy(&self, expn_id: LocalExpnId) -> bool;
1124 fn resolve_derives(
1126 &mut self,
1127 expn_id: LocalExpnId,
1128 force: bool,
1129 derive_paths: &dyn Fn() -> Vec<DeriveResolution>,
1130 ) -> Result<(), Indeterminate>;
1131 fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<Vec<DeriveResolution>>;
1134 fn cfg_accessible(
1136 &mut self,
1137 expn_id: LocalExpnId,
1138 path: &ast::Path,
1139 ) -> Result<bool, Indeterminate>;
1140 fn macro_accessible(
1141 &mut self,
1142 expn_id: LocalExpnId,
1143 path: &ast::Path,
1144 ) -> Result<bool, Indeterminate>;
1145
1146 fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span;
1149
1150 fn declare_proc_macro(&mut self, id: NodeId);
1157
1158 fn append_stripped_cfg_item(
1159 &mut self,
1160 parent_node: NodeId,
1161 ident: Ident,
1162 cfg: CfgEntry,
1163 cfg_span: Span,
1164 );
1165
1166 fn registered_tools(&self) -> &RegisteredTools;
1168
1169 fn register_glob_delegation(&mut self, invoc_id: LocalExpnId);
1171
1172 fn glob_delegation_suffixes(
1174 &self,
1175 trait_def_id: DefId,
1176 impl_def_id: LocalDefId,
1177 star_span: Span,
1178 ) -> Result<Vec<(Ident, Option<Ident>)>, Indeterminate>;
1179
1180 fn insert_impl_trait_name(&mut self, id: NodeId, name: Symbol);
1183
1184 fn mark_scope_with_compile_error(&mut self, parent_node: NodeId);
1187}
1188
1189pub trait LintStoreExpand {
1190 fn pre_expansion_lint(
1191 &self,
1192 sess: &Session,
1193 features: &Features,
1194 registered_tools: &RegisteredTools,
1195 node_id: NodeId,
1196 attrs: &[Attribute],
1197 items: &[Box<Item>],
1198 name: Symbol,
1199 );
1200}
1201
1202type LintStoreExpandDyn<'a> = Option<&'a (dyn LintStoreExpand + 'a)>;
1203
1204#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ModuleData {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f, "ModuleData",
"mod_path", &self.mod_path, "file_path_stack",
&self.file_path_stack, "dir_path", &&self.dir_path)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for ModuleData {
#[inline]
fn clone(&self) -> ModuleData {
ModuleData {
mod_path: ::core::clone::Clone::clone(&self.mod_path),
file_path_stack: ::core::clone::Clone::clone(&self.file_path_stack),
dir_path: ::core::clone::Clone::clone(&self.dir_path),
}
}
}Clone, #[automatically_derived]
impl ::core::default::Default for ModuleData {
#[inline]
fn default() -> ModuleData {
ModuleData {
mod_path: ::core::default::Default::default(),
file_path_stack: ::core::default::Default::default(),
dir_path: ::core::default::Default::default(),
}
}
}Default)]
1205pub struct ModuleData {
1206 pub mod_path: Vec<Ident>,
1208 pub file_path_stack: Vec<PathBuf>,
1211 pub dir_path: PathBuf,
1214}
1215
1216impl ModuleData {
1217 pub fn with_dir_path(&self, dir_path: PathBuf) -> ModuleData {
1218 ModuleData {
1219 mod_path: self.mod_path.clone(),
1220 file_path_stack: self.file_path_stack.clone(),
1221 dir_path,
1222 }
1223 }
1224}
1225
1226#[derive(#[automatically_derived]
impl ::core::clone::Clone for ExpansionData {
#[inline]
fn clone(&self) -> ExpansionData {
ExpansionData {
id: ::core::clone::Clone::clone(&self.id),
depth: ::core::clone::Clone::clone(&self.depth),
module: ::core::clone::Clone::clone(&self.module),
dir_ownership: ::core::clone::Clone::clone(&self.dir_ownership),
lint_node_id: ::core::clone::Clone::clone(&self.lint_node_id),
is_trailing_mac: ::core::clone::Clone::clone(&self.is_trailing_mac),
}
}
}Clone)]
1227pub struct ExpansionData {
1228 pub id: LocalExpnId,
1229 pub depth: usize,
1230 pub module: Rc<ModuleData>,
1231 pub dir_ownership: DirOwnership,
1232 pub lint_node_id: NodeId,
1234 pub is_trailing_mac: bool,
1235}
1236
1237pub struct ExtCtxt<'a> {
1241 pub sess: &'a Session,
1242 pub ecfg: expand::ExpansionConfig<'a>,
1243 pub num_standard_library_imports: usize,
1244 pub reduced_recursion_limit: Option<(Limit, ErrorGuaranteed)>,
1245 pub root_path: PathBuf,
1246 pub resolver: &'a mut dyn ResolverExpand,
1247 pub current_expansion: ExpansionData,
1248 pub force_mode: bool,
1251 pub expansions: FxIndexMap<Span, Vec<String>>,
1252 pub(super) lint_store: LintStoreExpandDyn<'a>,
1254 pub buffered_early_lint: Vec<BufferedEarlyLint>,
1256 pub(super) expanded_inert_attrs: MarkedAttrs,
1260 pub macro_stats: FxHashMap<(Symbol, MacroKind), MacroStat>,
1262 pub nb_macro_errors: usize,
1263}
1264
1265impl<'a> ExtCtxt<'a> {
1266 pub fn new(
1267 sess: &'a Session,
1268 ecfg: expand::ExpansionConfig<'a>,
1269 resolver: &'a mut dyn ResolverExpand,
1270 lint_store: LintStoreExpandDyn<'a>,
1271 ) -> ExtCtxt<'a> {
1272 ExtCtxt {
1273 sess,
1274 ecfg,
1275 num_standard_library_imports: 0,
1276 reduced_recursion_limit: None,
1277 resolver,
1278 lint_store,
1279 root_path: PathBuf::new(),
1280 current_expansion: ExpansionData {
1281 id: LocalExpnId::ROOT,
1282 depth: 0,
1283 module: Default::default(),
1284 dir_ownership: DirOwnership::Owned { relative: None },
1285 lint_node_id: ast::CRATE_NODE_ID,
1286 is_trailing_mac: false,
1287 },
1288 force_mode: false,
1289 expansions: FxIndexMap::default(),
1290 expanded_inert_attrs: MarkedAttrs::new(),
1291 buffered_early_lint: ::alloc::vec::Vec::new()vec![],
1292 macro_stats: Default::default(),
1293 nb_macro_errors: 0,
1294 }
1295 }
1296
1297 pub fn dcx(&self) -> DiagCtxtHandle<'a> {
1298 self.sess.dcx()
1299 }
1300
1301 pub fn expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
1303 expand::MacroExpander::new(self, false)
1304 }
1305
1306 pub fn monotonic_expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
1309 expand::MacroExpander::new(self, true)
1310 }
1311 pub fn new_parser_from_tts(&self, stream: TokenStream) -> Parser<'a> {
1312 Parser::new(&self.sess.psess, stream, MACRO_ARGUMENTS)
1313 }
1314 pub fn source_map(&self) -> &'a SourceMap {
1315 self.sess.psess.source_map()
1316 }
1317 pub fn psess(&self) -> &'a ParseSess {
1318 &self.sess.psess
1319 }
1320 pub fn call_site(&self) -> Span {
1321 self.current_expansion.id.expn_data().call_site
1322 }
1323
1324 pub(crate) fn expansion_descr(&self) -> String {
1326 let expn_data = self.current_expansion.id.expn_data();
1327 expn_data.kind.descr()
1328 }
1329
1330 pub fn with_def_site_ctxt(&self, span: Span) -> Span {
1333 span.with_def_site_ctxt(self.current_expansion.id.to_expn_id())
1334 }
1335
1336 pub fn with_call_site_ctxt(&self, span: Span) -> Span {
1339 span.with_call_site_ctxt(self.current_expansion.id.to_expn_id())
1340 }
1341
1342 pub fn with_mixed_site_ctxt(&self, span: Span) -> Span {
1345 span.with_mixed_site_ctxt(self.current_expansion.id.to_expn_id())
1346 }
1347
1348 pub fn expansion_cause(&self) -> Option<Span> {
1352 self.current_expansion.id.expansion_cause()
1353 }
1354
1355 pub fn macro_error_and_trace_macros_diag(&mut self) {
1357 self.nb_macro_errors += 1;
1358 self.trace_macros_diag();
1359 }
1360
1361 pub fn trace_macros_diag(&mut self) {
1362 for (span, notes) in self.expansions.iter() {
1363 let mut db = self.dcx().create_note(errors::TraceMacro { span: *span });
1364 for note in notes {
1365 db.note(note.clone());
1366 }
1367 db.emit();
1368 }
1369 self.expansions.clear();
1371 }
1372 pub fn trace_macros(&self) -> bool {
1373 self.ecfg.trace_mac
1374 }
1375 pub fn set_trace_macros(&mut self, x: bool) {
1376 self.ecfg.trace_mac = x
1377 }
1378 pub fn std_path(&self, components: &[Symbol]) -> Vec<Ident> {
1379 let def_site = self.with_def_site_ctxt(DUMMY_SP);
1380 iter::once(Ident::new(kw::DollarCrate, def_site))
1381 .chain(components.iter().map(|&s| Ident::with_dummy_span(s)))
1382 .collect()
1383 }
1384 pub fn def_site_path(&self, components: &[Symbol]) -> Vec<Ident> {
1385 let def_site = self.with_def_site_ctxt(DUMMY_SP);
1386 components.iter().map(|&s| Ident::new(s, def_site)).collect()
1387 }
1388
1389 pub fn check_unused_macros(&mut self) {
1390 self.resolver.check_unused_macros();
1391 }
1392}
1393
1394pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PResult<'_, PathBuf> {
1398 let path = path.into();
1399
1400 if !path.is_absolute() {
1403 let callsite = span.source_callsite();
1404 let source_map = sess.source_map();
1405 let Some(mut base_path) = source_map.span_to_filename(callsite).into_local_path() else {
1406 return Err(sess.dcx().create_err(errors::ResolveRelativePath {
1407 span,
1408 path: source_map
1409 .filename_for_diagnostics(&source_map.span_to_filename(callsite))
1410 .to_string(),
1411 }));
1412 };
1413 base_path.pop();
1414 base_path.push(path);
1415 Ok(base_path)
1416 } else {
1417 match path.components().next() {
1420 Some(Prefix(prefix)) if prefix.kind().is_verbatim() => Ok(path.components().collect()),
1421 _ => Ok(path),
1422 }
1423 }
1424}