Skip to main content

rustc_expand/
base.rs

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// When adding new variants, make sure to
42// adjust the `visit_*` / `flat_map_*` calls in `InvocationCollector`
43// to use `assign_id!`
44#[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
248/// Result of an expansion that may need to be retried.
249/// Consider using this for non-`MultiItemModifier` expanders as well.
250pub enum ExpandResult<T, U> {
251    /// Expansion produced a result (possibly dummy).
252    Ready(T),
253    /// Expansion could not produce a result and needs to be retried.
254    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    /// Creates a [`MacroExpanderResult::Ready`] from a [`TokenStream`].
268    ///
269    /// The `TokenStream` is forwarded without any expansion.
270    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        // Emit the SEMICOLON_IN_EXPRESSIONS_FROM_MACROS deprecation lint.
278        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    /// `meta_item` is the attribute, and `item` is the item being modified.
288    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        // FIXME setup implicit context in TLS before calling self.
334        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    // Default implementation for safe attributes; override if the attribute can be unsafe.
348    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        // FIXME setup implicit context in TLS before calling self.
375        Ok(self(annotation, annotated))
376    }
377}
378
379/// Represents a thing that maps token trees to Macro Results
380pub 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
412// Use a macro because forwarding to a simple function has type system issues
413macro_rules! make_stmts_default {
414    ($me:expr) => {
415        $me.make_expr().map(|e| {
416            smallvec![ast::Stmt {
417                id: ast::DUMMY_NODE_ID,
418                span: e.span,
419                kind: ast::StmtKind::Expr(e),
420            }]
421        })
422    };
423}
424
425/// The result of a macro expansion. The return values of the various
426/// methods are spliced into the AST at the callsite of the macro.
427pub trait MacResult {
428    /// Creates an expression.
429    fn make_expr(self: Box<Self>) -> Option<Box<ast::Expr>> {
430        None
431    }
432
433    /// Creates zero or more items.
434    fn make_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::Item>; 1]>> {
435        None
436    }
437
438    /// Creates zero or more impl items.
439    fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
440        None
441    }
442
443    /// Creates zero or more impl items.
444    fn make_trait_impl_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
445        None
446    }
447
448    /// Creates zero or more trait items.
449    fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
450        None
451    }
452
453    /// Creates zero or more items in an `extern {}` block
454    fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::ForeignItem>; 1]>> {
455        None
456    }
457
458    /// Creates a pattern.
459    fn make_pat(self: Box<Self>) -> Option<Box<ast::Pat>> {
460        None
461    }
462
463    /// Creates zero or more statements.
464    ///
465    /// By default this attempts to create an expression statement,
466    /// returning None if that fails.
467    fn make_stmts(self: Box<Self>) -> Option<SmallVec<[ast::Stmt; 1]>> {
468        self.make_expr().map(|e|
        {
            {
                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),
                                        }])))
                }
            }
        })make_stmts_default!(self)
469    }
470
471    fn make_ty(self: Box<Self>) -> Option<Box<ast::Ty>> {
472        None
473    }
474
475    fn make_arms(self: Box<Self>) -> Option<SmallVec<[ast::Arm; 1]>> {
476        None
477    }
478
479    fn make_expr_fields(self: Box<Self>) -> Option<SmallVec<[ast::ExprField; 1]>> {
480        None
481    }
482
483    fn make_pat_fields(self: Box<Self>) -> Option<SmallVec<[ast::PatField; 1]>> {
484        None
485    }
486
487    fn make_generic_params(self: Box<Self>) -> Option<SmallVec<[ast::GenericParam; 1]>> {
488        None
489    }
490
491    fn make_params(self: Box<Self>) -> Option<SmallVec<[ast::Param; 1]>> {
492        None
493    }
494
495    fn make_field_defs(self: Box<Self>) -> Option<SmallVec<[ast::FieldDef; 1]>> {
496        None
497    }
498
499    fn make_variants(self: Box<Self>) -> Option<SmallVec<[ast::Variant; 1]>> {
500        None
501    }
502
503    fn make_where_predicates(self: Box<Self>) -> Option<SmallVec<[ast::WherePredicate; 1]>> {
504        None
505    }
506
507    fn make_crate(self: Box<Self>) -> Option<ast::Crate> {
508        // Fn-like macros cannot produce a crate.
509        ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
510    }
511}
512
513macro_rules! make_MacEager {
514    ( $( $fld:ident: $t:ty, )* ) => {
515        /// `MacResult` implementation for the common case where you've already
516        /// built each form of AST that you might return.
517        #[derive(Default)]
518        pub struct MacEager {
519            $(
520                pub $fld: Option<$t>,
521            )*
522        }
523
524        impl MacEager {
525            $(
526                pub fn $fld(v: $t) -> Box<dyn MacResult> {
527                    Box::new(MacEager {
528                        $fld: Some(v),
529                        ..Default::default()
530                    })
531                }
532            )*
533        }
534    }
535}
536
537/// `MacResult` implementation for the common case where you've already
/// built each form of AST that you might return.
pub 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! {
538    expr: Box<ast::Expr>,
539    pat: Box<ast::Pat>,
540    items: SmallVec<[Box<ast::Item>; 1]>,
541    impl_items: SmallVec<[Box<ast::AssocItem>; 1]>,
542    trait_items: SmallVec<[Box<ast::AssocItem>; 1]>,
543    foreign_items: SmallVec<[Box<ast::ForeignItem>; 1]>,
544    stmts: SmallVec<[ast::Stmt; 1]>,
545    ty: Box<ast::Ty>,
546}
547
548impl MacResult for MacEager {
549    fn make_expr(self: Box<Self>) -> Option<Box<ast::Expr>> {
550        self.expr
551    }
552
553    fn make_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::Item>; 1]>> {
554        self.items
555    }
556
557    fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
558        self.impl_items
559    }
560
561    fn make_trait_impl_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
562        self.impl_items
563    }
564
565    fn make_trait_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
566        self.trait_items
567    }
568
569    fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::ForeignItem>; 1]>> {
570        self.foreign_items
571    }
572
573    fn make_stmts(self: Box<Self>) -> Option<SmallVec<[ast::Stmt; 1]>> {
574        match self.stmts.as_ref().map_or(0, |s| s.len()) {
575            0 => self.make_expr().map(|e|
        {
            {
                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),
                                        }])))
                }
            }
        })make_stmts_default!(self),
