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