rustc_ast/
ast.rs

1//! The Rust abstract syntax tree module.
2//!
3//! This module contains common structures forming the language AST.
4//! Two main entities in the module are [`Item`] (which represents an AST element with
5//! additional metadata), and [`ItemKind`] (which represents a concrete type and contains
6//! information specific to the type of the item).
7//!
8//! Other module items worth mentioning:
9//! - [`Ty`] and [`TyKind`]: A parsed Rust type.
10//! - [`Expr`] and [`ExprKind`]: A parsed Rust expression.
11//! - [`Pat`] and [`PatKind`]: A parsed Rust pattern. Patterns are often dual to expressions.
12//! - [`Stmt`] and [`StmtKind`]: An executable action that does not return a value.
13//! - [`FnDecl`], [`FnHeader`] and [`Param`]: Metadata associated with a function declaration.
14//! - [`Generics`], [`GenericParam`], [`WhereClause`]: Metadata associated with generic parameters.
15//! - [`EnumDef`] and [`Variant`]: Enum declaration.
16//! - [`MetaItemLit`] and [`LitKind`]: Literal expressions.
17//! - [`MacroDef`], [`MacStmtStyle`], [`MacCall`]: Macro definition and invocation.
18//! - [`Attribute`]: Metadata associated with item.
19//! - [`UnOp`], [`BinOp`], and [`BinOpKind`]: Unary and binary operators.
20
21use std::borrow::Cow;
22use std::sync::Arc;
23use std::{cmp, fmt};
24
25pub use GenericArgs::*;
26pub use UnsafeSource::*;
27pub use rustc_ast_ir::{Movability, Mutability, Pinnedness};
28use rustc_data_structures::packed::Pu128;
29use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
30use rustc_data_structures::stack::ensure_sufficient_stack;
31use rustc_data_structures::tagged_ptr::Tag;
32use rustc_macros::{Decodable, Encodable, HashStable_Generic};
33pub use rustc_span::AttrId;
34use rustc_span::source_map::{Spanned, respan};
35use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
36use thin_vec::{ThinVec, thin_vec};
37
38pub use crate::format::*;
39use crate::ptr::P;
40use crate::token::{self, CommentKind, Delimiter};
41use crate::tokenstream::{DelimSpan, LazyAttrTokenStream, TokenStream};
42use crate::util::parser::{ExprPrecedence, Fixity};
43
44/// A "Label" is an identifier of some point in sources,
45/// e.g. in the following code:
46///
47/// ```rust
48/// 'outer: loop {
49///     break 'outer;
50/// }
51/// ```
52///
53/// `'outer` is a label.
54#[derive(Clone, Encodable, Decodable, Copy, HashStable_Generic, Eq, PartialEq)]
55pub struct Label {
56    pub ident: Ident,
57}
58
59impl fmt::Debug for Label {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        write!(f, "label({:?})", self.ident)
62    }
63}
64
65/// A "Lifetime" is an annotation of the scope in which variable
66/// can be used, e.g. `'a` in `&'a i32`.
67#[derive(Clone, Encodable, Decodable, Copy, PartialEq, Eq, Hash)]
68pub struct Lifetime {
69    pub id: NodeId,
70    pub ident: Ident,
71}
72
73impl fmt::Debug for Lifetime {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        write!(f, "lifetime({}: {})", self.id, self)
76    }
77}
78
79impl fmt::Display for Lifetime {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        write!(f, "{}", self.ident.name)
82    }
83}
84
85/// A "Path" is essentially Rust's notion of a name.
86///
87/// It's represented as a sequence of identifiers,
88/// along with a bunch of supporting information.
89///
90/// E.g., `std::cmp::PartialEq`.
91#[derive(Clone, Encodable, Decodable, Debug)]
92pub struct Path {
93    pub span: Span,
94    /// The segments in the path: the things separated by `::`.
95    /// Global paths begin with `kw::PathRoot`.
96    pub segments: ThinVec<PathSegment>,
97    pub tokens: Option<LazyAttrTokenStream>,
98}
99
100impl PartialEq<Symbol> for Path {
101    #[inline]
102    fn eq(&self, symbol: &Symbol) -> bool {
103        matches!(&self.segments[..], [segment] if segment.ident.name == *symbol)
104    }
105}
106
107impl<CTX: rustc_span::HashStableContext> HashStable<CTX> for Path {
108    fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
109        self.segments.len().hash_stable(hcx, hasher);
110        for segment in &self.segments {
111            segment.ident.hash_stable(hcx, hasher);
112        }
113    }
114}
115
116impl Path {
117    /// Convert a span and an identifier to the corresponding
118    /// one-segment path.
119    pub fn from_ident(ident: Ident) -> Path {
120        Path { segments: thin_vec![PathSegment::from_ident(ident)], span: ident.span, tokens: None }
121    }
122
123    pub fn is_ident(&self, name: Symbol) -> bool {
124        if let [segment] = self.segments.as_ref()
125            && segment.args.is_none()
126            && segment.ident.name == name
127        {
128            true
129        } else {
130            false
131        }
132    }
133
134    pub fn is_global(&self) -> bool {
135        self.segments.first().is_some_and(|segment| segment.ident.name == kw::PathRoot)
136    }
137
138    /// Check if this path is potentially a trivial const arg, i.e., one that can _potentially_
139    /// be represented without an anon const in the HIR.
140    ///
141    /// If `allow_mgca_arg` is true (as should be the case in most situations when
142    /// `#![feature(min_generic_const_args)]` is enabled), then this always returns true
143    /// because all paths are valid.
144    ///
145    /// Otherwise, it returns true iff the path has exactly one segment, and it has no generic args
146    /// (i.e., it is _potentially_ a const parameter).
147    #[tracing::instrument(level = "debug", ret)]
148    pub fn is_potential_trivial_const_arg(&self, allow_mgca_arg: bool) -> bool {
149        allow_mgca_arg
150            || self.segments.len() == 1 && self.segments.iter().all(|seg| seg.args.is_none())
151    }
152}
153
154/// A segment of a path: an identifier, an optional lifetime, and a set of types.
155///
156/// E.g., `std`, `String` or `Box<T>`.
157#[derive(Clone, Encodable, Decodable, Debug)]
158pub struct PathSegment {
159    /// The identifier portion of this path segment.
160    pub ident: Ident,
161
162    pub id: NodeId,
163
164    /// Type/lifetime parameters attached to this path. They come in
165    /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`.
166    /// `None` means that no parameter list is supplied (`Path`),
167    /// `Some` means that parameter list is supplied (`Path<X, Y>`)
168    /// but it can be empty (`Path<>`).
169    /// `P` is used as a size optimization for the common case with no parameters.
170    pub args: Option<P<GenericArgs>>,
171}
172
173impl PathSegment {
174    pub fn from_ident(ident: Ident) -> Self {
175        PathSegment { ident, id: DUMMY_NODE_ID, args: None }
176    }
177
178    pub fn path_root(span: Span) -> Self {
179        PathSegment::from_ident(Ident::new(kw::PathRoot, span))
180    }
181
182    pub fn span(&self) -> Span {
183        match &self.args {
184            Some(args) => self.ident.span.to(args.span()),
185            None => self.ident.span,
186        }
187    }
188}
189
190/// The generic arguments and associated item constraints of a path segment.
191///
192/// E.g., `<A, B>` as in `Foo<A, B>` or `(A, B)` as in `Foo(A, B)`.
193#[derive(Clone, Encodable, Decodable, Debug)]
194pub enum GenericArgs {
195    /// The `<'a, A, B, C>` in `foo::bar::baz::<'a, A, B, C>`.
196    AngleBracketed(AngleBracketedArgs),
197    /// The `(A, B)` and `C` in `Foo(A, B) -> C`.
198    Parenthesized(ParenthesizedArgs),
199    /// `(..)` in return type notation.
200    ParenthesizedElided(Span),
201}
202
203impl GenericArgs {
204    pub fn is_angle_bracketed(&self) -> bool {
205        matches!(self, AngleBracketed(..))
206    }
207
208    pub fn span(&self) -> Span {
209        match self {
210            AngleBracketed(data) => data.span,
211            Parenthesized(data) => data.span,
212            ParenthesizedElided(span) => *span,
213        }
214    }
215}
216
217/// Concrete argument in the sequence of generic args.
218#[derive(Clone, Encodable, Decodable, Debug)]
219pub enum GenericArg {
220    /// `'a` in `Foo<'a>`.
221    Lifetime(Lifetime),
222    /// `Bar` in `Foo<Bar>`.
223    Type(P<Ty>),
224    /// `1` in `Foo<1>`.
225    Const(AnonConst),
226}
227
228impl GenericArg {
229    pub fn span(&self) -> Span {
230        match self {
231            GenericArg::Lifetime(lt) => lt.ident.span,
232            GenericArg::Type(ty) => ty.span,
233            GenericArg::Const(ct) => ct.value.span,
234        }
235    }
236}
237
238/// A path like `Foo<'a, T>`.
239#[derive(Clone, Encodable, Decodable, Debug, Default)]
240pub struct AngleBracketedArgs {
241    /// The overall span.
242    pub span: Span,
243    /// The comma separated parts in the `<...>`.
244    pub args: ThinVec<AngleBracketedArg>,
245}
246
247/// Either an argument for a generic parameter or a constraint on an associated item.
248#[derive(Clone, Encodable, Decodable, Debug)]
249pub enum AngleBracketedArg {
250    /// A generic argument for a generic parameter.
251    Arg(GenericArg),
252    /// A constraint on an associated item.
253    Constraint(AssocItemConstraint),
254}
255
256impl AngleBracketedArg {
257    pub fn span(&self) -> Span {
258        match self {
259            AngleBracketedArg::Arg(arg) => arg.span(),
260            AngleBracketedArg::Constraint(constraint) => constraint.span,
261        }
262    }
263}
264
265impl From<AngleBracketedArgs> for P<GenericArgs> {
266    fn from(val: AngleBracketedArgs) -> Self {
267        P(GenericArgs::AngleBracketed(val))
268    }
269}
270
271impl From<ParenthesizedArgs> for P<GenericArgs> {
272    fn from(val: ParenthesizedArgs) -> Self {
273        P(GenericArgs::Parenthesized(val))
274    }
275}
276
277/// A path like `Foo(A, B) -> C`.
278#[derive(Clone, Encodable, Decodable, Debug)]
279pub struct ParenthesizedArgs {
280    /// ```text
281    /// Foo(A, B) -> C
282    /// ^^^^^^^^^^^^^^
283    /// ```
284    pub span: Span,
285
286    /// `(A, B)`
287    pub inputs: ThinVec<P<Ty>>,
288
289    /// ```text
290    /// Foo(A, B) -> C
291    ///    ^^^^^^
292    /// ```
293    pub inputs_span: Span,
294
295    /// `C`
296    pub output: FnRetTy,
297}
298
299impl ParenthesizedArgs {
300    pub fn as_angle_bracketed_args(&self) -> AngleBracketedArgs {
301        let args = self
302            .inputs
303            .iter()
304            .cloned()
305            .map(|input| AngleBracketedArg::Arg(GenericArg::Type(input)))
306            .collect();
307        AngleBracketedArgs { span: self.inputs_span, args }
308    }
309}
310
311pub use crate::node_id::{CRATE_NODE_ID, DUMMY_NODE_ID, NodeId};
312
313/// Modifiers on a trait bound like `~const`, `?` and `!`.
314#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug)]
315pub struct TraitBoundModifiers {
316    pub constness: BoundConstness,
317    pub asyncness: BoundAsyncness,
318    pub polarity: BoundPolarity,
319}
320
321impl TraitBoundModifiers {
322    pub const NONE: Self = Self {
323        constness: BoundConstness::Never,
324        asyncness: BoundAsyncness::Normal,
325        polarity: BoundPolarity::Positive,
326    };
327}
328
329#[derive(Clone, Encodable, Decodable, Debug)]
330pub enum GenericBound {
331    Trait(PolyTraitRef),
332    Outlives(Lifetime),
333    /// Precise capturing syntax: `impl Sized + use<'a>`
334    Use(ThinVec<PreciseCapturingArg>, Span),
335}
336
337impl GenericBound {
338    pub fn span(&self) -> Span {
339        match self {
340            GenericBound::Trait(t, ..) => t.span,
341            GenericBound::Outlives(l) => l.ident.span,
342            GenericBound::Use(_, span) => *span,
343        }
344    }
345}
346
347pub type GenericBounds = Vec<GenericBound>;
348
349/// Specifies the enforced ordering for generic parameters. In the future,
350/// if we wanted to relax this order, we could override `PartialEq` and
351/// `PartialOrd`, to allow the kinds to be unordered.
352#[derive(Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
353pub enum ParamKindOrd {
354    Lifetime,
355    TypeOrConst,
356}
357
358impl fmt::Display for ParamKindOrd {
359    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
360        match self {
361            ParamKindOrd::Lifetime => "lifetime".fmt(f),
362            ParamKindOrd::TypeOrConst => "type and const".fmt(f),
363        }
364    }
365}
366
367#[derive(Clone, Encodable, Decodable, Debug)]
368pub enum GenericParamKind {
369    /// A lifetime definition (e.g., `'a: 'b + 'c + 'd`).
370    Lifetime,
371    Type {
372        default: Option<P<Ty>>,
373    },
374    Const {
375        ty: P<Ty>,
376        /// Span of the `const` keyword.
377        kw_span: Span,
378        /// Optional default value for the const generic param.
379        default: Option<AnonConst>,
380    },
381}
382
383#[derive(Clone, Encodable, Decodable, Debug)]
384pub struct GenericParam {
385    pub id: NodeId,
386    pub ident: Ident,
387    pub attrs: AttrVec,
388    pub bounds: GenericBounds,
389    pub is_placeholder: bool,
390    pub kind: GenericParamKind,
391    pub colon_span: Option<Span>,
392}
393
394impl GenericParam {
395    pub fn span(&self) -> Span {
396        match &self.kind {
397            GenericParamKind::Lifetime | GenericParamKind::Type { default: None } => {
398                self.ident.span
399            }
400            GenericParamKind::Type { default: Some(ty) } => self.ident.span.to(ty.span),
401            GenericParamKind::Const { kw_span, default: Some(default), .. } => {
402                kw_span.to(default.value.span)
403            }
404            GenericParamKind::Const { kw_span, default: None, ty } => kw_span.to(ty.span),
405        }
406    }
407}
408
409/// Represents lifetime, type and const parameters attached to a declaration of
410/// a function, enum, trait, etc.
411#[derive(Clone, Encodable, Decodable, Debug, Default)]
412pub struct Generics {
413    pub params: ThinVec<GenericParam>,
414    pub where_clause: WhereClause,
415    pub span: Span,
416}
417
418/// A where-clause in a definition.
419#[derive(Clone, Encodable, Decodable, Debug, Default)]
420pub struct WhereClause {
421    /// `true` if we ate a `where` token.
422    ///
423    /// This can happen if we parsed no predicates, e.g., `struct Foo where {}`.
424    /// This allows us to pretty-print accurately and provide correct suggestion diagnostics.
425    pub has_where_token: bool,
426    pub predicates: ThinVec<WherePredicate>,
427    pub span: Span,
428}
429
430impl WhereClause {
431    pub fn is_empty(&self) -> bool {
432        !self.has_where_token && self.predicates.is_empty()
433    }
434}
435
436/// A single predicate in a where-clause.
437#[derive(Clone, Encodable, Decodable, Debug)]
438pub struct WherePredicate {
439    pub attrs: AttrVec,
440    pub kind: WherePredicateKind,
441    pub id: NodeId,
442    pub span: Span,
443    pub is_placeholder: bool,
444}
445
446/// Predicate kind in where-clause.
447#[derive(Clone, Encodable, Decodable, Debug)]
448pub enum WherePredicateKind {
449    /// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
450    BoundPredicate(WhereBoundPredicate),
451    /// A lifetime predicate (e.g., `'a: 'b + 'c`).
452    RegionPredicate(WhereRegionPredicate),
453    /// An equality predicate (unsupported).
454    EqPredicate(WhereEqPredicate),
455}
456
457/// A type bound.
458///
459/// E.g., `for<'c> Foo: Send + Clone + 'c`.
460#[derive(Clone, Encodable, Decodable, Debug)]
461pub struct WhereBoundPredicate {
462    /// Any generics from a `for` binding.
463    pub bound_generic_params: ThinVec<GenericParam>,
464    /// The type being bounded.
465    pub bounded_ty: P<Ty>,
466    /// Trait and lifetime bounds (`Clone + Send + 'static`).
467    pub bounds: GenericBounds,
468}
469
470/// A lifetime predicate.
471///
472/// E.g., `'a: 'b + 'c`.
473#[derive(Clone, Encodable, Decodable, Debug)]
474pub struct WhereRegionPredicate {
475    pub lifetime: Lifetime,
476    pub bounds: GenericBounds,
477}
478
479/// An equality predicate (unsupported).
480///
481/// E.g., `T = int`.
482#[derive(Clone, Encodable, Decodable, Debug)]
483pub struct WhereEqPredicate {
484    pub lhs_ty: P<Ty>,
485    pub rhs_ty: P<Ty>,
486}
487
488#[derive(Clone, Encodable, Decodable, Debug)]
489pub struct Crate {
490    pub attrs: AttrVec,
491    pub items: ThinVec<P<Item>>,
492    pub spans: ModSpans,
493    /// Must be equal to `CRATE_NODE_ID` after the crate root is expanded, but may hold
494    /// expansion placeholders or an unassigned value (`DUMMY_NODE_ID`) before that.
495    pub id: NodeId,
496    pub is_placeholder: bool,
497}
498
499/// A semantic representation of a meta item. A meta item is a slightly
500/// restricted form of an attribute -- it can only contain expressions in
501/// certain leaf positions, rather than arbitrary token streams -- that is used
502/// for most built-in attributes.
503///
504/// E.g., `#[test]`, `#[derive(..)]`, `#[rustfmt::skip]` or `#[feature = "foo"]`.
505#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
506pub struct MetaItem {
507    pub unsafety: Safety,
508    pub path: Path,
509    pub kind: MetaItemKind,
510    pub span: Span,
511}
512
513/// The meta item kind, containing the data after the initial path.
514#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
515pub enum MetaItemKind {
516    /// Word meta item.
517    ///
518    /// E.g., `#[test]`, which lacks any arguments after `test`.
519    Word,
520
521    /// List meta item.
522    ///
523    /// E.g., `#[derive(..)]`, where the field represents the `..`.
524    List(ThinVec<MetaItemInner>),
525
526    /// Name value meta item.
527    ///
528    /// E.g., `#[feature = "foo"]`, where the field represents the `"foo"`.
529    NameValue(MetaItemLit),
530}
531
532/// Values inside meta item lists.
533///
534/// E.g., each of `Clone`, `Copy` in `#[derive(Clone, Copy)]`.
535#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
536pub enum MetaItemInner {
537    /// A full MetaItem, for recursive meta items.
538    MetaItem(MetaItem),
539
540    /// A literal.
541    ///
542    /// E.g., `"foo"`, `64`, `true`.
543    Lit(MetaItemLit),
544}
545
546/// A block (`{ .. }`).
547///
548/// E.g., `{ .. }` as in `fn foo() { .. }`.
549#[derive(Clone, Encodable, Decodable, Debug)]
550pub struct Block {
551    /// The statements in the block.
552    pub stmts: ThinVec<Stmt>,
553    pub id: NodeId,
554    /// Distinguishes between `unsafe { ... }` and `{ ... }`.
555    pub rules: BlockCheckMode,
556    pub span: Span,
557    pub tokens: Option<LazyAttrTokenStream>,
558}
559
560/// A match pattern.
561///
562/// Patterns appear in match statements and some other contexts, such as `let` and `if let`.
563#[derive(Clone, Encodable, Decodable, Debug)]
564pub struct Pat {
565    pub id: NodeId,
566    pub kind: PatKind,
567    pub span: Span,
568    pub tokens: Option<LazyAttrTokenStream>,
569}
570
571impl Pat {
572    /// Attempt reparsing the pattern as a type.
573    /// This is intended for use by diagnostics.
574    pub fn to_ty(&self) -> Option<P<Ty>> {
575        let kind = match &self.kind {
576            PatKind::Missing => unreachable!(),
577            // In a type expression `_` is an inference variable.
578            PatKind::Wild => TyKind::Infer,
579            // An IDENT pattern with no binding mode would be valid as path to a type. E.g. `u32`.
580            PatKind::Ident(BindingMode::NONE, ident, None) => {
581                TyKind::Path(None, Path::from_ident(*ident))
582            }
583            PatKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
584            PatKind::MacCall(mac) => TyKind::MacCall(mac.clone()),
585            // `&mut? P` can be reinterpreted as `&mut? T` where `T` is `P` reparsed as a type.
586            PatKind::Ref(pat, mutbl) => {
587                pat.to_ty().map(|ty| TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }))?
588            }
589            // A slice/array pattern `[P]` can be reparsed as `[T]`, an unsized array,
590            // when `P` can be reparsed as a type `T`.
591            PatKind::Slice(pats) if let [pat] = pats.as_slice() => {
592                pat.to_ty().map(TyKind::Slice)?
593            }
594            // A tuple pattern `(P0, .., Pn)` can be reparsed as `(T0, .., Tn)`
595            // assuming `T0` to `Tn` are all syntactically valid as types.
596            PatKind::Tuple(pats) => {
597                let mut tys = ThinVec::with_capacity(pats.len());
598                // FIXME(#48994) - could just be collected into an Option<Vec>
599                for pat in pats {
600                    tys.push(pat.to_ty()?);
601                }
602                TyKind::Tup(tys)
603            }
604            _ => return None,
605        };
606
607        Some(P(Ty { kind, id: self.id, span: self.span, tokens: None }))
608    }
609
610    /// Walk top-down and call `it` in each place where a pattern occurs
611    /// starting with the root pattern `walk` is called on. If `it` returns
612    /// false then we will descend no further but siblings will be processed.
613    pub fn walk<'ast>(&'ast self, it: &mut impl FnMut(&'ast Pat) -> bool) {
614        if !it(self) {
615            return;
616        }
617
618        match &self.kind {
619            // Walk into the pattern associated with `Ident` (if any).
620            PatKind::Ident(_, _, Some(p)) => p.walk(it),
621
622            // Walk into each field of struct.
623            PatKind::Struct(_, _, fields, _) => fields.iter().for_each(|field| field.pat.walk(it)),
624
625            // Sequence of patterns.
626            PatKind::TupleStruct(_, _, s)
627            | PatKind::Tuple(s)
628            | PatKind::Slice(s)
629            | PatKind::Or(s) => s.iter().for_each(|p| p.walk(it)),
630
631            // Trivial wrappers over inner patterns.
632            PatKind::Box(s)
633            | PatKind::Deref(s)
634            | PatKind::Ref(s, _)
635            | PatKind::Paren(s)
636            | PatKind::Guard(s, _) => s.walk(it),
637
638            // These patterns do not contain subpatterns, skip.
639            PatKind::Missing
640            | PatKind::Wild
641            | PatKind::Rest
642            | PatKind::Never
643            | PatKind::Expr(_)
644            | PatKind::Range(..)
645            | PatKind::Ident(..)
646            | PatKind::Path(..)
647            | PatKind::MacCall(_)
648            | PatKind::Err(_) => {}
649        }
650    }
651
652    /// Is this a `..` pattern?
653    pub fn is_rest(&self) -> bool {
654        matches!(self.kind, PatKind::Rest)
655    }
656
657    /// Whether this could be a never pattern, taking into account that a macro invocation can
658    /// return a never pattern. Used to inform errors during parsing.
659    pub fn could_be_never_pattern(&self) -> bool {
660        let mut could_be_never_pattern = false;
661        self.walk(&mut |pat| match &pat.kind {
662            PatKind::Never | PatKind::MacCall(_) => {
663                could_be_never_pattern = true;
664                false
665            }
666            PatKind::Or(s) => {
667                could_be_never_pattern = s.iter().all(|p| p.could_be_never_pattern());
668                false
669            }
670            _ => true,
671        });
672        could_be_never_pattern
673    }
674
675    /// Whether this contains a `!` pattern. This in particular means that a feature gate error will
676    /// be raised if the feature is off. Used to avoid gating the feature twice.
677    pub fn contains_never_pattern(&self) -> bool {
678        let mut contains_never_pattern = false;
679        self.walk(&mut |pat| {
680            if matches!(pat.kind, PatKind::Never) {
681                contains_never_pattern = true;
682            }
683            true
684        });
685        contains_never_pattern
686    }
687
688    /// Return a name suitable for diagnostics.
689    pub fn descr(&self) -> Option<String> {
690        match &self.kind {
691            PatKind::Missing => unreachable!(),
692            PatKind::Wild => Some("_".to_string()),
693            PatKind::Ident(BindingMode::NONE, ident, None) => Some(format!("{ident}")),
694            PatKind::Ref(pat, mutbl) => pat.descr().map(|d| format!("&{}{d}", mutbl.prefix_str())),
695            _ => None,
696        }
697    }
698}
699
700/// A single field in a struct pattern.
701///
702/// Patterns like the fields of `Foo { x, ref y, ref mut z }`
703/// are treated the same as `x: x, y: ref y, z: ref mut z`,
704/// except when `is_shorthand` is true.
705#[derive(Clone, Encodable, Decodable, Debug)]
706pub struct PatField {
707    /// The identifier for the field.
708    pub ident: Ident,
709    /// The pattern the field is destructured to.
710    pub pat: P<Pat>,
711    pub is_shorthand: bool,
712    pub attrs: AttrVec,
713    pub id: NodeId,
714    pub span: Span,
715    pub is_placeholder: bool,
716}
717
718#[derive(Clone, Copy, Debug, Eq, PartialEq)]
719#[derive(Encodable, Decodable, HashStable_Generic)]
720pub enum ByRef {
721    Yes(Mutability),
722    No,
723}
724
725impl ByRef {
726    #[must_use]
727    pub fn cap_ref_mutability(mut self, mutbl: Mutability) -> Self {
728        if let ByRef::Yes(old_mutbl) = &mut self {
729            *old_mutbl = cmp::min(*old_mutbl, mutbl);
730        }
731        self
732    }
733}
734
735/// The mode of a binding (`mut`, `ref mut`, etc).
736/// Used for both the explicit binding annotations given in the HIR for a binding
737/// and the final binding mode that we infer after type inference/match ergonomics.
738/// `.0` is the by-reference mode (`ref`, `ref mut`, or by value),
739/// `.1` is the mutability of the binding.
740#[derive(Clone, Copy, Debug, Eq, PartialEq)]
741#[derive(Encodable, Decodable, HashStable_Generic)]
742pub struct BindingMode(pub ByRef, pub Mutability);
743
744impl BindingMode {
745    pub const NONE: Self = Self(ByRef::No, Mutability::Not);
746    pub const REF: Self = Self(ByRef::Yes(Mutability::Not), Mutability::Not);
747    pub const MUT: Self = Self(ByRef::No, Mutability::Mut);
748    pub const REF_MUT: Self = Self(ByRef::Yes(Mutability::Mut), Mutability::Not);
749    pub const MUT_REF: Self = Self(ByRef::Yes(Mutability::Not), Mutability::Mut);
750    pub const MUT_REF_MUT: Self = Self(ByRef::Yes(Mutability::Mut), Mutability::Mut);
751
752    pub fn prefix_str(self) -> &'static str {
753        match self {
754            Self::NONE => "",
755            Self::REF => "ref ",
756            Self::MUT => "mut ",
757            Self::REF_MUT => "ref mut ",
758            Self::MUT_REF => "mut ref ",
759            Self::MUT_REF_MUT => "mut ref mut ",
760        }
761    }
762}
763
764#[derive(Clone, Encodable, Decodable, Debug)]
765pub enum RangeEnd {
766    /// `..=` or `...`
767    Included(RangeSyntax),
768    /// `..`
769    Excluded,
770}
771
772#[derive(Clone, Encodable, Decodable, Debug)]
773pub enum RangeSyntax {
774    /// `...`
775    DotDotDot,
776    /// `..=`
777    DotDotEq,
778}
779
780/// All the different flavors of pattern that Rust recognizes.
781//
782// Adding a new variant? Please update `test_pat` in `tests/ui/macros/stringify.rs`.
783#[derive(Clone, Encodable, Decodable, Debug)]
784pub enum PatKind {
785    /// A missing pattern, e.g. for an anonymous param in a bare fn like `fn f(u32)`.
786    Missing,
787
788    /// Represents a wildcard pattern (`_`).
789    Wild,
790
791    /// A `PatKind::Ident` may either be a new bound variable (`ref mut binding @ OPT_SUBPATTERN`),
792    /// or a unit struct/variant pattern, or a const pattern (in the last two cases the third
793    /// field must be `None`). Disambiguation cannot be done with parser alone, so it happens
794    /// during name resolution.
795    Ident(BindingMode, Ident, Option<P<Pat>>),
796
797    /// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
798    Struct(Option<P<QSelf>>, Path, ThinVec<PatField>, PatFieldsRest),
799
800    /// A tuple struct/variant pattern (`Variant(x, y, .., z)`).
801    TupleStruct(Option<P<QSelf>>, Path, ThinVec<P<Pat>>),
802
803    /// An or-pattern `A | B | C`.
804    /// Invariant: `pats.len() >= 2`.
805    Or(ThinVec<P<Pat>>),
806
807    /// A possibly qualified path pattern.
808    /// Unqualified path patterns `A::B::C` can legally refer to variants, structs, constants
809    /// or associated constants. Qualified path patterns `<A>::B::C`/`<A as Trait>::B::C` can
810    /// only legally refer to associated constants.
811    Path(Option<P<QSelf>>, Path),
812
813    /// A tuple pattern (`(a, b)`).
814    Tuple(ThinVec<P<Pat>>),
815
816    /// A `box` pattern.
817    Box(P<Pat>),
818
819    /// A `deref` pattern (currently `deref!()` macro-based syntax).
820    Deref(P<Pat>),
821
822    /// A reference pattern (e.g., `&mut (a, b)`).
823    Ref(P<Pat>, Mutability),
824
825    /// A literal, const block or path.
826    Expr(P<Expr>),
827
828    /// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
829    Range(Option<P<Expr>>, Option<P<Expr>>, Spanned<RangeEnd>),
830
831    /// A slice pattern `[a, b, c]`.
832    Slice(ThinVec<P<Pat>>),
833
834    /// A rest pattern `..`.
835    ///
836    /// Syntactically it is valid anywhere.
837    ///
838    /// Semantically however, it only has meaning immediately inside:
839    /// - a slice pattern: `[a, .., b]`,
840    /// - a binding pattern immediately inside a slice pattern: `[a, r @ ..]`,
841    /// - a tuple pattern: `(a, .., b)`,
842    /// - a tuple struct/variant pattern: `$path(a, .., b)`.
843    ///
844    /// In all of these cases, an additional restriction applies,
845    /// only one rest pattern may occur in the pattern sequences.
846    Rest,
847
848    // A never pattern `!`.
849    Never,
850
851    /// A guard pattern (e.g., `x if guard(x)`).
852    Guard(P<Pat>, P<Expr>),
853
854    /// Parentheses in patterns used for grouping (i.e., `(PAT)`).
855    Paren(P<Pat>),
856
857    /// A macro pattern; pre-expansion.
858    MacCall(P<MacCall>),
859
860    /// Placeholder for a pattern that wasn't syntactically well formed in some way.
861    Err(ErrorGuaranteed),
862}
863
864/// Whether the `..` is present in a struct fields pattern.
865#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq)]
866pub enum PatFieldsRest {
867    /// `module::StructName { field, ..}`
868    Rest,
869    /// `module::StructName { field, syntax error }`
870    Recovered(ErrorGuaranteed),
871    /// `module::StructName { field }`
872    None,
873}
874
875/// The kind of borrow in an `AddrOf` expression,
876/// e.g., `&place` or `&raw const place`.
877#[derive(Clone, Copy, PartialEq, Eq, Debug)]
878#[derive(Encodable, Decodable, HashStable_Generic)]
879pub enum BorrowKind {
880    /// A normal borrow, `&$expr` or `&mut $expr`.
881    /// The resulting type is either `&'a T` or `&'a mut T`
882    /// where `T = typeof($expr)` and `'a` is some lifetime.
883    Ref,
884    /// A raw borrow, `&raw const $expr` or `&raw mut $expr`.
885    /// The resulting type is either `*const T` or `*mut T`
886    /// where `T = typeof($expr)`.
887    Raw,
888}
889
890#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
891pub enum BinOpKind {
892    /// The `+` operator (addition)
893    Add,
894    /// The `-` operator (subtraction)
895    Sub,
896    /// The `*` operator (multiplication)
897    Mul,
898    /// The `/` operator (division)
899    Div,
900    /// The `%` operator (modulus)
901    Rem,
902    /// The `&&` operator (logical and)
903    And,
904    /// The `||` operator (logical or)
905    Or,
906    /// The `^` operator (bitwise xor)
907    BitXor,
908    /// The `&` operator (bitwise and)
909    BitAnd,
910    /// The `|` operator (bitwise or)
911    BitOr,
912    /// The `<<` operator (shift left)
913    Shl,
914    /// The `>>` operator (shift right)
915    Shr,
916    /// The `==` operator (equality)
917    Eq,
918    /// The `<` operator (less than)
919    Lt,
920    /// The `<=` operator (less than or equal to)
921    Le,
922    /// The `!=` operator (not equal to)
923    Ne,
924    /// The `>=` operator (greater than or equal to)
925    Ge,
926    /// The `>` operator (greater than)
927    Gt,
928}
929
930impl BinOpKind {
931    pub fn as_str(&self) -> &'static str {
932        use BinOpKind::*;
933        match self {
934            Add => "+",
935            Sub => "-",
936            Mul => "*",
937            Div => "/",
938            Rem => "%",
939            And => "&&",
940            Or => "||",
941            BitXor => "^",
942            BitAnd => "&",
943            BitOr => "|",
944            Shl => "<<",
945            Shr => ">>",
946            Eq => "==",
947            Lt => "<",
948            Le => "<=",
949            Ne => "!=",
950            Ge => ">=",
951            Gt => ">",
952        }
953    }
954
955    pub fn is_lazy(&self) -> bool {
956        matches!(self, BinOpKind::And | BinOpKind::Or)
957    }
958
959    pub fn precedence(&self) -> ExprPrecedence {
960        use BinOpKind::*;
961        match *self {
962            Mul | Div | Rem => ExprPrecedence::Product,
963            Add | Sub => ExprPrecedence::Sum,
964            Shl | Shr => ExprPrecedence::Shift,
965            BitAnd => ExprPrecedence::BitAnd,
966            BitXor => ExprPrecedence::BitXor,
967            BitOr => ExprPrecedence::BitOr,
968            Lt | Gt | Le | Ge | Eq | Ne => ExprPrecedence::Compare,
969            And => ExprPrecedence::LAnd,
970            Or => ExprPrecedence::LOr,
971        }
972    }
973
974    pub fn fixity(&self) -> Fixity {
975        use BinOpKind::*;
976        match self {
977            Eq | Ne | Lt | Le | Gt | Ge => Fixity::None,
978            Add | Sub | Mul | Div | Rem | And | Or | BitXor | BitAnd | BitOr | Shl | Shr => {
979                Fixity::Left
980            }
981        }
982    }
983
984    pub fn is_comparison(self) -> bool {
985        use BinOpKind::*;
986        match self {
987            Eq | Ne | Lt | Le | Gt | Ge => true,
988            Add | Sub | Mul | Div | Rem | And | Or | BitXor | BitAnd | BitOr | Shl | Shr => false,
989        }
990    }
991
992    /// Returns `true` if the binary operator takes its arguments by value.
993    pub fn is_by_value(self) -> bool {
994        !self.is_comparison()
995    }
996}
997
998pub type BinOp = Spanned<BinOpKind>;
999
1000// Sometimes `BinOpKind` and `AssignOpKind` need the same treatment. The
1001// operations covered by `AssignOpKind` are a subset of those covered by
1002// `BinOpKind`, so it makes sense to convert `AssignOpKind` to `BinOpKind`.
1003impl From<AssignOpKind> for BinOpKind {
1004    fn from(op: AssignOpKind) -> BinOpKind {
1005        match op {
1006            AssignOpKind::AddAssign => BinOpKind::Add,
1007            AssignOpKind::SubAssign => BinOpKind::Sub,
1008            AssignOpKind::MulAssign => BinOpKind::Mul,
1009            AssignOpKind::DivAssign => BinOpKind::Div,
1010            AssignOpKind::RemAssign => BinOpKind::Rem,
1011            AssignOpKind::BitXorAssign => BinOpKind::BitXor,
1012            AssignOpKind::BitAndAssign => BinOpKind::BitAnd,
1013            AssignOpKind::BitOrAssign => BinOpKind::BitOr,
1014            AssignOpKind::ShlAssign => BinOpKind::Shl,
1015            AssignOpKind::ShrAssign => BinOpKind::Shr,
1016        }
1017    }
1018}
1019
1020#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
1021pub enum AssignOpKind {
1022    /// The `+=` operator (addition)
1023    AddAssign,
1024    /// The `-=` operator (subtraction)
1025    SubAssign,
1026    /// The `*=` operator (multiplication)
1027    MulAssign,
1028    /// The `/=` operator (division)
1029    DivAssign,
1030    /// The `%=` operator (modulus)
1031    RemAssign,
1032    /// The `^=` operator (bitwise xor)
1033    BitXorAssign,
1034    /// The `&=` operator (bitwise and)
1035    BitAndAssign,
1036    /// The `|=` operator (bitwise or)
1037    BitOrAssign,
1038    /// The `<<=` operator (shift left)
1039    ShlAssign,
1040    /// The `>>=` operator (shift right)
1041    ShrAssign,
1042}
1043
1044impl AssignOpKind {
1045    pub fn as_str(&self) -> &'static str {
1046        use AssignOpKind::*;
1047        match self {
1048            AddAssign => "+=",
1049            SubAssign => "-=",
1050            MulAssign => "*=",
1051            DivAssign => "/=",
1052            RemAssign => "%=",
1053            BitXorAssign => "^=",
1054            BitAndAssign => "&=",
1055            BitOrAssign => "|=",
1056            ShlAssign => "<<=",
1057            ShrAssign => ">>=",
1058        }
1059    }
1060
1061    /// AssignOps are always by value.
1062    pub fn is_by_value(self) -> bool {
1063        true
1064    }
1065}
1066
1067pub type AssignOp = Spanned<AssignOpKind>;
1068
1069/// Unary operator.
1070///
1071/// Note that `&data` is not an operator, it's an `AddrOf` expression.
1072#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
1073pub enum UnOp {
1074    /// The `*` operator for dereferencing
1075    Deref,
1076    /// The `!` operator for logical inversion
1077    Not,
1078    /// The `-` operator for negation
1079    Neg,
1080}
1081
1082impl UnOp {
1083    pub fn as_str(&self) -> &'static str {
1084        match self {
1085            UnOp::Deref => "*",
1086            UnOp::Not => "!",
1087            UnOp::Neg => "-",
1088        }
1089    }
1090
1091    /// Returns `true` if the unary operator takes its argument by value.
1092    pub fn is_by_value(self) -> bool {
1093        matches!(self, Self::Neg | Self::Not)
1094    }
1095}
1096
1097/// A statement. No `attrs` or `tokens` fields because each `StmtKind` variant
1098/// contains an AST node with those fields. (Except for `StmtKind::Empty`,
1099/// which never has attrs or tokens)
1100#[derive(Clone, Encodable, Decodable, Debug)]
1101pub struct Stmt {
1102    pub id: NodeId,
1103    pub kind: StmtKind,
1104    pub span: Span,
1105}
1106
1107impl Stmt {
1108    pub fn has_trailing_semicolon(&self) -> bool {
1109        match &self.kind {
1110            StmtKind::Semi(_) => true,
1111            StmtKind::MacCall(mac) => matches!(mac.style, MacStmtStyle::Semicolon),
1112            _ => false,
1113        }
1114    }
1115
1116    /// Converts a parsed `Stmt` to a `Stmt` with
1117    /// a trailing semicolon.
1118    ///
1119    /// This only modifies the parsed AST struct, not the attached
1120    /// `LazyAttrTokenStream`. The parser is responsible for calling
1121    /// `ToAttrTokenStream::add_trailing_semi` when there is actually
1122    /// a semicolon in the tokenstream.
1123    pub fn add_trailing_semicolon(mut self) -> Self {
1124        self.kind = match self.kind {
1125            StmtKind::Expr(expr) => StmtKind::Semi(expr),
1126            StmtKind::MacCall(mac) => {
1127                StmtKind::MacCall(mac.map(|MacCallStmt { mac, style: _, attrs, tokens }| {
1128                    MacCallStmt { mac, style: MacStmtStyle::Semicolon, attrs, tokens }
1129                }))
1130            }
1131            kind => kind,
1132        };
1133
1134        self
1135    }
1136
1137    pub fn is_item(&self) -> bool {
1138        matches!(self.kind, StmtKind::Item(_))
1139    }
1140
1141    pub fn is_expr(&self) -> bool {
1142        matches!(self.kind, StmtKind::Expr(_))
1143    }
1144}
1145
1146// Adding a new variant? Please update `test_stmt` in `tests/ui/macros/stringify.rs`.
1147#[derive(Clone, Encodable, Decodable, Debug)]
1148pub enum StmtKind {
1149    /// A local (let) binding.
1150    Let(P<Local>),
1151    /// An item definition.
1152    Item(P<Item>),
1153    /// Expr without trailing semi-colon.
1154    Expr(P<Expr>),
1155    /// Expr with a trailing semi-colon.
1156    Semi(P<Expr>),
1157    /// Just a trailing semi-colon.
1158    Empty,
1159    /// Macro.
1160    MacCall(P<MacCallStmt>),
1161}
1162
1163#[derive(Clone, Encodable, Decodable, Debug)]
1164pub struct MacCallStmt {
1165    pub mac: P<MacCall>,
1166    pub style: MacStmtStyle,
1167    pub attrs: AttrVec,
1168    pub tokens: Option<LazyAttrTokenStream>,
1169}
1170
1171#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug)]
1172pub enum MacStmtStyle {
1173    /// The macro statement had a trailing semicolon (e.g., `foo! { ... };`
1174    /// `foo!(...);`, `foo![...];`).
1175    Semicolon,
1176    /// The macro statement had braces (e.g., `foo! { ... }`).
1177    Braces,
1178    /// The macro statement had parentheses or brackets and no semicolon (e.g.,
1179    /// `foo!(...)`). All of these will end up being converted into macro
1180    /// expressions.
1181    NoBraces,
1182}
1183
1184/// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`.
1185#[derive(Clone, Encodable, Decodable, Debug)]
1186pub struct Local {
1187    pub id: NodeId,
1188    pub super_: Option<Span>,
1189    pub pat: P<Pat>,
1190    pub ty: Option<P<Ty>>,
1191    pub kind: LocalKind,
1192    pub span: Span,
1193    pub colon_sp: Option<Span>,
1194    pub attrs: AttrVec,
1195    pub tokens: Option<LazyAttrTokenStream>,
1196}
1197
1198#[derive(Clone, Encodable, Decodable, Debug)]
1199pub enum LocalKind {
1200    /// Local declaration.
1201    /// Example: `let x;`
1202    Decl,
1203    /// Local declaration with an initializer.
1204    /// Example: `let x = y;`
1205    Init(P<Expr>),
1206    /// Local declaration with an initializer and an `else` clause.
1207    /// Example: `let Some(x) = y else { return };`
1208    InitElse(P<Expr>, P<Block>),
1209}
1210
1211impl LocalKind {
1212    pub fn init(&self) -> Option<&Expr> {
1213        match self {
1214            Self::Decl => None,
1215            Self::Init(i) | Self::InitElse(i, _) => Some(i),
1216        }
1217    }
1218
1219    pub fn init_else_opt(&self) -> Option<(&Expr, Option<&Block>)> {
1220        match self {
1221            Self::Decl => None,
1222            Self::Init(init) => Some((init, None)),
1223            Self::InitElse(init, els) => Some((init, Some(els))),
1224        }
1225    }
1226}
1227
1228/// An arm of a 'match'.
1229///
1230/// E.g., `0..=10 => { println!("match!") }` as in
1231///
1232/// ```
1233/// match 123 {
1234///     0..=10 => { println!("match!") },
1235///     _ => { println!("no match!") },
1236/// }
1237/// ```
1238#[derive(Clone, Encodable, Decodable, Debug)]
1239pub struct Arm {
1240    pub attrs: AttrVec,
1241    /// Match arm pattern, e.g. `10` in `match foo { 10 => {}, _ => {} }`.
1242    pub pat: P<Pat>,
1243    /// Match arm guard, e.g. `n > 10` in `match foo { n if n > 10 => {}, _ => {} }`.
1244    pub guard: Option<P<Expr>>,
1245    /// Match arm body. Omitted if the pattern is a never pattern.
1246    pub body: Option<P<Expr>>,
1247    pub span: Span,
1248    pub id: NodeId,
1249    pub is_placeholder: bool,
1250}
1251
1252/// A single field in a struct expression, e.g. `x: value` and `y` in `Foo { x: value, y }`.
1253#[derive(Clone, Encodable, Decodable, Debug)]
1254pub struct ExprField {
1255    pub attrs: AttrVec,
1256    pub id: NodeId,
1257    pub span: Span,
1258    pub ident: Ident,
1259    pub expr: P<Expr>,
1260    pub is_shorthand: bool,
1261    pub is_placeholder: bool,
1262}
1263
1264#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
1265pub enum BlockCheckMode {
1266    Default,
1267    Unsafe(UnsafeSource),
1268}
1269
1270#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy)]
1271pub enum UnsafeSource {
1272    CompilerGenerated,
1273    UserProvided,
1274}
1275
1276/// A constant (expression) that's not an item or associated item,
1277/// but needs its own `DefId` for type-checking, const-eval, etc.
1278/// These are usually found nested inside types (e.g., array lengths)
1279/// or expressions (e.g., repeat counts), and also used to define
1280/// explicit discriminant values for enum variants.
1281#[derive(Clone, Encodable, Decodable, Debug)]
1282pub struct AnonConst {
1283    pub id: NodeId,
1284    pub value: P<Expr>,
1285}
1286
1287/// An expression.
1288#[derive(Clone, Encodable, Decodable, Debug)]
1289pub struct Expr {
1290    pub id: NodeId,
1291    pub kind: ExprKind,
1292    pub span: Span,
1293    pub attrs: AttrVec,
1294    pub tokens: Option<LazyAttrTokenStream>,
1295}
1296
1297impl Expr {
1298    /// Check if this expression is potentially a trivial const arg, i.e., one that can _potentially_
1299    /// be represented without an anon const in the HIR.
1300    ///
1301    /// This will unwrap at most one block level (curly braces). After that, if the expression
1302    /// is a path, it mostly dispatches to [`Path::is_potential_trivial_const_arg`].
1303    /// See there for more info about `allow_mgca_arg`.
1304    ///
1305    /// The only additional thing to note is that when `allow_mgca_arg` is false, this function
1306    /// will only allow paths with no qself, before dispatching to the `Path` function of
1307    /// the same name.
1308    ///
1309    /// Does not ensure that the path resolves to a const param/item, the caller should check this.
1310    /// This also does not consider macros, so it's only correct after macro-expansion.
1311    pub fn is_potential_trivial_const_arg(&self, allow_mgca_arg: bool) -> bool {
1312        let this = self.maybe_unwrap_block();
1313        if allow_mgca_arg {
1314            matches!(this.kind, ExprKind::Path(..))
1315        } else {
1316            if let ExprKind::Path(None, path) = &this.kind
1317                && path.is_potential_trivial_const_arg(allow_mgca_arg)
1318            {
1319                true
1320            } else {
1321                false
1322            }
1323        }
1324    }
1325
1326    /// Returns an expression with (when possible) *one* outter brace removed
1327    pub fn maybe_unwrap_block(&self) -> &Expr {
1328        if let ExprKind::Block(block, None) = &self.kind
1329            && let [stmt] = block.stmts.as_slice()
1330            && let StmtKind::Expr(expr) = &stmt.kind
1331        {
1332            expr
1333        } else {
1334            self
1335        }
1336    }
1337
1338    /// Determines whether this expression is a macro call optionally wrapped in braces . If
1339    /// `already_stripped_block` is set then we do not attempt to peel off a layer of braces.
1340    ///
1341    /// Returns the [`NodeId`] of the macro call and whether a layer of braces has been peeled
1342    /// either before, or part of, this function.
1343    pub fn optionally_braced_mac_call(
1344        &self,
1345        already_stripped_block: bool,
1346    ) -> Option<(bool, NodeId)> {
1347        match &self.kind {
1348            ExprKind::Block(block, None)
1349                if let [stmt] = &*block.stmts
1350                    && !already_stripped_block =>
1351            {
1352                match &stmt.kind {
1353                    StmtKind::MacCall(_) => Some((true, stmt.id)),
1354                    StmtKind::Expr(expr) if let ExprKind::MacCall(_) = &expr.kind => {
1355                        Some((true, expr.id))
1356                    }
1357                    _ => None,
1358                }
1359            }
1360            ExprKind::MacCall(_) => Some((already_stripped_block, self.id)),
1361            _ => None,
1362        }
1363    }
1364
1365    pub fn to_bound(&self) -> Option<GenericBound> {
1366        match &self.kind {
1367            ExprKind::Path(None, path) => Some(GenericBound::Trait(PolyTraitRef::new(
1368                ThinVec::new(),
1369                path.clone(),
1370                TraitBoundModifiers::NONE,
1371                self.span,
1372            ))),
1373            _ => None,
1374        }
1375    }
1376
1377    pub fn peel_parens(&self) -> &Expr {
1378        let mut expr = self;
1379        while let ExprKind::Paren(inner) = &expr.kind {
1380            expr = inner;
1381        }
1382        expr
1383    }
1384
1385    pub fn peel_parens_and_refs(&self) -> &Expr {
1386        let mut expr = self;
1387        while let ExprKind::Paren(inner) | ExprKind::AddrOf(BorrowKind::Ref, _, inner) = &expr.kind
1388        {
1389            expr = inner;
1390        }
1391        expr
1392    }
1393
1394    /// Attempts to reparse as `Ty` (for diagnostic purposes).
1395    pub fn to_ty(&self) -> Option<P<Ty>> {
1396        let kind = match &self.kind {
1397            // Trivial conversions.
1398            ExprKind::Path(qself, path) => TyKind::Path(qself.clone(), path.clone()),
1399            ExprKind::MacCall(mac) => TyKind::MacCall(mac.clone()),
1400
1401            ExprKind::Paren(expr) => expr.to_ty().map(TyKind::Paren)?,
1402
1403            ExprKind::AddrOf(BorrowKind::Ref, mutbl, expr) => {
1404                expr.to_ty().map(|ty| TyKind::Ref(None, MutTy { ty, mutbl: *mutbl }))?
1405            }
1406
1407            ExprKind::Repeat(expr, expr_len) => {
1408                expr.to_ty().map(|ty| TyKind::Array(ty, expr_len.clone()))?
1409            }
1410
1411            ExprKind::Array(exprs) if let [expr] = exprs.as_slice() => {
1412                expr.to_ty().map(TyKind::Slice)?
1413            }
1414
1415            ExprKind::Tup(exprs) => {
1416                let tys = exprs.iter().map(|expr| expr.to_ty()).collect::<Option<ThinVec<_>>>()?;
1417                TyKind::Tup(tys)
1418            }
1419
1420            // If binary operator is `Add` and both `lhs` and `rhs` are trait bounds,
1421            // then type of result is trait object.
1422            // Otherwise we don't assume the result type.
1423            ExprKind::Binary(binop, lhs, rhs) if binop.node == BinOpKind::Add => {
1424                if let (Some(lhs), Some(rhs)) = (lhs.to_bound(), rhs.to_bound()) {
1425                    TyKind::TraitObject(vec![lhs, rhs], TraitObjectSyntax::None)
1426                } else {
1427                    return None;
1428                }
1429            }
1430
1431            ExprKind::Underscore => TyKind::Infer,
1432
1433            // This expression doesn't look like a type syntactically.
1434            _ => return None,
1435        };
1436
1437        Some(P(Ty { kind, id: self.id, span: self.span, tokens: None }))
1438    }
1439
1440    pub fn precedence(&self) -> ExprPrecedence {
1441        match &self.kind {
1442            ExprKind::Closure(closure) => {
1443                match closure.fn_decl.output {
1444                    FnRetTy::Default(_) => ExprPrecedence::Jump,
1445                    FnRetTy::Ty(_) => ExprPrecedence::Unambiguous,
1446                }
1447            }
1448
1449            ExprKind::Break(..)
1450            | ExprKind::Ret(..)
1451            | ExprKind::Yield(..)
1452            | ExprKind::Yeet(..)
1453            | ExprKind::Become(..) => ExprPrecedence::Jump,
1454
1455            // `Range` claims to have higher precedence than `Assign`, but `x .. x = x` fails to
1456            // parse, instead of parsing as `(x .. x) = x`. Giving `Range` a lower precedence
1457            // ensures that `pprust` will add parentheses in the right places to get the desired
1458            // parse.
1459            ExprKind::Range(..) => ExprPrecedence::Range,
1460
1461            // Binop-like expr kinds, handled by `AssocOp`.
1462            ExprKind::Binary(op, ..) => op.node.precedence(),
1463            ExprKind::Cast(..) => ExprPrecedence::Cast,
1464
1465            ExprKind::Assign(..) |
1466            ExprKind::AssignOp(..) => ExprPrecedence::Assign,
1467
1468            // Unary, prefix
1469            ExprKind::AddrOf(..)
1470            // Here `let pats = expr` has `let pats =` as a "unary" prefix of `expr`.
1471            // However, this is not exactly right. When `let _ = a` is the LHS of a binop we
1472            // need parens sometimes. E.g. we can print `(let _ = a) && b` as `let _ = a && b`
1473            // but we need to print `(let _ = a) < b` as-is with parens.
1474            | ExprKind::Let(..)
1475            | ExprKind::Unary(..) => ExprPrecedence::Prefix,
1476
1477            // Never need parens
1478            ExprKind::Array(_)
1479            | ExprKind::Await(..)
1480            | ExprKind::Use(..)
1481            | ExprKind::Block(..)
1482            | ExprKind::Call(..)
1483            | ExprKind::ConstBlock(_)
1484            | ExprKind::Continue(..)
1485            | ExprKind::Field(..)
1486            | ExprKind::ForLoop { .. }
1487            | ExprKind::FormatArgs(..)
1488            | ExprKind::Gen(..)
1489            | ExprKind::If(..)
1490            | ExprKind::IncludedBytes(..)
1491            | ExprKind::Index(..)
1492            | ExprKind::InlineAsm(..)
1493            | ExprKind::Lit(_)
1494            | ExprKind::Loop(..)
1495            | ExprKind::MacCall(..)
1496            | ExprKind::Match(..)
1497            | ExprKind::MethodCall(..)
1498            | ExprKind::OffsetOf(..)
1499            | ExprKind::Paren(..)
1500            | ExprKind::Path(..)
1501            | ExprKind::Repeat(..)
1502            | ExprKind::Struct(..)
1503            | ExprKind::Try(..)
1504            | ExprKind::TryBlock(..)
1505            | ExprKind::Tup(_)
1506            | ExprKind::Type(..)
1507            | ExprKind::Underscore
1508            | ExprKind::UnsafeBinderCast(..)
1509            | ExprKind::While(..)
1510            | ExprKind::Err(_)
1511            | ExprKind::Dummy => ExprPrecedence::Unambiguous,
1512        }
1513    }
1514
1515    /// To a first-order approximation, is this a pattern?
1516    pub fn is_approximately_pattern(&self) -> bool {
1517        matches!(
1518            &self.peel_parens().kind,
1519            ExprKind::Array(_)
1520                | ExprKind::Call(_, _)
1521                | ExprKind::Tup(_)
1522                | ExprKind::Lit(_)
1523                | ExprKind::Range(_, _, _)
1524                | ExprKind::Underscore
1525                | ExprKind::Path(_, _)
1526                | ExprKind::Struct(_)
1527        )
1528    }
1529}
1530
1531#[derive(Clone, Encodable, Decodable, Debug)]
1532pub struct Closure {
1533    pub binder: ClosureBinder,
1534    pub capture_clause: CaptureBy,
1535    pub constness: Const,
1536    pub coroutine_kind: Option<CoroutineKind>,
1537    pub movability: Movability,
1538    pub fn_decl: P<FnDecl>,
1539    pub body: P<Expr>,
1540    /// The span of the declaration block: 'move |...| -> ...'
1541    pub fn_decl_span: Span,
1542    /// The span of the argument block `|...|`
1543    pub fn_arg_span: Span,
1544}
1545
1546/// Limit types of a range (inclusive or exclusive).
1547#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug)]
1548pub enum RangeLimits {
1549    /// Inclusive at the beginning, exclusive at the end.
1550    HalfOpen,
1551    /// Inclusive at the beginning and end.
1552    Closed,
1553}
1554
1555impl RangeLimits {
1556    pub fn as_str(&self) -> &'static str {
1557        match self {
1558            RangeLimits::HalfOpen => "..",
1559            RangeLimits::Closed => "..=",
1560        }
1561    }
1562}
1563
1564/// A method call (e.g. `x.foo::<Bar, Baz>(a, b, c)`).
1565#[derive(Clone, Encodable, Decodable, Debug)]
1566pub struct MethodCall {
1567    /// The method name and its generic arguments, e.g. `foo::<Bar, Baz>`.
1568    pub seg: PathSegment,
1569    /// The receiver, e.g. `x`.
1570    pub receiver: P<Expr>,
1571    /// The arguments, e.g. `a, b, c`.
1572    pub args: ThinVec<P<Expr>>,
1573    /// The span of the function, without the dot and receiver e.g. `foo::<Bar,
1574    /// Baz>(a, b, c)`.
1575    pub span: Span,
1576}
1577
1578#[derive(Clone, Encodable, Decodable, Debug)]
1579pub enum StructRest {
1580    /// `..x`.
1581    Base(P<Expr>),
1582    /// `..`.
1583    Rest(Span),
1584    /// No trailing `..` or expression.
1585    None,
1586}
1587
1588#[derive(Clone, Encodable, Decodable, Debug)]
1589pub struct StructExpr {
1590    pub qself: Option<P<QSelf>>,
1591    pub path: Path,
1592    pub fields: ThinVec<ExprField>,
1593    pub rest: StructRest,
1594}
1595
1596// Adding a new variant? Please update `test_expr` in `tests/ui/macros/stringify.rs`.
1597#[derive(Clone, Encodable, Decodable, Debug)]
1598pub enum ExprKind {
1599    /// An array (e.g, `[a, b, c, d]`).
1600    Array(ThinVec<P<Expr>>),
1601    /// Allow anonymous constants from an inline `const` block.
1602    ConstBlock(AnonConst),
1603    /// A function call.
1604    ///
1605    /// The first field resolves to the function itself,
1606    /// and the second field is the list of arguments.
1607    /// This also represents calling the constructor of
1608    /// tuple-like ADTs such as tuple structs and enum variants.
1609    Call(P<Expr>, ThinVec<P<Expr>>),
1610    /// A method call (e.g., `x.foo::<Bar, Baz>(a, b, c)`).
1611    MethodCall(Box<MethodCall>),
1612    /// A tuple (e.g., `(a, b, c, d)`).
1613    Tup(ThinVec<P<Expr>>),
1614    /// A binary operation (e.g., `a + b`, `a * b`).
1615    Binary(BinOp, P<Expr>, P<Expr>),
1616    /// A unary operation (e.g., `!x`, `*x`).
1617    Unary(UnOp, P<Expr>),
1618    /// A literal (e.g., `1`, `"foo"`).
1619    Lit(token::Lit),
1620    /// A cast (e.g., `foo as f64`).
1621    Cast(P<Expr>, P<Ty>),
1622    /// A type ascription (e.g., `builtin # type_ascribe(42, usize)`).
1623    ///
1624    /// Usually not written directly in user code but
1625    /// indirectly via the macro `type_ascribe!(...)`.
1626    Type(P<Expr>, P<Ty>),
1627    /// A `let pat = expr` expression that is only semantically allowed in the condition
1628    /// of `if` / `while` expressions. (e.g., `if let 0 = x { .. }`).
1629    ///
1630    /// `Span` represents the whole `let pat = expr` statement.
1631    Let(P<Pat>, P<Expr>, Span, Recovered),
1632    /// An `if` block, with an optional `else` block.
1633    ///
1634    /// `if expr { block } else { expr }`
1635    ///
1636    /// If present, the "else" expr is always `ExprKind::Block` (for `else`) or
1637    /// `ExprKind::If` (for `else if`).
1638    If(P<Expr>, P<Block>, Option<P<Expr>>),
1639    /// A while loop, with an optional label.
1640    ///
1641    /// `'label: while expr { block }`
1642    While(P<Expr>, P<Block>, Option<Label>),
1643    /// A `for` loop, with an optional label.
1644    ///
1645    /// `'label: for await? pat in iter { block }`
1646    ///
1647    /// This is desugared to a combination of `loop` and `match` expressions.
1648    ForLoop {
1649        pat: P<Pat>,
1650        iter: P<Expr>,
1651        body: P<Block>,
1652        label: Option<Label>,
1653        kind: ForLoopKind,
1654    },
1655    /// Conditionless loop (can be exited with `break`, `continue`, or `return`).
1656    ///
1657    /// `'label: loop { block }`
1658    Loop(P<Block>, Option<Label>, Span),
1659    /// A `match` block.
1660    Match(P<Expr>, ThinVec<Arm>, MatchKind),
1661    /// A closure (e.g., `move |a, b, c| a + b + c`).
1662    Closure(Box<Closure>),
1663    /// A block (`'label: { ... }`).
1664    Block(P<Block>, Option<Label>),
1665    /// An `async` block (`async move { ... }`),
1666    /// or a `gen` block (`gen move { ... }`).
1667    ///
1668    /// The span is the "decl", which is the header before the body `{ }`
1669    /// including the `asyng`/`gen` keywords and possibly `move`.
1670    Gen(CaptureBy, P<Block>, GenBlockKind, Span),
1671    /// An await expression (`my_future.await`). Span is of await keyword.
1672    Await(P<Expr>, Span),
1673    /// A use expression (`x.use`). Span is of use keyword.
1674    Use(P<Expr>, Span),
1675
1676    /// A try block (`try { ... }`).
1677    TryBlock(P<Block>),
1678
1679    /// An assignment (`a = foo()`).
1680    /// The `Span` argument is the span of the `=` token.
1681    Assign(P<Expr>, P<Expr>, Span),
1682    /// An assignment with an operator.
1683    ///
1684    /// E.g., `a += 1`.
1685    AssignOp(AssignOp, P<Expr>, P<Expr>),
1686    /// Access of a named (e.g., `obj.foo`) or unnamed (e.g., `obj.0`) struct field.
1687    Field(P<Expr>, Ident),
1688    /// An indexing operation (e.g., `foo[2]`).
1689    /// The span represents the span of the `[2]`, including brackets.
1690    Index(P<Expr>, P<Expr>, Span),
1691    /// A range (e.g., `1..2`, `1..`, `..2`, `1..=2`, `..=2`; and `..` in destructuring assignment).
1692    Range(Option<P<Expr>>, Option<P<Expr>>, RangeLimits),
1693    /// An underscore, used in destructuring assignment to ignore a value.
1694    Underscore,
1695
1696    /// Variable reference, possibly containing `::` and/or type
1697    /// parameters (e.g., `foo::bar::<baz>`).
1698    ///
1699    /// Optionally "qualified" (e.g., `<Vec<T> as SomeTrait>::SomeType`).
1700    Path(Option<P<QSelf>>, Path),
1701
1702    /// A referencing operation (`&a`, `&mut a`, `&raw const a` or `&raw mut a`).
1703    AddrOf(BorrowKind, Mutability, P<Expr>),
1704    /// A `break`, with an optional label to break, and an optional expression.
1705    Break(Option<Label>, Option<P<Expr>>),
1706    /// A `continue`, with an optional label.
1707    Continue(Option<Label>),
1708    /// A `return`, with an optional value to be returned.
1709    Ret(Option<P<Expr>>),
1710
1711    /// Output of the `asm!()` macro.
1712    InlineAsm(P<InlineAsm>),
1713
1714    /// An `offset_of` expression (e.g., `builtin # offset_of(Struct, field)`).
1715    ///
1716    /// Usually not written directly in user code but
1717    /// indirectly via the macro `core::mem::offset_of!(...)`.
1718    OffsetOf(P<Ty>, P<[Ident]>),
1719
1720    /// A macro invocation; pre-expansion.
1721    MacCall(P<MacCall>),
1722
1723    /// A struct literal expression.
1724    ///
1725    /// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. rest}`.
1726    Struct(P<StructExpr>),
1727
1728    /// An array literal constructed from one repeated element.
1729    ///
1730    /// E.g., `[1; 5]`. The expression is the element to be
1731    /// repeated; the constant is the number of times to repeat it.
1732    Repeat(P<Expr>, AnonConst),
1733
1734    /// No-op: used solely so we can pretty-print faithfully.
1735    Paren(P<Expr>),
1736
1737    /// A try expression (`expr?`).
1738    Try(P<Expr>),
1739
1740    /// A `yield`, with an optional value to be yielded.
1741    Yield(YieldKind),
1742
1743    /// A `do yeet` (aka `throw`/`fail`/`bail`/`raise`/whatever),
1744    /// with an optional value to be returned.
1745    Yeet(Option<P<Expr>>),
1746
1747    /// A tail call return, with the value to be returned.
1748    ///
1749    /// While `.0` must be a function call, we check this later, after parsing.
1750    Become(P<Expr>),
1751
1752    /// Bytes included via `include_bytes!`
1753    /// Added for optimization purposes to avoid the need to escape
1754    /// large binary blobs - should always behave like [`ExprKind::Lit`]
1755    /// with a `ByteStr` literal.
1756    IncludedBytes(Arc<[u8]>),
1757
1758    /// A `format_args!()` expression.
1759    FormatArgs(P<FormatArgs>),
1760
1761    UnsafeBinderCast(UnsafeBinderCastKind, P<Expr>, Option<P<Ty>>),
1762
1763    /// Placeholder for an expression that wasn't syntactically well formed in some way.
1764    Err(ErrorGuaranteed),
1765
1766    /// Acts as a null expression. Lowering it will always emit a bug.
1767    Dummy,
1768}
1769
1770/// Used to differentiate between `for` loops and `for await` loops.
1771#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq, Eq)]
1772pub enum ForLoopKind {
1773    For,
1774    ForAwait,
1775}
1776
1777/// Used to differentiate between `async {}` blocks and `gen {}` blocks.
1778#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
1779pub enum GenBlockKind {
1780    Async,
1781    Gen,
1782    AsyncGen,
1783}
1784
1785impl fmt::Display for GenBlockKind {
1786    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1787        self.modifier().fmt(f)
1788    }
1789}
1790
1791impl GenBlockKind {
1792    pub fn modifier(&self) -> &'static str {
1793        match self {
1794            GenBlockKind::Async => "async",
1795            GenBlockKind::Gen => "gen",
1796            GenBlockKind::AsyncGen => "async gen",
1797        }
1798    }
1799}
1800
1801/// Whether we're unwrapping or wrapping an unsafe binder
1802#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
1803#[derive(Encodable, Decodable, HashStable_Generic)]
1804pub enum UnsafeBinderCastKind {
1805    // e.g. `&i32` -> `unsafe<'a> &'a i32`
1806    Wrap,
1807    // e.g. `unsafe<'a> &'a i32` -> `&i32`
1808    Unwrap,
1809}
1810
1811/// The explicit `Self` type in a "qualified path". The actual
1812/// path, including the trait and the associated item, is stored
1813/// separately. `position` represents the index of the associated
1814/// item qualified with this `Self` type.
1815///
1816/// ```ignore (only-for-syntax-highlight)
1817/// <Vec<T> as a::b::Trait>::AssociatedItem
1818///  ^~~~~     ~~~~~~~~~~~~~~^
1819///  ty        position = 3
1820///
1821/// <Vec<T>>::AssociatedItem
1822///  ^~~~~    ^
1823///  ty       position = 0
1824/// ```
1825#[derive(Clone, Encodable, Decodable, Debug)]
1826pub struct QSelf {
1827    pub ty: P<Ty>,
1828
1829    /// The span of `a::b::Trait` in a path like `<Vec<T> as
1830    /// a::b::Trait>::AssociatedItem`; in the case where `position ==
1831    /// 0`, this is an empty span.
1832    pub path_span: Span,
1833    pub position: usize,
1834}
1835
1836/// A capture clause used in closures and `async` blocks.
1837#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
1838pub enum CaptureBy {
1839    /// `move |x| y + x`.
1840    Value {
1841        /// The span of the `move` keyword.
1842        move_kw: Span,
1843    },
1844    /// `move` or `use` keywords were not specified.
1845    Ref,
1846    /// `use |x| y + x`.
1847    ///
1848    /// Note that if you have a regular closure like `|| x.use`, this will *not* result
1849    /// in a `Use` capture. Instead, the `ExprUseVisitor` will look at the type
1850    /// of `x` and treat `x.use` as either a copy/clone/move as appropriate.
1851    Use {
1852        /// The span of the `use` keyword.
1853        use_kw: Span,
1854    },
1855}
1856
1857/// Closure lifetime binder, `for<'a, 'b>` in `for<'a, 'b> |_: &'a (), _: &'b ()|`.
1858#[derive(Clone, Encodable, Decodable, Debug)]
1859pub enum ClosureBinder {
1860    /// The binder is not present, all closure lifetimes are inferred.
1861    NotPresent,
1862    /// The binder is present.
1863    For {
1864        /// Span of the whole `for<>` clause
1865        ///
1866        /// ```text
1867        /// for<'a, 'b> |_: &'a (), _: &'b ()| { ... }
1868        /// ^^^^^^^^^^^ -- this
1869        /// ```
1870        span: Span,
1871
1872        /// Lifetimes in the `for<>` closure
1873        ///
1874        /// ```text
1875        /// for<'a, 'b> |_: &'a (), _: &'b ()| { ... }
1876        ///     ^^^^^^ -- this
1877        /// ```
1878        generic_params: ThinVec<GenericParam>,
1879    },
1880}
1881
1882/// Represents a macro invocation. The `path` indicates which macro
1883/// is being invoked, and the `args` are arguments passed to it.
1884#[derive(Clone, Encodable, Decodable, Debug)]
1885pub struct MacCall {
1886    pub path: Path,
1887    pub args: P<DelimArgs>,
1888}
1889
1890impl MacCall {
1891    pub fn span(&self) -> Span {
1892        self.path.span.to(self.args.dspan.entire())
1893    }
1894}
1895
1896/// Arguments passed to an attribute macro.
1897#[derive(Clone, Encodable, Decodable, Debug)]
1898pub enum AttrArgs {
1899    /// No arguments: `#[attr]`.
1900    Empty,
1901    /// Delimited arguments: `#[attr()/[]/{}]`.
1902    Delimited(DelimArgs),
1903    /// Arguments of a key-value attribute: `#[attr = "value"]`.
1904    Eq {
1905        /// Span of the `=` token.
1906        eq_span: Span,
1907        expr: P<Expr>,
1908    },
1909}
1910
1911impl AttrArgs {
1912    pub fn span(&self) -> Option<Span> {
1913        match self {
1914            AttrArgs::Empty => None,
1915            AttrArgs::Delimited(args) => Some(args.dspan.entire()),
1916            AttrArgs::Eq { eq_span, expr } => Some(eq_span.to(expr.span)),
1917        }
1918    }
1919
1920    /// Tokens inside the delimiters or after `=`.
1921    /// Proc macros see these tokens, for example.
1922    pub fn inner_tokens(&self) -> TokenStream {
1923        match self {
1924            AttrArgs::Empty => TokenStream::default(),
1925            AttrArgs::Delimited(args) => args.tokens.clone(),
1926            AttrArgs::Eq { expr, .. } => TokenStream::from_ast(expr),
1927        }
1928    }
1929}
1930
1931/// Delimited arguments, as used in `#[attr()/[]/{}]` or `mac!()/[]/{}`.
1932#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
1933pub struct DelimArgs {
1934    pub dspan: DelimSpan,
1935    pub delim: Delimiter, // Note: `Delimiter::Invisible` never occurs
1936    pub tokens: TokenStream,
1937}
1938
1939impl DelimArgs {
1940    /// Whether a macro with these arguments needs a semicolon
1941    /// when used as a standalone item or statement.
1942    pub fn need_semicolon(&self) -> bool {
1943        !matches!(self, DelimArgs { delim: Delimiter::Brace, .. })
1944    }
1945}
1946
1947/// Represents a macro definition.
1948#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
1949pub struct MacroDef {
1950    pub body: P<DelimArgs>,
1951    /// `true` if macro was defined with `macro_rules`.
1952    pub macro_rules: bool,
1953}
1954
1955#[derive(Clone, Encodable, Decodable, Debug, Copy, Hash, Eq, PartialEq)]
1956#[derive(HashStable_Generic)]
1957pub enum StrStyle {
1958    /// A regular string, like `"foo"`.
1959    Cooked,
1960    /// A raw string, like `r##"foo"##`.
1961    ///
1962    /// The value is the number of `#` symbols used.
1963    Raw(u8),
1964}
1965
1966/// The kind of match expression
1967#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq)]
1968pub enum MatchKind {
1969    /// match expr { ... }
1970    Prefix,
1971    /// expr.match { ... }
1972    Postfix,
1973}
1974
1975/// The kind of yield expression
1976#[derive(Clone, Encodable, Decodable, Debug)]
1977pub enum YieldKind {
1978    /// yield expr { ... }
1979    Prefix(Option<P<Expr>>),
1980    /// expr.yield { ... }
1981    Postfix(P<Expr>),
1982}
1983
1984impl YieldKind {
1985    /// Returns the expression inside the yield expression, if any.
1986    ///
1987    /// For postfix yields, this is guaranteed to be `Some`.
1988    pub const fn expr(&self) -> Option<&P<Expr>> {
1989        match self {
1990            YieldKind::Prefix(expr) => expr.as_ref(),
1991            YieldKind::Postfix(expr) => Some(expr),
1992        }
1993    }
1994
1995    /// Returns a mutable reference to the expression being yielded, if any.
1996    pub const fn expr_mut(&mut self) -> Option<&mut P<Expr>> {
1997        match self {
1998            YieldKind::Prefix(expr) => expr.as_mut(),
1999            YieldKind::Postfix(expr) => Some(expr),
2000        }
2001    }
2002
2003    /// Returns true if both yields are prefix or both are postfix.
2004    pub const fn same_kind(&self, other: &Self) -> bool {
2005        match (self, other) {
2006            (YieldKind::Prefix(_), YieldKind::Prefix(_)) => true,
2007            (YieldKind::Postfix(_), YieldKind::Postfix(_)) => true,
2008            _ => false,
2009        }
2010    }
2011}
2012
2013/// A literal in a meta item.
2014#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
2015pub struct MetaItemLit {
2016    /// The original literal as written in the source code.
2017    pub symbol: Symbol,
2018    /// The original suffix as written in the source code.
2019    pub suffix: Option<Symbol>,
2020    /// The "semantic" representation of the literal lowered from the original tokens.
2021    /// Strings are unescaped, hexadecimal forms are eliminated, etc.
2022    pub kind: LitKind,
2023    pub span: Span,
2024}
2025
2026/// Similar to `MetaItemLit`, but restricted to string literals.
2027#[derive(Clone, Copy, Encodable, Decodable, Debug)]
2028pub struct StrLit {
2029    /// The original literal as written in source code.
2030    pub symbol: Symbol,
2031    /// The original suffix as written in source code.
2032    pub suffix: Option<Symbol>,
2033    /// The semantic (unescaped) representation of the literal.
2034    pub symbol_unescaped: Symbol,
2035    pub style: StrStyle,
2036    pub span: Span,
2037}
2038
2039impl StrLit {
2040    pub fn as_token_lit(&self) -> token::Lit {
2041        let token_kind = match self.style {
2042            StrStyle::Cooked => token::Str,
2043            StrStyle::Raw(n) => token::StrRaw(n),
2044        };
2045        token::Lit::new(token_kind, self.symbol, self.suffix)
2046    }
2047}
2048
2049/// Type of the integer literal based on provided suffix.
2050#[derive(Clone, Copy, Encodable, Decodable, Debug, Hash, Eq, PartialEq)]
2051#[derive(HashStable_Generic)]
2052pub enum LitIntType {
2053    /// e.g. `42_i32`.
2054    Signed(IntTy),
2055    /// e.g. `42_u32`.
2056    Unsigned(UintTy),
2057    /// e.g. `42`.
2058    Unsuffixed,
2059}
2060
2061/// Type of the float literal based on provided suffix.
2062#[derive(Clone, Copy, Encodable, Decodable, Debug, Hash, Eq, PartialEq)]
2063#[derive(HashStable_Generic)]
2064pub enum LitFloatType {
2065    /// A float literal with a suffix (`1f32` or `1E10f32`).
2066    Suffixed(FloatTy),
2067    /// A float literal without a suffix (`1.0 or 1.0E10`).
2068    Unsuffixed,
2069}
2070
2071/// This type is used within both `ast::MetaItemLit` and `hir::Lit`.
2072///
2073/// Note that the entire literal (including the suffix) is considered when
2074/// deciding the `LitKind`. This means that float literals like `1f32` are
2075/// classified by this type as `Float`. This is different to `token::LitKind`
2076/// which does *not* consider the suffix.
2077#[derive(Clone, Encodable, Decodable, Debug, Hash, Eq, PartialEq, HashStable_Generic)]
2078pub enum LitKind {
2079    /// A string literal (`"foo"`). The symbol is unescaped, and so may differ
2080    /// from the original token's symbol.
2081    Str(Symbol, StrStyle),
2082    /// A byte string (`b"foo"`). Not stored as a symbol because it might be
2083    /// non-utf8, and symbols only allow utf8 strings.
2084    ByteStr(Arc<[u8]>, StrStyle),
2085    /// A C String (`c"foo"`). Guaranteed to only have `\0` at the end.
2086    CStr(Arc<[u8]>, StrStyle),
2087    /// A byte char (`b'f'`).
2088    Byte(u8),
2089    /// A character literal (`'a'`).
2090    Char(char),
2091    /// An integer literal (`1`).
2092    Int(Pu128, LitIntType),
2093    /// A float literal (`1.0`, `1f64` or `1E10f64`). The pre-suffix part is
2094    /// stored as a symbol rather than `f64` so that `LitKind` can impl `Eq`
2095    /// and `Hash`.
2096    Float(Symbol, LitFloatType),
2097    /// A boolean literal (`true`, `false`).
2098    Bool(bool),
2099    /// Placeholder for a literal that wasn't well-formed in some way.
2100    Err(ErrorGuaranteed),
2101}
2102
2103impl LitKind {
2104    pub fn str(&self) -> Option<Symbol> {
2105        match *self {
2106            LitKind::Str(s, _) => Some(s),
2107            _ => None,
2108        }
2109    }
2110
2111    /// Returns `true` if this literal is a string.
2112    pub fn is_str(&self) -> bool {
2113        matches!(self, LitKind::Str(..))
2114    }
2115
2116    /// Returns `true` if this literal is byte literal string.
2117    pub fn is_bytestr(&self) -> bool {
2118        matches!(self, LitKind::ByteStr(..))
2119    }
2120
2121    /// Returns `true` if this is a numeric literal.
2122    pub fn is_numeric(&self) -> bool {
2123        matches!(self, LitKind::Int(..) | LitKind::Float(..))
2124    }
2125
2126    /// Returns `true` if this literal has no suffix.
2127    /// Note: this will return true for literals with prefixes such as raw strings and byte strings.
2128    pub fn is_unsuffixed(&self) -> bool {
2129        !self.is_suffixed()
2130    }
2131
2132    /// Returns `true` if this literal has a suffix.
2133    pub fn is_suffixed(&self) -> bool {
2134        match *self {
2135            // suffixed variants
2136            LitKind::Int(_, LitIntType::Signed(..) | LitIntType::Unsigned(..))
2137            | LitKind::Float(_, LitFloatType::Suffixed(..)) => true,
2138            // unsuffixed variants
2139            LitKind::Str(..)
2140            | LitKind::ByteStr(..)
2141            | LitKind::CStr(..)
2142            | LitKind::Byte(..)
2143            | LitKind::Char(..)
2144            | LitKind::Int(_, LitIntType::Unsuffixed)
2145            | LitKind::Float(_, LitFloatType::Unsuffixed)
2146            | LitKind::Bool(..)
2147            | LitKind::Err(_) => false,
2148        }
2149    }
2150}
2151
2152// N.B., If you change this, you'll probably want to change the corresponding
2153// type structure in `middle/ty.rs` as well.
2154#[derive(Clone, Encodable, Decodable, Debug)]
2155pub struct MutTy {
2156    pub ty: P<Ty>,
2157    pub mutbl: Mutability,
2158}
2159
2160/// Represents a function's signature in a trait declaration,
2161/// trait implementation, or free function.
2162#[derive(Clone, Encodable, Decodable, Debug)]
2163pub struct FnSig {
2164    pub header: FnHeader,
2165    pub decl: P<FnDecl>,
2166    pub span: Span,
2167}
2168
2169#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
2170#[derive(Encodable, Decodable, HashStable_Generic)]
2171pub enum FloatTy {
2172    F16,
2173    F32,
2174    F64,
2175    F128,
2176}
2177
2178impl FloatTy {
2179    pub fn name_str(self) -> &'static str {
2180        match self {
2181            FloatTy::F16 => "f16",
2182            FloatTy::F32 => "f32",
2183            FloatTy::F64 => "f64",
2184            FloatTy::F128 => "f128",
2185        }
2186    }
2187
2188    pub fn name(self) -> Symbol {
2189        match self {
2190            FloatTy::F16 => sym::f16,
2191            FloatTy::F32 => sym::f32,
2192            FloatTy::F64 => sym::f64,
2193            FloatTy::F128 => sym::f128,
2194        }
2195    }
2196}
2197
2198#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
2199#[derive(Encodable, Decodable, HashStable_Generic)]
2200pub enum IntTy {
2201    Isize,
2202    I8,
2203    I16,
2204    I32,
2205    I64,
2206    I128,
2207}
2208
2209impl IntTy {
2210    pub fn name_str(&self) -> &'static str {
2211        match *self {
2212            IntTy::Isize => "isize",
2213            IntTy::I8 => "i8",
2214            IntTy::I16 => "i16",
2215            IntTy::I32 => "i32",
2216            IntTy::I64 => "i64",
2217            IntTy::I128 => "i128",
2218        }
2219    }
2220
2221    pub fn name(&self) -> Symbol {
2222        match *self {
2223            IntTy::Isize => sym::isize,
2224            IntTy::I8 => sym::i8,
2225            IntTy::I16 => sym::i16,
2226            IntTy::I32 => sym::i32,
2227            IntTy::I64 => sym::i64,
2228            IntTy::I128 => sym::i128,
2229        }
2230    }
2231}
2232
2233#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
2234#[derive(Encodable, Decodable, HashStable_Generic)]
2235pub enum UintTy {
2236    Usize,
2237    U8,
2238    U16,
2239    U32,
2240    U64,
2241    U128,
2242}
2243
2244impl UintTy {
2245    pub fn name_str(&self) -> &'static str {
2246        match *self {
2247            UintTy::Usize => "usize",
2248            UintTy::U8 => "u8",
2249            UintTy::U16 => "u16",
2250            UintTy::U32 => "u32",
2251            UintTy::U64 => "u64",
2252            UintTy::U128 => "u128",
2253        }
2254    }
2255
2256    pub fn name(&self) -> Symbol {
2257        match *self {
2258            UintTy::Usize => sym::usize,
2259            UintTy::U8 => sym::u8,
2260            UintTy::U16 => sym::u16,
2261            UintTy::U32 => sym::u32,
2262            UintTy::U64 => sym::u64,
2263            UintTy::U128 => sym::u128,
2264        }
2265    }
2266}
2267
2268/// A constraint on an associated item.
2269///
2270/// ### Examples
2271///
2272/// * the `A = Ty` and `B = Ty` in `Trait<A = Ty, B = Ty>`
2273/// * the `G<Ty> = Ty` in `Trait<G<Ty> = Ty>`
2274/// * the `A: Bound` in `Trait<A: Bound>`
2275/// * the `RetTy` in `Trait(ArgTy, ArgTy) -> RetTy`
2276/// * the `C = { Ct }` in `Trait<C = { Ct }>` (feature `associated_const_equality`)
2277/// * the `f(..): Bound` in `Trait<f(..): Bound>` (feature `return_type_notation`)
2278#[derive(Clone, Encodable, Decodable, Debug)]
2279pub struct AssocItemConstraint {
2280    pub id: NodeId,
2281    pub ident: Ident,
2282    pub gen_args: Option<GenericArgs>,
2283    pub kind: AssocItemConstraintKind,
2284    pub span: Span,
2285}
2286
2287#[derive(Clone, Encodable, Decodable, Debug)]
2288pub enum Term {
2289    Ty(P<Ty>),
2290    Const(AnonConst),
2291}
2292
2293impl From<P<Ty>> for Term {
2294    fn from(v: P<Ty>) -> Self {
2295        Term::Ty(v)
2296    }
2297}
2298
2299impl From<AnonConst> for Term {
2300    fn from(v: AnonConst) -> Self {
2301        Term::Const(v)
2302    }
2303}
2304
2305/// The kind of [associated item constraint][AssocItemConstraint].
2306#[derive(Clone, Encodable, Decodable, Debug)]
2307pub enum AssocItemConstraintKind {
2308    /// An equality constraint for an associated item (e.g., `AssocTy = Ty` in `Trait<AssocTy = Ty>`).
2309    ///
2310    /// Also known as an *associated item binding* (we *bind* an associated item to a term).
2311    ///
2312    /// Furthermore, associated type equality constraints can also be referred to as *associated type
2313    /// bindings*. Similarly with associated const equality constraints and *associated const bindings*.
2314    Equality { term: Term },
2315    /// A bound on an associated type (e.g., `AssocTy: Bound` in `Trait<AssocTy: Bound>`).
2316    Bound { bounds: GenericBounds },
2317}
2318
2319#[derive(Encodable, Decodable, Debug)]
2320pub struct Ty {
2321    pub id: NodeId,
2322    pub kind: TyKind,
2323    pub span: Span,
2324    pub tokens: Option<LazyAttrTokenStream>,
2325}
2326
2327impl Clone for Ty {
2328    fn clone(&self) -> Self {
2329        ensure_sufficient_stack(|| Self {
2330            id: self.id,
2331            kind: self.kind.clone(),
2332            span: self.span,
2333            tokens: self.tokens.clone(),
2334        })
2335    }
2336}
2337
2338impl Ty {
2339    pub fn peel_refs(&self) -> &Self {
2340        let mut final_ty = self;
2341        while let TyKind::Ref(_, MutTy { ty, .. }) | TyKind::Ptr(MutTy { ty, .. }) = &final_ty.kind
2342        {
2343            final_ty = ty;
2344        }
2345        final_ty
2346    }
2347
2348    pub fn is_maybe_parenthesised_infer(&self) -> bool {
2349        match &self.kind {
2350            TyKind::Infer => true,
2351            TyKind::Paren(inner) => inner.is_maybe_parenthesised_infer(),
2352            _ => false,
2353        }
2354    }
2355}
2356
2357#[derive(Clone, Encodable, Decodable, Debug)]
2358pub struct BareFnTy {
2359    pub safety: Safety,
2360    pub ext: Extern,
2361    pub generic_params: ThinVec<GenericParam>,
2362    pub decl: P<FnDecl>,
2363    /// Span of the `[unsafe] [extern] fn(...) -> ...` part, i.e. everything
2364    /// after the generic params (if there are any, e.g. `for<'a>`).
2365    pub decl_span: Span,
2366}
2367
2368#[derive(Clone, Encodable, Decodable, Debug)]
2369pub struct UnsafeBinderTy {
2370    pub generic_params: ThinVec<GenericParam>,
2371    pub inner_ty: P<Ty>,
2372}
2373
2374/// The various kinds of type recognized by the compiler.
2375//
2376// Adding a new variant? Please update `test_ty` in `tests/ui/macros/stringify.rs`.
2377#[derive(Clone, Encodable, Decodable, Debug)]
2378pub enum TyKind {
2379    /// A variable-length slice (`[T]`).
2380    Slice(P<Ty>),
2381    /// A fixed length array (`[T; n]`).
2382    Array(P<Ty>, AnonConst),
2383    /// A raw pointer (`*const T` or `*mut T`).
2384    Ptr(MutTy),
2385    /// A reference (`&'a T` or `&'a mut T`).
2386    Ref(Option<Lifetime>, MutTy),
2387    /// A pinned reference (`&'a pin const T` or `&'a pin mut T`).
2388    ///
2389    /// Desugars into `Pin<&'a T>` or `Pin<&'a mut T>`.
2390    PinnedRef(Option<Lifetime>, MutTy),
2391    /// A bare function (e.g., `fn(usize) -> bool`).
2392    BareFn(P<BareFnTy>),
2393    /// An unsafe existential lifetime binder (e.g., `unsafe<'a> &'a ()`).
2394    UnsafeBinder(P<UnsafeBinderTy>),
2395    /// The never type (`!`).
2396    Never,
2397    /// A tuple (`(A, B, C, D,...)`).
2398    Tup(ThinVec<P<Ty>>),
2399    /// A path (`module::module::...::Type`), optionally
2400    /// "qualified", e.g., `<Vec<T> as SomeTrait>::SomeType`.
2401    ///
2402    /// Type parameters are stored in the `Path` itself.
2403    Path(Option<P<QSelf>>, Path),
2404    /// A trait object type `Bound1 + Bound2 + Bound3`
2405    /// where `Bound` is a trait or a lifetime.
2406    TraitObject(GenericBounds, TraitObjectSyntax),
2407    /// An `impl Bound1 + Bound2 + Bound3` type
2408    /// where `Bound` is a trait or a lifetime.
2409    ///
2410    /// The `NodeId` exists to prevent lowering from having to
2411    /// generate `NodeId`s on the fly, which would complicate
2412    /// the generation of opaque `type Foo = impl Trait` items significantly.
2413    ImplTrait(NodeId, GenericBounds),
2414    /// No-op; kept solely so that we can pretty-print faithfully.
2415    Paren(P<Ty>),
2416    /// Unused for now.
2417    Typeof(AnonConst),
2418    /// This means the type should be inferred instead of it having been
2419    /// specified. This can appear anywhere in a type.
2420    Infer,
2421    /// Inferred type of a `self` or `&self` argument in a method.
2422    ImplicitSelf,
2423    /// A macro in the type position.
2424    MacCall(P<MacCall>),
2425    /// Placeholder for a `va_list`.
2426    CVarArgs,
2427    /// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZero<u32>`,
2428    /// just as part of the type system.
2429    Pat(P<Ty>, P<TyPat>),
2430    /// Sometimes we need a dummy value when no error has occurred.
2431    Dummy,
2432    /// Placeholder for a kind that has failed to be defined.
2433    Err(ErrorGuaranteed),
2434}
2435
2436impl TyKind {
2437    pub fn is_implicit_self(&self) -> bool {
2438        matches!(self, TyKind::ImplicitSelf)
2439    }
2440
2441    pub fn is_unit(&self) -> bool {
2442        matches!(self, TyKind::Tup(tys) if tys.is_empty())
2443    }
2444
2445    pub fn is_simple_path(&self) -> Option<Symbol> {
2446        if let TyKind::Path(None, Path { segments, .. }) = &self
2447            && let [segment] = &segments[..]
2448            && segment.args.is_none()
2449        {
2450            Some(segment.ident.name)
2451        } else {
2452            None
2453        }
2454    }
2455}
2456
2457/// A pattern type pattern.
2458#[derive(Clone, Encodable, Decodable, Debug)]
2459pub struct TyPat {
2460    pub id: NodeId,
2461    pub kind: TyPatKind,
2462    pub span: Span,
2463    pub tokens: Option<LazyAttrTokenStream>,
2464}
2465
2466/// All the different flavors of pattern that Rust recognizes.
2467//
2468// Adding a new variant? Please update `test_pat` in `tests/ui/macros/stringify.rs`.
2469#[derive(Clone, Encodable, Decodable, Debug)]
2470pub enum TyPatKind {
2471    /// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
2472    Range(Option<P<AnonConst>>, Option<P<AnonConst>>, Spanned<RangeEnd>),
2473
2474    Or(ThinVec<P<TyPat>>),
2475
2476    /// Placeholder for a pattern that wasn't syntactically well formed in some way.
2477    Err(ErrorGuaranteed),
2478}
2479
2480/// Syntax used to declare a trait object.
2481#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
2482#[repr(u8)]
2483pub enum TraitObjectSyntax {
2484    // SAFETY: When adding new variants make sure to update the `Tag` impl.
2485    Dyn = 0,
2486    DynStar = 1,
2487    None = 2,
2488}
2489
2490/// SAFETY: `TraitObjectSyntax` only has 3 data-less variants which means
2491/// it can be represented with a `u2`. We use `repr(u8)` to guarantee the
2492/// discriminants of the variants are no greater than `3`.
2493unsafe impl Tag for TraitObjectSyntax {
2494    const BITS: u32 = 2;
2495
2496    fn into_usize(self) -> usize {
2497        self as u8 as usize
2498    }
2499
2500    unsafe fn from_usize(tag: usize) -> Self {
2501        match tag {
2502            0 => TraitObjectSyntax::Dyn,
2503            1 => TraitObjectSyntax::DynStar,
2504            2 => TraitObjectSyntax::None,
2505            _ => unreachable!(),
2506        }
2507    }
2508}
2509
2510#[derive(Clone, Encodable, Decodable, Debug)]
2511pub enum PreciseCapturingArg {
2512    /// Lifetime parameter.
2513    Lifetime(Lifetime),
2514    /// Type or const parameter.
2515    Arg(Path, NodeId),
2516}
2517
2518/// Inline assembly operand explicit register or register class.
2519///
2520/// E.g., `"eax"` as in `asm!("mov eax, 2", out("eax") result)`.
2521#[derive(Clone, Copy, Encodable, Decodable, Debug)]
2522pub enum InlineAsmRegOrRegClass {
2523    Reg(Symbol),
2524    RegClass(Symbol),
2525}
2526
2527#[derive(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
2528pub struct InlineAsmOptions(u16);
2529bitflags::bitflags! {
2530    impl InlineAsmOptions: u16 {
2531        const PURE            = 1 << 0;
2532        const NOMEM           = 1 << 1;
2533        const READONLY        = 1 << 2;
2534        const PRESERVES_FLAGS = 1 << 3;
2535        const NORETURN        = 1 << 4;
2536        const NOSTACK         = 1 << 5;
2537        const ATT_SYNTAX      = 1 << 6;
2538        const RAW             = 1 << 7;
2539        const MAY_UNWIND      = 1 << 8;
2540    }
2541}
2542
2543impl InlineAsmOptions {
2544    pub const COUNT: usize = Self::all().bits().count_ones() as usize;
2545
2546    pub const GLOBAL_OPTIONS: Self = Self::ATT_SYNTAX.union(Self::RAW);
2547    pub const NAKED_OPTIONS: Self = Self::ATT_SYNTAX.union(Self::RAW);
2548
2549    pub fn human_readable_names(&self) -> Vec<&'static str> {
2550        let mut options = vec![];
2551
2552        if self.contains(InlineAsmOptions::PURE) {
2553            options.push("pure");
2554        }
2555        if self.contains(InlineAsmOptions::NOMEM) {
2556            options.push("nomem");
2557        }
2558        if self.contains(InlineAsmOptions::READONLY) {
2559            options.push("readonly");
2560        }
2561        if self.contains(InlineAsmOptions::PRESERVES_FLAGS) {
2562            options.push("preserves_flags");
2563        }
2564        if self.contains(InlineAsmOptions::NORETURN) {
2565            options.push("noreturn");
2566        }
2567        if self.contains(InlineAsmOptions::NOSTACK) {
2568            options.push("nostack");
2569        }
2570        if self.contains(InlineAsmOptions::ATT_SYNTAX) {
2571            options.push("att_syntax");
2572        }
2573        if self.contains(InlineAsmOptions::RAW) {
2574            options.push("raw");
2575        }
2576        if self.contains(InlineAsmOptions::MAY_UNWIND) {
2577            options.push("may_unwind");
2578        }
2579
2580        options
2581    }
2582}
2583
2584impl std::fmt::Debug for InlineAsmOptions {
2585    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2586        bitflags::parser::to_writer(self, f)
2587    }
2588}
2589
2590#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Hash, HashStable_Generic)]
2591pub enum InlineAsmTemplatePiece {
2592    String(Cow<'static, str>),
2593    Placeholder { operand_idx: usize, modifier: Option<char>, span: Span },
2594}
2595
2596impl fmt::Display for InlineAsmTemplatePiece {
2597    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2598        match self {
2599            Self::String(s) => {
2600                for c in s.chars() {
2601                    match c {
2602                        '{' => f.write_str("{{")?,
2603                        '}' => f.write_str("}}")?,
2604                        _ => c.fmt(f)?,
2605                    }
2606                }
2607                Ok(())
2608            }
2609            Self::Placeholder { operand_idx, modifier: Some(modifier), .. } => {
2610                write!(f, "{{{operand_idx}:{modifier}}}")
2611            }
2612            Self::Placeholder { operand_idx, modifier: None, .. } => {
2613                write!(f, "{{{operand_idx}}}")
2614            }
2615        }
2616    }
2617}
2618
2619impl InlineAsmTemplatePiece {
2620    /// Rebuilds the asm template string from its pieces.
2621    pub fn to_string(s: &[Self]) -> String {
2622        use fmt::Write;
2623        let mut out = String::new();
2624        for p in s.iter() {
2625            let _ = write!(out, "{p}");
2626        }
2627        out
2628    }
2629}
2630
2631/// Inline assembly symbol operands get their own AST node that is somewhat
2632/// similar to `AnonConst`.
2633///
2634/// The main difference is that we specifically don't assign it `DefId` in
2635/// `DefCollector`. Instead this is deferred until AST lowering where we
2636/// lower it to an `AnonConst` (for functions) or a `Path` (for statics)
2637/// depending on what the path resolves to.
2638#[derive(Clone, Encodable, Decodable, Debug)]
2639pub struct InlineAsmSym {
2640    pub id: NodeId,
2641    pub qself: Option<P<QSelf>>,
2642    pub path: Path,
2643}
2644
2645/// Inline assembly operand.
2646///
2647/// E.g., `out("eax") result` as in `asm!("mov eax, 2", out("eax") result)`.
2648#[derive(Clone, Encodable, Decodable, Debug)]
2649pub enum InlineAsmOperand {
2650    In {
2651        reg: InlineAsmRegOrRegClass,
2652        expr: P<Expr>,
2653    },
2654    Out {
2655        reg: InlineAsmRegOrRegClass,
2656        late: bool,
2657        expr: Option<P<Expr>>,
2658    },
2659    InOut {
2660        reg: InlineAsmRegOrRegClass,
2661        late: bool,
2662        expr: P<Expr>,
2663    },
2664    SplitInOut {
2665        reg: InlineAsmRegOrRegClass,
2666        late: bool,
2667        in_expr: P<Expr>,
2668        out_expr: Option<P<Expr>>,
2669    },
2670    Const {
2671        anon_const: AnonConst,
2672    },
2673    Sym {
2674        sym: InlineAsmSym,
2675    },
2676    Label {
2677        block: P<Block>,
2678    },
2679}
2680
2681impl InlineAsmOperand {
2682    pub fn reg(&self) -> Option<&InlineAsmRegOrRegClass> {
2683        match self {
2684            Self::In { reg, .. }
2685            | Self::Out { reg, .. }
2686            | Self::InOut { reg, .. }
2687            | Self::SplitInOut { reg, .. } => Some(reg),
2688            Self::Const { .. } | Self::Sym { .. } | Self::Label { .. } => None,
2689        }
2690    }
2691}
2692
2693#[derive(Clone, Copy, Encodable, Decodable, Debug, HashStable_Generic)]
2694pub enum AsmMacro {
2695    /// The `asm!` macro
2696    Asm,
2697    /// The `global_asm!` macro
2698    GlobalAsm,
2699    /// The `naked_asm!` macro
2700    NakedAsm,
2701}
2702
2703impl AsmMacro {
2704    pub const fn macro_name(self) -> &'static str {
2705        match self {
2706            AsmMacro::Asm => "asm",
2707            AsmMacro::GlobalAsm => "global_asm",
2708            AsmMacro::NakedAsm => "naked_asm",
2709        }
2710    }
2711
2712    pub const fn is_supported_option(self, option: InlineAsmOptions) -> bool {
2713        match self {
2714            AsmMacro::Asm => true,
2715            AsmMacro::GlobalAsm => InlineAsmOptions::GLOBAL_OPTIONS.contains(option),
2716            AsmMacro::NakedAsm => InlineAsmOptions::NAKED_OPTIONS.contains(option),
2717        }
2718    }
2719
2720    pub const fn diverges(self, options: InlineAsmOptions) -> bool {
2721        match self {
2722            AsmMacro::Asm => options.contains(InlineAsmOptions::NORETURN),
2723            AsmMacro::GlobalAsm => true,
2724            AsmMacro::NakedAsm => true,
2725        }
2726    }
2727}
2728
2729/// Inline assembly.
2730///
2731/// E.g., `asm!("NOP");`.
2732#[derive(Clone, Encodable, Decodable, Debug)]
2733pub struct InlineAsm {
2734    pub asm_macro: AsmMacro,
2735    pub template: Vec<InlineAsmTemplatePiece>,
2736    pub template_strs: Box<[(Symbol, Option<Symbol>, Span)]>,
2737    pub operands: Vec<(InlineAsmOperand, Span)>,
2738    pub clobber_abis: Vec<(Symbol, Span)>,
2739    pub options: InlineAsmOptions,
2740    pub line_spans: Vec<Span>,
2741}
2742
2743/// A parameter in a function header.
2744///
2745/// E.g., `bar: usize` as in `fn foo(bar: usize)`.
2746#[derive(Clone, Encodable, Decodable, Debug)]
2747pub struct Param {
2748    pub attrs: AttrVec,
2749    pub ty: P<Ty>,
2750    pub pat: P<Pat>,
2751    pub id: NodeId,
2752    pub span: Span,
2753    pub is_placeholder: bool,
2754}
2755
2756/// Alternative representation for `Arg`s describing `self` parameter of methods.
2757///
2758/// E.g., `&mut self` as in `fn foo(&mut self)`.
2759#[derive(Clone, Encodable, Decodable, Debug)]
2760pub enum SelfKind {
2761    /// `self`, `mut self`
2762    Value(Mutability),
2763    /// `&'lt self`, `&'lt mut self`
2764    Region(Option<Lifetime>, Mutability),
2765    /// `&'lt pin const self`, `&'lt pin mut self`
2766    Pinned(Option<Lifetime>, Mutability),
2767    /// `self: TYPE`, `mut self: TYPE`
2768    Explicit(P<Ty>, Mutability),
2769}
2770
2771impl SelfKind {
2772    pub fn to_ref_suggestion(&self) -> String {
2773        match self {
2774            SelfKind::Region(None, mutbl) => mutbl.ref_prefix_str().to_string(),
2775            SelfKind::Region(Some(lt), mutbl) => format!("&{lt} {}", mutbl.prefix_str()),
2776            SelfKind::Pinned(None, mutbl) => format!("&pin {}", mutbl.ptr_str()),
2777            SelfKind::Pinned(Some(lt), mutbl) => format!("&{lt} pin {}", mutbl.ptr_str()),
2778            SelfKind::Value(_) | SelfKind::Explicit(_, _) => {
2779                unreachable!("if we had an explicit self, we wouldn't be here")
2780            }
2781        }
2782    }
2783}
2784
2785pub type ExplicitSelf = Spanned<SelfKind>;
2786
2787impl Param {
2788    /// Attempts to cast parameter to `ExplicitSelf`.
2789    pub fn to_self(&self) -> Option<ExplicitSelf> {
2790        if let PatKind::Ident(BindingMode(ByRef::No, mutbl), ident, _) = self.pat.kind {
2791            if ident.name == kw::SelfLower {
2792                return match self.ty.kind {
2793                    TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
2794                    TyKind::Ref(lt, MutTy { ref ty, mutbl }) if ty.kind.is_implicit_self() => {
2795                        Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
2796                    }
2797                    TyKind::PinnedRef(lt, MutTy { ref ty, mutbl })
2798                        if ty.kind.is_implicit_self() =>
2799                    {
2800                        Some(respan(self.pat.span, SelfKind::Pinned(lt, mutbl)))
2801                    }
2802                    _ => Some(respan(
2803                        self.pat.span.to(self.ty.span),
2804                        SelfKind::Explicit(self.ty.clone(), mutbl),
2805                    )),
2806                };
2807            }
2808        }
2809        None
2810    }
2811
2812    /// Returns `true` if parameter is `self`.
2813    pub fn is_self(&self) -> bool {
2814        if let PatKind::Ident(_, ident, _) = self.pat.kind {
2815            ident.name == kw::SelfLower
2816        } else {
2817            false
2818        }
2819    }
2820
2821    /// Builds a `Param` object from `ExplicitSelf`.
2822    pub fn from_self(attrs: AttrVec, eself: ExplicitSelf, eself_ident: Ident) -> Param {
2823        let span = eself.span.to(eself_ident.span);
2824        let infer_ty = P(Ty {
2825            id: DUMMY_NODE_ID,
2826            kind: TyKind::ImplicitSelf,
2827            span: eself_ident.span,
2828            tokens: None,
2829        });
2830        let (mutbl, ty) = match eself.node {
2831            SelfKind::Explicit(ty, mutbl) => (mutbl, ty),
2832            SelfKind::Value(mutbl) => (mutbl, infer_ty),
2833            SelfKind::Region(lt, mutbl) => (
2834                Mutability::Not,
2835                P(Ty {
2836                    id: DUMMY_NODE_ID,
2837                    kind: TyKind::Ref(lt, MutTy { ty: infer_ty, mutbl }),
2838                    span,
2839                    tokens: None,
2840                }),
2841            ),
2842            SelfKind::Pinned(lt, mutbl) => (
2843                mutbl,
2844                P(Ty {
2845                    id: DUMMY_NODE_ID,
2846                    kind: TyKind::PinnedRef(lt, MutTy { ty: infer_ty, mutbl }),
2847                    span,
2848                    tokens: None,
2849                }),
2850            ),
2851        };
2852        Param {
2853            attrs,
2854            pat: P(Pat {
2855                id: DUMMY_NODE_ID,
2856                kind: PatKind::Ident(BindingMode(ByRef::No, mutbl), eself_ident, None),
2857                span,
2858                tokens: None,
2859            }),
2860            span,
2861            ty,
2862            id: DUMMY_NODE_ID,
2863            is_placeholder: false,
2864        }
2865    }
2866}
2867
2868/// A signature (not the body) of a function declaration.
2869///
2870/// E.g., `fn foo(bar: baz)`.
2871///
2872/// Please note that it's different from `FnHeader` structure
2873/// which contains metadata about function safety, asyncness, constness and ABI.
2874#[derive(Clone, Encodable, Decodable, Debug)]
2875pub struct FnDecl {
2876    pub inputs: ThinVec<Param>,
2877    pub output: FnRetTy,
2878}
2879
2880impl FnDecl {
2881    pub fn has_self(&self) -> bool {
2882        self.inputs.get(0).is_some_and(Param::is_self)
2883    }
2884    pub fn c_variadic(&self) -> bool {
2885        self.inputs.last().is_some_and(|arg| matches!(arg.ty.kind, TyKind::CVarArgs))
2886    }
2887}
2888
2889/// Is the trait definition an auto trait?
2890#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
2891pub enum IsAuto {
2892    Yes,
2893    No,
2894}
2895
2896/// Safety of items.
2897#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
2898#[derive(HashStable_Generic)]
2899pub enum Safety {
2900    /// `unsafe` an item is explicitly marked as `unsafe`.
2901    Unsafe(Span),
2902    /// `safe` an item is explicitly marked as `safe`.
2903    Safe(Span),
2904    /// Default means no value was provided, it will take a default value given the context in
2905    /// which is used.
2906    Default,
2907}
2908
2909/// Describes what kind of coroutine markers, if any, a function has.
2910///
2911/// Coroutine markers are things that cause the function to generate a coroutine, such as `async`,
2912/// which makes the function return `impl Future`, or `gen`, which makes the function return `impl
2913/// Iterator`.
2914#[derive(Copy, Clone, Encodable, Decodable, Debug)]
2915pub enum CoroutineKind {
2916    /// `async`, which returns an `impl Future`.
2917    Async { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2918    /// `gen`, which returns an `impl Iterator`.
2919    Gen { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2920    /// `async gen`, which returns an `impl AsyncIterator`.
2921    AsyncGen { span: Span, closure_id: NodeId, return_impl_trait_id: NodeId },
2922}
2923
2924impl CoroutineKind {
2925    pub fn span(self) -> Span {
2926        match self {
2927            CoroutineKind::Async { span, .. } => span,
2928            CoroutineKind::Gen { span, .. } => span,
2929            CoroutineKind::AsyncGen { span, .. } => span,
2930        }
2931    }
2932
2933    pub fn as_str(self) -> &'static str {
2934        match self {
2935            CoroutineKind::Async { .. } => "async",
2936            CoroutineKind::Gen { .. } => "gen",
2937            CoroutineKind::AsyncGen { .. } => "async gen",
2938        }
2939    }
2940
2941    pub fn closure_id(self) -> NodeId {
2942        match self {
2943            CoroutineKind::Async { closure_id, .. }
2944            | CoroutineKind::Gen { closure_id, .. }
2945            | CoroutineKind::AsyncGen { closure_id, .. } => closure_id,
2946        }
2947    }
2948
2949    /// In this case this is an `async` or `gen` return, the `NodeId` for the generated `impl Trait`
2950    /// item.
2951    pub fn return_id(self) -> (NodeId, Span) {
2952        match self {
2953            CoroutineKind::Async { return_impl_trait_id, span, .. }
2954            | CoroutineKind::Gen { return_impl_trait_id, span, .. }
2955            | CoroutineKind::AsyncGen { return_impl_trait_id, span, .. } => {
2956                (return_impl_trait_id, span)
2957            }
2958        }
2959    }
2960}
2961
2962#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
2963#[derive(HashStable_Generic)]
2964pub enum Const {
2965    Yes(Span),
2966    No,
2967}
2968
2969/// Item defaultness.
2970/// For details see the [RFC #2532](https://github.com/rust-lang/rfcs/pull/2532).
2971#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
2972pub enum Defaultness {
2973    Default(Span),
2974    Final,
2975}
2976
2977#[derive(Copy, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
2978pub enum ImplPolarity {
2979    /// `impl Trait for Type`
2980    Positive,
2981    /// `impl !Trait for Type`
2982    Negative(Span),
2983}
2984
2985impl fmt::Debug for ImplPolarity {
2986    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2987        match *self {
2988            ImplPolarity::Positive => "positive".fmt(f),
2989            ImplPolarity::Negative(_) => "negative".fmt(f),
2990        }
2991    }
2992}
2993
2994/// The polarity of a trait bound.
2995#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, Hash)]
2996#[derive(HashStable_Generic)]
2997pub enum BoundPolarity {
2998    /// `Type: Trait`
2999    Positive,
3000    /// `Type: !Trait`
3001    Negative(Span),
3002    /// `Type: ?Trait`
3003    Maybe(Span),
3004}
3005
3006impl BoundPolarity {
3007    pub fn as_str(self) -> &'static str {
3008        match self {
3009            Self::Positive => "",
3010            Self::Negative(_) => "!",
3011            Self::Maybe(_) => "?",
3012        }
3013    }
3014}
3015
3016/// The constness of a trait bound.
3017#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, Hash)]
3018#[derive(HashStable_Generic)]
3019pub enum BoundConstness {
3020    /// `Type: Trait`
3021    Never,
3022    /// `Type: const Trait`
3023    Always(Span),
3024    /// `Type: ~const Trait`
3025    Maybe(Span),
3026}
3027
3028impl BoundConstness {
3029    pub fn as_str(self) -> &'static str {
3030        match self {
3031            Self::Never => "",
3032            Self::Always(_) => "const",
3033            Self::Maybe(_) => "~const",
3034        }
3035    }
3036}
3037
3038/// The asyncness of a trait bound.
3039#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug)]
3040#[derive(HashStable_Generic)]
3041pub enum BoundAsyncness {
3042    /// `Type: Trait`
3043    Normal,
3044    /// `Type: async Trait`
3045    Async(Span),
3046}
3047
3048impl BoundAsyncness {
3049    pub fn as_str(self) -> &'static str {
3050        match self {
3051            Self::Normal => "",
3052            Self::Async(_) => "async",
3053        }
3054    }
3055}
3056
3057#[derive(Clone, Encodable, Decodable, Debug)]
3058pub enum FnRetTy {
3059    /// Returns type is not specified.
3060    ///
3061    /// Functions default to `()` and closures default to inference.
3062    /// Span points to where return type would be inserted.
3063    Default(Span),
3064    /// Everything else.
3065    Ty(P<Ty>),
3066}
3067
3068impl FnRetTy {
3069    pub fn span(&self) -> Span {
3070        match self {
3071            &FnRetTy::Default(span) => span,
3072            FnRetTy::Ty(ty) => ty.span,
3073        }
3074    }
3075}
3076
3077#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug)]
3078pub enum Inline {
3079    Yes,
3080    No,
3081}
3082
3083/// Module item kind.
3084#[derive(Clone, Encodable, Decodable, Debug)]
3085pub enum ModKind {
3086    /// Module with inlined definition `mod foo { ... }`,
3087    /// or with definition outlined to a separate file `mod foo;` and already loaded from it.
3088    /// The inner span is from the first token past `{` to the last token until `}`,
3089    /// or from the first to the last token in the loaded file.
3090    Loaded(ThinVec<P<Item>>, Inline, ModSpans, Result<(), ErrorGuaranteed>),
3091    /// Module with definition outlined to a separate file `mod foo;` but not yet loaded from it.
3092    Unloaded,
3093}
3094
3095#[derive(Copy, Clone, Encodable, Decodable, Debug, Default)]
3096pub struct ModSpans {
3097    /// `inner_span` covers the body of the module; for a file module, its the whole file.
3098    /// For an inline module, its the span inside the `{ ... }`, not including the curly braces.
3099    pub inner_span: Span,
3100    pub inject_use_span: Span,
3101}
3102
3103/// Foreign module declaration.
3104///
3105/// E.g., `extern { .. }` or `extern "C" { .. }`.
3106#[derive(Clone, Encodable, Decodable, Debug)]
3107pub struct ForeignMod {
3108    /// Span of the `extern` keyword.
3109    pub extern_span: Span,
3110    /// `unsafe` keyword accepted syntactically for macro DSLs, but not
3111    /// semantically by Rust.
3112    pub safety: Safety,
3113    pub abi: Option<StrLit>,
3114    pub items: ThinVec<P<ForeignItem>>,
3115}
3116
3117#[derive(Clone, Encodable, Decodable, Debug)]
3118pub struct EnumDef {
3119    pub variants: ThinVec<Variant>,
3120}
3121/// Enum variant.
3122#[derive(Clone, Encodable, Decodable, Debug)]
3123pub struct Variant {
3124    /// Attributes of the variant.
3125    pub attrs: AttrVec,
3126    /// Id of the variant (not the constructor, see `VariantData::ctor_id()`).
3127    pub id: NodeId,
3128    /// Span
3129    pub span: Span,
3130    /// The visibility of the variant. Syntactically accepted but not semantically.
3131    pub vis: Visibility,
3132    /// Name of the variant.
3133    pub ident: Ident,
3134
3135    /// Fields and constructor id of the variant.
3136    pub data: VariantData,
3137    /// Explicit discriminant, e.g., `Foo = 1`.
3138    pub disr_expr: Option<AnonConst>,
3139    /// Is a macro placeholder.
3140    pub is_placeholder: bool,
3141}
3142
3143/// Part of `use` item to the right of its prefix.
3144#[derive(Clone, Encodable, Decodable, Debug)]
3145pub enum UseTreeKind {
3146    /// `use prefix` or `use prefix as rename`
3147    Simple(Option<Ident>),
3148    /// `use prefix::{...}`
3149    ///
3150    /// The span represents the braces of the nested group and all elements within:
3151    ///
3152    /// ```text
3153    /// use foo::{bar, baz};
3154    ///          ^^^^^^^^^^
3155    /// ```
3156    Nested { items: ThinVec<(UseTree, NodeId)>, span: Span },
3157    /// `use prefix::*`
3158    Glob,
3159}
3160
3161/// A tree of paths sharing common prefixes.
3162/// Used in `use` items both at top-level and inside of braces in import groups.
3163#[derive(Clone, Encodable, Decodable, Debug)]
3164pub struct UseTree {
3165    pub prefix: Path,
3166    pub kind: UseTreeKind,
3167    pub span: Span,
3168}
3169
3170impl UseTree {
3171    pub fn ident(&self) -> Ident {
3172        match self.kind {
3173            UseTreeKind::Simple(Some(rename)) => rename,
3174            UseTreeKind::Simple(None) => {
3175                self.prefix.segments.last().expect("empty prefix in a simple import").ident
3176            }
3177            _ => panic!("`UseTree::ident` can only be used on a simple import"),
3178        }
3179    }
3180}
3181
3182/// Distinguishes between `Attribute`s that decorate items and Attributes that
3183/// are contained as statements within items. These two cases need to be
3184/// distinguished for pretty-printing.
3185#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy, HashStable_Generic)]
3186pub enum AttrStyle {
3187    Outer,
3188    Inner,
3189}
3190
3191/// A list of attributes.
3192pub type AttrVec = ThinVec<Attribute>;
3193
3194/// A syntax-level representation of an attribute.
3195#[derive(Clone, Encodable, Decodable, Debug)]
3196pub struct Attribute {
3197    pub kind: AttrKind,
3198    pub id: AttrId,
3199    /// Denotes if the attribute decorates the following construct (outer)
3200    /// or the construct this attribute is contained within (inner).
3201    pub style: AttrStyle,
3202    pub span: Span,
3203}
3204
3205#[derive(Clone, Encodable, Decodable, Debug)]
3206pub enum AttrKind {
3207    /// A normal attribute.
3208    Normal(P<NormalAttr>),
3209
3210    /// A doc comment (e.g. `/// ...`, `//! ...`, `/** ... */`, `/*! ... */`).
3211    /// Doc attributes (e.g. `#[doc="..."]`) are represented with the `Normal`
3212    /// variant (which is much less compact and thus more expensive).
3213    DocComment(CommentKind, Symbol),
3214}
3215
3216#[derive(Clone, Encodable, Decodable, Debug)]
3217pub struct NormalAttr {
3218    pub item: AttrItem,
3219    // Tokens for the full attribute, e.g. `#[foo]`, `#![bar]`.
3220    pub tokens: Option<LazyAttrTokenStream>,
3221}
3222
3223impl NormalAttr {
3224    pub fn from_ident(ident: Ident) -> Self {
3225        Self {
3226            item: AttrItem {
3227                unsafety: Safety::Default,
3228                path: Path::from_ident(ident),
3229                args: AttrArgs::Empty,
3230                tokens: None,
3231            },
3232            tokens: None,
3233        }
3234    }
3235}
3236
3237#[derive(Clone, Encodable, Decodable, Debug)]
3238pub struct AttrItem {
3239    pub unsafety: Safety,
3240    pub path: Path,
3241    pub args: AttrArgs,
3242    // Tokens for the meta item, e.g. just the `foo` within `#[foo]` or `#![foo]`.
3243    pub tokens: Option<LazyAttrTokenStream>,
3244}
3245
3246impl AttrItem {
3247    pub fn is_valid_for_outer_style(&self) -> bool {
3248        self.path == sym::cfg_attr
3249            || self.path == sym::cfg
3250            || self.path == sym::forbid
3251            || self.path == sym::warn
3252            || self.path == sym::allow
3253            || self.path == sym::deny
3254    }
3255}
3256
3257/// `TraitRef`s appear in impls.
3258///
3259/// Resolution maps each `TraitRef`'s `ref_id` to its defining trait; that's all
3260/// that the `ref_id` is for. The `impl_id` maps to the "self type" of this impl.
3261/// If this impl is an `ItemKind::Impl`, the `impl_id` is redundant (it could be the
3262/// same as the impl's `NodeId`).
3263#[derive(Clone, Encodable, Decodable, Debug)]
3264pub struct TraitRef {
3265    pub path: Path,
3266    pub ref_id: NodeId,
3267}
3268
3269#[derive(Clone, Encodable, Decodable, Debug)]
3270pub struct PolyTraitRef {
3271    /// The `'a` in `for<'a> Foo<&'a T>`.
3272    pub bound_generic_params: ThinVec<GenericParam>,
3273
3274    // Optional constness, asyncness, or polarity.
3275    pub modifiers: TraitBoundModifiers,
3276
3277    /// The `Foo<&'a T>` in `<'a> Foo<&'a T>`.
3278    pub trait_ref: TraitRef,
3279
3280    pub span: Span,
3281}
3282
3283impl PolyTraitRef {
3284    pub fn new(
3285        generic_params: ThinVec<GenericParam>,
3286        path: Path,
3287        modifiers: TraitBoundModifiers,
3288        span: Span,
3289    ) -> Self {
3290        PolyTraitRef {
3291            bound_generic_params: generic_params,
3292            modifiers,
3293            trait_ref: TraitRef { path, ref_id: DUMMY_NODE_ID },
3294            span,
3295        }
3296    }
3297}
3298
3299#[derive(Clone, Encodable, Decodable, Debug)]
3300pub struct Visibility {
3301    pub kind: VisibilityKind,
3302    pub span: Span,
3303    pub tokens: Option<LazyAttrTokenStream>,
3304}
3305
3306#[derive(Clone, Encodable, Decodable, Debug)]
3307pub enum VisibilityKind {
3308    Public,
3309    Restricted { path: P<Path>, id: NodeId, shorthand: bool },
3310    Inherited,
3311}
3312
3313impl VisibilityKind {
3314    pub fn is_pub(&self) -> bool {
3315        matches!(self, VisibilityKind::Public)
3316    }
3317}
3318
3319/// Field definition in a struct, variant or union.
3320///
3321/// E.g., `bar: usize` as in `struct Foo { bar: usize }`.
3322#[derive(Clone, Encodable, Decodable, Debug)]
3323pub struct FieldDef {
3324    pub attrs: AttrVec,
3325    pub id: NodeId,
3326    pub span: Span,
3327    pub vis: Visibility,
3328    pub safety: Safety,
3329    pub ident: Option<Ident>,
3330
3331    pub ty: P<Ty>,
3332    pub default: Option<AnonConst>,
3333    pub is_placeholder: bool,
3334}
3335
3336/// Was parsing recovery performed?
3337#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic)]
3338pub enum Recovered {
3339    No,
3340    Yes(ErrorGuaranteed),
3341}
3342
3343/// Fields and constructor ids of enum variants and structs.
3344#[derive(Clone, Encodable, Decodable, Debug)]
3345pub enum VariantData {
3346    /// Struct variant.
3347    ///
3348    /// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
3349    Struct { fields: ThinVec<FieldDef>, recovered: Recovered },
3350    /// Tuple variant.
3351    ///
3352    /// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
3353    Tuple(ThinVec<FieldDef>, NodeId),
3354    /// Unit variant.
3355    ///
3356    /// E.g., `Bar = ..` as in `enum Foo { Bar = .. }`.
3357    Unit(NodeId),
3358}
3359
3360impl VariantData {
3361    /// Return the fields of this variant.
3362    pub fn fields(&self) -> &[FieldDef] {
3363        match self {
3364            VariantData::Struct { fields, .. } | VariantData::Tuple(fields, _) => fields,
3365            _ => &[],
3366        }
3367    }
3368
3369    /// Return the `NodeId` of this variant's constructor, if it has one.
3370    pub fn ctor_node_id(&self) -> Option<NodeId> {
3371        match *self {
3372            VariantData::Struct { .. } => None,
3373            VariantData::Tuple(_, id) | VariantData::Unit(id) => Some(id),
3374        }
3375    }
3376}
3377
3378/// An item definition.
3379#[derive(Clone, Encodable, Decodable, Debug)]
3380pub struct Item<K = ItemKind> {
3381    pub attrs: AttrVec,
3382    pub id: NodeId,
3383    pub span: Span,
3384    pub vis: Visibility,
3385
3386    pub kind: K,
3387
3388    /// Original tokens this item was parsed from. This isn't necessarily
3389    /// available for all items, although over time more and more items should
3390    /// have this be `Some`. Right now this is primarily used for procedural
3391    /// macros, notably custom attributes.
3392    ///
3393    /// Note that the tokens here do not include the outer attributes, but will
3394    /// include inner attributes.
3395    pub tokens: Option<LazyAttrTokenStream>,
3396}
3397
3398impl Item {
3399    /// Return the span that encompasses the attributes.
3400    pub fn span_with_attributes(&self) -> Span {
3401        self.attrs.iter().fold(self.span, |acc, attr| acc.to(attr.span))
3402    }
3403
3404    pub fn opt_generics(&self) -> Option<&Generics> {
3405        match &self.kind {
3406            ItemKind::ExternCrate(..)
3407            | ItemKind::Use(_)
3408            | ItemKind::Mod(..)
3409            | ItemKind::ForeignMod(_)
3410            | ItemKind::GlobalAsm(_)
3411            | ItemKind::MacCall(_)
3412            | ItemKind::Delegation(_)
3413            | ItemKind::DelegationMac(_)
3414            | ItemKind::MacroDef(..) => None,
3415            ItemKind::Static(_) => None,
3416            ItemKind::Const(i) => Some(&i.generics),
3417            ItemKind::Fn(i) => Some(&i.generics),
3418            ItemKind::TyAlias(i) => Some(&i.generics),
3419            ItemKind::TraitAlias(_, generics, _)
3420            | ItemKind::Enum(_, _, generics)
3421            | ItemKind::Struct(_, _, generics)
3422            | ItemKind::Union(_, _, generics) => Some(&generics),
3423            ItemKind::Trait(i) => Some(&i.generics),
3424            ItemKind::Impl(i) => Some(&i.generics),
3425        }
3426    }
3427}
3428
3429/// `extern` qualifier on a function item or function type.
3430#[derive(Clone, Copy, Encodable, Decodable, Debug)]
3431pub enum Extern {
3432    /// No explicit extern keyword was used.
3433    ///
3434    /// E.g. `fn foo() {}`.
3435    None,
3436    /// An explicit extern keyword was used, but with implicit ABI.
3437    ///
3438    /// E.g. `extern fn foo() {}`.
3439    ///
3440    /// This is just `extern "C"` (see `rustc_abi::ExternAbi::FALLBACK`).
3441    Implicit(Span),
3442    /// An explicit extern keyword was used with an explicit ABI.
3443    ///
3444    /// E.g. `extern "C" fn foo() {}`.
3445    Explicit(StrLit, Span),
3446}
3447
3448impl Extern {
3449    pub fn from_abi(abi: Option<StrLit>, span: Span) -> Extern {
3450        match abi {
3451            Some(name) => Extern::Explicit(name, span),
3452            None => Extern::Implicit(span),
3453        }
3454    }
3455}
3456
3457/// A function header.
3458///
3459/// All the information between the visibility and the name of the function is
3460/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
3461#[derive(Clone, Copy, Encodable, Decodable, Debug)]
3462pub struct FnHeader {
3463    /// Whether this is `unsafe`, or has a default safety.
3464    pub safety: Safety,
3465    /// Whether this is `async`, `gen`, or nothing.
3466    pub coroutine_kind: Option<CoroutineKind>,
3467    /// The `const` keyword, if any
3468    pub constness: Const,
3469    /// The `extern` keyword and corresponding ABI string, if any.
3470    pub ext: Extern,
3471}
3472
3473impl FnHeader {
3474    /// Does this function header have any qualifiers or is it empty?
3475    pub fn has_qualifiers(&self) -> bool {
3476        let Self { safety, coroutine_kind, constness, ext } = self;
3477        matches!(safety, Safety::Unsafe(_))
3478            || coroutine_kind.is_some()
3479            || matches!(constness, Const::Yes(_))
3480            || !matches!(ext, Extern::None)
3481    }
3482}
3483
3484impl Default for FnHeader {
3485    fn default() -> FnHeader {
3486        FnHeader {
3487            safety: Safety::Default,
3488            coroutine_kind: None,
3489            constness: Const::No,
3490            ext: Extern::None,
3491        }
3492    }
3493}
3494
3495#[derive(Clone, Encodable, Decodable, Debug)]
3496pub struct Trait {
3497    pub safety: Safety,
3498    pub is_auto: IsAuto,
3499    pub ident: Ident,
3500    pub generics: Generics,
3501    pub bounds: GenericBounds,
3502    pub items: ThinVec<P<AssocItem>>,
3503}
3504
3505/// The location of a where clause on a `TyAlias` (`Span`) and whether there was
3506/// a `where` keyword (`bool`). This is split out from `WhereClause`, since there
3507/// are two locations for where clause on type aliases, but their predicates
3508/// are concatenated together.
3509///
3510/// Take this example:
3511/// ```ignore (only-for-syntax-highlight)
3512/// trait Foo {
3513///   type Assoc<'a, 'b> where Self: 'a, Self: 'b;
3514/// }
3515/// impl Foo for () {
3516///   type Assoc<'a, 'b> where Self: 'a = () where Self: 'b;
3517///   //                 ^^^^^^^^^^^^^^ first where clause
3518///   //                                     ^^^^^^^^^^^^^^ second where clause
3519/// }
3520/// ```
3521///
3522/// If there is no where clause, then this is `false` with `DUMMY_SP`.
3523#[derive(Copy, Clone, Encodable, Decodable, Debug, Default)]
3524pub struct TyAliasWhereClause {
3525    pub has_where_token: bool,
3526    pub span: Span,
3527}
3528
3529/// The span information for the two where clauses on a `TyAlias`.
3530#[derive(Copy, Clone, Encodable, Decodable, Debug, Default)]
3531pub struct TyAliasWhereClauses {
3532    /// Before the equals sign.
3533    pub before: TyAliasWhereClause,
3534    /// After the equals sign.
3535    pub after: TyAliasWhereClause,
3536    /// The index in `TyAlias.generics.where_clause.predicates` that would split
3537    /// into predicates from the where clause before the equals sign and the ones
3538    /// from the where clause after the equals sign.
3539    pub split: usize,
3540}
3541
3542#[derive(Clone, Encodable, Decodable, Debug)]
3543pub struct TyAlias {
3544    pub defaultness: Defaultness,
3545    pub ident: Ident,
3546    pub generics: Generics,
3547    pub where_clauses: TyAliasWhereClauses,
3548    pub bounds: GenericBounds,
3549    pub ty: Option<P<Ty>>,
3550}
3551
3552#[derive(Clone, Encodable, Decodable, Debug)]
3553pub struct Impl {
3554    pub defaultness: Defaultness,
3555    pub safety: Safety,
3556    pub generics: Generics,
3557    pub constness: Const,
3558    pub polarity: ImplPolarity,
3559    /// The trait being implemented, if any.
3560    pub of_trait: Option<TraitRef>,
3561    pub self_ty: P<Ty>,
3562    pub items: ThinVec<P<AssocItem>>,
3563}
3564
3565#[derive(Clone, Encodable, Decodable, Debug, Default)]
3566pub struct FnContract {
3567    pub requires: Option<P<Expr>>,
3568    pub ensures: Option<P<Expr>>,
3569}
3570
3571#[derive(Clone, Encodable, Decodable, Debug)]
3572pub struct Fn {
3573    pub defaultness: Defaultness,
3574    pub ident: Ident,
3575    pub generics: Generics,
3576    pub sig: FnSig,
3577    pub contract: Option<P<FnContract>>,
3578    pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
3579    pub body: Option<P<Block>>,
3580}
3581
3582#[derive(Clone, Encodable, Decodable, Debug)]
3583pub struct Delegation {
3584    /// Path resolution id.
3585    pub id: NodeId,
3586    pub qself: Option<P<QSelf>>,
3587    pub path: Path,
3588    pub ident: Ident,
3589    pub rename: Option<Ident>,
3590    pub body: Option<P<Block>>,
3591    /// The item was expanded from a glob delegation item.
3592    pub from_glob: bool,
3593}
3594
3595#[derive(Clone, Encodable, Decodable, Debug)]
3596pub struct DelegationMac {
3597    pub qself: Option<P<QSelf>>,
3598    pub prefix: Path,
3599    // Some for list delegation, and None for glob delegation.
3600    pub suffixes: Option<ThinVec<(Ident, Option<Ident>)>>,
3601    pub body: Option<P<Block>>,
3602}
3603
3604#[derive(Clone, Encodable, Decodable, Debug)]
3605pub struct StaticItem {
3606    pub ident: Ident,
3607    pub ty: P<Ty>,
3608    pub safety: Safety,
3609    pub mutability: Mutability,
3610    pub expr: Option<P<Expr>>,
3611    pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
3612}
3613
3614#[derive(Clone, Encodable, Decodable, Debug)]
3615pub struct ConstItem {
3616    pub defaultness: Defaultness,
3617    pub ident: Ident,
3618    pub generics: Generics,
3619    pub ty: P<Ty>,
3620    pub expr: Option<P<Expr>>,
3621    pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
3622}
3623
3624// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.
3625#[derive(Clone, Encodable, Decodable, Debug)]
3626pub enum ItemKind {
3627    /// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
3628    ///
3629    /// E.g., `extern crate foo` or `extern crate foo_bar as foo`.
3630    ExternCrate(Option<Symbol>, Ident),
3631    /// A use declaration item (`use`).
3632    ///
3633    /// E.g., `use foo;`, `use foo::bar;` or `use foo::bar as FooBar;`.
3634    Use(UseTree),
3635    /// A static item (`static`).
3636    ///
3637    /// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`.
3638    Static(Box<StaticItem>),
3639    /// A constant item (`const`).
3640    ///
3641    /// E.g., `const FOO: i32 = 42;`.
3642    Const(Box<ConstItem>),
3643    /// A function declaration (`fn`).
3644    ///
3645    /// E.g., `fn foo(bar: usize) -> usize { .. }`.
3646    Fn(Box<Fn>),
3647    /// A module declaration (`mod`).
3648    ///
3649    /// E.g., `mod foo;` or `mod foo { .. }`.
3650    /// `unsafe` keyword on modules is accepted syntactically for macro DSLs, but not
3651    /// semantically by Rust.
3652    Mod(Safety, Ident, ModKind),
3653    /// An external module (`extern`).
3654    ///
3655    /// E.g., `extern {}` or `extern "C" {}`.
3656    ForeignMod(ForeignMod),
3657    /// Module-level inline assembly (from `global_asm!()`).
3658    GlobalAsm(Box<InlineAsm>),
3659    /// A type alias (`type`).
3660    ///
3661    /// E.g., `type Foo = Bar<u8>;`.
3662    TyAlias(Box<TyAlias>),
3663    /// An enum definition (`enum`).
3664    ///
3665    /// E.g., `enum Foo<A, B> { C<A>, D<B> }`.
3666    Enum(Ident, EnumDef, Generics),
3667    /// A struct definition (`struct`).
3668    ///
3669    /// E.g., `struct Foo<A> { x: A }`.
3670    Struct(Ident, VariantData, Generics),
3671    /// A union definition (`union`).
3672    ///
3673    /// E.g., `union Foo<A, B> { x: A, y: B }`.
3674    Union(Ident, VariantData, Generics),
3675    /// A trait declaration (`trait`).
3676    ///
3677    /// E.g., `trait Foo { .. }`, `trait Foo<T> { .. }` or `auto trait Foo {}`.
3678    Trait(Box<Trait>),
3679    /// Trait alias.
3680    ///
3681    /// E.g., `trait Foo = Bar + Quux;`.
3682    TraitAlias(Ident, Generics, GenericBounds),
3683    /// An implementation.
3684    ///
3685    /// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
3686    Impl(Box<Impl>),
3687    /// A macro invocation.
3688    ///
3689    /// E.g., `foo!(..)`.
3690    MacCall(P<MacCall>),
3691
3692    /// A macro definition.
3693    MacroDef(Ident, MacroDef),
3694
3695    /// A single delegation item (`reuse`).
3696    ///
3697    /// E.g. `reuse <Type as Trait>::name { target_expr_template }`.
3698    Delegation(Box<Delegation>),
3699    /// A list or glob delegation item (`reuse prefix::{a, b, c}`, `reuse prefix::*`).
3700    /// Treated similarly to a macro call and expanded early.
3701    DelegationMac(Box<DelegationMac>),
3702}
3703
3704impl ItemKind {
3705    pub fn ident(&self) -> Option<Ident> {
3706        match *self {
3707            ItemKind::ExternCrate(_, ident)
3708            | ItemKind::Static(box StaticItem { ident, .. })
3709            | ItemKind::Const(box ConstItem { ident, .. })
3710            | ItemKind::Fn(box Fn { ident, .. })
3711            | ItemKind::Mod(_, ident, _)
3712            | ItemKind::TyAlias(box TyAlias { ident, .. })
3713            | ItemKind::Enum(ident, ..)
3714            | ItemKind::Struct(ident, ..)
3715            | ItemKind::Union(ident, ..)
3716            | ItemKind::Trait(box Trait { ident, .. })
3717            | ItemKind::TraitAlias(ident, ..)
3718            | ItemKind::MacroDef(ident, _)
3719            | ItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
3720
3721            ItemKind::Use(_)
3722            | ItemKind::ForeignMod(_)
3723            | ItemKind::GlobalAsm(_)
3724            | ItemKind::Impl(_)
3725            | ItemKind::MacCall(_)
3726            | ItemKind::DelegationMac(_) => None,
3727        }
3728    }
3729
3730    /// "a" or "an"
3731    pub fn article(&self) -> &'static str {
3732        use ItemKind::*;
3733        match self {
3734            Use(..) | Static(..) | Const(..) | Fn(..) | Mod(..) | GlobalAsm(..) | TyAlias(..)
3735            | Struct(..) | Union(..) | Trait(..) | TraitAlias(..) | MacroDef(..)
3736            | Delegation(..) | DelegationMac(..) => "a",
3737            ExternCrate(..) | ForeignMod(..) | MacCall(..) | Enum(..) | Impl { .. } => "an",
3738        }
3739    }
3740
3741    pub fn descr(&self) -> &'static str {
3742        match self {
3743            ItemKind::ExternCrate(..) => "extern crate",
3744            ItemKind::Use(..) => "`use` import",
3745            ItemKind::Static(..) => "static item",
3746            ItemKind::Const(..) => "constant item",
3747            ItemKind::Fn(..) => "function",
3748            ItemKind::Mod(..) => "module",
3749            ItemKind::ForeignMod(..) => "extern block",
3750            ItemKind::GlobalAsm(..) => "global asm item",
3751            ItemKind::TyAlias(..) => "type alias",
3752            ItemKind::Enum(..) => "enum",
3753            ItemKind::Struct(..) => "struct",
3754            ItemKind::Union(..) => "union",
3755            ItemKind::Trait(..) => "trait",
3756            ItemKind::TraitAlias(..) => "trait alias",
3757            ItemKind::MacCall(..) => "item macro invocation",
3758            ItemKind::MacroDef(..) => "macro definition",
3759            ItemKind::Impl { .. } => "implementation",
3760            ItemKind::Delegation(..) => "delegated function",
3761            ItemKind::DelegationMac(..) => "delegation",
3762        }
3763    }
3764
3765    pub fn generics(&self) -> Option<&Generics> {
3766        match self {
3767            Self::Fn(box Fn { generics, .. })
3768            | Self::TyAlias(box TyAlias { generics, .. })
3769            | Self::Const(box ConstItem { generics, .. })
3770            | Self::Enum(_, _, generics)
3771            | Self::Struct(_, _, generics)
3772            | Self::Union(_, _, generics)
3773            | Self::Trait(box Trait { generics, .. })
3774            | Self::TraitAlias(_, generics, _)
3775            | Self::Impl(box Impl { generics, .. }) => Some(generics),
3776            _ => None,
3777        }
3778    }
3779}
3780
3781/// Represents associated items.
3782/// These include items in `impl` and `trait` definitions.
3783pub type AssocItem = Item<AssocItemKind>;
3784
3785/// Represents associated item kinds.
3786///
3787/// The term "provided" in the variants below refers to the item having a default
3788/// definition / body. Meanwhile, a "required" item lacks a definition / body.
3789/// In an implementation, all items must be provided.
3790/// The `Option`s below denote the bodies, where `Some(_)`
3791/// means "provided" and conversely `None` means "required".
3792#[derive(Clone, Encodable, Decodable, Debug)]
3793pub enum AssocItemKind {
3794    /// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
3795    /// If `def` is parsed, then the constant is provided, and otherwise required.
3796    Const(Box<ConstItem>),
3797    /// An associated function.
3798    Fn(Box<Fn>),
3799    /// An associated type.
3800    Type(Box<TyAlias>),
3801    /// A macro expanding to associated items.
3802    MacCall(P<MacCall>),
3803    /// An associated delegation item.
3804    Delegation(Box<Delegation>),
3805    /// An associated list or glob delegation item.
3806    DelegationMac(Box<DelegationMac>),
3807}
3808
3809impl AssocItemKind {
3810    pub fn ident(&self) -> Option<Ident> {
3811        match *self {
3812            AssocItemKind::Const(box ConstItem { ident, .. })
3813            | AssocItemKind::Fn(box Fn { ident, .. })
3814            | AssocItemKind::Type(box TyAlias { ident, .. })
3815            | AssocItemKind::Delegation(box Delegation { ident, .. }) => Some(ident),
3816
3817            AssocItemKind::MacCall(_) | AssocItemKind::DelegationMac(_) => None,
3818        }
3819    }
3820
3821    pub fn defaultness(&self) -> Defaultness {
3822        match *self {
3823            Self::Const(box ConstItem { defaultness, .. })
3824            | Self::Fn(box Fn { defaultness, .. })
3825            | Self::Type(box TyAlias { defaultness, .. }) => defaultness,
3826            Self::MacCall(..) | Self::Delegation(..) | Self::DelegationMac(..) => {
3827                Defaultness::Final
3828            }
3829        }
3830    }
3831}
3832
3833impl From<AssocItemKind> for ItemKind {
3834    fn from(assoc_item_kind: AssocItemKind) -> ItemKind {
3835        match assoc_item_kind {
3836            AssocItemKind::Const(item) => ItemKind::Const(item),
3837            AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
3838            AssocItemKind::Type(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
3839            AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
3840            AssocItemKind::Delegation(delegation) => ItemKind::Delegation(delegation),
3841            AssocItemKind::DelegationMac(delegation) => ItemKind::DelegationMac(delegation),
3842        }
3843    }
3844}
3845
3846impl TryFrom<ItemKind> for AssocItemKind {
3847    type Error = ItemKind;
3848
3849    fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
3850        Ok(match item_kind {
3851            ItemKind::Const(item) => AssocItemKind::Const(item),
3852            ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
3853            ItemKind::TyAlias(ty_kind) => AssocItemKind::Type(ty_kind),
3854            ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
3855            ItemKind::Delegation(d) => AssocItemKind::Delegation(d),
3856            ItemKind::DelegationMac(d) => AssocItemKind::DelegationMac(d),
3857            _ => return Err(item_kind),
3858        })
3859    }
3860}
3861
3862/// An item in `extern` block.
3863#[derive(Clone, Encodable, Decodable, Debug)]
3864pub enum ForeignItemKind {
3865    /// A foreign static item (`static FOO: u8`).
3866    Static(Box<StaticItem>),
3867    /// A foreign function.
3868    Fn(Box<Fn>),
3869    /// A foreign type.
3870    TyAlias(Box<TyAlias>),
3871    /// A macro expanding to foreign items.
3872    MacCall(P<MacCall>),
3873}
3874
3875impl ForeignItemKind {
3876    pub fn ident(&self) -> Option<Ident> {
3877        match *self {
3878            ForeignItemKind::Static(box StaticItem { ident, .. })
3879            | ForeignItemKind::Fn(box Fn { ident, .. })
3880            | ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => Some(ident),
3881
3882            ForeignItemKind::MacCall(_) => None,
3883        }
3884    }
3885}
3886
3887impl From<ForeignItemKind> for ItemKind {
3888    fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
3889        match foreign_item_kind {
3890            ForeignItemKind::Static(box static_foreign_item) => {
3891                ItemKind::Static(Box::new(static_foreign_item))
3892            }
3893            ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
3894            ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
3895            ForeignItemKind::MacCall(a) => ItemKind::MacCall(a),
3896        }
3897    }
3898}
3899
3900impl TryFrom<ItemKind> for ForeignItemKind {
3901    type Error = ItemKind;
3902
3903    fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
3904        Ok(match item_kind {
3905            ItemKind::Static(box static_item) => ForeignItemKind::Static(Box::new(static_item)),
3906            ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
3907            ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
3908            ItemKind::MacCall(a) => ForeignItemKind::MacCall(a),
3909            _ => return Err(item_kind),
3910        })
3911    }
3912}
3913
3914pub type ForeignItem = Item<ForeignItemKind>;
3915
3916// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
3917#[cfg(target_pointer_width = "64")]
3918mod size_asserts {
3919    use rustc_data_structures::static_assert_size;
3920
3921    use super::*;
3922    // tidy-alphabetical-start
3923    static_assert_size!(AssocItem, 80);
3924    static_assert_size!(AssocItemKind, 16);
3925    static_assert_size!(Attribute, 32);
3926    static_assert_size!(Block, 32);
3927    static_assert_size!(Expr, 72);
3928    static_assert_size!(ExprKind, 40);
3929    static_assert_size!(Fn, 184);
3930    static_assert_size!(ForeignItem, 80);
3931    static_assert_size!(ForeignItemKind, 16);
3932    static_assert_size!(GenericArg, 24);
3933    static_assert_size!(GenericBound, 88);
3934    static_assert_size!(Generics, 40);
3935    static_assert_size!(Impl, 136);
3936    static_assert_size!(Item, 144);
3937    static_assert_size!(ItemKind, 80);
3938    static_assert_size!(LitKind, 24);
3939    static_assert_size!(Local, 96);
3940    static_assert_size!(MetaItemLit, 40);
3941    static_assert_size!(Param, 40);
3942    static_assert_size!(Pat, 72);
3943    static_assert_size!(Path, 24);
3944    static_assert_size!(PathSegment, 24);
3945    static_assert_size!(PatKind, 48);
3946    static_assert_size!(Stmt, 32);
3947    static_assert_size!(StmtKind, 16);
3948    static_assert_size!(Ty, 64);
3949    static_assert_size!(TyKind, 40);
3950    // tidy-alphabetical-end
3951}