576            _ => self.stmts,
577        }
578    }
579
580    fn make_pat(self: Box<Self>) -> Option<Box<ast::Pat>> {
581        if let Some(p) = self.pat {
582            return Some(p);
583        }
584        if let Some(e) = self.expr {
585            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(_)) {
586                return Some(Box::new(ast::Pat {
587                    id: ast::DUMMY_NODE_ID,
588                    span: e.span,
589                    kind: PatKind::Expr(e),
590                    tokens: None,
591                }));
592            }
593        }
594        None
595    }
596
597    fn make_ty(self: Box<Self>) -> Option<Box<ast::Ty>> {
598        self.ty
599    }
600}
601
602/// Fill-in macro expansion result, to allow compilation to continue
603/// after hitting errors.
604#[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)]
605pub struct DummyResult {
606    guar: Option<ErrorGuaranteed>,
607    span: Span,
608}
609
610impl DummyResult {
611    /// Creates a default MacResult that can be anything.
612    ///
613    /// Use this as a return value after hitting any errors and
614    /// calling `span_err`.
615    pub fn any(span: Span, guar: ErrorGuaranteed) -> Box<dyn MacResult + 'static> {
616        Box::new(DummyResult { guar: Some(guar), span })
617    }
618
619    /// Same as `any`, but must be a valid fragment, not error.
620    pub fn any_valid(span: Span) -> Box<dyn MacResult + 'static> {
621        Box::new(DummyResult { guar: None, span })
622    }
623
624    /// A plain dummy expression.
625    pub fn raw_expr(sp: Span, guar: Option<ErrorGuaranteed>) -> Box<ast::Expr> {
626        Box::new(ast::Expr {
627            id: ast::DUMMY_NODE_ID,
628            kind: if let Some(guar) = guar {
629                ast::ExprKind::Err(guar)
630            } else {
631                ast::ExprKind::Tup(ThinVec::new())
632            },
633            span: sp,
634            attrs: ast::AttrVec::new(),
635            tokens: None,
636        })
637    }
638}
639
640impl MacResult for DummyResult {
641    fn make_expr(self: Box<DummyResult>) -> Option<Box<ast::Expr>> {
642        Some(DummyResult::raw_expr(self.span, self.guar))
643    }
644
645    fn make_pat(self: Box<DummyResult>) -> Option<Box<ast::Pat>> {
646        Some(Box::new(ast::Pat {
647            id: ast::DUMMY_NODE_ID,
648            kind: PatKind::Wild,
649            span: self.span,
650            tokens: None,
651        }))
652    }
653
654    fn make_items(self: Box<DummyResult>) -> Option<SmallVec<[Box<ast::Item>; 1]>> {
655        Some(SmallVec::new())
656    }
657
658    fn make_impl_items(self: Box<DummyResult>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
659        Some(SmallVec::new())
660    }
661
662    fn make_trait_impl_items(self: Box<DummyResult>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
663        Some(SmallVec::new())
664    }
665
666    fn make_trait_items(self: Box<DummyResult>) -> Option<SmallVec<[Box<ast::AssocItem>; 1]>> {
667        Some(SmallVec::new())
668    }
669
670    fn make_foreign_items(self: Box<Self>) -> Option<SmallVec<[Box<ast::ForeignItem>; 1]>> {
671        Some(SmallVec::new())
672    }
673
674    fn make_stmts(self: Box<DummyResult>) -> Option<SmallVec<[ast::Stmt; 1]>> {
675        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 {
676            id: ast::DUMMY_NODE_ID,
677            kind: ast::StmtKind::Expr(DummyResult::raw_expr(self.span, self.guar)),
678            span: self.span,
679        }])
680    }
681
682    fn make_ty(self: Box<DummyResult>) -> Option<Box<ast::Ty>> {
683        // FIXME(nnethercote): you might expect `ast::TyKind::Dummy` to be used here, but some
684        // values produced here end up being lowered to HIR, which `ast::TyKind::Dummy` does not
685        // support, so we use an empty tuple instead.
686        Some(Box::new(ast::Ty {
687            id: ast::DUMMY_NODE_ID,
688            kind: ast::TyKind::Tup(ThinVec::new()),
689            span: self.span,
690            tokens: None,
691        }))
692    }
693
694    fn make_arms(self: Box<DummyResult>) -> Option<SmallVec<[ast::Arm; 1]>> {
695        Some(SmallVec::new())
696    }
697
698    fn make_expr_fields(self: Box<DummyResult>) -> Option<SmallVec<[ast::ExprField; 1]>> {
699        Some(SmallVec::new())
700    }
701
702    fn make_pat_fields(self: Box<DummyResult>) -> Option<SmallVec<[ast::PatField; 1]>> {
703        Some(SmallVec::new())
704    }
705
706    fn make_generic_params(self: Box<DummyResult>) -> Option<SmallVec<[ast::GenericParam; 1]>> {
707        Some(SmallVec::new())
708    }
709
710    fn make_params(self: Box<DummyResult>) -> Option<SmallVec<[ast::Param; 1]>> {
711        Some(SmallVec::new())
712    }
713
714    fn make_field_defs(self: Box<DummyResult>) -> Option<SmallVec<[ast::FieldDef; 1]>> {
715        Some(SmallVec::new())
716    }
717
718    fn make_variants(self: Box<DummyResult>) -> Option<SmallVec<[ast::Variant; 1]>> {
719        Some(SmallVec::new())
720    }
721
722    fn make_crate(self: Box<DummyResult>) -> Option<ast::Crate> {
723        Some(ast::Crate {
724            attrs: Default::default(),
725            items: Default::default(),
726            spans: Default::default(),
727            id: ast::DUMMY_NODE_ID,
728            is_placeholder: Default::default(),
729        })
730    }
731}
732
733/// A syntax extension kind.
734#[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)]
735pub enum SyntaxExtensionKind {
736    /// A `macro_rules!` macro that can work as any `MacroKind`
737    MacroRules(Arc<crate::MacroRulesMacroExpander>),
738
739    /// A token-based function-like macro.
740    Bang(
741        /// An expander with signature TokenStream -> TokenStream.
742        Arc<dyn BangProcMacro + sync::DynSync + sync::DynSend>,
743    ),
744
745    /// An AST-based function-like macro.
746    LegacyBang(
747        /// An expander with signature TokenStream -> AST.
748        Arc<dyn TTMacroExpander + sync::DynSync + sync::DynSend>,
749    ),
750
751    /// A token-based attribute macro.
752    Attr(
753        /// An expander with signature (TokenStream, TokenStream) -> TokenStream.
754        /// The first TokenStream is the attribute itself, the second is the annotated item.
755        /// The produced TokenStream replaces the input TokenStream.
756        Arc<dyn AttrProcMacro + sync::DynSync + sync::DynSend>,
757    ),
758
759    /// An AST-based attribute macro.
760    LegacyAttr(
761        /// An expander with signature (AST, AST) -> AST.
762        /// The first AST fragment is the attribute itself, the second is the annotated item.
763        /// The produced AST fragment replaces the input AST fragment.
764        Arc<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
765    ),
766
767    /// A trivial attribute "macro" that does nothing,
768    /// only keeps the attribute and marks it as inert,
769    /// thus making it ineligible for further expansion.
770    /// E.g. `#[default]`, `#[rustfmt::skip]`.
771    NonMacroAttr,
772
773    /// A token-based derive macro.
774    Derive(
775        /// An expander with signature TokenStream -> TokenStream.
776        /// The produced TokenStream is appended to the input TokenStream.
777        ///
778        /// FIXME: The text above describes how this should work. Currently it
779        /// is handled identically to `LegacyDerive`. It should be migrated to
780        /// a token-based representation like `Bang` and `Attr`, instead of
781        /// using `MultiItemModifier`.
782        Arc<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
783    ),
784
785    /// An AST-based derive macro.
786    LegacyDerive(
787        /// An expander with signature AST -> AST.
788        /// The produced AST fragment is appended to the input AST fragment.
789        Arc<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
790    ),
791
792    /// A glob delegation.
793    ///
794    /// This is for delegated function implementations, and has nothing to do with glob imports.
795    GlobDelegation(Arc<dyn GlobDelegationExpander + sync::DynSync + sync::DynSend>),
796}
797
798impl SyntaxExtensionKind {
799    /// Returns `Some(expander)` for a macro usable as a `LegacyBang`; otherwise returns `None`
800    ///
801    /// This includes a `MacroRules` with function-like rules.
802    pub fn as_legacy_bang(&self) -> Option<&(dyn TTMacroExpander + sync::DynSync + sync::DynSend)> {
803        match self {
804            SyntaxExtensionKind::LegacyBang(exp) => Some(exp.as_ref()),
805            SyntaxExtensionKind::MacroRules(exp) if exp.kinds().contains(MacroKinds::BANG) => {
806                Some(exp.as_ref())
807            }
808            _ => None,
809        }
810    }
811
812    /// Returns `Some(expander)` for a macro usable as an `Attr`; otherwise returns `None`
813    ///
814    /// This includes a `MacroRules` with `attr` rules.
815    pub fn as_attr(&self) -> Option<&(dyn AttrProcMacro + sync::DynSync + sync::DynSend)> {
816        match self {
817            SyntaxExtensionKind::Attr(exp) => Some(exp.as_ref()),
818            SyntaxExtensionKind::MacroRules(exp) if exp.kinds().contains(MacroKinds::ATTR) => {
819                Some(exp.as_ref())
820            }
821            _ => None,
822        }
823    }
824}
825
826/// A struct representing a macro definition in "lowered" form ready for expansion.
827pub struct SyntaxExtension {
828    /// A syntax extension kind.
829    pub kind: SyntaxExtensionKind,
830    /// Span of the macro definition.
831    pub span: Span,
832    /// List of unstable features that are treated as stable inside this macro.
833    pub allow_internal_unstable: Option<Arc<[Symbol]>>,
834    /// The macro's stability info.
835    pub stability: Option<Stability>,
836    /// The macro's deprecation info.
837    pub deprecation: Option<Deprecation>,
838    /// Names of helper attributes registered by this macro.
839    pub helper_attrs: Vec<Symbol>,
840    /// Edition of the crate in which this macro is defined.
841    pub edition: Edition,
842    /// Built-in macros have a couple of special properties like availability
843    /// in `#[no_implicit_prelude]` modules, so we have to keep this flag.
844    pub builtin_name: Option<Symbol>,
845    /// Suppresses the `unsafe_code` lint for code produced by this macro.
846    pub allow_internal_unsafe: bool,
847    /// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`) for this macro.
848    pub local_inner_macros: bool,
849    /// Should debuginfo for the macro be collapsed to the outermost expansion site (in other
850    /// words, was the macro definition annotated with `#[collapse_debuginfo]`)?
851    pub collapse_debuginfo: bool,
852    /// Suppresses the "this error originates in the macro" note when a diagnostic points at this
853    /// macro.
854    pub hide_backtrace: bool,
855}
856
857impl SyntaxExtension {
858    /// Returns which kinds of macro call this syntax extension.
859    pub fn macro_kinds(&self) -> MacroKinds {
860        match self.kind {
861            SyntaxExtensionKind::Bang(..)
862            | SyntaxExtensionKind::LegacyBang(..)
863            | SyntaxExtensionKind::GlobDelegation(..) => MacroKinds::BANG,
864            SyntaxExtensionKind::Attr(..)
865            | SyntaxExtensionKind::LegacyAttr(..)
866            | SyntaxExtensionKind::NonMacroAttr => MacroKinds::ATTR,
867            SyntaxExtensionKind::Derive(..) | SyntaxExtensionKind::LegacyDerive(..) => {
868                MacroKinds::DERIVE
869            }
870            SyntaxExtensionKind::MacroRules(ref m) => m.kinds(),
871        }
872    }
873
874    /// Constructs a syntax extension with default properties.
875    pub fn default(kind: SyntaxExtensionKind, edition: Edition) -> SyntaxExtension {
876        SyntaxExtension {
877            span: DUMMY_SP,
878            allow_internal_unstable: None,
879            stability: None,
880            deprecation: None,
881            helper_attrs: Vec::new(),
882            edition,
883            builtin_name: None,
884            kind,
885            allow_internal_unsafe: false,
886            local_inner_macros: false,
887            collapse_debuginfo: false,
888            hide_backtrace: false,
889        }
890    }
891
892    /// if-ext - if macro from different crate (related to callsite code)
893    /// | cmd \ attr    | no  | (unspecified) | external | yes |
894    /// | no            | no  | no            | no       | no  |
895    /// | (unspecified) | no  | if-ext        | if-ext   | yes |
896    /// | external      | no  | if-ext        | if-ext   | yes |
897    /// | yes           | yes | yes           | yes      | yes |
898    fn get_collapse_debuginfo(sess: &Session, attrs: &[hir::Attribute], ext: bool) -> bool {
899        let flag = sess.opts.cg.collapse_macro_debuginfo;
900        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) {
901            info.clone()
902        } 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 { .. }) {
903            CollapseMacroDebuginfo::Yes
904        } else {
905            CollapseMacroDebuginfo::Unspecified
906        };
907
908        #[rustfmt::skip]
909        let collapse_table = [
910            [false, false, false, false],
911            [false, ext,   ext,   true],
912            [false, ext,   ext,   true],
913            [true,  true,  true,  true],
914        ];
915        collapse_table[flag as usize][attr as usize]
916    }
917
918    fn get_hide_backtrace(attrs: &[hir::Attribute]) -> bool {
919        // FIXME(estebank): instead of reusing `#[rustc_diagnostic_item]` as a proxy, introduce a
920        // new attribute purely for this under the `#[diagnostic]` namespace.
921        {
    {
            '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(..))
922    }
923
924    /// Constructs a syntax extension with the given properties
925    /// and other properties converted from attributes.
926    pub fn new(
927        sess: &Session,
928        kind: SyntaxExtensionKind,
929        span: Span,
930        helper_attrs: Vec<Symbol>,
931        edition: Edition,
932        name: Symbol,
933        attrs: &[hir::Attribute],
934        is_local: bool,
935    ) -> SyntaxExtension {
936        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)
937            .map(|i| i.as_slice())
938            .unwrap_or_default();
939        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(_));
940
941        let local_inner_macros =
942            *{
    '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);
943        let collapse_debuginfo = Self::get_collapse_debuginfo(sess, attrs, !is_local);
944        {
    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:944",
                        "rustc_expand::base", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/base.rs"),
                        ::tracing_core::__macro_support::Option::Some(944u32),
                        ::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);
945
946        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))
947        {
948            // Override `helper_attrs` passed above if it's a built-in macro,
949            // marking `proc_macro_derive` macros as built-in is not a realistic use case.
950            Some((Some(name), helper_attrs)) => {
951                (Some(*name), helper_attrs.iter().copied().collect())
952            }
953            Some((None, _)) => (Some(name), Vec::new()),
954
955            // Not a built-in macro
956            None => (None, helper_attrs),
957        };
958        let hide_backtrace = builtin_name.is_some() || Self::get_hide_backtrace(attrs);
959
960        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);
961
962        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) {
963            sess.dcx().emit_err(errors::MacroBodyStability {
964                span: sp,
965                head_span: sess.source_map().guess_head_span(span),
966            });
967        }
968
969        SyntaxExtension {
970            kind,
971            span,
972            allow_internal_unstable: (!allow_internal_unstable.is_empty())
973                // FIXME(jdonszelmann): avoid the into_iter/collect?
974                .then(|| allow_internal_unstable.iter().map(|i| i.0).collect::<Vec<_>>().into()),
975            stability,
976            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!(
977                attrs,
978                Deprecated { deprecation, .. } => *deprecation
979            ),
980            helper_attrs,
981            edition,
982            builtin_name,
983            allow_internal_unsafe,
984            local_inner_macros,
985            collapse_debuginfo,
986            hide_backtrace,
987        }
988    }
989
990    /// A dummy bang macro `foo!()`.
991    pub fn dummy_bang(edition: Edition) -> SyntaxExtension {
992        fn expand(
993            ecx: &mut ExtCtxt<'_>,
994            span: Span,
995            _ts: TokenStream,
996        ) -> Result<TokenStream, ErrorGuaranteed> {
997            Err(ecx.dcx().span_delayed_bug(span, "expanded a dummy bang macro"))
998        }
999        SyntaxExtension::default(SyntaxExtensionKind::Bang(Arc::new(expand)), edition)
1000    }
1001
1002    /// A dummy derive macro `#[derive(Foo)]`.
1003    pub fn dummy_derive(edition: Edition) -> SyntaxExtension {
1004        fn expander(
1005            _: &mut ExtCtxt<'_>,
1006            _: Span,
1007            _: &ast::MetaItem,
1008            _: Annotatable,
1009        ) -> Vec<Annotatable> {
1010            Vec::new()
1011        }
1012        SyntaxExtension::default(SyntaxExtensionKind::Derive(Arc::new(expander)), edition)
1013    }
1014
1015    pub fn non_macro_attr(edition: Edition) -> SyntaxExtension {
1016        SyntaxExtension::default(SyntaxExtensionKind::NonMacroAttr, edition)
1017    }
1018
1019    pub fn glob_delegation(
1020        trait_def_id: DefId,
1021        impl_def_id: LocalDefId,
1022        edition: Edition,
1023    ) -> SyntaxExtension {
1024        struct GlobDelegationExpanderImpl {
1025            trait_def_id: DefId,
1026            impl_def_id: LocalDefId,
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(self.trait_def_id, self.impl_def_id) {
1034                    Ok(suffixes) => ExpandResult::Ready(suffixes),
1035                    Err(Indeterminate) if ecx.force_mode => ExpandResult::Ready(Vec::new()),
1036                    Err(Indeterminate) => ExpandResult::Retry(()),
1037                }
1038            }
1039        }
1040
1041        let expander = GlobDelegationExpanderImpl { trait_def_id, impl_def_id };
1042        SyntaxExtension::default(SyntaxExtensionKind::GlobDelegation(Arc::new(expander)), edition)
1043    }
1044
1045    pub fn expn_data(
1046        &self,
1047        parent: LocalExpnId,
1048        call_site: Span,
1049        descr: Symbol,
1050        kind: MacroKind,
1051        macro_def_id: Option<DefId>,
1052        parent_module: Option<DefId>,
1053    ) -> ExpnData {
1054        ExpnData::new(
1055            ExpnKind::Macro(kind, descr),
1056            parent.to_expn_id(),
1057            call_site,
1058            self.span,
1059            self.allow_internal_unstable.clone(),
1060            self.edition,
1061            macro_def_id,
1062            parent_module,
1063            self.allow_internal_unsafe,
1064            self.local_inner_macros,
1065            self.collapse_debuginfo,
1066            self.hide_backtrace,
1067        )
1068    }
1069}
1070
1071/// Error type that denotes indeterminacy.
1072pub struct Indeterminate;
1073
1074pub struct DeriveResolution {
1075    pub path: ast::Path,
1076    pub item: Annotatable,
1077    // FIXME: currently this field is only used in `is_none`/`is_some` conditions. However, the
1078    // `Arc<SyntaxExtension>` will be used if the FIXME in `MacroExpander::fully_expand_fragment`
1079    // is completed.
1080    pub exts: Option<Arc<SyntaxExtension>>,
1081    pub is_const: bool,
1082}
1083
1084pub trait ResolverExpand {
1085    fn next_node_id(&mut self) -> NodeId;
1086    fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId;
1087
1088    fn resolve_dollar_crates(&self);
1089    fn visit_ast_fragment_with_placeholders(
1090        &mut self,
1091        expn_id: LocalExpnId,
1092        fragment: &AstFragment,
1093    );
1094    fn register_builtin_macro(&mut self, name: Symbol, ext: SyntaxExtensionKind);
1095
1096    fn expansion_for_ast_pass(
1097        &mut self,
1098        call_site: Span,
1099        pass: AstPass,
1100        features: &[Symbol],
1101        parent_module_id: Option<NodeId>,
1102    ) -> LocalExpnId;
1103
1104    fn resolve_imports(&mut self);
1105
1106    fn resolve_macro_invocation(
1107        &mut self,
1108        invoc: &Invocation,
1109        eager_expansion_root: LocalExpnId,
1110        force: bool,
1111    ) -> Result<Arc<SyntaxExtension>, Indeterminate>;
1112
1113    fn record_macro_rule_usage(&mut self, mac_id: NodeId, rule_index: usize);
1114
1115    fn check_unused_macros(&mut self);
1116
1117    // Resolver interfaces for specific built-in macros.
1118    /// Does `#[derive(...)]` attribute with the given `ExpnId` have built-in `Copy` inside it?
1119    fn has_derive_copy(&self, expn_id: LocalExpnId) -> bool;
1120    /// Resolve paths inside the `#[derive(...)]` attribute with the given `ExpnId`.
1121    fn resolve_derives(
1122        &mut self,
1123        expn_id: LocalExpnId,
1124        force: bool,
1125        derive_paths: &dyn Fn() -> Vec<DeriveResolution>,
1126    ) -> Result<(), Indeterminate>;
1127    /// Take resolutions for paths inside the `#[derive(...)]` attribute with the given `ExpnId`
1128    /// back from resolver.
1129    fn take_derive_resolutions(&mut self, expn_id: LocalExpnId) -> Option<Vec<DeriveResolution>>;
1130    /// Path resolution logic for `#[cfg_accessible(path)]`.
1131    fn cfg_accessible(
1132        &mut self,
1133        expn_id: LocalExpnId,
1134        path: &ast::Path,
1135    ) -> Result<bool, Indeterminate>;
1136    fn macro_accessible(
1137        &mut self,
1138        expn_id: LocalExpnId,
1139        path: &ast::Path,
1140    ) -> Result<bool, Indeterminate>;
1141
1142    /// Decodes the proc-macro quoted span in the specified crate, with the specified id.
1143    /// No caching is performed.
1144    fn get_proc_macro_quoted_span(&self, krate: CrateNum, id: usize) -> Span;
1145
1146    /// The order of items in the HIR is unrelated to the order of
1147    /// items in the AST. However, we generate proc macro harnesses
1148    /// based on the AST order, and later refer to these harnesses
1149    /// from the HIR. This field keeps track of the order in which
1150    /// we generated proc macros harnesses, so that we can map
1151    /// HIR proc macros items back to their harness items.
1152    fn declare_proc_macro(&mut self, id: NodeId);
1153
1154    fn append_stripped_cfg_item(
1155        &mut self,
1156        parent_node: NodeId,
1157        ident: Ident,
1158        cfg: CfgEntry,
1159        cfg_span: Span,
1160    );
1161
1162    /// Tools registered with `#![register_tool]` and used by tool attributes and lints.
1163    fn registered_tools(&self) -> &RegisteredTools;
1164
1165    /// Mark this invocation id as a glob delegation.
1166    fn register_glob_delegation(&mut self, invoc_id: LocalExpnId);
1167
1168    /// Names of specific methods to which glob delegation expands.
1169    fn glob_delegation_suffixes(
1170        &self,
1171        trait_def_id: DefId,
1172        impl_def_id: LocalDefId,
1173    ) -> Result<Vec<(Ident, Option<Ident>)>, Indeterminate>;
1174
1175    /// Record the name of an opaque `Ty::ImplTrait` pre-expansion so that it can be used
1176    /// to generate an item name later that does not reference placeholder macros.
1177    fn insert_impl_trait_name(&mut self, id: NodeId, name: Symbol);
1178
1179    /// Mark the scope as having a compile error so that error for lookup in this scope
1180    /// should be suppressed
1181    fn mark_scope_with_compile_error(&mut self, parent_node: NodeId);
1182}
1183
1184pub trait LintStoreExpand {
1185    fn pre_expansion_lint(
1186        &self,
1187        sess: &Session,
1188        features: &Features,
1189        registered_tools: &RegisteredTools,
1190        node_id: NodeId,
1191        attrs: &[Attribute],
1192        items: &[Box<Item>],
1193        name: Symbol,
1194    );
1195}
1196
1197type LintStoreExpandDyn<'a> = Option<&'a (dyn LintStoreExpand + 'a)>;
1198
1199#[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)]
1200pub struct ModuleData {
1201    /// Path to the module starting from the crate name, like `my_crate::foo::bar`.
1202    pub mod_path: Vec<Ident>,
1203    /// Stack of paths to files loaded by out-of-line module items,
1204    /// used to detect and report recursive module inclusions.
1205    pub file_path_stack: Vec<PathBuf>,
1206    /// Directory to search child module files in,
1207    /// often (but not necessarily) the parent of the top file path on the `file_path_stack`.
1208    pub dir_path: PathBuf,
1209}
1210
1211impl ModuleData {
1212    pub fn with_dir_path(&self, dir_path: PathBuf) -> ModuleData {
1213        ModuleData {
1214            mod_path: self.mod_path.clone(),
1215            file_path_stack: self.file_path_stack.clone(),
1216            dir_path,
1217        }
1218    }
1219}
1220
1221#[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)]
1222pub struct ExpansionData {
1223    pub id: LocalExpnId,
1224    pub depth: usize,
1225    pub module: Rc<ModuleData>,
1226    pub dir_ownership: DirOwnership,
1227    /// Some parent node that is close to this macro call
1228    pub lint_node_id: NodeId,
1229    pub is_trailing_mac: bool,
1230}
1231
1232/// One of these is made during expansion and incrementally updated as we go;
1233/// when a macro expansion occurs, the resulting nodes have the `backtrace()
1234/// -> expn_data` of their expansion context stored into their span.
1235pub struct ExtCtxt<'a> {
1236    pub sess: &'a Session,
1237    pub ecfg: expand::ExpansionConfig<'a>,
1238    pub num_standard_library_imports: usize,
1239    pub reduced_recursion_limit: Option<(Limit, ErrorGuaranteed)>,
1240    pub root_path: PathBuf,
1241    pub resolver: &'a mut dyn ResolverExpand,
1242    pub current_expansion: ExpansionData,
1243    /// Error recovery mode entered when expansion is stuck
1244    /// (or during eager expansion, but that's a hack).
1245    pub force_mode: bool,
1246    pub expansions: FxIndexMap<Span, Vec<String>>,
1247    /// Used for running pre-expansion lints on freshly loaded modules.
1248    pub(super) lint_store: LintStoreExpandDyn<'a>,
1249    /// Used for storing lints generated during expansion, like `NAMED_ARGUMENTS_USED_POSITIONALLY`
1250    pub buffered_early_lint: Vec<BufferedEarlyLint>,
1251    /// When we 'expand' an inert attribute, we leave it
1252    /// in the AST, but insert it here so that we know
1253    /// not to expand it again.
1254    pub(super) expanded_inert_attrs: MarkedAttrs,
1255    /// `-Zmacro-stats` data.
1256    pub macro_stats: FxHashMap<(Symbol, MacroKind), MacroStat>,
1257    pub nb_macro_errors: usize,
1258}
1259
1260impl<'a> ExtCtxt<'a> {
1261    pub fn new(
1262        sess: &'a Session,
1263        ecfg: expand::ExpansionConfig<'a>,
1264        resolver: &'a mut dyn ResolverExpand,
1265        lint_store: LintStoreExpandDyn<'a>,
1266    ) -> ExtCtxt<'a> {
1267        ExtCtxt {
1268            sess,
1269            ecfg,
1270            num_standard_library_imports: 0,
1271            reduced_recursion_limit: None,
1272            resolver,
1273            lint_store,
1274            root_path: PathBuf::new(),
1275            current_expansion: ExpansionData {
1276                id: LocalExpnId::ROOT,
1277                depth: 0,
1278                module: Default::default(),
1279                dir_ownership: DirOwnership::Owned { relative: None },
1280                lint_node_id: ast::CRATE_NODE_ID,
1281                is_trailing_mac: false,
1282            },
1283            force_mode: false,
1284            expansions: FxIndexMap::default(),
1285            expanded_inert_attrs: MarkedAttrs::new(),
1286            buffered_early_lint: ::alloc::vec::Vec::new()vec![],
1287            macro_stats: Default::default(),
1288            nb_macro_errors: 0,
1289        }
1290    }
1291
1292    pub fn dcx(&self) -> DiagCtxtHandle<'a> {
1293        self.sess.dcx()
1294    }
1295
1296    /// Returns a `Folder` for deeply expanding all macros in an AST node.
1297    pub fn expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
1298        expand::MacroExpander::new(self, false)
1299    }
1300
1301    /// Returns a `Folder` that deeply expands all macros and assigns all `NodeId`s in an AST node.
1302    /// Once `NodeId`s are assigned, the node may not be expanded, removed, or otherwise modified.
1303    pub fn monotonic_expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
1304        expand::MacroExpander::new(self, true)
1305    }
1306    pub fn new_parser_from_tts(&self, stream: TokenStream) -> Parser<'a> {
1307        Parser::new(&self.sess.psess, stream, MACRO_ARGUMENTS)
1308    }
1309    pub fn source_map(&self) -> &'a SourceMap {
1310        self.sess.psess.source_map()
1311    }
1312    pub fn psess(&self) -> &'a ParseSess {
1313        &self.sess.psess
1314    }
1315    pub fn call_site(&self) -> Span {
1316        self.current_expansion.id.expn_data().call_site
1317    }
1318
1319    /// Returns the current expansion kind's description.
1320    pub(crate) fn expansion_descr(&self) -> String {
1321        let expn_data = self.current_expansion.id.expn_data();
1322        expn_data.kind.descr()
1323    }
1324
1325    /// Equivalent of `Span::def_site` from the proc macro API,
1326    /// except that the location is taken from the span passed as an argument.
1327    pub fn with_def_site_ctxt(&self, span: Span) -> Span {
1328        span.with_def_site_ctxt(self.current_expansion.id.to_expn_id())
1329    }
1330
1331    /// Equivalent of `Span::call_site` from the proc macro API,
1332    /// except that the location is taken from the span passed as an argument.
1333    pub fn with_call_site_ctxt(&self, span: Span) -> Span {
1334        span.with_call_site_ctxt(self.current_expansion.id.to_expn_id())
1335    }
1336
1337    /// Equivalent of `Span::mixed_site` from the proc macro API,
1338    /// except that the location is taken from the span passed as an argument.
1339    pub fn with_mixed_site_ctxt(&self, span: Span) -> Span {
1340        span.with_mixed_site_ctxt(self.current_expansion.id.to_expn_id())
1341    }
1342
1343    /// Returns span for the macro which originally caused the current expansion to happen.
1344    ///
1345    /// Stops backtracing at include! boundary.
1346    pub fn expansion_cause(&self) -> Option<Span> {
1347        self.current_expansion.id.expansion_cause()
1348    }
1349
1350    /// This method increases the internal macro errors count and then call `trace_macros_diag`.
1351    pub fn macro_error_and_trace_macros_diag(&mut self) {
1352        self.nb_macro_errors += 1;
1353        self.trace_macros_diag();
1354    }
1355
1356    pub fn trace_macros_diag(&mut self) {
1357        for (span, notes) in self.expansions.iter() {
1358            let mut db = self.dcx().create_note(errors::TraceMacro { span: *span });
1359            for note in notes {
1360                db.note(note.clone());
1361            }
1362            db.emit();
1363        }
1364        // Fixme: does this result in errors?
1365        self.expansions.clear();
1366    }
1367    pub fn trace_macros(&self) -> bool {
1368        self.ecfg.trace_mac
1369    }
1370    pub fn set_trace_macros(&mut self, x: bool) {
1371        self.ecfg.trace_mac = x
1372    }
1373    pub fn std_path(&self, components: &[Symbol]) -> Vec<Ident> {
1374        let def_site = self.with_def_site_ctxt(DUMMY_SP);
1375        iter::once(Ident::new(kw::DollarCrate, def_site))
1376            .chain(components.iter().map(|&s| Ident::with_dummy_span(s)))
1377            .collect()
1378    }
1379    pub fn def_site_path(&self, components: &[Symbol]) -> Vec<Ident> {
1380        let def_site = self.with_def_site_ctxt(DUMMY_SP);
1381        components.iter().map(|&s| Ident::new(s, def_site)).collect()
1382    }
1383
1384    pub fn check_unused_macros(&mut self) {
1385        self.resolver.check_unused_macros();
1386    }
1387}
1388
1389/// Resolves a `path` mentioned inside Rust code, returning an absolute path.
1390///
1391/// This unifies the logic used for resolving `include_X!`.
1392pub fn resolve_path(sess: &Session, path: impl Into<PathBuf>, span: Span) -> PResult<'_, PathBuf> {
1393    let path = path.into();
1394
1395    // Relative paths are resolved relative to the file in which they are found
1396    // after macro expansion (that is, they are unhygienic).
1397    if !path.is_absolute() {
1398        let callsite = span.source_callsite();
1399        let source_map = sess.source_map();
1400        let Some(mut base_path) = source_map.span_to_filename(callsite).into_local_path() else {
1401            return Err(sess.dcx().create_err(errors::ResolveRelativePath {
1402                span,
1403                path: source_map
1404                    .filename_for_diagnostics(&source_map.span_to_filename(callsite))
1405                    .to_string(),
1406            }));
1407        };
1408        base_path.pop();
1409        base_path.push(path);
1410        Ok(base_path)
1411    } else {
1412        // This ensures that Windows verbatim paths are fixed if mixed path separators are used,
1413        // which can happen when `concat!` is used to join paths.
1414        match path.components().next() {
1415            Some(Prefix(prefix)) if prefix.kind().is_verbatim() => Ok(path.components().collect()),
1416            _ => Ok(path),
1417        }
1418    }
1419}