rustc_hir/
hir.rs

1// ignore-tidy-filelength
2use std::borrow::Cow;
3use std::fmt;
4
5use rustc_abi::ExternAbi;
6use rustc_ast::attr::AttributeExt;
7use rustc_ast::token::CommentKind;
8use rustc_ast::util::parser::ExprPrecedence;
9use rustc_ast::{
10    self as ast, FloatTy, InlineAsmOptions, InlineAsmTemplatePiece, IntTy, Label, LitIntType,
11    LitKind, TraitObjectSyntax, UintTy, UnsafeBinderCastKind, join_path_idents,
12};
13pub use rustc_ast::{
14    AssignOp, AssignOpKind, AttrId, AttrStyle, BinOp, BinOpKind, BindingMode, BorrowKind,
15    BoundConstness, BoundPolarity, ByRef, CaptureBy, DelimArgs, ImplPolarity, IsAuto,
16    MetaItemInner, MetaItemLit, Movability, Mutability, UnOp,
17};
18use rustc_data_structures::fingerprint::Fingerprint;
19use rustc_data_structures::sorted_map::SortedMap;
20use rustc_data_structures::tagged_ptr::TaggedRef;
21use rustc_error_messages::{DiagArgValue, IntoDiagArg};
22use rustc_index::IndexVec;
23use rustc_macros::{Decodable, Encodable, HashStable_Generic};
24use rustc_span::def_id::LocalDefId;
25use rustc_span::source_map::Spanned;
26use rustc_span::{BytePos, DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym};
27use rustc_target::asm::InlineAsmRegOrRegClass;
28use smallvec::SmallVec;
29use thin_vec::ThinVec;
30use tracing::debug;
31
32use crate::LangItem;
33use crate::attrs::AttributeKind;
34use crate::def::{CtorKind, DefKind, MacroKinds, PerNS, Res};
35use crate::def_id::{DefId, LocalDefIdMap};
36pub(crate) use crate::hir_id::{HirId, ItemLocalId, ItemLocalMap, OwnerId};
37use crate::intravisit::{FnKind, VisitorExt};
38use crate::lints::DelayedLints;
39
40#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
41pub enum AngleBrackets {
42    /// E.g. `Path`.
43    Missing,
44    /// E.g. `Path<>`.
45    Empty,
46    /// E.g. `Path<T>`.
47    Full,
48}
49
50#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
51pub enum LifetimeSource {
52    /// E.g. `&Type`, `&'_ Type`, `&'a Type`, `&mut Type`, `&'_ mut Type`, `&'a mut Type`
53    Reference,
54
55    /// E.g. `ContainsLifetime`, `ContainsLifetime<>`, `ContainsLifetime<'_>`,
56    /// `ContainsLifetime<'a>`
57    Path { angle_brackets: AngleBrackets },
58
59    /// E.g. `impl Trait + '_`, `impl Trait + 'a`
60    OutlivesBound,
61
62    /// E.g. `impl Trait + use<'_>`, `impl Trait + use<'a>`
63    PreciseCapturing,
64
65    /// Other usages which have not yet been categorized. Feel free to
66    /// add new sources that you find useful.
67    ///
68    /// Some non-exhaustive examples:
69    /// - `where T: 'a`
70    /// - `fn(_: dyn Trait + 'a)`
71    Other,
72}
73
74#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
75pub enum LifetimeSyntax {
76    /// E.g. `&Type`, `ContainsLifetime`
77    Implicit,
78
79    /// E.g. `&'_ Type`, `ContainsLifetime<'_>`, `impl Trait + '_`, `impl Trait + use<'_>`
80    ExplicitAnonymous,
81
82    /// E.g. `&'a Type`, `ContainsLifetime<'a>`, `impl Trait + 'a`, `impl Trait + use<'a>`
83    ExplicitBound,
84}
85
86impl From<Ident> for LifetimeSyntax {
87    fn from(ident: Ident) -> Self {
88        let name = ident.name;
89
90        if name == sym::empty {
91            unreachable!("A lifetime name should never be empty");
92        } else if name == kw::UnderscoreLifetime {
93            LifetimeSyntax::ExplicitAnonymous
94        } else {
95            debug_assert!(name.as_str().starts_with('\''));
96            LifetimeSyntax::ExplicitBound
97        }
98    }
99}
100
101/// A lifetime. The valid field combinations are non-obvious and not all
102/// combinations are possible. The following example shows some of
103/// them. See also the comments on `LifetimeKind` and `LifetimeSource`.
104///
105/// ```
106/// #[repr(C)]
107/// struct S<'a>(&'a u32);       // res=Param, name='a, source=Reference, syntax=ExplicitBound
108/// unsafe extern "C" {
109///     fn f1(s: S);             // res=Param, name='_, source=Path, syntax=Implicit
110///     fn f2(s: S<'_>);         // res=Param, name='_, source=Path, syntax=ExplicitAnonymous
111///     fn f3<'a>(s: S<'a>);     // res=Param, name='a, source=Path, syntax=ExplicitBound
112/// }
113///
114/// struct St<'a> { x: &'a u32 } // res=Param, name='a, source=Reference, syntax=ExplicitBound
115/// fn f() {
116///     _ = St { x: &0 };        // res=Infer, name='_, source=Path, syntax=Implicit
117///     _ = St::<'_> { x: &0 };  // res=Infer, name='_, source=Path, syntax=ExplicitAnonymous
118/// }
119///
120/// struct Name<'a>(&'a str);    // res=Param,  name='a, source=Reference, syntax=ExplicitBound
121/// const A: Name = Name("a");   // res=Static, name='_, source=Path, syntax=Implicit
122/// const B: &str = "";          // res=Static, name='_, source=Reference, syntax=Implicit
123/// static C: &'_ str = "";      // res=Static, name='_, source=Reference, syntax=ExplicitAnonymous
124/// static D: &'static str = ""; // res=Static, name='static, source=Reference, syntax=ExplicitBound
125///
126/// trait Tr {}
127/// fn tr(_: Box<dyn Tr>) {}     // res=ImplicitObjectLifetimeDefault, name='_, source=Other, syntax=Implicit
128///
129/// fn capture_outlives<'a>() ->
130///     impl FnOnce() + 'a       // res=Param, ident='a, source=OutlivesBound, syntax=ExplicitBound
131/// {
132///     || {}
133/// }
134///
135/// fn capture_precise<'a>() ->
136///     impl FnOnce() + use<'a>  // res=Param, ident='a, source=PreciseCapturing, syntax=ExplicitBound
137/// {
138///     || {}
139/// }
140///
141/// // (commented out because these cases trigger errors)
142/// // struct S1<'a>(&'a str);   // res=Param, name='a, source=Reference, syntax=ExplicitBound
143/// // struct S2(S1);            // res=Error, name='_, source=Path, syntax=Implicit
144/// // struct S3(S1<'_>);        // res=Error, name='_, source=Path, syntax=ExplicitAnonymous
145/// // struct S4(S1<'a>);        // res=Error, name='a, source=Path, syntax=ExplicitBound
146/// ```
147///
148/// Some combinations that cannot occur are `LifetimeSyntax::Implicit` with
149/// `LifetimeSource::OutlivesBound` or `LifetimeSource::PreciseCapturing`
150/// — there's no way to "elide" these lifetimes.
151#[derive(Debug, Copy, Clone, HashStable_Generic)]
152// Raise the aligement to at least 4 bytes - this is relied on in other parts of the compiler(for pointer tagging):
153// https://github.com/rust-lang/rust/blob/ce5fdd7d42aba9a2925692e11af2bd39cf37798a/compiler/rustc_data_structures/src/tagged_ptr.rs#L163
154// Removing this `repr(4)` will cause the compiler to not build on platforms like `m68k` Linux, where the aligement of u32 and usize is only 2.
155// Since `repr(align)` may only raise aligement, this has no effect on platforms where the aligement is already sufficient.
156#[repr(align(4))]
157pub struct Lifetime {
158    #[stable_hasher(ignore)]
159    pub hir_id: HirId,
160
161    /// Either a named lifetime definition (e.g. `'a`, `'static`) or an
162    /// anonymous lifetime (`'_`, either explicitly written, or inserted for
163    /// things like `&type`).
164    pub ident: Ident,
165
166    /// Semantics of this lifetime.
167    pub kind: LifetimeKind,
168
169    /// The context in which the lifetime occurred. See `Lifetime::suggestion`
170    /// for example use.
171    pub source: LifetimeSource,
172
173    /// The syntax that the user used to declare this lifetime. See
174    /// `Lifetime::suggestion` for example use.
175    pub syntax: LifetimeSyntax,
176}
177
178#[derive(Debug, Copy, Clone, HashStable_Generic)]
179pub enum ParamName {
180    /// Some user-given name like `T` or `'x`.
181    Plain(Ident),
182
183    /// Indicates an illegal name was given and an error has been
184    /// reported (so we should squelch other derived errors).
185    ///
186    /// Occurs when, e.g., `'_` is used in the wrong place, or a
187    /// lifetime name is duplicated.
188    Error(Ident),
189
190    /// Synthetic name generated when user elided a lifetime in an impl header.
191    ///
192    /// E.g., the lifetimes in cases like these:
193    /// ```ignore (fragment)
194    /// impl Foo for &u32
195    /// impl Foo<'_> for u32
196    /// ```
197    /// in that case, we rewrite to
198    /// ```ignore (fragment)
199    /// impl<'f> Foo for &'f u32
200    /// impl<'f> Foo<'f> for u32
201    /// ```
202    /// where `'f` is something like `Fresh(0)`. The indices are
203    /// unique per impl, but not necessarily continuous.
204    Fresh,
205}
206
207impl ParamName {
208    pub fn ident(&self) -> Ident {
209        match *self {
210            ParamName::Plain(ident) | ParamName::Error(ident) => ident,
211            ParamName::Fresh => Ident::with_dummy_span(kw::UnderscoreLifetime),
212        }
213    }
214}
215
216#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
217pub enum LifetimeKind {
218    /// User-given names or fresh (synthetic) names.
219    Param(LocalDefId),
220
221    /// Implicit lifetime in a context like `dyn Foo`. This is
222    /// distinguished from implicit lifetimes elsewhere because the
223    /// lifetime that they default to must appear elsewhere within the
224    /// enclosing type. This means that, in an `impl Trait` context, we
225    /// don't have to create a parameter for them. That is, `impl
226    /// Trait<Item = &u32>` expands to an opaque type like `type
227    /// Foo<'a> = impl Trait<Item = &'a u32>`, but `impl Trait<item =
228    /// dyn Bar>` expands to `type Foo = impl Trait<Item = dyn Bar +
229    /// 'static>`. The latter uses `ImplicitObjectLifetimeDefault` so
230    /// that surrounding code knows not to create a lifetime
231    /// parameter.
232    ImplicitObjectLifetimeDefault,
233
234    /// Indicates an error during lowering (usually `'_` in wrong place)
235    /// that was already reported.
236    Error,
237
238    /// User wrote an anonymous lifetime, either `'_` or nothing (which gets
239    /// converted to `'_`). The semantics of this lifetime should be inferred
240    /// by typechecking code.
241    Infer,
242
243    /// User wrote `'static` or nothing (which gets converted to `'_`).
244    Static,
245}
246
247impl LifetimeKind {
248    fn is_elided(&self) -> bool {
249        match self {
250            LifetimeKind::ImplicitObjectLifetimeDefault | LifetimeKind::Infer => true,
251
252            // It might seem surprising that `Fresh` counts as not *elided*
253            // -- but this is because, as far as the code in the compiler is
254            // concerned -- `Fresh` variants act equivalently to "some fresh name".
255            // They correspond to early-bound regions on an impl, in other words.
256            LifetimeKind::Error | LifetimeKind::Param(..) | LifetimeKind::Static => false,
257        }
258    }
259}
260
261impl fmt::Display for Lifetime {
262    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
263        self.ident.name.fmt(f)
264    }
265}
266
267impl Lifetime {
268    pub fn new(
269        hir_id: HirId,
270        ident: Ident,
271        kind: LifetimeKind,
272        source: LifetimeSource,
273        syntax: LifetimeSyntax,
274    ) -> Lifetime {
275        let lifetime = Lifetime { hir_id, ident, kind, source, syntax };
276
277        // Sanity check: elided lifetimes form a strict subset of anonymous lifetimes.
278        #[cfg(debug_assertions)]
279        match (lifetime.is_elided(), lifetime.is_anonymous()) {
280            (false, false) => {} // e.g. `'a`
281            (false, true) => {}  // e.g. explicit `'_`
282            (true, true) => {}   // e.g. `&x`
283            (true, false) => panic!("bad Lifetime"),
284        }
285
286        lifetime
287    }
288
289    pub fn is_elided(&self) -> bool {
290        self.kind.is_elided()
291    }
292
293    pub fn is_anonymous(&self) -> bool {
294        self.ident.name == kw::UnderscoreLifetime
295    }
296
297    pub fn is_implicit(&self) -> bool {
298        matches!(self.syntax, LifetimeSyntax::Implicit)
299    }
300
301    pub fn is_static(&self) -> bool {
302        self.kind == LifetimeKind::Static
303    }
304
305    pub fn suggestion(&self, new_lifetime: &str) -> (Span, String) {
306        use LifetimeSource::*;
307        use LifetimeSyntax::*;
308
309        debug_assert!(new_lifetime.starts_with('\''));
310
311        match (self.syntax, self.source) {
312            // The user wrote `'a` or `'_`.
313            (ExplicitBound | ExplicitAnonymous, _) => (self.ident.span, format!("{new_lifetime}")),
314
315            // The user wrote `Path<T>`, and omitted the `'_,`.
316            (Implicit, Path { angle_brackets: AngleBrackets::Full }) => {
317                (self.ident.span, format!("{new_lifetime}, "))
318            }
319
320            // The user wrote `Path<>`, and omitted the `'_`..
321            (Implicit, Path { angle_brackets: AngleBrackets::Empty }) => {
322                (self.ident.span, format!("{new_lifetime}"))
323            }
324
325            // The user wrote `Path` and omitted the `<'_>`.
326            (Implicit, Path { angle_brackets: AngleBrackets::Missing }) => {
327                (self.ident.span.shrink_to_hi(), format!("<{new_lifetime}>"))
328            }
329
330            // The user wrote `&type` or `&mut type`.
331            (Implicit, Reference) => (self.ident.span, format!("{new_lifetime} ")),
332
333            (Implicit, source) => {
334                unreachable!("can't suggest for a implicit lifetime of {source:?}")
335            }
336        }
337    }
338}
339
340/// A `Path` is essentially Rust's notion of a name; for instance,
341/// `std::cmp::PartialEq`. It's represented as a sequence of identifiers,
342/// along with a bunch of supporting information.
343#[derive(Debug, Clone, Copy, HashStable_Generic)]
344pub struct Path<'hir, R = Res> {
345    pub span: Span,
346    /// The resolution for the path.
347    pub res: R,
348    /// The segments in the path: the things separated by `::`.
349    pub segments: &'hir [PathSegment<'hir>],
350}
351
352/// Up to three resolutions for type, value and macro namespaces.
353pub type UsePath<'hir> = Path<'hir, PerNS<Option<Res>>>;
354
355impl Path<'_> {
356    pub fn is_global(&self) -> bool {
357        self.segments.first().is_some_and(|segment| segment.ident.name == kw::PathRoot)
358    }
359}
360
361/// A segment of a path: an identifier, an optional lifetime, and a set of
362/// types.
363#[derive(Debug, Clone, Copy, HashStable_Generic)]
364pub struct PathSegment<'hir> {
365    /// The identifier portion of this path segment.
366    pub ident: Ident,
367    #[stable_hasher(ignore)]
368    pub hir_id: HirId,
369    pub res: Res,
370
371    /// Type/lifetime parameters attached to this path. They come in
372    /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
373    /// this is more than just simple syntactic sugar; the use of
374    /// parens affects the region binding rules, so we preserve the
375    /// distinction.
376    pub args: Option<&'hir GenericArgs<'hir>>,
377
378    /// Whether to infer remaining type parameters, if any.
379    /// This only applies to expression and pattern paths, and
380    /// out of those only the segments with no type parameters
381    /// to begin with, e.g., `Vec::new` is `<Vec<..>>::new::<..>`.
382    pub infer_args: bool,
383}
384
385impl<'hir> PathSegment<'hir> {
386    /// Converts an identifier to the corresponding segment.
387    pub fn new(ident: Ident, hir_id: HirId, res: Res) -> PathSegment<'hir> {
388        PathSegment { ident, hir_id, res, infer_args: true, args: None }
389    }
390
391    pub fn invalid() -> Self {
392        Self::new(Ident::dummy(), HirId::INVALID, Res::Err)
393    }
394
395    pub fn args(&self) -> &GenericArgs<'hir> {
396        if let Some(ref args) = self.args {
397            args
398        } else {
399            const DUMMY: &GenericArgs<'_> = &GenericArgs::none();
400            DUMMY
401        }
402    }
403}
404
405/// A constant that enters the type system, used for arguments to const generics (e.g. array lengths).
406///
407/// These are distinct from [`AnonConst`] as anon consts in the type system are not allowed
408/// to use any generic parameters, therefore we must represent `N` differently. Additionally
409/// future designs for supporting generic parameters in const arguments will likely not use
410/// an anon const based design.
411///
412/// So, `ConstArg` (specifically, [`ConstArgKind`]) distinguishes between const args
413/// that are [just paths](ConstArgKind::Path) (currently just bare const params)
414/// versus const args that are literals or have arbitrary computations (e.g., `{ 1 + 3 }`).
415///
416/// For an explanation of the `Unambig` generic parameter see the dev-guide:
417/// <https://rustc-dev-guide.rust-lang.org/hir/ambig-unambig-ty-and-consts.html>
418#[derive(Clone, Copy, Debug, HashStable_Generic)]
419#[repr(C)]
420pub struct ConstArg<'hir, Unambig = ()> {
421    #[stable_hasher(ignore)]
422    pub hir_id: HirId,
423    pub kind: ConstArgKind<'hir, Unambig>,
424}
425
426impl<'hir> ConstArg<'hir, AmbigArg> {
427    /// Converts a `ConstArg` in an ambiguous position to one in an unambiguous position.
428    ///
429    /// Functions accepting unambiguous consts may expect the [`ConstArgKind::Infer`] variant
430    /// to be used. Care should be taken to separately handle infer consts when calling this
431    /// function as it cannot be handled by downstream code making use of the returned const.
432    ///
433    /// In practice this may mean overriding the [`Visitor::visit_infer`][visit_infer] method on hir visitors, or
434    /// specifically matching on [`GenericArg::Infer`] when handling generic arguments.
435    ///
436    /// [visit_infer]: [rustc_hir::intravisit::Visitor::visit_infer]
437    pub fn as_unambig_ct(&self) -> &ConstArg<'hir> {
438        // SAFETY: `ConstArg` is `repr(C)` and `ConstArgKind` is marked `repr(u8)` so that the
439        // layout is the same across different ZST type arguments.
440        let ptr = self as *const ConstArg<'hir, AmbigArg> as *const ConstArg<'hir, ()>;
441        unsafe { &*ptr }
442    }
443}
444
445impl<'hir> ConstArg<'hir> {
446    /// Converts a `ConstArg` in an unambiguous position to one in an ambiguous position. This is
447    /// fallible as the [`ConstArgKind::Infer`] variant is not present in ambiguous positions.
448    ///
449    /// Functions accepting ambiguous consts will not handle the [`ConstArgKind::Infer`] variant, if
450    /// infer consts are relevant to you then care should be taken to handle them separately.
451    pub fn try_as_ambig_ct(&self) -> Option<&ConstArg<'hir, AmbigArg>> {
452        if let ConstArgKind::Infer(_, ()) = self.kind {
453            return None;
454        }
455
456        // SAFETY: `ConstArg` is `repr(C)` and `ConstArgKind` is marked `repr(u8)` so that the layout is
457        // the same across different ZST type arguments. We also asserted that the `self` is
458        // not a `ConstArgKind::Infer` so there is no risk of transmuting a `()` to `AmbigArg`.
459        let ptr = self as *const ConstArg<'hir> as *const ConstArg<'hir, AmbigArg>;
460        Some(unsafe { &*ptr })
461    }
462}
463
464impl<'hir, Unambig> ConstArg<'hir, Unambig> {
465    pub fn anon_const_hir_id(&self) -> Option<HirId> {
466        match self.kind {
467            ConstArgKind::Anon(ac) => Some(ac.hir_id),
468            _ => None,
469        }
470    }
471
472    pub fn span(&self) -> Span {
473        match self.kind {
474            ConstArgKind::Path(path) => path.span(),
475            ConstArgKind::Anon(anon) => anon.span,
476            ConstArgKind::Infer(span, _) => span,
477        }
478    }
479}
480
481/// See [`ConstArg`].
482#[derive(Clone, Copy, Debug, HashStable_Generic)]
483#[repr(u8, C)]
484pub enum ConstArgKind<'hir, Unambig = ()> {
485    /// **Note:** Currently this is only used for bare const params
486    /// (`N` where `fn foo<const N: usize>(...)`),
487    /// not paths to any const (`N` where `const N: usize = ...`).
488    ///
489    /// However, in the future, we'll be using it for all of those.
490    Path(QPath<'hir>),
491    Anon(&'hir AnonConst),
492    /// This variant is not always used to represent inference consts, sometimes
493    /// [`GenericArg::Infer`] is used instead.
494    Infer(Span, Unambig),
495}
496
497#[derive(Clone, Copy, Debug, HashStable_Generic)]
498pub struct InferArg {
499    #[stable_hasher(ignore)]
500    pub hir_id: HirId,
501    pub span: Span,
502}
503
504impl InferArg {
505    pub fn to_ty(&self) -> Ty<'static> {
506        Ty { kind: TyKind::Infer(()), span: self.span, hir_id: self.hir_id }
507    }
508}
509
510#[derive(Debug, Clone, Copy, HashStable_Generic)]
511pub enum GenericArg<'hir> {
512    Lifetime(&'hir Lifetime),
513    Type(&'hir Ty<'hir, AmbigArg>),
514    Const(&'hir ConstArg<'hir, AmbigArg>),
515    /// Inference variables in [`GenericArg`] are always represented by
516    /// `GenericArg::Infer` instead of the `Infer` variants on [`TyKind`] and
517    /// [`ConstArgKind`] as it is not clear until hir ty lowering whether a
518    /// `_` argument is a type or const argument.
519    ///
520    /// However, some builtin types' generic arguments are represented by [`TyKind`]
521    /// without a [`GenericArg`], instead directly storing a [`Ty`] or [`ConstArg`]. In
522    /// such cases they *are* represented by the `Infer` variants on [`TyKind`] and
523    /// [`ConstArgKind`] as it is not ambiguous whether the argument is a type or const.
524    Infer(InferArg),
525}
526
527impl GenericArg<'_> {
528    pub fn span(&self) -> Span {
529        match self {
530            GenericArg::Lifetime(l) => l.ident.span,
531            GenericArg::Type(t) => t.span,
532            GenericArg::Const(c) => c.span(),
533            GenericArg::Infer(i) => i.span,
534        }
535    }
536
537    pub fn hir_id(&self) -> HirId {
538        match self {
539            GenericArg::Lifetime(l) => l.hir_id,
540            GenericArg::Type(t) => t.hir_id,
541            GenericArg::Const(c) => c.hir_id,
542            GenericArg::Infer(i) => i.hir_id,
543        }
544    }
545
546    pub fn descr(&self) -> &'static str {
547        match self {
548            GenericArg::Lifetime(_) => "lifetime",
549            GenericArg::Type(_) => "type",
550            GenericArg::Const(_) => "constant",
551            GenericArg::Infer(_) => "placeholder",
552        }
553    }
554
555    pub fn to_ord(&self) -> ast::ParamKindOrd {
556        match self {
557            GenericArg::Lifetime(_) => ast::ParamKindOrd::Lifetime,
558            GenericArg::Type(_) | GenericArg::Const(_) | GenericArg::Infer(_) => {
559                ast::ParamKindOrd::TypeOrConst
560            }
561        }
562    }
563
564    pub fn is_ty_or_const(&self) -> bool {
565        match self {
566            GenericArg::Lifetime(_) => false,
567            GenericArg::Type(_) | GenericArg::Const(_) | GenericArg::Infer(_) => true,
568        }
569    }
570}
571
572/// The generic arguments and associated item constraints of a path segment.
573#[derive(Debug, Clone, Copy, HashStable_Generic)]
574pub struct GenericArgs<'hir> {
575    /// The generic arguments for this path segment.
576    pub args: &'hir [GenericArg<'hir>],
577    /// The associated item constraints for this path segment.
578    pub constraints: &'hir [AssocItemConstraint<'hir>],
579    /// Whether the arguments were written in parenthesized form (e.g., `Fn(T) -> U`).
580    ///
581    /// This is required mostly for pretty-printing and diagnostics,
582    /// but also for changing lifetime elision rules to be "function-like".
583    pub parenthesized: GenericArgsParentheses,
584    /// The span encompassing the arguments, constraints and the surrounding brackets (`<>` or `()`).
585    ///
586    /// For example:
587    ///
588    /// ```ignore (illustrative)
589    ///       Foo<A, B, AssocTy = D>           Fn(T, U, V) -> W
590    ///          ^^^^^^^^^^^^^^^^^^^             ^^^^^^^^^
591    /// ```
592    ///
593    /// Note that this may be:
594    /// - empty, if there are no generic brackets (but there may be hidden lifetimes)
595    /// - dummy, if this was generated during desugaring
596    pub span_ext: Span,
597}
598
599impl<'hir> GenericArgs<'hir> {
600    pub const fn none() -> Self {
601        Self {
602            args: &[],
603            constraints: &[],
604            parenthesized: GenericArgsParentheses::No,
605            span_ext: DUMMY_SP,
606        }
607    }
608
609    /// Obtain the list of input types and the output type if the generic arguments are parenthesized.
610    ///
611    /// Returns the `Ty0, Ty1, ...` and the `RetTy` in `Trait(Ty0, Ty1, ...) -> RetTy`.
612    /// Panics if the parenthesized arguments have an incorrect form (this shouldn't happen).
613    pub fn paren_sugar_inputs_output(&self) -> Option<(&[Ty<'hir>], &Ty<'hir>)> {
614        if self.parenthesized != GenericArgsParentheses::ParenSugar {
615            return None;
616        }
617
618        let inputs = self
619            .args
620            .iter()
621            .find_map(|arg| {
622                let GenericArg::Type(ty) = arg else { return None };
623                let TyKind::Tup(tys) = &ty.kind else { return None };
624                Some(tys)
625            })
626            .unwrap();
627
628        Some((inputs, self.paren_sugar_output_inner()))
629    }
630
631    /// Obtain the output type if the generic arguments are parenthesized.
632    ///
633    /// Returns the `RetTy` in `Trait(Ty0, Ty1, ...) -> RetTy`.
634    /// Panics if the parenthesized arguments have an incorrect form (this shouldn't happen).
635    pub fn paren_sugar_output(&self) -> Option<&Ty<'hir>> {
636        (self.parenthesized == GenericArgsParentheses::ParenSugar)
637            .then(|| self.paren_sugar_output_inner())
638    }
639
640    fn paren_sugar_output_inner(&self) -> &Ty<'hir> {
641        let [constraint] = self.constraints.try_into().unwrap();
642        debug_assert_eq!(constraint.ident.name, sym::Output);
643        constraint.ty().unwrap()
644    }
645
646    pub fn has_err(&self) -> Option<ErrorGuaranteed> {
647        self.args
648            .iter()
649            .find_map(|arg| {
650                let GenericArg::Type(ty) = arg else { return None };
651                let TyKind::Err(guar) = ty.kind else { return None };
652                Some(guar)
653            })
654            .or_else(|| {
655                self.constraints.iter().find_map(|constraint| {
656                    let TyKind::Err(guar) = constraint.ty()?.kind else { return None };
657                    Some(guar)
658                })
659            })
660    }
661
662    #[inline]
663    pub fn num_lifetime_params(&self) -> usize {
664        self.args.iter().filter(|arg| matches!(arg, GenericArg::Lifetime(_))).count()
665    }
666
667    #[inline]
668    pub fn has_lifetime_params(&self) -> bool {
669        self.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)))
670    }
671
672    #[inline]
673    /// This function returns the number of type and const generic params.
674    /// It should only be used for diagnostics.
675    pub fn num_generic_params(&self) -> usize {
676        self.args.iter().filter(|arg| !matches!(arg, GenericArg::Lifetime(_))).count()
677    }
678
679    /// The span encompassing the arguments and constraints[^1] inside the surrounding brackets.
680    ///
681    /// Returns `None` if the span is empty (i.e., no brackets) or dummy.
682    ///
683    /// [^1]: Unless of the form `-> Ty` (see [`GenericArgsParentheses`]).
684    pub fn span(&self) -> Option<Span> {
685        let span_ext = self.span_ext()?;
686        Some(span_ext.with_lo(span_ext.lo() + BytePos(1)).with_hi(span_ext.hi() - BytePos(1)))
687    }
688
689    /// Returns span encompassing arguments and their surrounding `<>` or `()`
690    pub fn span_ext(&self) -> Option<Span> {
691        Some(self.span_ext).filter(|span| !span.is_empty())
692    }
693
694    pub fn is_empty(&self) -> bool {
695        self.args.is_empty()
696    }
697}
698
699#[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
700pub enum GenericArgsParentheses {
701    No,
702    /// Bounds for `feature(return_type_notation)`, like `T: Trait<method(..): Send>`,
703    /// where the args are explicitly elided with `..`
704    ReturnTypeNotation,
705    /// parenthesized function-family traits, like `T: Fn(u32) -> i32`
706    ParenSugar,
707}
708
709/// The modifiers on a trait bound.
710#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
711pub struct TraitBoundModifiers {
712    pub constness: BoundConstness,
713    pub polarity: BoundPolarity,
714}
715
716impl TraitBoundModifiers {
717    pub const NONE: Self =
718        TraitBoundModifiers { constness: BoundConstness::Never, polarity: BoundPolarity::Positive };
719}
720
721#[derive(Clone, Copy, Debug, HashStable_Generic)]
722pub enum GenericBound<'hir> {
723    Trait(PolyTraitRef<'hir>),
724    Outlives(&'hir Lifetime),
725    Use(&'hir [PreciseCapturingArg<'hir>], Span),
726}
727
728impl GenericBound<'_> {
729    pub fn trait_ref(&self) -> Option<&TraitRef<'_>> {
730        match self {
731            GenericBound::Trait(data) => Some(&data.trait_ref),
732            _ => None,
733        }
734    }
735
736    pub fn span(&self) -> Span {
737        match self {
738            GenericBound::Trait(t, ..) => t.span,
739            GenericBound::Outlives(l) => l.ident.span,
740            GenericBound::Use(_, span) => *span,
741        }
742    }
743}
744
745pub type GenericBounds<'hir> = &'hir [GenericBound<'hir>];
746
747#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable_Generic, Debug)]
748pub enum MissingLifetimeKind {
749    /// An explicit `'_`.
750    Underscore,
751    /// An elided lifetime `&' ty`.
752    Ampersand,
753    /// An elided lifetime in brackets with written brackets.
754    Comma,
755    /// An elided lifetime with elided brackets.
756    Brackets,
757}
758
759#[derive(Copy, Clone, Debug, HashStable_Generic)]
760pub enum LifetimeParamKind {
761    // Indicates that the lifetime definition was explicitly declared (e.g., in
762    // `fn foo<'a>(x: &'a u8) -> &'a u8 { x }`).
763    Explicit,
764
765    // Indication that the lifetime was elided (e.g., in both cases in
766    // `fn foo(x: &u8) -> &'_ u8 { x }`).
767    Elided(MissingLifetimeKind),
768
769    // Indication that the lifetime name was somehow in error.
770    Error,
771}
772
773#[derive(Debug, Clone, Copy, HashStable_Generic)]
774pub enum GenericParamKind<'hir> {
775    /// A lifetime definition (e.g., `'a: 'b + 'c + 'd`).
776    Lifetime {
777        kind: LifetimeParamKind,
778    },
779    Type {
780        default: Option<&'hir Ty<'hir>>,
781        synthetic: bool,
782    },
783    Const {
784        ty: &'hir Ty<'hir>,
785        /// Optional default value for the const generic param
786        default: Option<&'hir ConstArg<'hir>>,
787    },
788}
789
790#[derive(Debug, Clone, Copy, HashStable_Generic)]
791pub struct GenericParam<'hir> {
792    #[stable_hasher(ignore)]
793    pub hir_id: HirId,
794    pub def_id: LocalDefId,
795    pub name: ParamName,
796    pub span: Span,
797    pub pure_wrt_drop: bool,
798    pub kind: GenericParamKind<'hir>,
799    pub colon_span: Option<Span>,
800    pub source: GenericParamSource,
801}
802
803impl<'hir> GenericParam<'hir> {
804    /// Synthetic type-parameters are inserted after normal ones.
805    /// In order for normal parameters to be able to refer to synthetic ones,
806    /// scans them first.
807    pub fn is_impl_trait(&self) -> bool {
808        matches!(self.kind, GenericParamKind::Type { synthetic: true, .. })
809    }
810
811    /// This can happen for `async fn`, e.g. `async fn f<'_>(&'_ self)`.
812    ///
813    /// See `lifetime_to_generic_param` in `rustc_ast_lowering` for more information.
814    pub fn is_elided_lifetime(&self) -> bool {
815        matches!(self.kind, GenericParamKind::Lifetime { kind: LifetimeParamKind::Elided(_) })
816    }
817}
818
819/// Records where the generic parameter originated from.
820///
821/// This can either be from an item's generics, in which case it's typically
822/// early-bound (but can be a late-bound lifetime in functions, for example),
823/// or from a `for<...>` binder, in which case it's late-bound (and notably,
824/// does not show up in the parent item's generics).
825#[derive(Debug, Clone, Copy, HashStable_Generic)]
826pub enum GenericParamSource {
827    // Early or late-bound parameters defined on an item
828    Generics,
829    // Late-bound parameters defined via a `for<...>`
830    Binder,
831}
832
833#[derive(Default)]
834pub struct GenericParamCount {
835    pub lifetimes: usize,
836    pub types: usize,
837    pub consts: usize,
838    pub infer: usize,
839}
840
841/// Represents lifetimes and type parameters attached to a declaration
842/// of a function, enum, trait, etc.
843#[derive(Debug, Clone, Copy, HashStable_Generic)]
844pub struct Generics<'hir> {
845    pub params: &'hir [GenericParam<'hir>],
846    pub predicates: &'hir [WherePredicate<'hir>],
847    pub has_where_clause_predicates: bool,
848    pub where_clause_span: Span,
849    pub span: Span,
850}
851
852impl<'hir> Generics<'hir> {
853    pub const fn empty() -> &'hir Generics<'hir> {
854        const NOPE: Generics<'_> = Generics {
855            params: &[],
856            predicates: &[],
857            has_where_clause_predicates: false,
858            where_clause_span: DUMMY_SP,
859            span: DUMMY_SP,
860        };
861        &NOPE
862    }
863
864    pub fn get_named(&self, name: Symbol) -> Option<&GenericParam<'hir>> {
865        self.params.iter().find(|&param| name == param.name.ident().name)
866    }
867
868    /// If there are generic parameters, return where to introduce a new one.
869    pub fn span_for_lifetime_suggestion(&self) -> Option<Span> {
870        if let Some(first) = self.params.first()
871            && self.span.contains(first.span)
872        {
873            // `fn foo<A>(t: impl Trait)`
874            //         ^ suggest `'a, ` here
875            Some(first.span.shrink_to_lo())
876        } else {
877            None
878        }
879    }
880
881    /// If there are generic parameters, return where to introduce a new one.
882    pub fn span_for_param_suggestion(&self) -> Option<Span> {
883        self.params.iter().any(|p| self.span.contains(p.span)).then(|| {
884            // `fn foo<A>(t: impl Trait)`
885            //          ^ suggest `, T: Trait` here
886            self.span.with_lo(self.span.hi() - BytePos(1)).shrink_to_lo()
887        })
888    }
889
890    /// `Span` where further predicates would be suggested, accounting for trailing commas, like
891    ///  in `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
892    pub fn tail_span_for_predicate_suggestion(&self) -> Span {
893        let end = self.where_clause_span.shrink_to_hi();
894        if self.has_where_clause_predicates {
895            self.predicates
896                .iter()
897                .rfind(|&p| p.kind.in_where_clause())
898                .map_or(end, |p| p.span)
899                .shrink_to_hi()
900                .to(end)
901        } else {
902            end
903        }
904    }
905
906    pub fn add_where_or_trailing_comma(&self) -> &'static str {
907        if self.has_where_clause_predicates {
908            ","
909        } else if self.where_clause_span.is_empty() {
910            " where"
911        } else {
912            // No where clause predicates, but we have `where` token
913            ""
914        }
915    }
916
917    pub fn bounds_for_param(
918        &self,
919        param_def_id: LocalDefId,
920    ) -> impl Iterator<Item = &WhereBoundPredicate<'hir>> {
921        self.predicates.iter().filter_map(move |pred| match pred.kind {
922            WherePredicateKind::BoundPredicate(bp)
923                if bp.is_param_bound(param_def_id.to_def_id()) =>
924            {
925                Some(bp)
926            }
927            _ => None,
928        })
929    }
930
931    pub fn outlives_for_param(
932        &self,
933        param_def_id: LocalDefId,
934    ) -> impl Iterator<Item = &WhereRegionPredicate<'_>> {
935        self.predicates.iter().filter_map(move |pred| match pred.kind {
936            WherePredicateKind::RegionPredicate(rp) if rp.is_param_bound(param_def_id) => Some(rp),
937            _ => None,
938        })
939    }
940
941    /// Returns a suggestable empty span right after the "final" bound of the generic parameter.
942    ///
943    /// If that bound needs to be wrapped in parentheses to avoid ambiguity with
944    /// subsequent bounds, it also returns an empty span for an open parenthesis
945    /// as the second component.
946    ///
947    /// E.g., adding `+ 'static` after `Fn() -> dyn Future<Output = ()>` or
948    /// `Fn() -> &'static dyn Debug` requires parentheses:
949    /// `Fn() -> (dyn Future<Output = ()>) + 'static` and
950    /// `Fn() -> &'static (dyn Debug) + 'static`, respectively.
951    pub fn bounds_span_for_suggestions(
952        &self,
953        param_def_id: LocalDefId,
954    ) -> Option<(Span, Option<Span>)> {
955        self.bounds_for_param(param_def_id).flat_map(|bp| bp.bounds.iter().rev()).find_map(
956            |bound| {
957                let span_for_parentheses = if let Some(trait_ref) = bound.trait_ref()
958                    && let [.., segment] = trait_ref.path.segments
959                    && let Some(ret_ty) = segment.args().paren_sugar_output()
960                    && let ret_ty = ret_ty.peel_refs()
961                    && let TyKind::TraitObject(_, tagged_ptr) = ret_ty.kind
962                    && let TraitObjectSyntax::Dyn = tagged_ptr.tag()
963                    && ret_ty.span.can_be_used_for_suggestions()
964                {
965                    Some(ret_ty.span)
966                } else {
967                    None
968                };
969
970                span_for_parentheses.map_or_else(
971                    || {
972                        // We include bounds that come from a `#[derive(_)]` but point at the user's code,
973                        // as we use this method to get a span appropriate for suggestions.
974                        let bs = bound.span();
975                        bs.can_be_used_for_suggestions().then(|| (bs.shrink_to_hi(), None))
976                    },
977                    |span| Some((span.shrink_to_hi(), Some(span.shrink_to_lo()))),
978                )
979            },
980        )
981    }
982
983    pub fn span_for_predicate_removal(&self, pos: usize) -> Span {
984        let predicate = &self.predicates[pos];
985        let span = predicate.span;
986
987        if !predicate.kind.in_where_clause() {
988            // <T: ?Sized, U>
989            //   ^^^^^^^^
990            return span;
991        }
992
993        // We need to find out which comma to remove.
994        if pos < self.predicates.len() - 1 {
995            let next_pred = &self.predicates[pos + 1];
996            if next_pred.kind.in_where_clause() {
997                // where T: ?Sized, Foo: Bar,
998                //       ^^^^^^^^^^^
999                return span.until(next_pred.span);
1000            }
1001        }
1002
1003        if pos > 0 {
1004            let prev_pred = &self.predicates[pos - 1];
1005            if prev_pred.kind.in_where_clause() {
1006                // where Foo: Bar, T: ?Sized,
1007                //               ^^^^^^^^^^^
1008                return prev_pred.span.shrink_to_hi().to(span);
1009            }
1010        }
1011
1012        // This is the only predicate in the where clause.
1013        // where T: ?Sized
1014        // ^^^^^^^^^^^^^^^
1015        self.where_clause_span
1016    }
1017
1018    pub fn span_for_bound_removal(&self, predicate_pos: usize, bound_pos: usize) -> Span {
1019        let predicate = &self.predicates[predicate_pos];
1020        let bounds = predicate.kind.bounds();
1021
1022        if bounds.len() == 1 {
1023            return self.span_for_predicate_removal(predicate_pos);
1024        }
1025
1026        let bound_span = bounds[bound_pos].span();
1027        if bound_pos < bounds.len() - 1 {
1028            // If there's another bound after the current bound
1029            // include the following '+' e.g.:
1030            //
1031            //  `T: Foo + CurrentBound + Bar`
1032            //            ^^^^^^^^^^^^^^^
1033            bound_span.to(bounds[bound_pos + 1].span().shrink_to_lo())
1034        } else {
1035            // If the current bound is the last bound
1036            // include the preceding '+' E.g.:
1037            //
1038            //  `T: Foo + Bar + CurrentBound`
1039            //               ^^^^^^^^^^^^^^^
1040            bound_span.with_lo(bounds[bound_pos - 1].span().hi())
1041        }
1042    }
1043}
1044
1045/// A single predicate in a where-clause.
1046#[derive(Debug, Clone, Copy, HashStable_Generic)]
1047pub struct WherePredicate<'hir> {
1048    #[stable_hasher(ignore)]
1049    pub hir_id: HirId,
1050    pub span: Span,
1051    pub kind: &'hir WherePredicateKind<'hir>,
1052}
1053
1054/// The kind of a single predicate in a where-clause.
1055#[derive(Debug, Clone, Copy, HashStable_Generic)]
1056pub enum WherePredicateKind<'hir> {
1057    /// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
1058    BoundPredicate(WhereBoundPredicate<'hir>),
1059    /// A lifetime predicate (e.g., `'a: 'b + 'c`).
1060    RegionPredicate(WhereRegionPredicate<'hir>),
1061    /// An equality predicate (unsupported).
1062    EqPredicate(WhereEqPredicate<'hir>),
1063}
1064
1065impl<'hir> WherePredicateKind<'hir> {
1066    pub fn in_where_clause(&self) -> bool {
1067        match self {
1068            WherePredicateKind::BoundPredicate(p) => p.origin == PredicateOrigin::WhereClause,
1069            WherePredicateKind::RegionPredicate(p) => p.in_where_clause,
1070            WherePredicateKind::EqPredicate(_) => false,
1071        }
1072    }
1073
1074    pub fn bounds(&self) -> GenericBounds<'hir> {
1075        match self {
1076            WherePredicateKind::BoundPredicate(p) => p.bounds,
1077            WherePredicateKind::RegionPredicate(p) => p.bounds,
1078            WherePredicateKind::EqPredicate(_) => &[],
1079        }
1080    }
1081}
1082
1083#[derive(Copy, Clone, Debug, HashStable_Generic, PartialEq, Eq)]
1084pub enum PredicateOrigin {
1085    WhereClause,
1086    GenericParam,
1087    ImplTrait,
1088}
1089
1090/// A type bound (e.g., `for<'c> Foo: Send + Clone + 'c`).
1091#[derive(Debug, Clone, Copy, HashStable_Generic)]
1092pub struct WhereBoundPredicate<'hir> {
1093    /// Origin of the predicate.
1094    pub origin: PredicateOrigin,
1095    /// Any generics from a `for` binding.
1096    pub bound_generic_params: &'hir [GenericParam<'hir>],
1097    /// The type being bounded.
1098    pub bounded_ty: &'hir Ty<'hir>,
1099    /// Trait and lifetime bounds (e.g., `Clone + Send + 'static`).
1100    pub bounds: GenericBounds<'hir>,
1101}
1102
1103impl<'hir> WhereBoundPredicate<'hir> {
1104    /// Returns `true` if `param_def_id` matches the `bounded_ty` of this predicate.
1105    pub fn is_param_bound(&self, param_def_id: DefId) -> bool {
1106        self.bounded_ty.as_generic_param().is_some_and(|(def_id, _)| def_id == param_def_id)
1107    }
1108}
1109
1110/// A lifetime predicate (e.g., `'a: 'b + 'c`).
1111#[derive(Debug, Clone, Copy, HashStable_Generic)]
1112pub struct WhereRegionPredicate<'hir> {
1113    pub in_where_clause: bool,
1114    pub lifetime: &'hir Lifetime,
1115    pub bounds: GenericBounds<'hir>,
1116}
1117
1118impl<'hir> WhereRegionPredicate<'hir> {
1119    /// Returns `true` if `param_def_id` matches the `lifetime` of this predicate.
1120    fn is_param_bound(&self, param_def_id: LocalDefId) -> bool {
1121        self.lifetime.kind == LifetimeKind::Param(param_def_id)
1122    }
1123}
1124
1125/// An equality predicate (e.g., `T = int`); currently unsupported.
1126#[derive(Debug, Clone, Copy, HashStable_Generic)]
1127pub struct WhereEqPredicate<'hir> {
1128    pub lhs_ty: &'hir Ty<'hir>,
1129    pub rhs_ty: &'hir Ty<'hir>,
1130}
1131
1132/// HIR node coupled with its parent's id in the same HIR owner.
1133///
1134/// The parent is trash when the node is a HIR owner.
1135#[derive(Clone, Copy, Debug)]
1136pub struct ParentedNode<'tcx> {
1137    pub parent: ItemLocalId,
1138    pub node: Node<'tcx>,
1139}
1140
1141/// Arguments passed to an attribute macro.
1142#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable)]
1143pub enum AttrArgs {
1144    /// No arguments: `#[attr]`.
1145    Empty,
1146    /// Delimited arguments: `#[attr()/[]/{}]`.
1147    Delimited(DelimArgs),
1148    /// Arguments of a key-value attribute: `#[attr = "value"]`.
1149    Eq {
1150        /// Span of the `=` token.
1151        eq_span: Span,
1152        /// The "value".
1153        expr: MetaItemLit,
1154    },
1155}
1156
1157#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable)]
1158pub struct AttrPath {
1159    pub segments: Box<[Ident]>,
1160    pub span: Span,
1161}
1162
1163impl IntoDiagArg for AttrPath {
1164    fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
1165        self.to_string().into_diag_arg(path)
1166    }
1167}
1168
1169impl AttrPath {
1170    pub fn from_ast(path: &ast::Path) -> Self {
1171        AttrPath {
1172            segments: path.segments.iter().map(|i| i.ident).collect::<Vec<_>>().into_boxed_slice(),
1173            span: path.span,
1174        }
1175    }
1176}
1177
1178impl fmt::Display for AttrPath {
1179    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1180        write!(f, "{}", join_path_idents(&self.segments))
1181    }
1182}
1183
1184#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable)]
1185pub struct AttrItem {
1186    // Not lowered to hir::Path because we have no NodeId to resolve to.
1187    pub path: AttrPath,
1188    pub args: AttrArgs,
1189    pub id: HashIgnoredAttrId,
1190    /// Denotes if the attribute decorates the following construct (outer)
1191    /// or the construct this attribute is contained within (inner).
1192    pub style: AttrStyle,
1193    /// Span of the entire attribute
1194    pub span: Span,
1195}
1196
1197/// The derived implementation of [`HashStable_Generic`] on [`Attribute`]s shouldn't hash
1198/// [`AttrId`]s. By wrapping them in this, we make sure we never do.
1199#[derive(Copy, Debug, Encodable, Decodable, Clone)]
1200pub struct HashIgnoredAttrId {
1201    pub attr_id: AttrId,
1202}
1203
1204#[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic)]
1205pub enum Attribute {
1206    /// A parsed built-in attribute.
1207    ///
1208    /// Each attribute has a span connected to it. However, you must be somewhat careful using it.
1209    /// That's because sometimes we merge multiple attributes together, like when an item has
1210    /// multiple `repr` attributes. In this case the span might not be very useful.
1211    Parsed(AttributeKind),
1212
1213    /// An attribute that could not be parsed, out of a token-like representation.
1214    /// This is the case for custom tool attributes.
1215    Unparsed(Box<AttrItem>),
1216}
1217
1218impl Attribute {
1219    pub fn get_normal_item(&self) -> &AttrItem {
1220        match &self {
1221            Attribute::Unparsed(normal) => &normal,
1222            _ => panic!("unexpected parsed attribute"),
1223        }
1224    }
1225
1226    pub fn unwrap_normal_item(self) -> AttrItem {
1227        match self {
1228            Attribute::Unparsed(normal) => *normal,
1229            _ => panic!("unexpected parsed attribute"),
1230        }
1231    }
1232
1233    pub fn value_lit(&self) -> Option<&MetaItemLit> {
1234        match &self {
1235            Attribute::Unparsed(n) => match n.as_ref() {
1236                AttrItem { args: AttrArgs::Eq { eq_span: _, expr }, .. } => Some(expr),
1237                _ => None,
1238            },
1239            _ => None,
1240        }
1241    }
1242
1243    pub fn is_parsed_attr(&self) -> bool {
1244        match self {
1245            Attribute::Parsed(_) => true,
1246            Attribute::Unparsed(_) => false,
1247        }
1248    }
1249}
1250
1251impl AttributeExt for Attribute {
1252    #[inline]
1253    fn id(&self) -> AttrId {
1254        match &self {
1255            Attribute::Unparsed(u) => u.id.attr_id,
1256            _ => panic!(),
1257        }
1258    }
1259
1260    #[inline]
1261    fn meta_item_list(&self) -> Option<ThinVec<ast::MetaItemInner>> {
1262        match &self {
1263            Attribute::Unparsed(n) => match n.as_ref() {
1264                AttrItem { args: AttrArgs::Delimited(d), .. } => {
1265                    ast::MetaItemKind::list_from_tokens(d.tokens.clone())
1266                }
1267                _ => None,
1268            },
1269            _ => None,
1270        }
1271    }
1272
1273    #[inline]
1274    fn value_str(&self) -> Option<Symbol> {
1275        self.value_lit().and_then(|x| x.value_str())
1276    }
1277
1278    #[inline]
1279    fn value_span(&self) -> Option<Span> {
1280        self.value_lit().map(|i| i.span)
1281    }
1282
1283    /// For a single-segment attribute, returns its name; otherwise, returns `None`.
1284    #[inline]
1285    fn ident(&self) -> Option<Ident> {
1286        match &self {
1287            Attribute::Unparsed(n) => {
1288                if let [ident] = n.path.segments.as_ref() {
1289                    Some(*ident)
1290                } else {
1291                    None
1292                }
1293            }
1294            _ => None,
1295        }
1296    }
1297
1298    #[inline]
1299    fn path_matches(&self, name: &[Symbol]) -> bool {
1300        match &self {
1301            Attribute::Unparsed(n) => {
1302                n.path.segments.len() == name.len()
1303                    && n.path.segments.iter().zip(name).all(|(s, n)| s.name == *n)
1304            }
1305            _ => false,
1306        }
1307    }
1308
1309    #[inline]
1310    fn is_doc_comment(&self) -> bool {
1311        matches!(self, Attribute::Parsed(AttributeKind::DocComment { .. }))
1312    }
1313
1314    #[inline]
1315    fn span(&self) -> Span {
1316        match &self {
1317            Attribute::Unparsed(u) => u.span,
1318            // FIXME: should not be needed anymore when all attrs are parsed
1319            Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span,
1320            Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
1321            Attribute::Parsed(AttributeKind::AllowInternalUnsafe(span)) => *span,
1322            Attribute::Parsed(AttributeKind::Linkage(_, span)) => *span,
1323            a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
1324        }
1325    }
1326
1327    #[inline]
1328    fn is_word(&self) -> bool {
1329        match &self {
1330            Attribute::Unparsed(n) => {
1331                matches!(n.args, AttrArgs::Empty)
1332            }
1333            _ => false,
1334        }
1335    }
1336
1337    #[inline]
1338    fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
1339        match &self {
1340            Attribute::Unparsed(n) => Some(n.path.segments.iter().copied().collect()),
1341            _ => None,
1342        }
1343    }
1344
1345    #[inline]
1346    fn doc_str(&self) -> Option<Symbol> {
1347        match &self {
1348            Attribute::Parsed(AttributeKind::DocComment { comment, .. }) => Some(*comment),
1349            Attribute::Unparsed(_) if self.has_name(sym::doc) => self.value_str(),
1350            _ => None,
1351        }
1352    }
1353
1354    fn is_automatically_derived_attr(&self) -> bool {
1355        matches!(self, Attribute::Parsed(AttributeKind::AutomaticallyDerived(..)))
1356    }
1357
1358    #[inline]
1359    fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
1360        match &self {
1361            Attribute::Parsed(AttributeKind::DocComment { kind, comment, .. }) => {
1362                Some((*comment, *kind))
1363            }
1364            Attribute::Unparsed(_) if self.has_name(sym::doc) => {
1365                self.value_str().map(|s| (s, CommentKind::Line))
1366            }
1367            _ => None,
1368        }
1369    }
1370
1371    fn doc_resolution_scope(&self) -> Option<AttrStyle> {
1372        match self {
1373            Attribute::Parsed(AttributeKind::DocComment { style, .. }) => Some(*style),
1374            Attribute::Unparsed(attr) if self.has_name(sym::doc) && self.value_str().is_some() => {
1375                Some(attr.style)
1376            }
1377            _ => None,
1378        }
1379    }
1380
1381    fn is_proc_macro_attr(&self) -> bool {
1382        matches!(
1383            self,
1384            Attribute::Parsed(
1385                AttributeKind::ProcMacro(..)
1386                    | AttributeKind::ProcMacroAttribute(..)
1387                    | AttributeKind::ProcMacroDerive { .. }
1388            )
1389        )
1390    }
1391}
1392
1393// FIXME(fn_delegation): use function delegation instead of manually forwarding
1394impl Attribute {
1395    #[inline]
1396    pub fn id(&self) -> AttrId {
1397        AttributeExt::id(self)
1398    }
1399
1400    #[inline]
1401    pub fn name(&self) -> Option<Symbol> {
1402        AttributeExt::name(self)
1403    }
1404
1405    #[inline]
1406    pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
1407        AttributeExt::meta_item_list(self)
1408    }
1409
1410    #[inline]
1411    pub fn value_str(&self) -> Option<Symbol> {
1412        AttributeExt::value_str(self)
1413    }
1414
1415    #[inline]
1416    pub fn value_span(&self) -> Option<Span> {
1417        AttributeExt::value_span(self)
1418    }
1419
1420    #[inline]
1421    pub fn ident(&self) -> Option<Ident> {
1422        AttributeExt::ident(self)
1423    }
1424
1425    #[inline]
1426    pub fn path_matches(&self, name: &[Symbol]) -> bool {
1427        AttributeExt::path_matches(self, name)
1428    }
1429
1430    #[inline]
1431    pub fn is_doc_comment(&self) -> bool {
1432        AttributeExt::is_doc_comment(self)
1433    }
1434
1435    #[inline]
1436    pub fn has_name(&self, name: Symbol) -> bool {
1437        AttributeExt::has_name(self, name)
1438    }
1439
1440    #[inline]
1441    pub fn has_any_name(&self, names: &[Symbol]) -> bool {
1442        AttributeExt::has_any_name(self, names)
1443    }
1444
1445    #[inline]
1446    pub fn span(&self) -> Span {
1447        AttributeExt::span(self)
1448    }
1449
1450    #[inline]
1451    pub fn is_word(&self) -> bool {
1452        AttributeExt::is_word(self)
1453    }
1454
1455    #[inline]
1456    pub fn path(&self) -> SmallVec<[Symbol; 1]> {
1457        AttributeExt::path(self)
1458    }
1459
1460    #[inline]
1461    pub fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> {
1462        AttributeExt::ident_path(self)
1463    }
1464
1465    #[inline]
1466    pub fn doc_str(&self) -> Option<Symbol> {
1467        AttributeExt::doc_str(self)
1468    }
1469
1470    #[inline]
1471    pub fn is_proc_macro_attr(&self) -> bool {
1472        AttributeExt::is_proc_macro_attr(self)
1473    }
1474
1475    #[inline]
1476    pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
1477        AttributeExt::doc_str_and_comment_kind(self)
1478    }
1479}
1480
1481/// Attributes owned by a HIR owner.
1482#[derive(Debug)]
1483pub struct AttributeMap<'tcx> {
1484    pub map: SortedMap<ItemLocalId, &'tcx [Attribute]>,
1485    /// Preprocessed `#[define_opaque]` attribute.
1486    pub define_opaque: Option<&'tcx [(Span, LocalDefId)]>,
1487    // Only present when the crate hash is needed.
1488    pub opt_hash: Option<Fingerprint>,
1489}
1490
1491impl<'tcx> AttributeMap<'tcx> {
1492    pub const EMPTY: &'static AttributeMap<'static> = &AttributeMap {
1493        map: SortedMap::new(),
1494        opt_hash: Some(Fingerprint::ZERO),
1495        define_opaque: None,
1496    };
1497
1498    #[inline]
1499    pub fn get(&self, id: ItemLocalId) -> &'tcx [Attribute] {
1500        self.map.get(&id).copied().unwrap_or(&[])
1501    }
1502}
1503
1504/// Map of all HIR nodes inside the current owner.
1505/// These nodes are mapped by `ItemLocalId` alongside the index of their parent node.
1506/// The HIR tree, including bodies, is pre-hashed.
1507pub struct OwnerNodes<'tcx> {
1508    /// Pre-computed hash of the full HIR. Used in the crate hash. Only present
1509    /// when incr. comp. is enabled.
1510    pub opt_hash_including_bodies: Option<Fingerprint>,
1511    /// Full HIR for the current owner.
1512    // The zeroth node's parent should never be accessed: the owner's parent is computed by the
1513    // hir_owner_parent query. It is set to `ItemLocalId::INVALID` to force an ICE if accidentally
1514    // used.
1515    pub nodes: IndexVec<ItemLocalId, ParentedNode<'tcx>>,
1516    /// Content of local bodies.
1517    pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>,
1518}
1519
1520impl<'tcx> OwnerNodes<'tcx> {
1521    pub fn node(&self) -> OwnerNode<'tcx> {
1522        // Indexing must ensure it is an OwnerNode.
1523        self.nodes[ItemLocalId::ZERO].node.as_owner().unwrap()
1524    }
1525}
1526
1527impl fmt::Debug for OwnerNodes<'_> {
1528    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1529        f.debug_struct("OwnerNodes")
1530            // Do not print all the pointers to all the nodes, as it would be unreadable.
1531            .field("node", &self.nodes[ItemLocalId::ZERO])
1532            .field(
1533                "parents",
1534                &fmt::from_fn(|f| {
1535                    f.debug_list()
1536                        .entries(self.nodes.iter_enumerated().map(|(id, parented_node)| {
1537                            fmt::from_fn(move |f| write!(f, "({id:?}, {:?})", parented_node.parent))
1538                        }))
1539                        .finish()
1540                }),
1541            )
1542            .field("bodies", &self.bodies)
1543            .field("opt_hash_including_bodies", &self.opt_hash_including_bodies)
1544            .finish()
1545    }
1546}
1547
1548/// Full information resulting from lowering an AST node.
1549#[derive(Debug, HashStable_Generic)]
1550pub struct OwnerInfo<'hir> {
1551    /// Contents of the HIR.
1552    pub nodes: OwnerNodes<'hir>,
1553    /// Map from each nested owner to its parent's local id.
1554    pub parenting: LocalDefIdMap<ItemLocalId>,
1555    /// Collected attributes of the HIR nodes.
1556    pub attrs: AttributeMap<'hir>,
1557    /// Map indicating what traits are in scope for places where this
1558    /// is relevant; generated by resolve.
1559    pub trait_map: ItemLocalMap<Box<[TraitCandidate]>>,
1560
1561    /// Lints delayed during ast lowering to be emitted
1562    /// after hir has completely built
1563    pub delayed_lints: DelayedLints,
1564}
1565
1566impl<'tcx> OwnerInfo<'tcx> {
1567    #[inline]
1568    pub fn node(&self) -> OwnerNode<'tcx> {
1569        self.nodes.node()
1570    }
1571}
1572
1573#[derive(Copy, Clone, Debug, HashStable_Generic)]
1574pub enum MaybeOwner<'tcx> {
1575    Owner(&'tcx OwnerInfo<'tcx>),
1576    NonOwner(HirId),
1577    /// Used as a placeholder for unused LocalDefId.
1578    Phantom,
1579}
1580
1581impl<'tcx> MaybeOwner<'tcx> {
1582    pub fn as_owner(self) -> Option<&'tcx OwnerInfo<'tcx>> {
1583        match self {
1584            MaybeOwner::Owner(i) => Some(i),
1585            MaybeOwner::NonOwner(_) | MaybeOwner::Phantom => None,
1586        }
1587    }
1588
1589    pub fn unwrap(self) -> &'tcx OwnerInfo<'tcx> {
1590        self.as_owner().unwrap_or_else(|| panic!("Not a HIR owner"))
1591    }
1592}
1593
1594/// The top-level data structure that stores the entire contents of
1595/// the crate currently being compiled.
1596///
1597/// For more details, see the [rustc dev guide].
1598///
1599/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
1600#[derive(Debug)]
1601pub struct Crate<'hir> {
1602    pub owners: IndexVec<LocalDefId, MaybeOwner<'hir>>,
1603    // Only present when incr. comp. is enabled.
1604    pub opt_hir_hash: Option<Fingerprint>,
1605}
1606
1607#[derive(Debug, Clone, Copy, HashStable_Generic)]
1608pub struct Closure<'hir> {
1609    pub def_id: LocalDefId,
1610    pub binder: ClosureBinder,
1611    pub constness: Constness,
1612    pub capture_clause: CaptureBy,
1613    pub bound_generic_params: &'hir [GenericParam<'hir>],
1614    pub fn_decl: &'hir FnDecl<'hir>,
1615    pub body: BodyId,
1616    /// The span of the declaration block: 'move |...| -> ...'
1617    pub fn_decl_span: Span,
1618    /// The span of the argument block `|...|`
1619    pub fn_arg_span: Option<Span>,
1620    pub kind: ClosureKind,
1621}
1622
1623#[derive(Clone, PartialEq, Eq, Debug, Copy, Hash, HashStable_Generic, Encodable, Decodable)]
1624pub enum ClosureKind {
1625    /// This is a plain closure expression.
1626    Closure,
1627    /// This is a coroutine expression -- i.e. a closure expression in which
1628    /// we've found a `yield`. These can arise either from "plain" coroutine
1629    ///  usage (e.g. `let x = || { yield (); }`) or from a desugared expression
1630    /// (e.g. `async` and `gen` blocks).
1631    Coroutine(CoroutineKind),
1632    /// This is a coroutine-closure, which is a special sugared closure that
1633    /// returns one of the sugared coroutine (`async`/`gen`/`async gen`). It
1634    /// additionally allows capturing the coroutine's upvars by ref, and therefore
1635    /// needs to be specially treated during analysis and borrowck.
1636    CoroutineClosure(CoroutineDesugaring),
1637}
1638
1639/// A block of statements `{ .. }`, which may have a label (in this case the
1640/// `targeted_by_break` field will be `true`) and may be `unsafe` by means of
1641/// the `rules` being anything but `DefaultBlock`.
1642#[derive(Debug, Clone, Copy, HashStable_Generic)]
1643pub struct Block<'hir> {
1644    /// Statements in a block.
1645    pub stmts: &'hir [Stmt<'hir>],
1646    /// An expression at the end of the block
1647    /// without a semicolon, if any.
1648    pub expr: Option<&'hir Expr<'hir>>,
1649    #[stable_hasher(ignore)]
1650    pub hir_id: HirId,
1651    /// Distinguishes between `unsafe { ... }` and `{ ... }`.
1652    pub rules: BlockCheckMode,
1653    /// The span includes the curly braces `{` and `}` around the block.
1654    pub span: Span,
1655    /// If true, then there may exist `break 'a` values that aim to
1656    /// break out of this block early.
1657    /// Used by `'label: {}` blocks and by `try {}` blocks.
1658    pub targeted_by_break: bool,
1659}
1660
1661impl<'hir> Block<'hir> {
1662    pub fn innermost_block(&self) -> &Block<'hir> {
1663        let mut block = self;
1664        while let Some(Expr { kind: ExprKind::Block(inner_block, _), .. }) = block.expr {
1665            block = inner_block;
1666        }
1667        block
1668    }
1669}
1670
1671#[derive(Debug, Clone, Copy, HashStable_Generic)]
1672pub struct TyPat<'hir> {
1673    #[stable_hasher(ignore)]
1674    pub hir_id: HirId,
1675    pub kind: TyPatKind<'hir>,
1676    pub span: Span,
1677}
1678
1679#[derive(Debug, Clone, Copy, HashStable_Generic)]
1680pub struct Pat<'hir> {
1681    #[stable_hasher(ignore)]
1682    pub hir_id: HirId,
1683    pub kind: PatKind<'hir>,
1684    pub span: Span,
1685    /// Whether to use default binding modes.
1686    /// At present, this is false only for destructuring assignment.
1687    pub default_binding_modes: bool,
1688}
1689
1690impl<'hir> Pat<'hir> {
1691    fn walk_short_(&self, it: &mut impl FnMut(&Pat<'hir>) -> bool) -> bool {
1692        if !it(self) {
1693            return false;
1694        }
1695
1696        use PatKind::*;
1697        match self.kind {
1698            Missing => unreachable!(),
1699            Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Err(_) => true,
1700            Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_short_(it),
1701            Struct(_, fields, _) => fields.iter().all(|field| field.pat.walk_short_(it)),
1702            TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().all(|p| p.walk_short_(it)),
1703            Slice(before, slice, after) => {
1704                before.iter().chain(slice).chain(after.iter()).all(|p| p.walk_short_(it))
1705            }
1706        }
1707    }
1708
1709    /// Walk the pattern in left-to-right order,
1710    /// short circuiting (with `.all(..)`) if `false` is returned.
1711    ///
1712    /// Note that when visiting e.g. `Tuple(ps)`,
1713    /// if visiting `ps[0]` returns `false`,
1714    /// then `ps[1]` will not be visited.
1715    pub fn walk_short(&self, mut it: impl FnMut(&Pat<'hir>) -> bool) -> bool {
1716        self.walk_short_(&mut it)
1717    }
1718
1719    fn walk_(&self, it: &mut impl FnMut(&Pat<'hir>) -> bool) {
1720        if !it(self) {
1721            return;
1722        }
1723
1724        use PatKind::*;
1725        match self.kind {
1726            Missing | Wild | Never | Expr(_) | Range(..) | Binding(.., None) | Err(_) => {}
1727            Box(s) | Deref(s) | Ref(s, _) | Binding(.., Some(s)) | Guard(s, _) => s.walk_(it),
1728            Struct(_, fields, _) => fields.iter().for_each(|field| field.pat.walk_(it)),
1729            TupleStruct(_, s, _) | Tuple(s, _) | Or(s) => s.iter().for_each(|p| p.walk_(it)),
1730            Slice(before, slice, after) => {
1731                before.iter().chain(slice).chain(after.iter()).for_each(|p| p.walk_(it))
1732            }
1733        }
1734    }
1735
1736    /// Walk the pattern in left-to-right order.
1737    ///
1738    /// If `it(pat)` returns `false`, the children are not visited.
1739    pub fn walk(&self, mut it: impl FnMut(&Pat<'hir>) -> bool) {
1740        self.walk_(&mut it)
1741    }
1742
1743    /// Walk the pattern in left-to-right order.
1744    ///
1745    /// If you always want to recurse, prefer this method over `walk`.
1746    pub fn walk_always(&self, mut it: impl FnMut(&Pat<'_>)) {
1747        self.walk(|p| {
1748            it(p);
1749            true
1750        })
1751    }
1752
1753    /// Whether this a never pattern.
1754    pub fn is_never_pattern(&self) -> bool {
1755        let mut is_never_pattern = false;
1756        self.walk(|pat| match &pat.kind {
1757            PatKind::Never => {
1758                is_never_pattern = true;
1759                false
1760            }
1761            PatKind::Or(s) => {
1762                is_never_pattern = s.iter().all(|p| p.is_never_pattern());
1763                false
1764            }
1765            _ => true,
1766        });
1767        is_never_pattern
1768    }
1769}
1770
1771/// A single field in a struct pattern.
1772///
1773/// Patterns like the fields of Foo `{ x, ref y, ref mut z }`
1774/// are treated the same as` x: x, y: ref y, z: ref mut z`,
1775/// except `is_shorthand` is true.
1776#[derive(Debug, Clone, Copy, HashStable_Generic)]
1777pub struct PatField<'hir> {
1778    #[stable_hasher(ignore)]
1779    pub hir_id: HirId,
1780    /// The identifier for the field.
1781    pub ident: Ident,
1782    /// The pattern the field is destructured to.
1783    pub pat: &'hir Pat<'hir>,
1784    pub is_shorthand: bool,
1785    pub span: Span,
1786}
1787
1788#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic, Hash, Eq, Encodable, Decodable)]
1789pub enum RangeEnd {
1790    Included,
1791    Excluded,
1792}
1793
1794impl fmt::Display for RangeEnd {
1795    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1796        f.write_str(match self {
1797            RangeEnd::Included => "..=",
1798            RangeEnd::Excluded => "..",
1799        })
1800    }
1801}
1802
1803// Equivalent to `Option<usize>`. That type takes up 16 bytes on 64-bit, but
1804// this type only takes up 4 bytes, at the cost of being restricted to a
1805// maximum value of `u32::MAX - 1`. In practice, this is more than enough.
1806#[derive(Clone, Copy, PartialEq, Eq, Hash, HashStable_Generic)]
1807pub struct DotDotPos(u32);
1808
1809impl DotDotPos {
1810    /// Panics if n >= u32::MAX.
1811    pub fn new(n: Option<usize>) -> Self {
1812        match n {
1813            Some(n) => {
1814                assert!(n < u32::MAX as usize);
1815                Self(n as u32)
1816            }
1817            None => Self(u32::MAX),
1818        }
1819    }
1820
1821    pub fn as_opt_usize(&self) -> Option<usize> {
1822        if self.0 == u32::MAX { None } else { Some(self.0 as usize) }
1823    }
1824}
1825
1826impl fmt::Debug for DotDotPos {
1827    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1828        self.as_opt_usize().fmt(f)
1829    }
1830}
1831
1832#[derive(Debug, Clone, Copy, HashStable_Generic)]
1833pub struct PatExpr<'hir> {
1834    #[stable_hasher(ignore)]
1835    pub hir_id: HirId,
1836    pub span: Span,
1837    pub kind: PatExprKind<'hir>,
1838}
1839
1840#[derive(Debug, Clone, Copy, HashStable_Generic)]
1841pub enum PatExprKind<'hir> {
1842    Lit {
1843        lit: Lit,
1844        // FIXME: move this into `Lit` and handle negated literal expressions
1845        // once instead of matching on unop neg expressions everywhere.
1846        negated: bool,
1847    },
1848    ConstBlock(ConstBlock),
1849    /// A path pattern for a unit struct/variant or a (maybe-associated) constant.
1850    Path(QPath<'hir>),
1851}
1852
1853#[derive(Debug, Clone, Copy, HashStable_Generic)]
1854pub enum TyPatKind<'hir> {
1855    /// A range pattern (e.g., `1..=2` or `1..2`).
1856    Range(&'hir ConstArg<'hir>, &'hir ConstArg<'hir>),
1857
1858    /// A list of patterns where only one needs to be satisfied
1859    Or(&'hir [TyPat<'hir>]),
1860
1861    /// A placeholder for a pattern that wasn't well formed in some way.
1862    Err(ErrorGuaranteed),
1863}
1864
1865#[derive(Debug, Clone, Copy, HashStable_Generic)]
1866pub enum PatKind<'hir> {
1867    /// A missing pattern, e.g. for an anonymous param in a bare fn like `fn f(u32)`.
1868    Missing,
1869
1870    /// Represents a wildcard pattern (i.e., `_`).
1871    Wild,
1872
1873    /// A fresh binding `ref mut binding @ OPT_SUBPATTERN`.
1874    /// The `HirId` is the canonical ID for the variable being bound,
1875    /// (e.g., in `Ok(x) | Err(x)`, both `x` use the same canonical ID),
1876    /// which is the pattern ID of the first `x`.
1877    ///
1878    /// The `BindingMode` is what's provided by the user, before match
1879    /// ergonomics are applied. For the binding mode actually in use,
1880    /// see [`TypeckResults::extract_binding_mode`].
1881    ///
1882    /// [`TypeckResults::extract_binding_mode`]: ../../rustc_middle/ty/struct.TypeckResults.html#method.extract_binding_mode
1883    Binding(BindingMode, HirId, Ident, Option<&'hir Pat<'hir>>),
1884
1885    /// A struct or struct variant pattern (e.g., `Variant {x, y, ..}`).
1886    /// The `Option` contains the span of a possible `..`.
1887    Struct(QPath<'hir>, &'hir [PatField<'hir>], Option<Span>),
1888
1889    /// A tuple struct/variant pattern `Variant(x, y, .., z)`.
1890    /// If the `..` pattern fragment is present, then `DotDotPos` denotes its position.
1891    /// `0 <= position <= subpats.len()`
1892    TupleStruct(QPath<'hir>, &'hir [Pat<'hir>], DotDotPos),
1893
1894    /// An or-pattern `A | B | C`.
1895    /// Invariant: `pats.len() >= 2`.
1896    Or(&'hir [Pat<'hir>]),
1897
1898    /// A never pattern `!`.
1899    Never,
1900
1901    /// A tuple pattern (e.g., `(a, b)`).
1902    /// If the `..` pattern fragment is present, then `DotDotPos` denotes its position.
1903    /// `0 <= position <= subpats.len()`
1904    Tuple(&'hir [Pat<'hir>], DotDotPos),
1905
1906    /// A `box` pattern.
1907    Box(&'hir Pat<'hir>),
1908
1909    /// A `deref` pattern (currently `deref!()` macro-based syntax).
1910    Deref(&'hir Pat<'hir>),
1911
1912    /// A reference pattern (e.g., `&mut (a, b)`).
1913    Ref(&'hir Pat<'hir>, Mutability),
1914
1915    /// A literal, const block or path.
1916    Expr(&'hir PatExpr<'hir>),
1917
1918    /// A guard pattern (e.g., `x if guard(x)`).
1919    Guard(&'hir Pat<'hir>, &'hir Expr<'hir>),
1920
1921    /// A range pattern (e.g., `1..=2` or `1..2`).
1922    Range(Option<&'hir PatExpr<'hir>>, Option<&'hir PatExpr<'hir>>, RangeEnd),
1923
1924    /// A slice pattern, `[before_0, ..., before_n, (slice, after_0, ..., after_n)?]`.
1925    ///
1926    /// Here, `slice` is lowered from the syntax `($binding_mode $ident @)? ..`.
1927    /// If `slice` exists, then `after` can be non-empty.
1928    ///
1929    /// The representation for e.g., `[a, b, .., c, d]` is:
1930    /// ```ignore (illustrative)
1931    /// PatKind::Slice([Binding(a), Binding(b)], Some(Wild), [Binding(c), Binding(d)])
1932    /// ```
1933    Slice(&'hir [Pat<'hir>], Option<&'hir Pat<'hir>>, &'hir [Pat<'hir>]),
1934
1935    /// A placeholder for a pattern that wasn't well formed in some way.
1936    Err(ErrorGuaranteed),
1937}
1938
1939/// A statement.
1940#[derive(Debug, Clone, Copy, HashStable_Generic)]
1941pub struct Stmt<'hir> {
1942    #[stable_hasher(ignore)]
1943    pub hir_id: HirId,
1944    pub kind: StmtKind<'hir>,
1945    pub span: Span,
1946}
1947
1948/// The contents of a statement.
1949#[derive(Debug, Clone, Copy, HashStable_Generic)]
1950pub enum StmtKind<'hir> {
1951    /// A local (`let`) binding.
1952    Let(&'hir LetStmt<'hir>),
1953
1954    /// An item binding.
1955    Item(ItemId),
1956
1957    /// An expression without a trailing semi-colon (must have unit type).
1958    Expr(&'hir Expr<'hir>),
1959
1960    /// An expression with a trailing semi-colon (may have any type).
1961    Semi(&'hir Expr<'hir>),
1962}
1963
1964/// Represents a `let` statement (i.e., `let <pat>:<ty> = <init>;`).
1965#[derive(Debug, Clone, Copy, HashStable_Generic)]
1966pub struct LetStmt<'hir> {
1967    /// Span of `super` in `super let`.
1968    pub super_: Option<Span>,
1969    pub pat: &'hir Pat<'hir>,
1970    /// Type annotation, if any (otherwise the type will be inferred).
1971    pub ty: Option<&'hir Ty<'hir>>,
1972    /// Initializer expression to set the value, if any.
1973    pub init: Option<&'hir Expr<'hir>>,
1974    /// Else block for a `let...else` binding.
1975    pub els: Option<&'hir Block<'hir>>,
1976    #[stable_hasher(ignore)]
1977    pub hir_id: HirId,
1978    pub span: Span,
1979    /// Can be `ForLoopDesugar` if the `let` statement is part of a `for` loop
1980    /// desugaring, or `AssignDesugar` if it is the result of a complex
1981    /// assignment desugaring. Otherwise will be `Normal`.
1982    pub source: LocalSource,
1983}
1984
1985/// Represents a single arm of a `match` expression, e.g.
1986/// `<pat> (if <guard>) => <body>`.
1987#[derive(Debug, Clone, Copy, HashStable_Generic)]
1988pub struct Arm<'hir> {
1989    #[stable_hasher(ignore)]
1990    pub hir_id: HirId,
1991    pub span: Span,
1992    /// If this pattern and the optional guard matches, then `body` is evaluated.
1993    pub pat: &'hir Pat<'hir>,
1994    /// Optional guard clause.
1995    pub guard: Option<&'hir Expr<'hir>>,
1996    /// The expression the arm evaluates to if this arm matches.
1997    pub body: &'hir Expr<'hir>,
1998}
1999
2000/// Represents a `let <pat>[: <ty>] = <expr>` expression (not a [`LetStmt`]), occurring in an `if-let`
2001/// or `let-else`, evaluating to a boolean. Typically the pattern is refutable.
2002///
2003/// In an `if let`, imagine it as `if (let <pat> = <expr>) { ... }`; in a let-else, it is part of
2004/// the desugaring to if-let. Only let-else supports the type annotation at present.
2005#[derive(Debug, Clone, Copy, HashStable_Generic)]
2006pub struct LetExpr<'hir> {
2007    pub span: Span,
2008    pub pat: &'hir Pat<'hir>,
2009    pub ty: Option<&'hir Ty<'hir>>,
2010    pub init: &'hir Expr<'hir>,
2011    /// `Recovered::Yes` when this let expressions is not in a syntactically valid location.
2012    /// Used to prevent building MIR in such situations.
2013    pub recovered: ast::Recovered,
2014}
2015
2016#[derive(Debug, Clone, Copy, HashStable_Generic)]
2017pub struct ExprField<'hir> {
2018    #[stable_hasher(ignore)]
2019    pub hir_id: HirId,
2020    pub ident: Ident,
2021    pub expr: &'hir Expr<'hir>,
2022    pub span: Span,
2023    pub is_shorthand: bool,
2024}
2025
2026#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
2027pub enum BlockCheckMode {
2028    DefaultBlock,
2029    UnsafeBlock(UnsafeSource),
2030}
2031
2032#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
2033pub enum UnsafeSource {
2034    CompilerGenerated,
2035    UserProvided,
2036}
2037
2038#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
2039pub struct BodyId {
2040    pub hir_id: HirId,
2041}
2042
2043/// The body of a function, closure, or constant value. In the case of
2044/// a function, the body contains not only the function body itself
2045/// (which is an expression), but also the argument patterns, since
2046/// those are something that the caller doesn't really care about.
2047///
2048/// # Examples
2049///
2050/// ```
2051/// fn foo((x, y): (u32, u32)) -> u32 {
2052///     x + y
2053/// }
2054/// ```
2055///
2056/// Here, the `Body` associated with `foo()` would contain:
2057///
2058/// - an `params` array containing the `(x, y)` pattern
2059/// - a `value` containing the `x + y` expression (maybe wrapped in a block)
2060/// - `coroutine_kind` would be `None`
2061///
2062/// All bodies have an **owner**, which can be accessed via the HIR
2063/// map using `body_owner_def_id()`.
2064#[derive(Debug, Clone, Copy, HashStable_Generic)]
2065pub struct Body<'hir> {
2066    pub params: &'hir [Param<'hir>],
2067    pub value: &'hir Expr<'hir>,
2068}
2069
2070impl<'hir> Body<'hir> {
2071    pub fn id(&self) -> BodyId {
2072        BodyId { hir_id: self.value.hir_id }
2073    }
2074}
2075
2076/// The type of source expression that caused this coroutine to be created.
2077#[derive(Clone, PartialEq, Eq, Debug, Copy, Hash, HashStable_Generic, Encodable, Decodable)]
2078pub enum CoroutineKind {
2079    /// A coroutine that comes from a desugaring.
2080    Desugared(CoroutineDesugaring, CoroutineSource),
2081
2082    /// A coroutine literal created via a `yield` inside a closure.
2083    Coroutine(Movability),
2084}
2085
2086impl CoroutineKind {
2087    pub fn movability(self) -> Movability {
2088        match self {
2089            CoroutineKind::Desugared(CoroutineDesugaring::Async, _)
2090            | CoroutineKind::Desugared(CoroutineDesugaring::AsyncGen, _) => Movability::Static,
2091            CoroutineKind::Desugared(CoroutineDesugaring::Gen, _) => Movability::Movable,
2092            CoroutineKind::Coroutine(mov) => mov,
2093        }
2094    }
2095
2096    pub fn is_fn_like(self) -> bool {
2097        matches!(self, CoroutineKind::Desugared(_, CoroutineSource::Fn))
2098    }
2099
2100    pub fn to_plural_string(&self) -> String {
2101        match self {
2102            CoroutineKind::Desugared(d, CoroutineSource::Fn) => format!("{d:#}fn bodies"),
2103            CoroutineKind::Desugared(d, CoroutineSource::Block) => format!("{d:#}blocks"),
2104            CoroutineKind::Desugared(d, CoroutineSource::Closure) => format!("{d:#}closure bodies"),
2105            CoroutineKind::Coroutine(_) => "coroutines".to_string(),
2106        }
2107    }
2108}
2109
2110impl fmt::Display for CoroutineKind {
2111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2112        match self {
2113            CoroutineKind::Desugared(d, k) => {
2114                d.fmt(f)?;
2115                k.fmt(f)
2116            }
2117            CoroutineKind::Coroutine(_) => f.write_str("coroutine"),
2118        }
2119    }
2120}
2121
2122/// In the case of a coroutine created as part of an async/gen construct,
2123/// which kind of async/gen construct caused it to be created?
2124///
2125/// This helps error messages but is also used to drive coercions in
2126/// type-checking (see #60424).
2127#[derive(Clone, PartialEq, Eq, Hash, Debug, Copy, HashStable_Generic, Encodable, Decodable)]
2128pub enum CoroutineSource {
2129    /// An explicit `async`/`gen` block written by the user.
2130    Block,
2131
2132    /// An explicit `async`/`gen` closure written by the user.
2133    Closure,
2134
2135    /// The `async`/`gen` block generated as the body of an async/gen function.
2136    Fn,
2137}
2138
2139impl fmt::Display for CoroutineSource {
2140    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2141        match self {
2142            CoroutineSource::Block => "block",
2143            CoroutineSource::Closure => "closure body",
2144            CoroutineSource::Fn => "fn body",
2145        }
2146        .fmt(f)
2147    }
2148}
2149
2150#[derive(Clone, PartialEq, Eq, Debug, Copy, Hash, HashStable_Generic, Encodable, Decodable)]
2151pub enum CoroutineDesugaring {
2152    /// An explicit `async` block or the body of an `async` function.
2153    Async,
2154
2155    /// An explicit `gen` block or the body of a `gen` function.
2156    Gen,
2157
2158    /// An explicit `async gen` block or the body of an `async gen` function,
2159    /// which is able to both `yield` and `.await`.
2160    AsyncGen,
2161}
2162
2163impl fmt::Display for CoroutineDesugaring {
2164    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2165        match self {
2166            CoroutineDesugaring::Async => {
2167                if f.alternate() {
2168                    f.write_str("`async` ")?;
2169                } else {
2170                    f.write_str("async ")?
2171                }
2172            }
2173            CoroutineDesugaring::Gen => {
2174                if f.alternate() {
2175                    f.write_str("`gen` ")?;
2176                } else {
2177                    f.write_str("gen ")?
2178                }
2179            }
2180            CoroutineDesugaring::AsyncGen => {
2181                if f.alternate() {
2182                    f.write_str("`async gen` ")?;
2183                } else {
2184                    f.write_str("async gen ")?
2185                }
2186            }
2187        }
2188
2189        Ok(())
2190    }
2191}
2192
2193#[derive(Copy, Clone, Debug)]
2194pub enum BodyOwnerKind {
2195    /// Functions and methods.
2196    Fn,
2197
2198    /// Closures
2199    Closure,
2200
2201    /// Constants and associated constants, also including inline constants.
2202    Const { inline: bool },
2203
2204    /// Initializer of a `static` item.
2205    Static(Mutability),
2206
2207    /// Fake body for a global asm to store its const-like value types.
2208    GlobalAsm,
2209}
2210
2211impl BodyOwnerKind {
2212    pub fn is_fn_or_closure(self) -> bool {
2213        match self {
2214            BodyOwnerKind::Fn | BodyOwnerKind::Closure => true,
2215            BodyOwnerKind::Const { .. } | BodyOwnerKind::Static(_) | BodyOwnerKind::GlobalAsm => {
2216                false
2217            }
2218        }
2219    }
2220}
2221
2222/// The kind of an item that requires const-checking.
2223#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2224pub enum ConstContext {
2225    /// A `const fn`.
2226    ConstFn,
2227
2228    /// A `static` or `static mut`.
2229    Static(Mutability),
2230
2231    /// A `const`, associated `const`, or other const context.
2232    ///
2233    /// Other contexts include:
2234    /// - Array length expressions
2235    /// - Enum discriminants
2236    /// - Const generics
2237    ///
2238    /// For the most part, other contexts are treated just like a regular `const`, so they are
2239    /// lumped into the same category.
2240    Const { inline: bool },
2241}
2242
2243impl ConstContext {
2244    /// A description of this const context that can appear between backticks in an error message.
2245    ///
2246    /// E.g. `const` or `static mut`.
2247    pub fn keyword_name(self) -> &'static str {
2248        match self {
2249            Self::Const { .. } => "const",
2250            Self::Static(Mutability::Not) => "static",
2251            Self::Static(Mutability::Mut) => "static mut",
2252            Self::ConstFn => "const fn",
2253        }
2254    }
2255}
2256
2257/// A colloquial, trivially pluralizable description of this const context for use in error
2258/// messages.
2259impl fmt::Display for ConstContext {
2260    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2261        match *self {
2262            Self::Const { .. } => write!(f, "constant"),
2263            Self::Static(_) => write!(f, "static"),
2264            Self::ConstFn => write!(f, "constant function"),
2265        }
2266    }
2267}
2268
2269impl IntoDiagArg for ConstContext {
2270    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
2271        DiagArgValue::Str(Cow::Borrowed(match self {
2272            ConstContext::ConstFn => "const_fn",
2273            ConstContext::Static(_) => "static",
2274            ConstContext::Const { .. } => "const",
2275        }))
2276    }
2277}
2278
2279/// A literal.
2280pub type Lit = Spanned<LitKind>;
2281
2282/// A constant (expression) that's not an item or associated item,
2283/// but needs its own `DefId` for type-checking, const-eval, etc.
2284/// These are usually found nested inside types (e.g., array lengths)
2285/// or expressions (e.g., repeat counts), and also used to define
2286/// explicit discriminant values for enum variants.
2287///
2288/// You can check if this anon const is a default in a const param
2289/// `const N: usize = { ... }` with `tcx.hir_opt_const_param_default_param_def_id(..)`
2290#[derive(Copy, Clone, Debug, HashStable_Generic)]
2291pub struct AnonConst {
2292    #[stable_hasher(ignore)]
2293    pub hir_id: HirId,
2294    pub def_id: LocalDefId,
2295    pub body: BodyId,
2296    pub span: Span,
2297}
2298
2299/// An inline constant expression `const { something }`.
2300#[derive(Copy, Clone, Debug, HashStable_Generic)]
2301pub struct ConstBlock {
2302    #[stable_hasher(ignore)]
2303    pub hir_id: HirId,
2304    pub def_id: LocalDefId,
2305    pub body: BodyId,
2306}
2307
2308/// An expression.
2309///
2310/// For more details, see the [rust lang reference].
2311/// Note that the reference does not document nightly-only features.
2312/// There may be also slight differences in the names and representation of AST nodes between
2313/// the compiler and the reference.
2314///
2315/// [rust lang reference]: https://doc.rust-lang.org/reference/expressions.html
2316#[derive(Debug, Clone, Copy, HashStable_Generic)]
2317pub struct Expr<'hir> {
2318    #[stable_hasher(ignore)]
2319    pub hir_id: HirId,
2320    pub kind: ExprKind<'hir>,
2321    pub span: Span,
2322}
2323
2324impl Expr<'_> {
2325    pub fn precedence(&self, has_attr: &dyn Fn(HirId) -> bool) -> ExprPrecedence {
2326        let prefix_attrs_precedence = || -> ExprPrecedence {
2327            if has_attr(self.hir_id) { ExprPrecedence::Prefix } else { ExprPrecedence::Unambiguous }
2328        };
2329
2330        match &self.kind {
2331            ExprKind::Closure(closure) => {
2332                match closure.fn_decl.output {
2333                    FnRetTy::DefaultReturn(_) => ExprPrecedence::Jump,
2334                    FnRetTy::Return(_) => prefix_attrs_precedence(),
2335                }
2336            }
2337
2338            ExprKind::Break(..)
2339            | ExprKind::Ret(..)
2340            | ExprKind::Yield(..)
2341            | ExprKind::Become(..) => ExprPrecedence::Jump,
2342
2343            // Binop-like expr kinds, handled by `AssocOp`.
2344            ExprKind::Binary(op, ..) => op.node.precedence(),
2345            ExprKind::Cast(..) => ExprPrecedence::Cast,
2346
2347            ExprKind::Assign(..) |
2348            ExprKind::AssignOp(..) => ExprPrecedence::Assign,
2349
2350            // Unary, prefix
2351            ExprKind::AddrOf(..)
2352            // Here `let pats = expr` has `let pats =` as a "unary" prefix of `expr`.
2353            // However, this is not exactly right. When `let _ = a` is the LHS of a binop we
2354            // need parens sometimes. E.g. we can print `(let _ = a) && b` as `let _ = a && b`
2355            // but we need to print `(let _ = a) < b` as-is with parens.
2356            | ExprKind::Let(..)
2357            | ExprKind::Unary(..) => ExprPrecedence::Prefix,
2358
2359            // Need parens if and only if there are prefix attributes.
2360            ExprKind::Array(_)
2361            | ExprKind::Block(..)
2362            | ExprKind::Call(..)
2363            | ExprKind::ConstBlock(_)
2364            | ExprKind::Continue(..)
2365            | ExprKind::Field(..)
2366            | ExprKind::If(..)
2367            | ExprKind::Index(..)
2368            | ExprKind::InlineAsm(..)
2369            | ExprKind::Lit(_)
2370            | ExprKind::Loop(..)
2371            | ExprKind::Match(..)
2372            | ExprKind::MethodCall(..)
2373            | ExprKind::OffsetOf(..)
2374            | ExprKind::Path(..)
2375            | ExprKind::Repeat(..)
2376            | ExprKind::Struct(..)
2377            | ExprKind::Tup(_)
2378            | ExprKind::Type(..)
2379            | ExprKind::UnsafeBinderCast(..)
2380            | ExprKind::Use(..)
2381            | ExprKind::Err(_) => prefix_attrs_precedence(),
2382
2383            ExprKind::DropTemps(expr, ..) => expr.precedence(has_attr),
2384        }
2385    }
2386
2387    /// Whether this looks like a place expr, without checking for deref
2388    /// adjustments.
2389    /// This will return `true` in some potentially surprising cases such as
2390    /// `CONSTANT.field`.
2391    pub fn is_syntactic_place_expr(&self) -> bool {
2392        self.is_place_expr(|_| true)
2393    }
2394
2395    /// Whether this is a place expression.
2396    ///
2397    /// `allow_projections_from` should return `true` if indexing a field or index expression based
2398    /// on the given expression should be considered a place expression.
2399    pub fn is_place_expr(&self, mut allow_projections_from: impl FnMut(&Self) -> bool) -> bool {
2400        match self.kind {
2401            ExprKind::Path(QPath::Resolved(_, ref path)) => {
2402                matches!(path.res, Res::Local(..) | Res::Def(DefKind::Static { .. }, _) | Res::Err)
2403            }
2404
2405            // Type ascription inherits its place expression kind from its
2406            // operand. See:
2407            // https://github.com/rust-lang/rfcs/blob/master/text/0803-type-ascription.md#type-ascription-and-temporaries
2408            ExprKind::Type(ref e, _) => e.is_place_expr(allow_projections_from),
2409
2410            // Unsafe binder cast preserves place-ness of the sub-expression.
2411            ExprKind::UnsafeBinderCast(_, e, _) => e.is_place_expr(allow_projections_from),
2412
2413            ExprKind::Unary(UnOp::Deref, _) => true,
2414
2415            ExprKind::Field(ref base, _) | ExprKind::Index(ref base, _, _) => {
2416                allow_projections_from(base) || base.is_place_expr(allow_projections_from)
2417            }
2418
2419            // Lang item paths cannot currently be local variables or statics.
2420            ExprKind::Path(QPath::LangItem(..)) => false,
2421
2422            // Suppress errors for bad expressions.
2423            ExprKind::Err(_guar)
2424            | ExprKind::Let(&LetExpr { recovered: ast::Recovered::Yes(_guar), .. }) => true,
2425
2426            // Partially qualified paths in expressions can only legally
2427            // refer to associated items which are always rvalues.
2428            ExprKind::Path(QPath::TypeRelative(..))
2429            | ExprKind::Call(..)
2430            | ExprKind::MethodCall(..)
2431            | ExprKind::Use(..)
2432            | ExprKind::Struct(..)
2433            | ExprKind::Tup(..)
2434            | ExprKind::If(..)
2435            | ExprKind::Match(..)
2436            | ExprKind::Closure { .. }
2437            | ExprKind::Block(..)
2438            | ExprKind::Repeat(..)
2439            | ExprKind::Array(..)
2440            | ExprKind::Break(..)
2441            | ExprKind::Continue(..)
2442            | ExprKind::Ret(..)
2443            | ExprKind::Become(..)
2444            | ExprKind::Let(..)
2445            | ExprKind::Loop(..)
2446            | ExprKind::Assign(..)
2447            | ExprKind::InlineAsm(..)
2448            | ExprKind::OffsetOf(..)
2449            | ExprKind::AssignOp(..)
2450            | ExprKind::Lit(_)
2451            | ExprKind::ConstBlock(..)
2452            | ExprKind::Unary(..)
2453            | ExprKind::AddrOf(..)
2454            | ExprKind::Binary(..)
2455            | ExprKind::Yield(..)
2456            | ExprKind::Cast(..)
2457            | ExprKind::DropTemps(..) => false,
2458        }
2459    }
2460
2461    /// Check if expression is an integer literal that can be used
2462    /// where `usize` is expected.
2463    pub fn is_size_lit(&self) -> bool {
2464        matches!(
2465            self.kind,
2466            ExprKind::Lit(Lit {
2467                node: LitKind::Int(_, LitIntType::Unsuffixed | LitIntType::Unsigned(UintTy::Usize)),
2468                ..
2469            })
2470        )
2471    }
2472
2473    /// If `Self.kind` is `ExprKind::DropTemps(expr)`, drill down until we get a non-`DropTemps`
2474    /// `Expr`. This is used in suggestions to ignore this `ExprKind` as it is semantically
2475    /// silent, only signaling the ownership system. By doing this, suggestions that check the
2476    /// `ExprKind` of any given `Expr` for presentation don't have to care about `DropTemps`
2477    /// beyond remembering to call this function before doing analysis on it.
2478    pub fn peel_drop_temps(&self) -> &Self {
2479        let mut expr = self;
2480        while let ExprKind::DropTemps(inner) = &expr.kind {
2481            expr = inner;
2482        }
2483        expr
2484    }
2485
2486    pub fn peel_blocks(&self) -> &Self {
2487        let mut expr = self;
2488        while let ExprKind::Block(Block { expr: Some(inner), .. }, _) = &expr.kind {
2489            expr = inner;
2490        }
2491        expr
2492    }
2493
2494    pub fn peel_borrows(&self) -> &Self {
2495        let mut expr = self;
2496        while let ExprKind::AddrOf(.., inner) = &expr.kind {
2497            expr = inner;
2498        }
2499        expr
2500    }
2501
2502    pub fn can_have_side_effects(&self) -> bool {
2503        match self.peel_drop_temps().kind {
2504            ExprKind::Path(_) | ExprKind::Lit(_) | ExprKind::OffsetOf(..) | ExprKind::Use(..) => {
2505                false
2506            }
2507            ExprKind::Type(base, _)
2508            | ExprKind::Unary(_, base)
2509            | ExprKind::Field(base, _)
2510            | ExprKind::Index(base, _, _)
2511            | ExprKind::AddrOf(.., base)
2512            | ExprKind::Cast(base, _)
2513            | ExprKind::UnsafeBinderCast(_, base, _) => {
2514                // This isn't exactly true for `Index` and all `Unary`, but we are using this
2515                // method exclusively for diagnostics and there's a *cultural* pressure against
2516                // them being used only for its side-effects.
2517                base.can_have_side_effects()
2518            }
2519            ExprKind::Struct(_, fields, init) => {
2520                let init_side_effects = match init {
2521                    StructTailExpr::Base(init) => init.can_have_side_effects(),
2522                    StructTailExpr::DefaultFields(_) | StructTailExpr::None => false,
2523                };
2524                fields.iter().map(|field| field.expr).any(|e| e.can_have_side_effects())
2525                    || init_side_effects
2526            }
2527
2528            ExprKind::Array(args)
2529            | ExprKind::Tup(args)
2530            | ExprKind::Call(
2531                Expr {
2532                    kind:
2533                        ExprKind::Path(QPath::Resolved(
2534                            None,
2535                            Path { res: Res::Def(DefKind::Ctor(_, CtorKind::Fn), _), .. },
2536                        )),
2537                    ..
2538                },
2539                args,
2540            ) => args.iter().any(|arg| arg.can_have_side_effects()),
2541            ExprKind::If(..)
2542            | ExprKind::Match(..)
2543            | ExprKind::MethodCall(..)
2544            | ExprKind::Call(..)
2545            | ExprKind::Closure { .. }
2546            | ExprKind::Block(..)
2547            | ExprKind::Repeat(..)
2548            | ExprKind::Break(..)
2549            | ExprKind::Continue(..)
2550            | ExprKind::Ret(..)
2551            | ExprKind::Become(..)
2552            | ExprKind::Let(..)
2553            | ExprKind::Loop(..)
2554            | ExprKind::Assign(..)
2555            | ExprKind::InlineAsm(..)
2556            | ExprKind::AssignOp(..)
2557            | ExprKind::ConstBlock(..)
2558            | ExprKind::Binary(..)
2559            | ExprKind::Yield(..)
2560            | ExprKind::DropTemps(..)
2561            | ExprKind::Err(_) => true,
2562        }
2563    }
2564
2565    /// To a first-order approximation, is this a pattern?
2566    pub fn is_approximately_pattern(&self) -> bool {
2567        match &self.kind {
2568            ExprKind::Array(_)
2569            | ExprKind::Call(..)
2570            | ExprKind::Tup(_)
2571            | ExprKind::Lit(_)
2572            | ExprKind::Path(_)
2573            | ExprKind::Struct(..) => true,
2574            _ => false,
2575        }
2576    }
2577
2578    /// Whether this and the `other` expression are the same for purposes of an indexing operation.
2579    ///
2580    /// This is only used for diagnostics to see if we have things like `foo[i]` where `foo` is
2581    /// borrowed multiple times with `i`.
2582    pub fn equivalent_for_indexing(&self, other: &Expr<'_>) -> bool {
2583        match (self.kind, other.kind) {
2584            (ExprKind::Lit(lit1), ExprKind::Lit(lit2)) => lit1.node == lit2.node,
2585            (
2586                ExprKind::Path(QPath::LangItem(item1, _)),
2587                ExprKind::Path(QPath::LangItem(item2, _)),
2588            ) => item1 == item2,
2589            (
2590                ExprKind::Path(QPath::Resolved(None, path1)),
2591                ExprKind::Path(QPath::Resolved(None, path2)),
2592            ) => path1.res == path2.res,
2593            (
2594                ExprKind::Struct(
2595                    QPath::LangItem(LangItem::RangeTo, _),
2596                    [val1],
2597                    StructTailExpr::None,
2598                ),
2599                ExprKind::Struct(
2600                    QPath::LangItem(LangItem::RangeTo, _),
2601                    [val2],
2602                    StructTailExpr::None,
2603                ),
2604            )
2605            | (
2606                ExprKind::Struct(
2607                    QPath::LangItem(LangItem::RangeToInclusive, _),
2608                    [val1],
2609                    StructTailExpr::None,
2610                ),
2611                ExprKind::Struct(
2612                    QPath::LangItem(LangItem::RangeToInclusive, _),
2613                    [val2],
2614                    StructTailExpr::None,
2615                ),
2616            )
2617            | (
2618                ExprKind::Struct(
2619                    QPath::LangItem(LangItem::RangeToInclusiveCopy, _),
2620                    [val1],
2621                    StructTailExpr::None,
2622                ),
2623                ExprKind::Struct(
2624                    QPath::LangItem(LangItem::RangeToInclusiveCopy, _),
2625                    [val2],
2626                    StructTailExpr::None,
2627                ),
2628            )
2629            | (
2630                ExprKind::Struct(
2631                    QPath::LangItem(LangItem::RangeFrom, _),
2632                    [val1],
2633                    StructTailExpr::None,
2634                ),
2635                ExprKind::Struct(
2636                    QPath::LangItem(LangItem::RangeFrom, _),
2637                    [val2],
2638                    StructTailExpr::None,
2639                ),
2640            )
2641            | (
2642                ExprKind::Struct(
2643                    QPath::LangItem(LangItem::RangeFromCopy, _),
2644                    [val1],
2645                    StructTailExpr::None,
2646                ),
2647                ExprKind::Struct(
2648                    QPath::LangItem(LangItem::RangeFromCopy, _),
2649                    [val2],
2650                    StructTailExpr::None,
2651                ),
2652            ) => val1.expr.equivalent_for_indexing(val2.expr),
2653            (
2654                ExprKind::Struct(
2655                    QPath::LangItem(LangItem::Range, _),
2656                    [val1, val3],
2657                    StructTailExpr::None,
2658                ),
2659                ExprKind::Struct(
2660                    QPath::LangItem(LangItem::Range, _),
2661                    [val2, val4],
2662                    StructTailExpr::None,
2663                ),
2664            )
2665            | (
2666                ExprKind::Struct(
2667                    QPath::LangItem(LangItem::RangeCopy, _),
2668                    [val1, val3],
2669                    StructTailExpr::None,
2670                ),
2671                ExprKind::Struct(
2672                    QPath::LangItem(LangItem::RangeCopy, _),
2673                    [val2, val4],
2674                    StructTailExpr::None,
2675                ),
2676            )
2677            | (
2678                ExprKind::Struct(
2679                    QPath::LangItem(LangItem::RangeInclusiveCopy, _),
2680                    [val1, val3],
2681                    StructTailExpr::None,
2682                ),
2683                ExprKind::Struct(
2684                    QPath::LangItem(LangItem::RangeInclusiveCopy, _),
2685                    [val2, val4],
2686                    StructTailExpr::None,
2687                ),
2688            ) => {
2689                val1.expr.equivalent_for_indexing(val2.expr)
2690                    && val3.expr.equivalent_for_indexing(val4.expr)
2691            }
2692            _ => false,
2693        }
2694    }
2695
2696    pub fn method_ident(&self) -> Option<Ident> {
2697        match self.kind {
2698            ExprKind::MethodCall(receiver_method, ..) => Some(receiver_method.ident),
2699            ExprKind::Unary(_, expr) | ExprKind::AddrOf(.., expr) => expr.method_ident(),
2700            _ => None,
2701        }
2702    }
2703}
2704
2705/// Checks if the specified expression is a built-in range literal.
2706/// (See: `LoweringContext::lower_expr()`).
2707pub fn is_range_literal(expr: &Expr<'_>) -> bool {
2708    match expr.kind {
2709        // All built-in range literals but `..=` and `..` desugar to `Struct`s.
2710        ExprKind::Struct(ref qpath, _, _) => matches!(
2711            **qpath,
2712            QPath::LangItem(
2713                LangItem::Range
2714                    | LangItem::RangeTo
2715                    | LangItem::RangeFrom
2716                    | LangItem::RangeFull
2717                    | LangItem::RangeToInclusive
2718                    | LangItem::RangeCopy
2719                    | LangItem::RangeFromCopy
2720                    | LangItem::RangeInclusiveCopy
2721                    | LangItem::RangeToInclusiveCopy,
2722                ..
2723            )
2724        ),
2725
2726        // `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
2727        ExprKind::Call(ref func, _) => {
2728            matches!(func.kind, ExprKind::Path(QPath::LangItem(LangItem::RangeInclusiveNew, ..)))
2729        }
2730
2731        _ => false,
2732    }
2733}
2734
2735/// Checks if the specified expression needs parentheses for prefix
2736/// or postfix suggestions to be valid.
2737/// For example, `a + b` requires parentheses to suggest `&(a + b)`,
2738/// but just `a` does not.
2739/// Similarly, `(a + b).c()` also requires parentheses.
2740/// This should not be used for other types of suggestions.
2741pub fn expr_needs_parens(expr: &Expr<'_>) -> bool {
2742    match expr.kind {
2743        // parenthesize if needed (Issue #46756)
2744        ExprKind::Cast(_, _) | ExprKind::Binary(_, _, _) => true,
2745        // parenthesize borrows of range literals (Issue #54505)
2746        _ if is_range_literal(expr) => true,
2747        _ => false,
2748    }
2749}
2750
2751#[derive(Debug, Clone, Copy, HashStable_Generic)]
2752pub enum ExprKind<'hir> {
2753    /// Allow anonymous constants from an inline `const` block
2754    ConstBlock(ConstBlock),
2755    /// An array (e.g., `[a, b, c, d]`).
2756    Array(&'hir [Expr<'hir>]),
2757    /// A function call.
2758    ///
2759    /// The first field resolves to the function itself (usually an `ExprKind::Path`),
2760    /// and the second field is the list of arguments.
2761    /// This also represents calling the constructor of
2762    /// tuple-like ADTs such as tuple structs and enum variants.
2763    Call(&'hir Expr<'hir>, &'hir [Expr<'hir>]),
2764    /// A method call (e.g., `x.foo::<'static, Bar, Baz>(a, b, c, d)`).
2765    ///
2766    /// The `PathSegment` represents the method name and its generic arguments
2767    /// (within the angle brackets).
2768    /// The `&Expr` is the expression that evaluates
2769    /// to the object on which the method is being called on (the receiver),
2770    /// and the `&[Expr]` is the rest of the arguments.
2771    /// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
2772    /// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, x, [a, b, c, d], span)`.
2773    /// The final `Span` represents the span of the function and arguments
2774    /// (e.g. `foo::<Bar, Baz>(a, b, c, d)` in `x.foo::<Bar, Baz>(a, b, c, d)`
2775    ///
2776    /// To resolve the called method to a `DefId`, call [`type_dependent_def_id`] with
2777    /// the `hir_id` of the `MethodCall` node itself.
2778    ///
2779    /// [`type_dependent_def_id`]: ../../rustc_middle/ty/struct.TypeckResults.html#method.type_dependent_def_id
2780    MethodCall(&'hir PathSegment<'hir>, &'hir Expr<'hir>, &'hir [Expr<'hir>], Span),
2781    /// An use expression (e.g., `var.use`).
2782    Use(&'hir Expr<'hir>, Span),
2783    /// A tuple (e.g., `(a, b, c, d)`).
2784    Tup(&'hir [Expr<'hir>]),
2785    /// A binary operation (e.g., `a + b`, `a * b`).
2786    Binary(BinOp, &'hir Expr<'hir>, &'hir Expr<'hir>),
2787    /// A unary operation (e.g., `!x`, `*x`).
2788    Unary(UnOp, &'hir Expr<'hir>),
2789    /// A literal (e.g., `1`, `"foo"`).
2790    Lit(Lit),
2791    /// A cast (e.g., `foo as f64`).
2792    Cast(&'hir Expr<'hir>, &'hir Ty<'hir>),
2793    /// A type ascription (e.g., `x: Foo`). See RFC 3307.
2794    Type(&'hir Expr<'hir>, &'hir Ty<'hir>),
2795    /// Wraps the expression in a terminating scope.
2796    /// This makes it semantically equivalent to `{ let _t = expr; _t }`.
2797    ///
2798    /// This construct only exists to tweak the drop order in AST lowering.
2799    /// An example of that is the desugaring of `for` loops.
2800    DropTemps(&'hir Expr<'hir>),
2801    /// A `let $pat = $expr` expression.
2802    ///
2803    /// These are not [`LetStmt`] and only occur as expressions.
2804    /// The `let Some(x) = foo()` in `if let Some(x) = foo()` is an example of `Let(..)`.
2805    Let(&'hir LetExpr<'hir>),
2806    /// An `if` block, with an optional else block.
2807    ///
2808    /// I.e., `if <expr> { <expr> } else { <expr> }`.
2809    ///
2810    /// The "then" expr is always `ExprKind::Block`. If present, the "else" expr is always
2811    /// `ExprKind::Block` (for `else`) or `ExprKind::If` (for `else if`).
2812    /// Note that using an `Expr` instead of a `Block` for the "then" part is intentional,
2813    /// as it simplifies the type coercion machinery.
2814    If(&'hir Expr<'hir>, &'hir Expr<'hir>, Option<&'hir Expr<'hir>>),
2815    /// A conditionless loop (can be exited with `break`, `continue`, or `return`).
2816    ///
2817    /// I.e., `'label: loop { <block> }`.
2818    ///
2819    /// The `Span` is the loop header (`for x in y`/`while let pat = expr`).
2820    Loop(&'hir Block<'hir>, Option<Label>, LoopSource, Span),
2821    /// A `match` block, with a source that indicates whether or not it is
2822    /// the result of a desugaring, and if so, which kind.
2823    Match(&'hir Expr<'hir>, &'hir [Arm<'hir>], MatchSource),
2824    /// A closure (e.g., `move |a, b, c| {a + b + c}`).
2825    ///
2826    /// The `Span` is the argument block `|...|`.
2827    ///
2828    /// This may also be a coroutine literal or an `async block` as indicated by the
2829    /// `Option<Movability>`.
2830    Closure(&'hir Closure<'hir>),
2831    /// A block (e.g., `'label: { ... }`).
2832    Block(&'hir Block<'hir>, Option<Label>),
2833
2834    /// An assignment (e.g., `a = foo()`).
2835    Assign(&'hir Expr<'hir>, &'hir Expr<'hir>, Span),
2836    /// An assignment with an operator.
2837    ///
2838    /// E.g., `a += 1`.
2839    AssignOp(AssignOp, &'hir Expr<'hir>, &'hir Expr<'hir>),
2840    /// Access of a named (e.g., `obj.foo`) or unnamed (e.g., `obj.0`) struct or tuple field.
2841    Field(&'hir Expr<'hir>, Ident),
2842    /// An indexing operation (`foo[2]`).
2843    /// Similar to [`ExprKind::MethodCall`], the final `Span` represents the span of the brackets
2844    /// and index.
2845    Index(&'hir Expr<'hir>, &'hir Expr<'hir>, Span),
2846
2847    /// Path to a definition, possibly containing lifetime or type parameters.
2848    Path(QPath<'hir>),
2849
2850    /// A referencing operation (i.e., `&a` or `&mut a`).
2851    AddrOf(BorrowKind, Mutability, &'hir Expr<'hir>),
2852    /// A `break`, with an optional label to break.
2853    Break(Destination, Option<&'hir Expr<'hir>>),
2854    /// A `continue`, with an optional label.
2855    Continue(Destination),
2856    /// A `return`, with an optional value to be returned.
2857    Ret(Option<&'hir Expr<'hir>>),
2858    /// A `become`, with the value to be returned.
2859    Become(&'hir Expr<'hir>),
2860
2861    /// Inline assembly (from `asm!`), with its outputs and inputs.
2862    InlineAsm(&'hir InlineAsm<'hir>),
2863
2864    /// Field offset (`offset_of!`)
2865    OffsetOf(&'hir Ty<'hir>, &'hir [Ident]),
2866
2867    /// A struct or struct-like variant literal expression.
2868    ///
2869    /// E.g., `Foo {x: 1, y: 2}`, or `Foo {x: 1, .. base}`,
2870    /// where `base` is the `Option<Expr>`.
2871    Struct(&'hir QPath<'hir>, &'hir [ExprField<'hir>], StructTailExpr<'hir>),
2872
2873    /// An array literal constructed from one repeated element.
2874    ///
2875    /// E.g., `[1; 5]`. The first expression is the element
2876    /// to be repeated; the second is the number of times to repeat it.
2877    Repeat(&'hir Expr<'hir>, &'hir ConstArg<'hir>),
2878
2879    /// A suspension point for coroutines (i.e., `yield <expr>`).
2880    Yield(&'hir Expr<'hir>, YieldSource),
2881
2882    /// Operators which can be used to interconvert `unsafe` binder types.
2883    /// e.g. `unsafe<'a> &'a i32` <=> `&i32`.
2884    UnsafeBinderCast(UnsafeBinderCastKind, &'hir Expr<'hir>, Option<&'hir Ty<'hir>>),
2885
2886    /// A placeholder for an expression that wasn't syntactically well formed in some way.
2887    Err(rustc_span::ErrorGuaranteed),
2888}
2889
2890#[derive(Debug, Clone, Copy, HashStable_Generic)]
2891pub enum StructTailExpr<'hir> {
2892    /// A struct expression where all the fields are explicitly enumerated: `Foo { a, b }`.
2893    None,
2894    /// A struct expression with a "base", an expression of the same type as the outer struct that
2895    /// will be used to populate any fields not explicitly mentioned: `Foo { ..base }`
2896    Base(&'hir Expr<'hir>),
2897    /// A struct expression with a `..` tail but no "base" expression. The values from the struct
2898    /// fields' default values will be used to populate any fields not explicitly mentioned:
2899    /// `Foo { .. }`.
2900    DefaultFields(Span),
2901}
2902
2903/// Represents an optionally `Self`-qualified value/type path or associated extension.
2904///
2905/// To resolve the path to a `DefId`, call [`qpath_res`].
2906///
2907/// [`qpath_res`]: ../../rustc_middle/ty/struct.TypeckResults.html#method.qpath_res
2908#[derive(Debug, Clone, Copy, HashStable_Generic)]
2909pub enum QPath<'hir> {
2910    /// Path to a definition, optionally "fully-qualified" with a `Self`
2911    /// type, if the path points to an associated item in a trait.
2912    ///
2913    /// E.g., an unqualified path like `Clone::clone` has `None` for `Self`,
2914    /// while `<Vec<T> as Clone>::clone` has `Some(Vec<T>)` for `Self`,
2915    /// even though they both have the same two-segment `Clone::clone` `Path`.
2916    Resolved(Option<&'hir Ty<'hir>>, &'hir Path<'hir>),
2917
2918    /// Type-related paths (e.g., `<T>::default` or `<T>::Output`).
2919    /// Will be resolved by type-checking to an associated item.
2920    ///
2921    /// UFCS source paths can desugar into this, with `Vec::new` turning into
2922    /// `<Vec>::new`, and `T::X::Y::method` into `<<<T>::X>::Y>::method`,
2923    /// the `X` and `Y` nodes each being a `TyKind::Path(QPath::TypeRelative(..))`.
2924    TypeRelative(&'hir Ty<'hir>, &'hir PathSegment<'hir>),
2925
2926    /// Reference to a `#[lang = "foo"]` item.
2927    LangItem(LangItem, Span),
2928}
2929
2930impl<'hir> QPath<'hir> {
2931    /// Returns the span of this `QPath`.
2932    pub fn span(&self) -> Span {
2933        match *self {
2934            QPath::Resolved(_, path) => path.span,
2935            QPath::TypeRelative(qself, ps) => qself.span.to(ps.ident.span),
2936            QPath::LangItem(_, span) => span,
2937        }
2938    }
2939
2940    /// Returns the span of the qself of this `QPath`. For example, `()` in
2941    /// `<() as Trait>::method`.
2942    pub fn qself_span(&self) -> Span {
2943        match *self {
2944            QPath::Resolved(_, path) => path.span,
2945            QPath::TypeRelative(qself, _) => qself.span,
2946            QPath::LangItem(_, span) => span,
2947        }
2948    }
2949}
2950
2951/// Hints at the original code for a let statement.
2952#[derive(Copy, Clone, Debug, HashStable_Generic)]
2953pub enum LocalSource {
2954    /// A `match _ { .. }`.
2955    Normal,
2956    /// When lowering async functions, we create locals within the `async move` so that
2957    /// all parameters are dropped after the future is polled.
2958    ///
2959    /// ```ignore (pseudo-Rust)
2960    /// async fn foo(<pattern> @ x: Type) {
2961    ///     async move {
2962    ///         let <pattern> = x;
2963    ///     }
2964    /// }
2965    /// ```
2966    AsyncFn,
2967    /// A desugared `<expr>.await`.
2968    AwaitDesugar,
2969    /// A desugared `expr = expr`, where the LHS is a tuple, struct, array or underscore expression.
2970    /// The span is that of the `=` sign.
2971    AssignDesugar(Span),
2972    /// A contract `#[ensures(..)]` attribute injects a let binding for the check that runs at point of return.
2973    Contract,
2974}
2975
2976/// Hints at the original code for a `match _ { .. }`.
2977#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic, Encodable, Decodable)]
2978pub enum MatchSource {
2979    /// A `match _ { .. }`.
2980    Normal,
2981    /// A `expr.match { .. }`.
2982    Postfix,
2983    /// A desugared `for _ in _ { .. }` loop.
2984    ForLoopDesugar,
2985    /// A desugared `?` operator.
2986    TryDesugar(HirId),
2987    /// A desugared `<expr>.await`.
2988    AwaitDesugar,
2989    /// A desugared `format_args!()`.
2990    FormatArgs,
2991}
2992
2993impl MatchSource {
2994    #[inline]
2995    pub const fn name(self) -> &'static str {
2996        use MatchSource::*;
2997        match self {
2998            Normal => "match",
2999            Postfix => ".match",
3000            ForLoopDesugar => "for",
3001            TryDesugar(_) => "?",
3002            AwaitDesugar => ".await",
3003            FormatArgs => "format_args!()",
3004        }
3005    }
3006}
3007
3008/// The loop type that yielded an `ExprKind::Loop`.
3009#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
3010pub enum LoopSource {
3011    /// A `loop { .. }` loop.
3012    Loop,
3013    /// A `while _ { .. }` loop.
3014    While,
3015    /// A `for _ in _ { .. }` loop.
3016    ForLoop,
3017}
3018
3019impl LoopSource {
3020    pub fn name(self) -> &'static str {
3021        match self {
3022            LoopSource::Loop => "loop",
3023            LoopSource::While => "while",
3024            LoopSource::ForLoop => "for",
3025        }
3026    }
3027}
3028
3029#[derive(Copy, Clone, Debug, PartialEq, HashStable_Generic)]
3030pub enum LoopIdError {
3031    OutsideLoopScope,
3032    UnlabeledCfInWhileCondition,
3033    UnresolvedLabel,
3034}
3035
3036impl fmt::Display for LoopIdError {
3037    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3038        f.write_str(match self {
3039            LoopIdError::OutsideLoopScope => "not inside loop scope",
3040            LoopIdError::UnlabeledCfInWhileCondition => {
3041                "unlabeled control flow (break or continue) in while condition"
3042            }
3043            LoopIdError::UnresolvedLabel => "label not found",
3044        })
3045    }
3046}
3047
3048#[derive(Copy, Clone, Debug, PartialEq, HashStable_Generic)]
3049pub struct Destination {
3050    /// This is `Some(_)` iff there is an explicit user-specified 'label
3051    pub label: Option<Label>,
3052
3053    /// These errors are caught and then reported during the diagnostics pass in
3054    /// `librustc_passes/loops.rs`
3055    pub target_id: Result<HirId, LoopIdError>,
3056}
3057
3058/// The yield kind that caused an `ExprKind::Yield`.
3059#[derive(Copy, Clone, Debug, HashStable_Generic)]
3060pub enum YieldSource {
3061    /// An `<expr>.await`.
3062    Await { expr: Option<HirId> },
3063    /// A plain `yield`.
3064    Yield,
3065}
3066
3067impl fmt::Display for YieldSource {
3068    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3069        f.write_str(match self {
3070            YieldSource::Await { .. } => "`await`",
3071            YieldSource::Yield => "`yield`",
3072        })
3073    }
3074}
3075
3076// N.B., if you change this, you'll probably want to change the corresponding
3077// type structure in middle/ty.rs as well.
3078#[derive(Debug, Clone, Copy, HashStable_Generic)]
3079pub struct MutTy<'hir> {
3080    pub ty: &'hir Ty<'hir>,
3081    pub mutbl: Mutability,
3082}
3083
3084/// Represents a function's signature in a trait declaration,
3085/// trait implementation, or a free function.
3086#[derive(Debug, Clone, Copy, HashStable_Generic)]
3087pub struct FnSig<'hir> {
3088    pub header: FnHeader,
3089    pub decl: &'hir FnDecl<'hir>,
3090    pub span: Span,
3091}
3092
3093// The bodies for items are stored "out of line", in a separate
3094// hashmap in the `Crate`. Here we just record the hir-id of the item
3095// so it can fetched later.
3096#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
3097pub struct TraitItemId {
3098    pub owner_id: OwnerId,
3099}
3100
3101impl TraitItemId {
3102    #[inline]
3103    pub fn hir_id(&self) -> HirId {
3104        // Items are always HIR owners.
3105        HirId::make_owner(self.owner_id.def_id)
3106    }
3107}
3108
3109/// Represents an item declaration within a trait declaration,
3110/// possibly including a default implementation. A trait item is
3111/// either required (meaning it doesn't have an implementation, just a
3112/// signature) or provided (meaning it has a default implementation).
3113#[derive(Debug, Clone, Copy, HashStable_Generic)]
3114pub struct TraitItem<'hir> {
3115    pub ident: Ident,
3116    pub owner_id: OwnerId,
3117    pub generics: &'hir Generics<'hir>,
3118    pub kind: TraitItemKind<'hir>,
3119    pub span: Span,
3120    pub defaultness: Defaultness,
3121    pub has_delayed_lints: bool,
3122}
3123
3124macro_rules! expect_methods_self_kind {
3125    ( $( $name:ident, $ret_ty:ty, $pat:pat, $ret_val:expr; )* ) => {
3126        $(
3127            #[track_caller]
3128            pub fn $name(&self) -> $ret_ty {
3129                let $pat = &self.kind else { expect_failed(stringify!($ident), self) };
3130                $ret_val
3131            }
3132        )*
3133    }
3134}
3135
3136macro_rules! expect_methods_self {
3137    ( $( $name:ident, $ret_ty:ty, $pat:pat, $ret_val:expr; )* ) => {
3138        $(
3139            #[track_caller]
3140            pub fn $name(&self) -> $ret_ty {
3141                let $pat = self else { expect_failed(stringify!($ident), self) };
3142                $ret_val
3143            }
3144        )*
3145    }
3146}
3147
3148#[track_caller]
3149fn expect_failed<T: fmt::Debug>(ident: &'static str, found: T) -> ! {
3150    panic!("{ident}: found {found:?}")
3151}
3152
3153impl<'hir> TraitItem<'hir> {
3154    #[inline]
3155    pub fn hir_id(&self) -> HirId {
3156        // Items are always HIR owners.
3157        HirId::make_owner(self.owner_id.def_id)
3158    }
3159
3160    pub fn trait_item_id(&self) -> TraitItemId {
3161        TraitItemId { owner_id: self.owner_id }
3162    }
3163
3164    expect_methods_self_kind! {
3165        expect_const, (&'hir Ty<'hir>, Option<BodyId>),
3166            TraitItemKind::Const(ty, body), (ty, *body);
3167
3168        expect_fn, (&FnSig<'hir>, &TraitFn<'hir>),
3169            TraitItemKind::Fn(ty, trfn), (ty, trfn);
3170
3171        expect_type, (GenericBounds<'hir>, Option<&'hir Ty<'hir>>),
3172            TraitItemKind::Type(bounds, ty), (bounds, *ty);
3173    }
3174}
3175
3176/// Represents a trait method's body (or just argument names).
3177#[derive(Debug, Clone, Copy, HashStable_Generic)]
3178pub enum TraitFn<'hir> {
3179    /// No default body in the trait, just a signature.
3180    Required(&'hir [Option<Ident>]),
3181
3182    /// Both signature and body are provided in the trait.
3183    Provided(BodyId),
3184}
3185
3186/// Represents a trait method or associated constant or type
3187#[derive(Debug, Clone, Copy, HashStable_Generic)]
3188pub enum TraitItemKind<'hir> {
3189    /// An associated constant with an optional value (otherwise `impl`s must contain a value).
3190    Const(&'hir Ty<'hir>, Option<BodyId>),
3191    /// An associated function with an optional body.
3192    Fn(FnSig<'hir>, TraitFn<'hir>),
3193    /// An associated type with (possibly empty) bounds and optional concrete
3194    /// type.
3195    Type(GenericBounds<'hir>, Option<&'hir Ty<'hir>>),
3196}
3197
3198// The bodies for items are stored "out of line", in a separate
3199// hashmap in the `Crate`. Here we just record the hir-id of the item
3200// so it can fetched later.
3201#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
3202pub struct ImplItemId {
3203    pub owner_id: OwnerId,
3204}
3205
3206impl ImplItemId {
3207    #[inline]
3208    pub fn hir_id(&self) -> HirId {
3209        // Items are always HIR owners.
3210        HirId::make_owner(self.owner_id.def_id)
3211    }
3212}
3213
3214/// Represents an associated item within an impl block.
3215///
3216/// Refer to [`Impl`] for an impl block declaration.
3217#[derive(Debug, Clone, Copy, HashStable_Generic)]
3218pub struct ImplItem<'hir> {
3219    pub ident: Ident,
3220    pub owner_id: OwnerId,
3221    pub generics: &'hir Generics<'hir>,
3222    pub kind: ImplItemKind<'hir>,
3223    pub impl_kind: ImplItemImplKind,
3224    pub span: Span,
3225    pub has_delayed_lints: bool,
3226}
3227
3228#[derive(Debug, Clone, Copy, HashStable_Generic)]
3229pub enum ImplItemImplKind {
3230    Inherent {
3231        vis_span: Span,
3232    },
3233    Trait {
3234        defaultness: Defaultness,
3235        /// Item in the trait that this item implements
3236        trait_item_def_id: Result<DefId, ErrorGuaranteed>,
3237    },
3238}
3239
3240impl<'hir> ImplItem<'hir> {
3241    #[inline]
3242    pub fn hir_id(&self) -> HirId {
3243        // Items are always HIR owners.
3244        HirId::make_owner(self.owner_id.def_id)
3245    }
3246
3247    pub fn impl_item_id(&self) -> ImplItemId {
3248        ImplItemId { owner_id: self.owner_id }
3249    }
3250
3251    pub fn vis_span(&self) -> Option<Span> {
3252        match self.impl_kind {
3253            ImplItemImplKind::Trait { .. } => None,
3254            ImplItemImplKind::Inherent { vis_span, .. } => Some(vis_span),
3255        }
3256    }
3257
3258    expect_methods_self_kind! {
3259        expect_const, (&'hir Ty<'hir>, BodyId), ImplItemKind::Const(ty, body), (ty, *body);
3260        expect_fn,    (&FnSig<'hir>, BodyId),   ImplItemKind::Fn(ty, body),    (ty, *body);
3261        expect_type,  &'hir Ty<'hir>,           ImplItemKind::Type(ty),        ty;
3262    }
3263}
3264
3265/// Represents various kinds of content within an `impl`.
3266#[derive(Debug, Clone, Copy, HashStable_Generic)]
3267pub enum ImplItemKind<'hir> {
3268    /// An associated constant of the given type, set to the constant result
3269    /// of the expression.
3270    Const(&'hir Ty<'hir>, BodyId),
3271    /// An associated function implementation with the given signature and body.
3272    Fn(FnSig<'hir>, BodyId),
3273    /// An associated type.
3274    Type(&'hir Ty<'hir>),
3275}
3276
3277/// A constraint on an associated item.
3278///
3279/// ### Examples
3280///
3281/// * the `A = Ty` and `B = Ty` in `Trait<A = Ty, B = Ty>`
3282/// * the `G<Ty> = Ty` in `Trait<G<Ty> = Ty>`
3283/// * the `A: Bound` in `Trait<A: Bound>`
3284/// * the `RetTy` in `Trait(ArgTy, ArgTy) -> RetTy`
3285/// * the `C = { Ct }` in `Trait<C = { Ct }>` (feature `associated_const_equality`)
3286/// * the `f(..): Bound` in `Trait<f(..): Bound>` (feature `return_type_notation`)
3287#[derive(Debug, Clone, Copy, HashStable_Generic)]
3288pub struct AssocItemConstraint<'hir> {
3289    #[stable_hasher(ignore)]
3290    pub hir_id: HirId,
3291    pub ident: Ident,
3292    pub gen_args: &'hir GenericArgs<'hir>,
3293    pub kind: AssocItemConstraintKind<'hir>,
3294    pub span: Span,
3295}
3296
3297impl<'hir> AssocItemConstraint<'hir> {
3298    /// Obtain the type on the RHS of an assoc ty equality constraint if applicable.
3299    pub fn ty(self) -> Option<&'hir Ty<'hir>> {
3300        match self.kind {
3301            AssocItemConstraintKind::Equality { term: Term::Ty(ty) } => Some(ty),
3302            _ => None,
3303        }
3304    }
3305
3306    /// Obtain the const on the RHS of an assoc const equality constraint if applicable.
3307    pub fn ct(self) -> Option<&'hir ConstArg<'hir>> {
3308        match self.kind {
3309            AssocItemConstraintKind::Equality { term: Term::Const(ct) } => Some(ct),
3310            _ => None,
3311        }
3312    }
3313}
3314
3315#[derive(Debug, Clone, Copy, HashStable_Generic)]
3316pub enum Term<'hir> {
3317    Ty(&'hir Ty<'hir>),
3318    Const(&'hir ConstArg<'hir>),
3319}
3320
3321impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
3322    fn from(ty: &'hir Ty<'hir>) -> Self {
3323        Term::Ty(ty)
3324    }
3325}
3326
3327impl<'hir> From<&'hir ConstArg<'hir>> for Term<'hir> {
3328    fn from(c: &'hir ConstArg<'hir>) -> Self {
3329        Term::Const(c)
3330    }
3331}
3332
3333/// The kind of [associated item constraint][AssocItemConstraint].
3334#[derive(Debug, Clone, Copy, HashStable_Generic)]
3335pub enum AssocItemConstraintKind<'hir> {
3336    /// An equality constraint for an associated item (e.g., `AssocTy = Ty` in `Trait<AssocTy = Ty>`).
3337    ///
3338    /// Also known as an *associated item binding* (we *bind* an associated item to a term).
3339    ///
3340    /// Furthermore, associated type equality constraints can also be referred to as *associated type
3341    /// bindings*. Similarly with associated const equality constraints and *associated const bindings*.
3342    Equality { term: Term<'hir> },
3343    /// A bound on an associated type (e.g., `AssocTy: Bound` in `Trait<AssocTy: Bound>`).
3344    Bound { bounds: &'hir [GenericBound<'hir>] },
3345}
3346
3347impl<'hir> AssocItemConstraintKind<'hir> {
3348    pub fn descr(&self) -> &'static str {
3349        match self {
3350            AssocItemConstraintKind::Equality { .. } => "binding",
3351            AssocItemConstraintKind::Bound { .. } => "constraint",
3352        }
3353    }
3354}
3355
3356/// An uninhabited enum used to make `Infer` variants on [`Ty`] and [`ConstArg`] be
3357/// unreachable. Zero-Variant enums are guaranteed to have the same layout as the never
3358/// type.
3359#[derive(Debug, Clone, Copy, HashStable_Generic)]
3360pub enum AmbigArg {}
3361
3362/// Represents a type in the `HIR`.
3363///
3364/// For an explanation of the `Unambig` generic parameter see the dev-guide:
3365/// <https://rustc-dev-guide.rust-lang.org/hir/ambig-unambig-ty-and-consts.html>
3366#[derive(Debug, Clone, Copy, HashStable_Generic)]
3367#[repr(C)]
3368pub struct Ty<'hir, Unambig = ()> {
3369    #[stable_hasher(ignore)]
3370    pub hir_id: HirId,
3371    pub span: Span,
3372    pub kind: TyKind<'hir, Unambig>,
3373}
3374
3375impl<'hir> Ty<'hir, AmbigArg> {
3376    /// Converts a `Ty` in an ambiguous position to one in an unambiguous position.
3377    ///
3378    /// Functions accepting an unambiguous types may expect the [`TyKind::Infer`] variant
3379    /// to be used. Care should be taken to separately handle infer types when calling this
3380    /// function as it cannot be handled by downstream code making use of the returned ty.
3381    ///
3382    /// In practice this may mean overriding the [`Visitor::visit_infer`][visit_infer] method on hir visitors, or
3383    /// specifically matching on [`GenericArg::Infer`] when handling generic arguments.
3384    ///
3385    /// [visit_infer]: [rustc_hir::intravisit::Visitor::visit_infer]
3386    pub fn as_unambig_ty(&self) -> &Ty<'hir> {
3387        // SAFETY: `Ty` is `repr(C)` and `TyKind` is marked `repr(u8)` so that the layout is
3388        // the same across different ZST type arguments.
3389        let ptr = self as *const Ty<'hir, AmbigArg> as *const Ty<'hir, ()>;
3390        unsafe { &*ptr }
3391    }
3392}
3393
3394impl<'hir> Ty<'hir> {
3395    /// Converts a `Ty` in an unambiguous position to one in an ambiguous position. This is
3396    /// fallible as the [`TyKind::Infer`] variant is not present in ambiguous positions.
3397    ///
3398    /// Functions accepting ambiguous types will not handle the [`TyKind::Infer`] variant, if
3399    /// infer types are relevant to you then care should be taken to handle them separately.
3400    pub fn try_as_ambig_ty(&self) -> Option<&Ty<'hir, AmbigArg>> {
3401        if let TyKind::Infer(()) = self.kind {
3402            return None;
3403        }
3404
3405        // SAFETY: `Ty` is `repr(C)` and `TyKind` is marked `repr(u8)` so that the layout is
3406        // the same across different ZST type arguments. We also asserted that the `self` is
3407        // not a `TyKind::Infer` so there is no risk of transmuting a `()` to `AmbigArg`.
3408        let ptr = self as *const Ty<'hir> as *const Ty<'hir, AmbigArg>;
3409        Some(unsafe { &*ptr })
3410    }
3411}
3412
3413impl<'hir> Ty<'hir, AmbigArg> {
3414    pub fn peel_refs(&self) -> &Ty<'hir> {
3415        let mut final_ty = self.as_unambig_ty();
3416        while let TyKind::Ref(_, MutTy { ty, .. }) = &final_ty.kind {
3417            final_ty = ty;
3418        }
3419        final_ty
3420    }
3421}
3422
3423impl<'hir> Ty<'hir> {
3424    pub fn peel_refs(&self) -> &Self {
3425        let mut final_ty = self;
3426        while let TyKind::Ref(_, MutTy { ty, .. }) = &final_ty.kind {
3427            final_ty = ty;
3428        }
3429        final_ty
3430    }
3431
3432    /// Returns `true` if `param_def_id` matches the `bounded_ty` of this predicate.
3433    pub fn as_generic_param(&self) -> Option<(DefId, Ident)> {
3434        let TyKind::Path(QPath::Resolved(None, path)) = self.kind else {
3435            return None;
3436        };
3437        let [segment] = &path.segments else {
3438            return None;
3439        };
3440        match path.res {
3441            Res::Def(DefKind::TyParam, def_id) | Res::SelfTyParam { trait_: def_id } => {
3442                Some((def_id, segment.ident))
3443            }
3444            _ => None,
3445        }
3446    }
3447
3448    pub fn find_self_aliases(&self) -> Vec<Span> {
3449        use crate::intravisit::Visitor;
3450        struct MyVisitor(Vec<Span>);
3451        impl<'v> Visitor<'v> for MyVisitor {
3452            fn visit_ty(&mut self, t: &'v Ty<'v, AmbigArg>) {
3453                if matches!(
3454                    &t.kind,
3455                    TyKind::Path(QPath::Resolved(
3456                        _,
3457                        Path { res: crate::def::Res::SelfTyAlias { .. }, .. },
3458                    ))
3459                ) {
3460                    self.0.push(t.span);
3461                    return;
3462                }
3463                crate::intravisit::walk_ty(self, t);
3464            }
3465        }
3466
3467        let mut my_visitor = MyVisitor(vec![]);
3468        my_visitor.visit_ty_unambig(self);
3469        my_visitor.0
3470    }
3471
3472    /// Whether `ty` is a type with `_` placeholders that can be inferred. Used in diagnostics only to
3473    /// use inference to provide suggestions for the appropriate type if possible.
3474    pub fn is_suggestable_infer_ty(&self) -> bool {
3475        fn are_suggestable_generic_args(generic_args: &[GenericArg<'_>]) -> bool {
3476            generic_args.iter().any(|arg| match arg {
3477                GenericArg::Type(ty) => ty.as_unambig_ty().is_suggestable_infer_ty(),
3478                GenericArg::Infer(_) => true,
3479                _ => false,
3480            })
3481        }
3482        debug!(?self);
3483        match &self.kind {
3484            TyKind::Infer(()) => true,
3485            TyKind::Slice(ty) => ty.is_suggestable_infer_ty(),
3486            TyKind::Array(ty, length) => {
3487                ty.is_suggestable_infer_ty() || matches!(length.kind, ConstArgKind::Infer(..))
3488            }
3489            TyKind::Tup(tys) => tys.iter().any(Self::is_suggestable_infer_ty),
3490            TyKind::Ptr(mut_ty) | TyKind::Ref(_, mut_ty) => mut_ty.ty.is_suggestable_infer_ty(),
3491            TyKind::Path(QPath::TypeRelative(ty, segment)) => {
3492                ty.is_suggestable_infer_ty() || are_suggestable_generic_args(segment.args().args)
3493            }
3494            TyKind::Path(QPath::Resolved(ty_opt, Path { segments, .. })) => {
3495                ty_opt.is_some_and(Self::is_suggestable_infer_ty)
3496                    || segments
3497                        .iter()
3498                        .any(|segment| are_suggestable_generic_args(segment.args().args))
3499            }
3500            _ => false,
3501        }
3502    }
3503}
3504
3505/// Not represented directly in the AST; referred to by name through a `ty_path`.
3506#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, Debug, HashStable_Generic)]
3507pub enum PrimTy {
3508    Int(IntTy),
3509    Uint(UintTy),
3510    Float(FloatTy),
3511    Str,
3512    Bool,
3513    Char,
3514}
3515
3516impl PrimTy {
3517    /// All of the primitive types
3518    pub const ALL: [Self; 19] = [
3519        // any changes here should also be reflected in `PrimTy::from_name`
3520        Self::Int(IntTy::I8),
3521        Self::Int(IntTy::I16),
3522        Self::Int(IntTy::I32),
3523        Self::Int(IntTy::I64),
3524        Self::Int(IntTy::I128),
3525        Self::Int(IntTy::Isize),
3526        Self::Uint(UintTy::U8),
3527        Self::Uint(UintTy::U16),
3528        Self::Uint(UintTy::U32),
3529        Self::Uint(UintTy::U64),
3530        Self::Uint(UintTy::U128),
3531        Self::Uint(UintTy::Usize),
3532        Self::Float(FloatTy::F16),
3533        Self::Float(FloatTy::F32),
3534        Self::Float(FloatTy::F64),
3535        Self::Float(FloatTy::F128),
3536        Self::Bool,
3537        Self::Char,
3538        Self::Str,
3539    ];
3540
3541    /// Like [`PrimTy::name`], but returns a &str instead of a symbol.
3542    ///
3543    /// Used by clippy.
3544    pub fn name_str(self) -> &'static str {
3545        match self {
3546            PrimTy::Int(i) => i.name_str(),
3547            PrimTy::Uint(u) => u.name_str(),
3548            PrimTy::Float(f) => f.name_str(),
3549            PrimTy::Str => "str",
3550            PrimTy::Bool => "bool",
3551            PrimTy::Char => "char",
3552        }
3553    }
3554
3555    pub fn name(self) -> Symbol {
3556        match self {
3557            PrimTy::Int(i) => i.name(),
3558            PrimTy::Uint(u) => u.name(),
3559            PrimTy::Float(f) => f.name(),
3560            PrimTy::Str => sym::str,
3561            PrimTy::Bool => sym::bool,
3562            PrimTy::Char => sym::char,
3563        }
3564    }
3565
3566    /// Returns the matching `PrimTy` for a `Symbol` such as "str" or "i32".
3567    /// Returns `None` if no matching type is found.
3568    pub fn from_name(name: Symbol) -> Option<Self> {
3569        let ty = match name {
3570            // any changes here should also be reflected in `PrimTy::ALL`
3571            sym::i8 => Self::Int(IntTy::I8),
3572            sym::i16 => Self::Int(IntTy::I16),
3573            sym::i32 => Self::Int(IntTy::I32),
3574            sym::i64 => Self::Int(IntTy::I64),
3575            sym::i128 => Self::Int(IntTy::I128),
3576            sym::isize => Self::Int(IntTy::Isize),
3577            sym::u8 => Self::Uint(UintTy::U8),
3578            sym::u16 => Self::Uint(UintTy::U16),
3579            sym::u32 => Self::Uint(UintTy::U32),
3580            sym::u64 => Self::Uint(UintTy::U64),
3581            sym::u128 => Self::Uint(UintTy::U128),
3582            sym::usize => Self::Uint(UintTy::Usize),
3583            sym::f16 => Self::Float(FloatTy::F16),
3584            sym::f32 => Self::Float(FloatTy::F32),
3585            sym::f64 => Self::Float(FloatTy::F64),
3586            sym::f128 => Self::Float(FloatTy::F128),
3587            sym::bool => Self::Bool,
3588            sym::char => Self::Char,
3589            sym::str => Self::Str,
3590            _ => return None,
3591        };
3592        Some(ty)
3593    }
3594}
3595
3596#[derive(Debug, Clone, Copy, HashStable_Generic)]
3597pub struct FnPtrTy<'hir> {
3598    pub safety: Safety,
3599    pub abi: ExternAbi,
3600    pub generic_params: &'hir [GenericParam<'hir>],
3601    pub decl: &'hir FnDecl<'hir>,
3602    // `Option` because bare fn parameter identifiers are optional. We also end up
3603    // with `None` in some error cases, e.g. invalid parameter patterns.
3604    pub param_idents: &'hir [Option<Ident>],
3605}
3606
3607#[derive(Debug, Clone, Copy, HashStable_Generic)]
3608pub struct UnsafeBinderTy<'hir> {
3609    pub generic_params: &'hir [GenericParam<'hir>],
3610    pub inner_ty: &'hir Ty<'hir>,
3611}
3612
3613#[derive(Debug, Clone, Copy, HashStable_Generic)]
3614pub struct OpaqueTy<'hir> {
3615    #[stable_hasher(ignore)]
3616    pub hir_id: HirId,
3617    pub def_id: LocalDefId,
3618    pub bounds: GenericBounds<'hir>,
3619    pub origin: OpaqueTyOrigin<LocalDefId>,
3620    pub span: Span,
3621}
3622
3623#[derive(Debug, Clone, Copy, HashStable_Generic, Encodable, Decodable)]
3624pub enum PreciseCapturingArgKind<T, U> {
3625    Lifetime(T),
3626    /// Non-lifetime argument (type or const)
3627    Param(U),
3628}
3629
3630pub type PreciseCapturingArg<'hir> =
3631    PreciseCapturingArgKind<&'hir Lifetime, PreciseCapturingNonLifetimeArg>;
3632
3633impl PreciseCapturingArg<'_> {
3634    pub fn hir_id(self) -> HirId {
3635        match self {
3636            PreciseCapturingArg::Lifetime(lt) => lt.hir_id,
3637            PreciseCapturingArg::Param(param) => param.hir_id,
3638        }
3639    }
3640
3641    pub fn name(self) -> Symbol {
3642        match self {
3643            PreciseCapturingArg::Lifetime(lt) => lt.ident.name,
3644            PreciseCapturingArg::Param(param) => param.ident.name,
3645        }
3646    }
3647}
3648
3649/// We need to have a [`Node`] for the [`HirId`] that we attach the type/const param
3650/// resolution to. Lifetimes don't have this problem, and for them, it's actually
3651/// kind of detrimental to use a custom node type versus just using [`Lifetime`],
3652/// since resolve_bound_vars operates on `Lifetime`s.
3653#[derive(Debug, Clone, Copy, HashStable_Generic)]
3654pub struct PreciseCapturingNonLifetimeArg {
3655    #[stable_hasher(ignore)]
3656    pub hir_id: HirId,
3657    pub ident: Ident,
3658    pub res: Res,
3659}
3660
3661#[derive(Copy, Clone, PartialEq, Eq, Debug)]
3662#[derive(HashStable_Generic, Encodable, Decodable)]
3663pub enum RpitContext {
3664    Trait,
3665    TraitImpl,
3666}
3667
3668/// From whence the opaque type came.
3669#[derive(Copy, Clone, PartialEq, Eq, Debug)]
3670#[derive(HashStable_Generic, Encodable, Decodable)]
3671pub enum OpaqueTyOrigin<D> {
3672    /// `-> impl Trait`
3673    FnReturn {
3674        /// The defining function.
3675        parent: D,
3676        // Whether this is an RPITIT (return position impl trait in trait)
3677        in_trait_or_impl: Option<RpitContext>,
3678    },
3679    /// `async fn`
3680    AsyncFn {
3681        /// The defining function.
3682        parent: D,
3683        // Whether this is an AFIT (async fn in trait)
3684        in_trait_or_impl: Option<RpitContext>,
3685    },
3686    /// type aliases: `type Foo = impl Trait;`
3687    TyAlias {
3688        /// The type alias or associated type parent of the TAIT/ATPIT
3689        parent: D,
3690        /// associated types in impl blocks for traits.
3691        in_assoc_ty: bool,
3692    },
3693}
3694
3695#[derive(Debug, Clone, Copy, PartialEq, Eq, HashStable_Generic)]
3696pub enum InferDelegationKind {
3697    Input(usize),
3698    Output,
3699}
3700
3701/// The various kinds of types recognized by the compiler.
3702///
3703/// For an explanation of the `Unambig` generic parameter see the dev-guide:
3704/// <https://rustc-dev-guide.rust-lang.org/hir/ambig-unambig-ty-and-consts.html>
3705// SAFETY: `repr(u8)` is required so that `TyKind<()>` and `TyKind<!>` are layout compatible
3706#[repr(u8, C)]
3707#[derive(Debug, Clone, Copy, HashStable_Generic)]
3708pub enum TyKind<'hir, Unambig = ()> {
3709    /// Actual type should be inherited from `DefId` signature
3710    InferDelegation(DefId, InferDelegationKind),
3711    /// A variable length slice (i.e., `[T]`).
3712    Slice(&'hir Ty<'hir>),
3713    /// A fixed length array (i.e., `[T; n]`).
3714    Array(&'hir Ty<'hir>, &'hir ConstArg<'hir>),
3715    /// A raw pointer (i.e., `*const T` or `*mut T`).
3716    Ptr(MutTy<'hir>),
3717    /// A reference (i.e., `&'a T` or `&'a mut T`).
3718    Ref(&'hir Lifetime, MutTy<'hir>),
3719    /// A function pointer (e.g., `fn(usize) -> bool`).
3720    FnPtr(&'hir FnPtrTy<'hir>),
3721    /// An unsafe binder type (e.g. `unsafe<'a> Foo<'a>`).
3722    UnsafeBinder(&'hir UnsafeBinderTy<'hir>),
3723    /// The never type (`!`).
3724    Never,
3725    /// A tuple (`(A, B, C, D, ...)`).
3726    Tup(&'hir [Ty<'hir>]),
3727    /// A path to a type definition (`module::module::...::Type`), or an
3728    /// associated type (e.g., `<Vec<T> as Trait>::Type` or `<T>::Target`).
3729    ///
3730    /// Type parameters may be stored in each `PathSegment`.
3731    Path(QPath<'hir>),
3732    /// An opaque type definition itself. This is only used for `impl Trait`.
3733    OpaqueDef(&'hir OpaqueTy<'hir>),
3734    /// A trait ascription type, which is `impl Trait` within a local binding.
3735    TraitAscription(GenericBounds<'hir>),
3736    /// A trait object type `Bound1 + Bound2 + Bound3`
3737    /// where `Bound` is a trait or a lifetime.
3738    ///
3739    /// We use pointer tagging to represent a `&'hir Lifetime` and `TraitObjectSyntax` pair
3740    /// as otherwise this type being `repr(C)` would result in `TyKind` increasing in size.
3741    TraitObject(&'hir [PolyTraitRef<'hir>], TaggedRef<'hir, Lifetime, TraitObjectSyntax>),
3742    /// Unused for now.
3743    Typeof(&'hir AnonConst),
3744    /// Placeholder for a type that has failed to be defined.
3745    Err(rustc_span::ErrorGuaranteed),
3746    /// Pattern types (`pattern_type!(u32 is 1..)`)
3747    Pat(&'hir Ty<'hir>, &'hir TyPat<'hir>),
3748    /// `TyKind::Infer` means the type should be inferred instead of it having been
3749    /// specified. This can appear anywhere in a type.
3750    ///
3751    /// This variant is not always used to represent inference types, sometimes
3752    /// [`GenericArg::Infer`] is used instead.
3753    Infer(Unambig),
3754}
3755
3756#[derive(Debug, Clone, Copy, HashStable_Generic)]
3757pub enum InlineAsmOperand<'hir> {
3758    In {
3759        reg: InlineAsmRegOrRegClass,
3760        expr: &'hir Expr<'hir>,
3761    },
3762    Out {
3763        reg: InlineAsmRegOrRegClass,
3764        late: bool,
3765        expr: Option<&'hir Expr<'hir>>,
3766    },
3767    InOut {
3768        reg: InlineAsmRegOrRegClass,
3769        late: bool,
3770        expr: &'hir Expr<'hir>,
3771    },
3772    SplitInOut {
3773        reg: InlineAsmRegOrRegClass,
3774        late: bool,
3775        in_expr: &'hir Expr<'hir>,
3776        out_expr: Option<&'hir Expr<'hir>>,
3777    },
3778    Const {
3779        anon_const: ConstBlock,
3780    },
3781    SymFn {
3782        expr: &'hir Expr<'hir>,
3783    },
3784    SymStatic {
3785        path: QPath<'hir>,
3786        def_id: DefId,
3787    },
3788    Label {
3789        block: &'hir Block<'hir>,
3790    },
3791}
3792
3793impl<'hir> InlineAsmOperand<'hir> {
3794    pub fn reg(&self) -> Option<InlineAsmRegOrRegClass> {
3795        match *self {
3796            Self::In { reg, .. }
3797            | Self::Out { reg, .. }
3798            | Self::InOut { reg, .. }
3799            | Self::SplitInOut { reg, .. } => Some(reg),
3800            Self::Const { .. }
3801            | Self::SymFn { .. }
3802            | Self::SymStatic { .. }
3803            | Self::Label { .. } => None,
3804        }
3805    }
3806
3807    pub fn is_clobber(&self) -> bool {
3808        matches!(
3809            self,
3810            InlineAsmOperand::Out { reg: InlineAsmRegOrRegClass::Reg(_), late: _, expr: None }
3811        )
3812    }
3813}
3814
3815#[derive(Debug, Clone, Copy, HashStable_Generic)]
3816pub struct InlineAsm<'hir> {
3817    pub asm_macro: ast::AsmMacro,
3818    pub template: &'hir [InlineAsmTemplatePiece],
3819    pub template_strs: &'hir [(Symbol, Option<Symbol>, Span)],
3820    pub operands: &'hir [(InlineAsmOperand<'hir>, Span)],
3821    pub options: InlineAsmOptions,
3822    pub line_spans: &'hir [Span],
3823}
3824
3825impl InlineAsm<'_> {
3826    pub fn contains_label(&self) -> bool {
3827        self.operands.iter().any(|x| matches!(x.0, InlineAsmOperand::Label { .. }))
3828    }
3829}
3830
3831/// Represents a parameter in a function header.
3832#[derive(Debug, Clone, Copy, HashStable_Generic)]
3833pub struct Param<'hir> {
3834    #[stable_hasher(ignore)]
3835    pub hir_id: HirId,
3836    pub pat: &'hir Pat<'hir>,
3837    pub ty_span: Span,
3838    pub span: Span,
3839}
3840
3841/// Represents the header (not the body) of a function declaration.
3842#[derive(Debug, Clone, Copy, HashStable_Generic)]
3843pub struct FnDecl<'hir> {
3844    /// The types of the function's parameters.
3845    ///
3846    /// Additional argument data is stored in the function's [body](Body::params).
3847    pub inputs: &'hir [Ty<'hir>],
3848    pub output: FnRetTy<'hir>,
3849    pub c_variadic: bool,
3850    /// Does the function have an implicit self?
3851    pub implicit_self: ImplicitSelfKind,
3852    /// Is lifetime elision allowed.
3853    pub lifetime_elision_allowed: bool,
3854}
3855
3856impl<'hir> FnDecl<'hir> {
3857    pub fn opt_delegation_sig_id(&self) -> Option<DefId> {
3858        if let FnRetTy::Return(ty) = self.output
3859            && let TyKind::InferDelegation(sig_id, _) = ty.kind
3860        {
3861            return Some(sig_id);
3862        }
3863        None
3864    }
3865}
3866
3867/// Represents what type of implicit self a function has, if any.
3868#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
3869pub enum ImplicitSelfKind {
3870    /// Represents a `fn x(self);`.
3871    Imm,
3872    /// Represents a `fn x(mut self);`.
3873    Mut,
3874    /// Represents a `fn x(&self);`.
3875    RefImm,
3876    /// Represents a `fn x(&mut self);`.
3877    RefMut,
3878    /// Represents when a function does not have a self argument or
3879    /// when a function has a `self: X` argument.
3880    None,
3881}
3882
3883impl ImplicitSelfKind {
3884    /// Does this represent an implicit self?
3885    pub fn has_implicit_self(&self) -> bool {
3886        !matches!(*self, ImplicitSelfKind::None)
3887    }
3888}
3889
3890#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
3891pub enum IsAsync {
3892    Async(Span),
3893    NotAsync,
3894}
3895
3896impl IsAsync {
3897    pub fn is_async(self) -> bool {
3898        matches!(self, IsAsync::Async(_))
3899    }
3900}
3901
3902#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, HashStable_Generic)]
3903pub enum Defaultness {
3904    Default { has_value: bool },
3905    Final,
3906}
3907
3908impl Defaultness {
3909    pub fn has_value(&self) -> bool {
3910        match *self {
3911            Defaultness::Default { has_value } => has_value,
3912            Defaultness::Final => true,
3913        }
3914    }
3915
3916    pub fn is_final(&self) -> bool {
3917        *self == Defaultness::Final
3918    }
3919
3920    pub fn is_default(&self) -> bool {
3921        matches!(*self, Defaultness::Default { .. })
3922    }
3923}
3924
3925#[derive(Debug, Clone, Copy, HashStable_Generic)]
3926pub enum FnRetTy<'hir> {
3927    /// Return type is not specified.
3928    ///
3929    /// Functions default to `()` and
3930    /// closures default to inference. Span points to where return
3931    /// type would be inserted.
3932    DefaultReturn(Span),
3933    /// Everything else.
3934    Return(&'hir Ty<'hir>),
3935}
3936
3937impl<'hir> FnRetTy<'hir> {
3938    #[inline]
3939    pub fn span(&self) -> Span {
3940        match *self {
3941            Self::DefaultReturn(span) => span,
3942            Self::Return(ref ty) => ty.span,
3943        }
3944    }
3945
3946    pub fn is_suggestable_infer_ty(&self) -> Option<&'hir Ty<'hir>> {
3947        if let Self::Return(ty) = self
3948            && ty.is_suggestable_infer_ty()
3949        {
3950            return Some(*ty);
3951        }
3952        None
3953    }
3954}
3955
3956/// Represents `for<...>` binder before a closure
3957#[derive(Copy, Clone, Debug, HashStable_Generic)]
3958pub enum ClosureBinder {
3959    /// Binder is not specified.
3960    Default,
3961    /// Binder is specified.
3962    ///
3963    /// Span points to the whole `for<...>`.
3964    For { span: Span },
3965}
3966
3967#[derive(Debug, Clone, Copy, HashStable_Generic)]
3968pub struct Mod<'hir> {
3969    pub spans: ModSpans,
3970    pub item_ids: &'hir [ItemId],
3971}
3972
3973#[derive(Copy, Clone, Debug, HashStable_Generic)]
3974pub struct ModSpans {
3975    /// A span from the first token past `{` to the last token until `}`.
3976    /// For `mod foo;`, the inner span ranges from the first token
3977    /// to the last token in the external file.
3978    pub inner_span: Span,
3979    pub inject_use_span: Span,
3980}
3981
3982#[derive(Debug, Clone, Copy, HashStable_Generic)]
3983pub struct EnumDef<'hir> {
3984    pub variants: &'hir [Variant<'hir>],
3985}
3986
3987#[derive(Debug, Clone, Copy, HashStable_Generic)]
3988pub struct Variant<'hir> {
3989    /// Name of the variant.
3990    pub ident: Ident,
3991    /// Id of the variant (not the constructor, see `VariantData::ctor_hir_id()`).
3992    #[stable_hasher(ignore)]
3993    pub hir_id: HirId,
3994    pub def_id: LocalDefId,
3995    /// Fields and constructor id of the variant.
3996    pub data: VariantData<'hir>,
3997    /// Explicit discriminant (e.g., `Foo = 1`).
3998    pub disr_expr: Option<&'hir AnonConst>,
3999    /// Span
4000    pub span: Span,
4001}
4002
4003#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
4004pub enum UseKind {
4005    /// One import, e.g., `use foo::bar` or `use foo::bar as baz`.
4006    /// Also produced for each element of a list `use`, e.g.
4007    /// `use foo::{a, b}` lowers to `use foo::a; use foo::b;`.
4008    ///
4009    /// The identifier is the name defined by the import. E.g. for `use
4010    /// foo::bar` it is `bar`, for `use foo::bar as baz` it is `baz`.
4011    Single(Ident),
4012
4013    /// Glob import, e.g., `use foo::*`.
4014    Glob,
4015
4016    /// Degenerate list import, e.g., `use foo::{a, b}` produces
4017    /// an additional `use foo::{}` for performing checks such as
4018    /// unstable feature gating. May be removed in the future.
4019    ListStem,
4020}
4021
4022/// References to traits in impls.
4023///
4024/// `resolve` maps each `TraitRef`'s `ref_id` to its defining trait; that's all
4025/// that the `ref_id` is for. Note that `ref_id`'s value is not the `HirId` of the
4026/// trait being referred to but just a unique `HirId` that serves as a key
4027/// within the resolution map.
4028#[derive(Clone, Debug, Copy, HashStable_Generic)]
4029pub struct TraitRef<'hir> {
4030    pub path: &'hir Path<'hir>,
4031    // Don't hash the `ref_id`. It is tracked via the thing it is used to access.
4032    #[stable_hasher(ignore)]
4033    pub hir_ref_id: HirId,
4034}
4035
4036impl TraitRef<'_> {
4037    /// Gets the `DefId` of the referenced trait. It _must_ actually be a trait or trait alias.
4038    pub fn trait_def_id(&self) -> Option<DefId> {
4039        match self.path.res {
4040            Res::Def(DefKind::Trait | DefKind::TraitAlias, did) => Some(did),
4041            Res::Err => None,
4042            res => panic!("{res:?} did not resolve to a trait or trait alias"),
4043        }
4044    }
4045}
4046
4047#[derive(Clone, Debug, Copy, HashStable_Generic)]
4048pub struct PolyTraitRef<'hir> {
4049    /// The `'a` in `for<'a> Foo<&'a T>`.
4050    pub bound_generic_params: &'hir [GenericParam<'hir>],
4051
4052    /// The constness and polarity of the trait ref.
4053    ///
4054    /// The `async` modifier is lowered directly into a different trait for now.
4055    pub modifiers: TraitBoundModifiers,
4056
4057    /// The `Foo<&'a T>` in `for<'a> Foo<&'a T>`.
4058    pub trait_ref: TraitRef<'hir>,
4059
4060    pub span: Span,
4061}
4062
4063#[derive(Debug, Clone, Copy, HashStable_Generic)]
4064pub struct FieldDef<'hir> {
4065    pub span: Span,
4066    pub vis_span: Span,
4067    pub ident: Ident,
4068    #[stable_hasher(ignore)]
4069    pub hir_id: HirId,
4070    pub def_id: LocalDefId,
4071    pub ty: &'hir Ty<'hir>,
4072    pub safety: Safety,
4073    pub default: Option<&'hir AnonConst>,
4074}
4075
4076impl FieldDef<'_> {
4077    // Still necessary in couple of places
4078    pub fn is_positional(&self) -> bool {
4079        self.ident.as_str().as_bytes()[0].is_ascii_digit()
4080    }
4081}
4082
4083/// Fields and constructor IDs of enum variants and structs.
4084#[derive(Debug, Clone, Copy, HashStable_Generic)]
4085pub enum VariantData<'hir> {
4086    /// A struct variant.
4087    ///
4088    /// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
4089    Struct { fields: &'hir [FieldDef<'hir>], recovered: ast::Recovered },
4090    /// A tuple variant.
4091    ///
4092    /// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
4093    Tuple(&'hir [FieldDef<'hir>], #[stable_hasher(ignore)] HirId, LocalDefId),
4094    /// A unit variant.
4095    ///
4096    /// E.g., `Bar = ..` as in `enum Foo { Bar = .. }`.
4097    Unit(#[stable_hasher(ignore)] HirId, LocalDefId),
4098}
4099
4100impl<'hir> VariantData<'hir> {
4101    /// Return the fields of this variant.
4102    pub fn fields(&self) -> &'hir [FieldDef<'hir>] {
4103        match *self {
4104            VariantData::Struct { fields, .. } | VariantData::Tuple(fields, ..) => fields,
4105            _ => &[],
4106        }
4107    }
4108
4109    pub fn ctor(&self) -> Option<(CtorKind, HirId, LocalDefId)> {
4110        match *self {
4111            VariantData::Tuple(_, hir_id, def_id) => Some((CtorKind::Fn, hir_id, def_id)),
4112            VariantData::Unit(hir_id, def_id) => Some((CtorKind::Const, hir_id, def_id)),
4113            VariantData::Struct { .. } => None,
4114        }
4115    }
4116
4117    #[inline]
4118    pub fn ctor_kind(&self) -> Option<CtorKind> {
4119        self.ctor().map(|(kind, ..)| kind)
4120    }
4121
4122    /// Return the `HirId` of this variant's constructor, if it has one.
4123    #[inline]
4124    pub fn ctor_hir_id(&self) -> Option<HirId> {
4125        self.ctor().map(|(_, hir_id, _)| hir_id)
4126    }
4127
4128    /// Return the `LocalDefId` of this variant's constructor, if it has one.
4129    #[inline]
4130    pub fn ctor_def_id(&self) -> Option<LocalDefId> {
4131        self.ctor().map(|(.., def_id)| def_id)
4132    }
4133}
4134
4135// The bodies for items are stored "out of line", in a separate
4136// hashmap in the `Crate`. Here we just record the hir-id of the item
4137// so it can fetched later.
4138#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, Hash, HashStable_Generic)]
4139pub struct ItemId {
4140    pub owner_id: OwnerId,
4141}
4142
4143impl ItemId {
4144    #[inline]
4145    pub fn hir_id(&self) -> HirId {
4146        // Items are always HIR owners.
4147        HirId::make_owner(self.owner_id.def_id)
4148    }
4149}
4150
4151/// An item
4152///
4153/// For more details, see the [rust lang reference].
4154/// Note that the reference does not document nightly-only features.
4155/// There may be also slight differences in the names and representation of AST nodes between
4156/// the compiler and the reference.
4157///
4158/// [rust lang reference]: https://doc.rust-lang.org/reference/items.html
4159#[derive(Debug, Clone, Copy, HashStable_Generic)]
4160pub struct Item<'hir> {
4161    pub owner_id: OwnerId,
4162    pub kind: ItemKind<'hir>,
4163    pub span: Span,
4164    pub vis_span: Span,
4165    pub has_delayed_lints: bool,
4166}
4167
4168impl<'hir> Item<'hir> {
4169    #[inline]
4170    pub fn hir_id(&self) -> HirId {
4171        // Items are always HIR owners.
4172        HirId::make_owner(self.owner_id.def_id)
4173    }
4174
4175    pub fn item_id(&self) -> ItemId {
4176        ItemId { owner_id: self.owner_id }
4177    }
4178
4179    /// Check if this is an [`ItemKind::Enum`], [`ItemKind::Struct`] or
4180    /// [`ItemKind::Union`].
4181    pub fn is_adt(&self) -> bool {
4182        matches!(self.kind, ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..))
4183    }
4184
4185    /// Check if this is an [`ItemKind::Struct`] or [`ItemKind::Union`].
4186    pub fn is_struct_or_union(&self) -> bool {
4187        matches!(self.kind, ItemKind::Struct(..) | ItemKind::Union(..))
4188    }
4189
4190    expect_methods_self_kind! {
4191        expect_extern_crate, (Option<Symbol>, Ident),
4192            ItemKind::ExternCrate(s, ident), (*s, *ident);
4193
4194        expect_use, (&'hir UsePath<'hir>, UseKind), ItemKind::Use(p, uk), (p, *uk);
4195
4196        expect_static, (Mutability, Ident, &'hir Ty<'hir>, BodyId),
4197            ItemKind::Static(mutbl, ident, ty, body), (*mutbl, *ident, ty, *body);
4198
4199        expect_const, (Ident, &'hir Generics<'hir>, &'hir Ty<'hir>, BodyId),
4200            ItemKind::Const(ident, generics, ty, body), (*ident, generics, ty, *body);
4201
4202        expect_fn, (Ident, &FnSig<'hir>, &'hir Generics<'hir>, BodyId),
4203            ItemKind::Fn { ident, sig, generics, body, .. }, (*ident, sig, generics, *body);
4204
4205        expect_macro, (Ident, &ast::MacroDef, MacroKinds),
4206            ItemKind::Macro(ident, def, mk), (*ident, def, *mk);
4207
4208        expect_mod, (Ident, &'hir Mod<'hir>), ItemKind::Mod(ident, m), (*ident, m);
4209
4210        expect_foreign_mod, (ExternAbi, &'hir [ForeignItemId]),
4211            ItemKind::ForeignMod { abi, items }, (*abi, items);
4212
4213        expect_global_asm, &'hir InlineAsm<'hir>, ItemKind::GlobalAsm { asm, .. }, asm;
4214
4215        expect_ty_alias, (Ident, &'hir Generics<'hir>, &'hir Ty<'hir>),
4216            ItemKind::TyAlias(ident, generics, ty), (*ident, generics, ty);
4217
4218        expect_enum, (Ident, &'hir Generics<'hir>, &EnumDef<'hir>),
4219            ItemKind::Enum(ident, generics, def), (*ident, generics, def);
4220
4221        expect_struct, (Ident, &'hir Generics<'hir>, &VariantData<'hir>),
4222            ItemKind::Struct(ident, generics, data), (*ident, generics, data);
4223
4224        expect_union, (Ident, &'hir Generics<'hir>, &VariantData<'hir>),
4225            ItemKind::Union(ident, generics, data), (*ident, generics, data);
4226
4227        expect_trait,
4228            (
4229                Constness,
4230                IsAuto,
4231                Safety,
4232                Ident,
4233                &'hir Generics<'hir>,
4234                GenericBounds<'hir>,
4235                &'hir [TraitItemId]
4236            ),
4237            ItemKind::Trait(constness, is_auto, safety, ident, generics, bounds, items),
4238            (*constness, *is_auto, *safety, *ident, generics, bounds, items);
4239
4240        expect_trait_alias, (Ident, &'hir Generics<'hir>, GenericBounds<'hir>),
4241            ItemKind::TraitAlias(ident, generics, bounds), (*ident, generics, bounds);
4242
4243        expect_impl, &Impl<'hir>, ItemKind::Impl(imp), imp;
4244    }
4245}
4246
4247#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
4248#[derive(Encodable, Decodable, HashStable_Generic)]
4249pub enum Safety {
4250    Unsafe,
4251    Safe,
4252}
4253
4254impl Safety {
4255    pub fn prefix_str(self) -> &'static str {
4256        match self {
4257            Self::Unsafe => "unsafe ",
4258            Self::Safe => "",
4259        }
4260    }
4261
4262    #[inline]
4263    pub fn is_unsafe(self) -> bool {
4264        !self.is_safe()
4265    }
4266
4267    #[inline]
4268    pub fn is_safe(self) -> bool {
4269        match self {
4270            Self::Unsafe => false,
4271            Self::Safe => true,
4272        }
4273    }
4274}
4275
4276impl fmt::Display for Safety {
4277    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4278        f.write_str(match *self {
4279            Self::Unsafe => "unsafe",
4280            Self::Safe => "safe",
4281        })
4282    }
4283}
4284
4285#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, HashStable_Generic)]
4286pub enum Constness {
4287    Const,
4288    NotConst,
4289}
4290
4291impl fmt::Display for Constness {
4292    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4293        f.write_str(match *self {
4294            Self::Const => "const",
4295            Self::NotConst => "non-const",
4296        })
4297    }
4298}
4299
4300/// The actual safety specified in syntax. We may treat
4301/// its safety different within the type system to create a
4302/// "sound by default" system that needs checking this enum
4303/// explicitly to allow unsafe operations.
4304#[derive(Copy, Clone, Debug, HashStable_Generic, PartialEq, Eq)]
4305pub enum HeaderSafety {
4306    /// A safe function annotated with `#[target_features]`.
4307    /// The type system treats this function as an unsafe function,
4308    /// but safety checking will check this enum to treat it as safe
4309    /// and allowing calling other safe target feature functions with
4310    /// the same features without requiring an additional unsafe block.
4311    SafeTargetFeatures,
4312    Normal(Safety),
4313}
4314
4315impl From<Safety> for HeaderSafety {
4316    fn from(v: Safety) -> Self {
4317        Self::Normal(v)
4318    }
4319}
4320
4321#[derive(Copy, Clone, Debug, HashStable_Generic)]
4322pub struct FnHeader {
4323    pub safety: HeaderSafety,
4324    pub constness: Constness,
4325    pub asyncness: IsAsync,
4326    pub abi: ExternAbi,
4327}
4328
4329impl FnHeader {
4330    pub fn is_async(&self) -> bool {
4331        matches!(self.asyncness, IsAsync::Async(_))
4332    }
4333
4334    pub fn is_const(&self) -> bool {
4335        matches!(self.constness, Constness::Const)
4336    }
4337
4338    pub fn is_unsafe(&self) -> bool {
4339        self.safety().is_unsafe()
4340    }
4341
4342    pub fn is_safe(&self) -> bool {
4343        self.safety().is_safe()
4344    }
4345
4346    pub fn safety(&self) -> Safety {
4347        match self.safety {
4348            HeaderSafety::SafeTargetFeatures => Safety::Unsafe,
4349            HeaderSafety::Normal(safety) => safety,
4350        }
4351    }
4352}
4353
4354#[derive(Debug, Clone, Copy, HashStable_Generic)]
4355pub enum ItemKind<'hir> {
4356    /// An `extern crate` item, with optional *original* crate name if the crate was renamed.
4357    ///
4358    /// E.g., `extern crate foo` or `extern crate foo_bar as foo`.
4359    ExternCrate(Option<Symbol>, Ident),
4360
4361    /// `use foo::bar::*;` or `use foo::bar::baz as quux;`
4362    ///
4363    /// or just
4364    ///
4365    /// `use foo::bar::baz;` (with `as baz` implicitly on the right).
4366    Use(&'hir UsePath<'hir>, UseKind),
4367
4368    /// A `static` item.
4369    Static(Mutability, Ident, &'hir Ty<'hir>, BodyId),
4370    /// A `const` item.
4371    Const(Ident, &'hir Generics<'hir>, &'hir Ty<'hir>, BodyId),
4372    /// A function declaration.
4373    Fn {
4374        sig: FnSig<'hir>,
4375        ident: Ident,
4376        generics: &'hir Generics<'hir>,
4377        body: BodyId,
4378        /// Whether this function actually has a body.
4379        /// For functions without a body, `body` is synthesized (to avoid ICEs all over the
4380        /// compiler), but that code should never be translated.
4381        has_body: bool,
4382    },
4383    /// A MBE macro definition (`macro_rules!` or `macro`).
4384    Macro(Ident, &'hir ast::MacroDef, MacroKinds),
4385    /// A module.
4386    Mod(Ident, &'hir Mod<'hir>),
4387    /// An external module, e.g. `extern { .. }`.
4388    ForeignMod { abi: ExternAbi, items: &'hir [ForeignItemId] },
4389    /// Module-level inline assembly (from `global_asm!`).
4390    GlobalAsm {
4391        asm: &'hir InlineAsm<'hir>,
4392        /// A fake body which stores typeck results for the global asm's sym_fn
4393        /// operands, which are represented as path expressions. This body contains
4394        /// a single [`ExprKind::InlineAsm`] which points to the asm in the field
4395        /// above, and which is typechecked like a inline asm expr just for the
4396        /// typeck results.
4397        fake_body: BodyId,
4398    },
4399    /// A type alias, e.g., `type Foo = Bar<u8>`.
4400    TyAlias(Ident, &'hir Generics<'hir>, &'hir Ty<'hir>),
4401    /// An enum definition, e.g., `enum Foo<A, B> { C<A>, D<B> }`.
4402    Enum(Ident, &'hir Generics<'hir>, EnumDef<'hir>),
4403    /// A struct definition, e.g., `struct Foo<A> {x: A}`.
4404    Struct(Ident, &'hir Generics<'hir>, VariantData<'hir>),
4405    /// A union definition, e.g., `union Foo<A, B> {x: A, y: B}`.
4406    Union(Ident, &'hir Generics<'hir>, VariantData<'hir>),
4407    /// A trait definition.
4408    Trait(
4409        Constness,
4410        IsAuto,
4411        Safety,
4412        Ident,
4413        &'hir Generics<'hir>,
4414        GenericBounds<'hir>,
4415        &'hir [TraitItemId],
4416    ),
4417    /// A trait alias.
4418    TraitAlias(Ident, &'hir Generics<'hir>, GenericBounds<'hir>),
4419
4420    /// An implementation, e.g., `impl<A> Trait for Foo { .. }`.
4421    Impl(Impl<'hir>),
4422}
4423
4424/// Represents an impl block declaration.
4425///
4426/// E.g., `impl $Type { .. }` or `impl $Trait for $Type { .. }`
4427/// Refer to [`ImplItem`] for an associated item within an impl block.
4428#[derive(Debug, Clone, Copy, HashStable_Generic)]
4429pub struct Impl<'hir> {
4430    pub generics: &'hir Generics<'hir>,
4431    pub of_trait: Option<&'hir TraitImplHeader<'hir>>,
4432    pub self_ty: &'hir Ty<'hir>,
4433    pub items: &'hir [ImplItemId],
4434}
4435
4436#[derive(Debug, Clone, Copy, HashStable_Generic)]
4437pub struct TraitImplHeader<'hir> {
4438    pub constness: Constness,
4439    pub safety: Safety,
4440    pub polarity: ImplPolarity,
4441    pub defaultness: Defaultness,
4442    // We do not put a `Span` in `Defaultness` because it breaks foreign crate metadata
4443    // decoding as `Span`s cannot be decoded when a `Session` is not available.
4444    pub defaultness_span: Option<Span>,
4445    pub trait_ref: TraitRef<'hir>,
4446}
4447
4448impl ItemKind<'_> {
4449    pub fn ident(&self) -> Option<Ident> {
4450        match *self {
4451            ItemKind::ExternCrate(_, ident)
4452            | ItemKind::Use(_, UseKind::Single(ident))
4453            | ItemKind::Static(_, ident, ..)
4454            | ItemKind::Const(ident, ..)
4455            | ItemKind::Fn { ident, .. }
4456            | ItemKind::Macro(ident, ..)
4457            | ItemKind::Mod(ident, ..)
4458            | ItemKind::TyAlias(ident, ..)
4459            | ItemKind::Enum(ident, ..)
4460            | ItemKind::Struct(ident, ..)
4461            | ItemKind::Union(ident, ..)
4462            | ItemKind::Trait(_, _, _, ident, ..)
4463            | ItemKind::TraitAlias(ident, ..) => Some(ident),
4464
4465            ItemKind::Use(_, UseKind::Glob | UseKind::ListStem)
4466            | ItemKind::ForeignMod { .. }
4467            | ItemKind::GlobalAsm { .. }
4468            | ItemKind::Impl(_) => None,
4469        }
4470    }
4471
4472    pub fn generics(&self) -> Option<&Generics<'_>> {
4473        Some(match self {
4474            ItemKind::Fn { generics, .. }
4475            | ItemKind::TyAlias(_, generics, _)
4476            | ItemKind::Const(_, generics, _, _)
4477            | ItemKind::Enum(_, generics, _)
4478            | ItemKind::Struct(_, generics, _)
4479            | ItemKind::Union(_, generics, _)
4480            | ItemKind::Trait(_, _, _, _, generics, _, _)
4481            | ItemKind::TraitAlias(_, generics, _)
4482            | ItemKind::Impl(Impl { generics, .. }) => generics,
4483            _ => return None,
4484        })
4485    }
4486}
4487
4488// The bodies for items are stored "out of line", in a separate
4489// hashmap in the `Crate`. Here we just record the hir-id of the item
4490// so it can fetched later.
4491#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
4492pub struct ForeignItemId {
4493    pub owner_id: OwnerId,
4494}
4495
4496impl ForeignItemId {
4497    #[inline]
4498    pub fn hir_id(&self) -> HirId {
4499        // Items are always HIR owners.
4500        HirId::make_owner(self.owner_id.def_id)
4501    }
4502}
4503
4504#[derive(Debug, Clone, Copy, HashStable_Generic)]
4505pub struct ForeignItem<'hir> {
4506    pub ident: Ident,
4507    pub kind: ForeignItemKind<'hir>,
4508    pub owner_id: OwnerId,
4509    pub span: Span,
4510    pub vis_span: Span,
4511    pub has_delayed_lints: bool,
4512}
4513
4514impl ForeignItem<'_> {
4515    #[inline]
4516    pub fn hir_id(&self) -> HirId {
4517        // Items are always HIR owners.
4518        HirId::make_owner(self.owner_id.def_id)
4519    }
4520
4521    pub fn foreign_item_id(&self) -> ForeignItemId {
4522        ForeignItemId { owner_id: self.owner_id }
4523    }
4524}
4525
4526/// An item within an `extern` block.
4527#[derive(Debug, Clone, Copy, HashStable_Generic)]
4528pub enum ForeignItemKind<'hir> {
4529    /// A foreign function.
4530    ///
4531    /// All argument idents are actually always present (i.e. `Some`), but
4532    /// `&[Option<Ident>]` is used because of code paths shared with `TraitFn`
4533    /// and `FnPtrTy`. The sharing is due to all of these cases not allowing
4534    /// arbitrary patterns for parameters.
4535    Fn(FnSig<'hir>, &'hir [Option<Ident>], &'hir Generics<'hir>),
4536    /// A foreign static item (`static ext: u8`).
4537    Static(&'hir Ty<'hir>, Mutability, Safety),
4538    /// A foreign type.
4539    Type,
4540}
4541
4542/// A variable captured by a closure.
4543#[derive(Debug, Copy, Clone, HashStable_Generic)]
4544pub struct Upvar {
4545    /// First span where it is accessed (there can be multiple).
4546    pub span: Span,
4547}
4548
4549// The TraitCandidate's import_ids is empty if the trait is defined in the same module, and
4550// has length > 0 if the trait is found through an chain of imports, starting with the
4551// import/use statement in the scope where the trait is used.
4552#[derive(Debug, Clone, HashStable_Generic)]
4553pub struct TraitCandidate {
4554    pub def_id: DefId,
4555    pub import_ids: SmallVec<[LocalDefId; 1]>,
4556}
4557
4558#[derive(Copy, Clone, Debug, HashStable_Generic)]
4559pub enum OwnerNode<'hir> {
4560    Item(&'hir Item<'hir>),
4561    ForeignItem(&'hir ForeignItem<'hir>),
4562    TraitItem(&'hir TraitItem<'hir>),
4563    ImplItem(&'hir ImplItem<'hir>),
4564    Crate(&'hir Mod<'hir>),
4565    Synthetic,
4566}
4567
4568impl<'hir> OwnerNode<'hir> {
4569    pub fn span(&self) -> Span {
4570        match self {
4571            OwnerNode::Item(Item { span, .. })
4572            | OwnerNode::ForeignItem(ForeignItem { span, .. })
4573            | OwnerNode::ImplItem(ImplItem { span, .. })
4574            | OwnerNode::TraitItem(TraitItem { span, .. }) => *span,
4575            OwnerNode::Crate(Mod { spans: ModSpans { inner_span, .. }, .. }) => *inner_span,
4576            OwnerNode::Synthetic => unreachable!(),
4577        }
4578    }
4579
4580    pub fn fn_sig(self) -> Option<&'hir FnSig<'hir>> {
4581        match self {
4582            OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })
4583            | OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
4584            | OwnerNode::Item(Item { kind: ItemKind::Fn { sig: fn_sig, .. }, .. })
4585            | OwnerNode::ForeignItem(ForeignItem {
4586                kind: ForeignItemKind::Fn(fn_sig, _, _), ..
4587            }) => Some(fn_sig),
4588            _ => None,
4589        }
4590    }
4591
4592    pub fn fn_decl(self) -> Option<&'hir FnDecl<'hir>> {
4593        match self {
4594            OwnerNode::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })
4595            | OwnerNode::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
4596            | OwnerNode::Item(Item { kind: ItemKind::Fn { sig: fn_sig, .. }, .. })
4597            | OwnerNode::ForeignItem(ForeignItem {
4598                kind: ForeignItemKind::Fn(fn_sig, _, _), ..
4599            }) => Some(fn_sig.decl),
4600            _ => None,
4601        }
4602    }
4603
4604    pub fn body_id(&self) -> Option<BodyId> {
4605        match self {
4606            OwnerNode::Item(Item {
4607                kind:
4608                    ItemKind::Static(_, _, _, body)
4609                    | ItemKind::Const(_, _, _, body)
4610                    | ItemKind::Fn { body, .. },
4611                ..
4612            })
4613            | OwnerNode::TraitItem(TraitItem {
4614                kind:
4615                    TraitItemKind::Fn(_, TraitFn::Provided(body)) | TraitItemKind::Const(_, Some(body)),
4616                ..
4617            })
4618            | OwnerNode::ImplItem(ImplItem {
4619                kind: ImplItemKind::Fn(_, body) | ImplItemKind::Const(_, body),
4620                ..
4621            }) => Some(*body),
4622            _ => None,
4623        }
4624    }
4625
4626    pub fn generics(self) -> Option<&'hir Generics<'hir>> {
4627        Node::generics(self.into())
4628    }
4629
4630    pub fn def_id(self) -> OwnerId {
4631        match self {
4632            OwnerNode::Item(Item { owner_id, .. })
4633            | OwnerNode::TraitItem(TraitItem { owner_id, .. })
4634            | OwnerNode::ImplItem(ImplItem { owner_id, .. })
4635            | OwnerNode::ForeignItem(ForeignItem { owner_id, .. }) => *owner_id,
4636            OwnerNode::Crate(..) => crate::CRATE_HIR_ID.owner,
4637            OwnerNode::Synthetic => unreachable!(),
4638        }
4639    }
4640
4641    /// Check if node is an impl block.
4642    pub fn is_impl_block(&self) -> bool {
4643        matches!(self, OwnerNode::Item(Item { kind: ItemKind::Impl(_), .. }))
4644    }
4645
4646    expect_methods_self! {
4647        expect_item,         &'hir Item<'hir>,        OwnerNode::Item(n),        n;
4648        expect_foreign_item, &'hir ForeignItem<'hir>, OwnerNode::ForeignItem(n), n;
4649        expect_impl_item,    &'hir ImplItem<'hir>,    OwnerNode::ImplItem(n),    n;
4650        expect_trait_item,   &'hir TraitItem<'hir>,   OwnerNode::TraitItem(n),   n;
4651    }
4652}
4653
4654impl<'hir> From<&'hir Item<'hir>> for OwnerNode<'hir> {
4655    fn from(val: &'hir Item<'hir>) -> Self {
4656        OwnerNode::Item(val)
4657    }
4658}
4659
4660impl<'hir> From<&'hir ForeignItem<'hir>> for OwnerNode<'hir> {
4661    fn from(val: &'hir ForeignItem<'hir>) -> Self {
4662        OwnerNode::ForeignItem(val)
4663    }
4664}
4665
4666impl<'hir> From<&'hir ImplItem<'hir>> for OwnerNode<'hir> {
4667    fn from(val: &'hir ImplItem<'hir>) -> Self {
4668        OwnerNode::ImplItem(val)
4669    }
4670}
4671
4672impl<'hir> From<&'hir TraitItem<'hir>> for OwnerNode<'hir> {
4673    fn from(val: &'hir TraitItem<'hir>) -> Self {
4674        OwnerNode::TraitItem(val)
4675    }
4676}
4677
4678impl<'hir> From<OwnerNode<'hir>> for Node<'hir> {
4679    fn from(val: OwnerNode<'hir>) -> Self {
4680        match val {
4681            OwnerNode::Item(n) => Node::Item(n),
4682            OwnerNode::ForeignItem(n) => Node::ForeignItem(n),
4683            OwnerNode::ImplItem(n) => Node::ImplItem(n),
4684            OwnerNode::TraitItem(n) => Node::TraitItem(n),
4685            OwnerNode::Crate(n) => Node::Crate(n),
4686            OwnerNode::Synthetic => Node::Synthetic,
4687        }
4688    }
4689}
4690
4691#[derive(Copy, Clone, Debug, HashStable_Generic)]
4692pub enum Node<'hir> {
4693    Param(&'hir Param<'hir>),
4694    Item(&'hir Item<'hir>),
4695    ForeignItem(&'hir ForeignItem<'hir>),
4696    TraitItem(&'hir TraitItem<'hir>),
4697    ImplItem(&'hir ImplItem<'hir>),
4698    Variant(&'hir Variant<'hir>),
4699    Field(&'hir FieldDef<'hir>),
4700    AnonConst(&'hir AnonConst),
4701    ConstBlock(&'hir ConstBlock),
4702    ConstArg(&'hir ConstArg<'hir>),
4703    Expr(&'hir Expr<'hir>),
4704    ExprField(&'hir ExprField<'hir>),
4705    Stmt(&'hir Stmt<'hir>),
4706    PathSegment(&'hir PathSegment<'hir>),
4707    Ty(&'hir Ty<'hir>),
4708    AssocItemConstraint(&'hir AssocItemConstraint<'hir>),
4709    TraitRef(&'hir TraitRef<'hir>),
4710    OpaqueTy(&'hir OpaqueTy<'hir>),
4711    TyPat(&'hir TyPat<'hir>),
4712    Pat(&'hir Pat<'hir>),
4713    PatField(&'hir PatField<'hir>),
4714    /// Needed as its own node with its own HirId for tracking
4715    /// the unadjusted type of literals within patterns
4716    /// (e.g. byte str literals not being of slice type).
4717    PatExpr(&'hir PatExpr<'hir>),
4718    Arm(&'hir Arm<'hir>),
4719    Block(&'hir Block<'hir>),
4720    LetStmt(&'hir LetStmt<'hir>),
4721    /// `Ctor` refers to the constructor of an enum variant or struct. Only tuple or unit variants
4722    /// with synthesized constructors.
4723    Ctor(&'hir VariantData<'hir>),
4724    Lifetime(&'hir Lifetime),
4725    GenericParam(&'hir GenericParam<'hir>),
4726    Crate(&'hir Mod<'hir>),
4727    Infer(&'hir InferArg),
4728    WherePredicate(&'hir WherePredicate<'hir>),
4729    PreciseCapturingNonLifetimeArg(&'hir PreciseCapturingNonLifetimeArg),
4730    // Created by query feeding
4731    Synthetic,
4732    Err(Span),
4733}
4734
4735impl<'hir> Node<'hir> {
4736    /// Get the identifier of this `Node`, if applicable.
4737    ///
4738    /// # Edge cases
4739    ///
4740    /// Calling `.ident()` on a [`Node::Ctor`] will return `None`
4741    /// because `Ctor`s do not have identifiers themselves.
4742    /// Instead, call `.ident()` on the parent struct/variant, like so:
4743    ///
4744    /// ```ignore (illustrative)
4745    /// ctor
4746    ///     .ctor_hir_id()
4747    ///     .map(|ctor_id| tcx.parent_hir_node(ctor_id))
4748    ///     .and_then(|parent| parent.ident())
4749    /// ```
4750    pub fn ident(&self) -> Option<Ident> {
4751        match self {
4752            Node::Item(item) => item.kind.ident(),
4753            Node::TraitItem(TraitItem { ident, .. })
4754            | Node::ImplItem(ImplItem { ident, .. })
4755            | Node::ForeignItem(ForeignItem { ident, .. })
4756            | Node::Field(FieldDef { ident, .. })
4757            | Node::Variant(Variant { ident, .. })
4758            | Node::PathSegment(PathSegment { ident, .. }) => Some(*ident),
4759            Node::Lifetime(lt) => Some(lt.ident),
4760            Node::GenericParam(p) => Some(p.name.ident()),
4761            Node::AssocItemConstraint(c) => Some(c.ident),
4762            Node::PatField(f) => Some(f.ident),
4763            Node::ExprField(f) => Some(f.ident),
4764            Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
4765            Node::Param(..)
4766            | Node::AnonConst(..)
4767            | Node::ConstBlock(..)
4768            | Node::ConstArg(..)
4769            | Node::Expr(..)
4770            | Node::Stmt(..)
4771            | Node::Block(..)
4772            | Node::Ctor(..)
4773            | Node::Pat(..)
4774            | Node::TyPat(..)
4775            | Node::PatExpr(..)
4776            | Node::Arm(..)
4777            | Node::LetStmt(..)
4778            | Node::Crate(..)
4779            | Node::Ty(..)
4780            | Node::TraitRef(..)
4781            | Node::OpaqueTy(..)
4782            | Node::Infer(..)
4783            | Node::WherePredicate(..)
4784            | Node::Synthetic
4785            | Node::Err(..) => None,
4786        }
4787    }
4788
4789    pub fn fn_decl(self) -> Option<&'hir FnDecl<'hir>> {
4790        match self {
4791            Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })
4792            | Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
4793            | Node::Item(Item { kind: ItemKind::Fn { sig: fn_sig, .. }, .. })
4794            | Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(fn_sig, _, _), .. }) => {
4795                Some(fn_sig.decl)
4796            }
4797            Node::Expr(Expr { kind: ExprKind::Closure(Closure { fn_decl, .. }), .. }) => {
4798                Some(fn_decl)
4799            }
4800            _ => None,
4801        }
4802    }
4803
4804    /// Get a `hir::Impl` if the node is an impl block for the given `trait_def_id`.
4805    pub fn impl_block_of_trait(self, trait_def_id: DefId) -> Option<&'hir Impl<'hir>> {
4806        if let Node::Item(Item { kind: ItemKind::Impl(impl_block), .. }) = self
4807            && let Some(of_trait) = impl_block.of_trait
4808            && let Some(trait_id) = of_trait.trait_ref.trait_def_id()
4809            && trait_id == trait_def_id
4810        {
4811            Some(impl_block)
4812        } else {
4813            None
4814        }
4815    }
4816
4817    pub fn fn_sig(self) -> Option<&'hir FnSig<'hir>> {
4818        match self {
4819            Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })
4820            | Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(fn_sig, _), .. })
4821            | Node::Item(Item { kind: ItemKind::Fn { sig: fn_sig, .. }, .. })
4822            | Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(fn_sig, _, _), .. }) => {
4823                Some(fn_sig)
4824            }
4825            _ => None,
4826        }
4827    }
4828
4829    /// Get the type for constants, assoc types, type aliases and statics.
4830    pub fn ty(self) -> Option<&'hir Ty<'hir>> {
4831        match self {
4832            Node::Item(it) => match it.kind {
4833                ItemKind::TyAlias(_, _, ty)
4834                | ItemKind::Static(_, _, ty, _)
4835                | ItemKind::Const(_, _, ty, _) => Some(ty),
4836                ItemKind::Impl(impl_item) => Some(&impl_item.self_ty),
4837                _ => None,
4838            },
4839            Node::TraitItem(it) => match it.kind {
4840                TraitItemKind::Const(ty, _) => Some(ty),
4841                TraitItemKind::Type(_, ty) => ty,
4842                _ => None,
4843            },
4844            Node::ImplItem(it) => match it.kind {
4845                ImplItemKind::Const(ty, _) => Some(ty),
4846                ImplItemKind::Type(ty) => Some(ty),
4847                _ => None,
4848            },
4849            Node::ForeignItem(it) => match it.kind {
4850                ForeignItemKind::Static(ty, ..) => Some(ty),
4851                _ => None,
4852            },
4853            _ => None,
4854        }
4855    }
4856
4857    pub fn alias_ty(self) -> Option<&'hir Ty<'hir>> {
4858        match self {
4859            Node::Item(Item { kind: ItemKind::TyAlias(_, _, ty), .. }) => Some(ty),
4860            _ => None,
4861        }
4862    }
4863
4864    #[inline]
4865    pub fn associated_body(&self) -> Option<(LocalDefId, BodyId)> {
4866        match self {
4867            Node::Item(Item {
4868                owner_id,
4869                kind:
4870                    ItemKind::Const(_, _, _, body)
4871                    | ItemKind::Static(.., body)
4872                    | ItemKind::Fn { body, .. },
4873                ..
4874            })
4875            | Node::TraitItem(TraitItem {
4876                owner_id,
4877                kind:
4878                    TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
4879                ..
4880            })
4881            | Node::ImplItem(ImplItem {
4882                owner_id,
4883                kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
4884                ..
4885            }) => Some((owner_id.def_id, *body)),
4886
4887            Node::Item(Item {
4888                owner_id, kind: ItemKind::GlobalAsm { asm: _, fake_body }, ..
4889            }) => Some((owner_id.def_id, *fake_body)),
4890
4891            Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. }) => {
4892                Some((*def_id, *body))
4893            }
4894
4895            Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
4896            Node::ConstBlock(constant) => Some((constant.def_id, constant.body)),
4897
4898            _ => None,
4899        }
4900    }
4901
4902    pub fn body_id(&self) -> Option<BodyId> {
4903        Some(self.associated_body()?.1)
4904    }
4905
4906    pub fn generics(self) -> Option<&'hir Generics<'hir>> {
4907        match self {
4908            Node::ForeignItem(ForeignItem {
4909                kind: ForeignItemKind::Fn(_, _, generics), ..
4910            })
4911            | Node::TraitItem(TraitItem { generics, .. })
4912            | Node::ImplItem(ImplItem { generics, .. }) => Some(generics),
4913            Node::Item(item) => item.kind.generics(),
4914            _ => None,
4915        }
4916    }
4917
4918    pub fn as_owner(self) -> Option<OwnerNode<'hir>> {
4919        match self {
4920            Node::Item(i) => Some(OwnerNode::Item(i)),
4921            Node::ForeignItem(i) => Some(OwnerNode::ForeignItem(i)),
4922            Node::TraitItem(i) => Some(OwnerNode::TraitItem(i)),
4923            Node::ImplItem(i) => Some(OwnerNode::ImplItem(i)),
4924            Node::Crate(i) => Some(OwnerNode::Crate(i)),
4925            Node::Synthetic => Some(OwnerNode::Synthetic),
4926            _ => None,
4927        }
4928    }
4929
4930    pub fn fn_kind(self) -> Option<FnKind<'hir>> {
4931        match self {
4932            Node::Item(i) => match i.kind {
4933                ItemKind::Fn { ident, sig, generics, .. } => {
4934                    Some(FnKind::ItemFn(ident, generics, sig.header))
4935                }
4936                _ => None,
4937            },
4938            Node::TraitItem(ti) => match ti.kind {
4939                TraitItemKind::Fn(ref sig, _) => Some(FnKind::Method(ti.ident, sig)),
4940                _ => None,
4941            },
4942            Node::ImplItem(ii) => match ii.kind {
4943                ImplItemKind::Fn(ref sig, _) => Some(FnKind::Method(ii.ident, sig)),
4944                _ => None,
4945            },
4946            Node::Expr(e) => match e.kind {
4947                ExprKind::Closure { .. } => Some(FnKind::Closure),
4948                _ => None,
4949            },
4950            _ => None,
4951        }
4952    }
4953
4954    expect_methods_self! {
4955        expect_param,         &'hir Param<'hir>,        Node::Param(n),        n;
4956        expect_item,          &'hir Item<'hir>,         Node::Item(n),         n;
4957        expect_foreign_item,  &'hir ForeignItem<'hir>,  Node::ForeignItem(n),  n;
4958        expect_trait_item,    &'hir TraitItem<'hir>,    Node::TraitItem(n),    n;
4959        expect_impl_item,     &'hir ImplItem<'hir>,     Node::ImplItem(n),     n;
4960        expect_variant,       &'hir Variant<'hir>,      Node::Variant(n),      n;
4961        expect_field,         &'hir FieldDef<'hir>,     Node::Field(n),        n;
4962        expect_anon_const,    &'hir AnonConst,          Node::AnonConst(n),    n;
4963        expect_inline_const,  &'hir ConstBlock,         Node::ConstBlock(n),   n;
4964        expect_expr,          &'hir Expr<'hir>,         Node::Expr(n),         n;
4965        expect_expr_field,    &'hir ExprField<'hir>,    Node::ExprField(n),    n;
4966        expect_stmt,          &'hir Stmt<'hir>,         Node::Stmt(n),         n;
4967        expect_path_segment,  &'hir PathSegment<'hir>,  Node::PathSegment(n),  n;
4968        expect_ty,            &'hir Ty<'hir>,           Node::Ty(n),           n;
4969        expect_assoc_item_constraint,  &'hir AssocItemConstraint<'hir>,  Node::AssocItemConstraint(n),  n;
4970        expect_trait_ref,     &'hir TraitRef<'hir>,     Node::TraitRef(n),     n;
4971        expect_opaque_ty,     &'hir OpaqueTy<'hir>,     Node::OpaqueTy(n),     n;
4972        expect_pat,           &'hir Pat<'hir>,          Node::Pat(n),          n;
4973        expect_pat_field,     &'hir PatField<'hir>,     Node::PatField(n),     n;
4974        expect_arm,           &'hir Arm<'hir>,          Node::Arm(n),          n;
4975        expect_block,         &'hir Block<'hir>,        Node::Block(n),        n;
4976        expect_let_stmt,      &'hir LetStmt<'hir>,      Node::LetStmt(n),      n;
4977        expect_ctor,          &'hir VariantData<'hir>,  Node::Ctor(n),         n;
4978        expect_lifetime,      &'hir Lifetime,           Node::Lifetime(n),     n;
4979        expect_generic_param, &'hir GenericParam<'hir>, Node::GenericParam(n), n;
4980        expect_crate,         &'hir Mod<'hir>,          Node::Crate(n),        n;
4981        expect_infer,         &'hir InferArg,           Node::Infer(n),        n;
4982        expect_closure,       &'hir Closure<'hir>, Node::Expr(Expr { kind: ExprKind::Closure(n), .. }), n;
4983    }
4984}
4985
4986// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
4987#[cfg(target_pointer_width = "64")]
4988mod size_asserts {
4989    use rustc_data_structures::static_assert_size;
4990
4991    use super::*;
4992    // tidy-alphabetical-start
4993    static_assert_size!(Block<'_>, 48);
4994    static_assert_size!(Body<'_>, 24);
4995    static_assert_size!(Expr<'_>, 64);
4996    static_assert_size!(ExprKind<'_>, 48);
4997    static_assert_size!(FnDecl<'_>, 40);
4998    static_assert_size!(ForeignItem<'_>, 96);
4999    static_assert_size!(ForeignItemKind<'_>, 56);
5000    static_assert_size!(GenericArg<'_>, 16);
5001    static_assert_size!(GenericBound<'_>, 64);
5002    static_assert_size!(Generics<'_>, 56);
5003    static_assert_size!(Impl<'_>, 40);
5004    static_assert_size!(ImplItem<'_>, 88);
5005    static_assert_size!(ImplItemKind<'_>, 40);
5006    static_assert_size!(Item<'_>, 88);
5007    static_assert_size!(ItemKind<'_>, 64);
5008    static_assert_size!(LetStmt<'_>, 72);
5009    static_assert_size!(Param<'_>, 32);
5010    static_assert_size!(Pat<'_>, 80);
5011    static_assert_size!(PatKind<'_>, 56);
5012    static_assert_size!(Path<'_>, 40);
5013    static_assert_size!(PathSegment<'_>, 48);
5014    static_assert_size!(QPath<'_>, 24);
5015    static_assert_size!(Res, 12);
5016    static_assert_size!(Stmt<'_>, 32);
5017    static_assert_size!(StmtKind<'_>, 16);
5018    static_assert_size!(TraitImplHeader<'_>, 48);
5019    static_assert_size!(TraitItem<'_>, 88);
5020    static_assert_size!(TraitItemKind<'_>, 48);
5021    static_assert_size!(Ty<'_>, 48);
5022    static_assert_size!(TyKind<'_>, 32);
5023    // tidy-alphabetical-end
5024}
5025
5026#[cfg(test)]
5027mod tests;