DefKind in rustc_hir::def - Rust
Description: What kind of definition something is; e.g., `mod` vs `struct`. `enum DefPathData` may need to be updated if a new variant is added here.

Skip to main content

## DefKind

# Enum DefKind

Source

    pub enum DefKind {
    Show 31 variants    Mod,
        Struct,
        Union,
        Enum,
        Variant,
        Trait,
        TyAlias,
        ForeignTy,
        TraitAlias,
        AssocTy,
        TyParam,
        Fn,
        Const {
            is_type_const: bool,
        },
        ConstParam,
        Static {
            safety: Safety,
            mutability: Mutability,
            nested: bool,
        },
        Ctor(CtorOf, CtorKind),
        AssocFn,
        AssocConst {
            is_type_const: bool,
        },
        Macro(MacroKinds),
        ExternCrate,
        Use,
        ForeignMod,
        AnonConst,
        InlineConst,
        OpaqueTy,
        Field,
        LifetimeParam,
        GlobalAsm,
        Impl {
            of_trait: bool,
        },
        Closure,
        SyntheticCoroutineBody,
    }

Expand description

What kind of definition something is; e.g., `mod` vs `struct`. `enum DefPathData` may need to be updated if a new variant is added here.

## Variants§

§

### Mod

§

### Struct

Refers to the struct itself, `DefKind::Ctor` refers to its constructor if it exists.

§

### Union

§

### Enum

§

### Variant

Refers to the variant itself, `DefKind::Ctor` refers to its constructor if it exists.

§

### Trait

§

### TyAlias

Type alias: `type Foo = Bar;`

§

### ForeignTy

Type from an `extern` block.

§

### TraitAlias

Trait alias: `trait IntIterator = Iterator<Item = i32>;`

§

### AssocTy

Associated type: `trait MyTrait { type Assoc; }`

§

### TyParam

Type parameter: the `T` in `struct Vec<T> { ... }`

§

### Fn

§

### Const

#### Fields

§`is_type_const: bool`
§

### ConstParam

Constant generic parameter: `struct Foo<const N: usize> { ... }`

§

### Static

#### Fields

§`safety: Safety`

Whether it’s a `unsafe static`, `safe static` (inside extern only) or just a `static`.

§`mutability: Mutability`

Whether it’s a `static mut` or just a `static`.

§`nested: bool`

Whether it’s an anonymous static generated for nested allocations.

§

### Ctor(CtorOf, CtorKind)

Refers to the struct or enum variant’s constructor.

    struct S;
    let x = S; // S in the value namespace is a Ctor

The reason `Ctor` exists in addition to `DefKind::Struct` and `DefKind::Variant` is because structs and enum variants exist in the type namespace, whereas struct and enum variant constructors exist in the value namespace.

You may wonder why enum variants exist in the type namespace as opposed to the value namespace. Check out RFC 2593 for intuition on why that is.

§

### AssocFn

Associated function: `impl MyStruct { fn associated() {} }` or `trait Foo { fn associated() {} }`

§

### AssocConst

Associated constant: `trait MyTrait { const ASSOC: usize; }`

#### Fields

§`is_type_const: bool`
§

### Macro(MacroKinds)

§

### ExternCrate

§

### Use

§

### ForeignMod

An `extern` block.

§

### AnonConst

Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`.

Not all anon-consts are actually still relevant in the HIR. We lower trivial const-arguments directly to `hir::ConstArgKind::Path`, at which point the definition for the anon-const ends up unused and incomplete.

We do not provide any a `Span` for the definition and pretty much all other queries also ICE when using this `DefId`. Given that the `DefId` of such constants should only be reachable by iterating all definitions of a given crate, you should not have to worry about this.

§

### InlineConst

An inline constant, e.g. `const { 1 + 2 }`

§

### OpaqueTy

Opaque type, aka `impl Trait`.

§

### Field

A field in a struct, enum or union. e.g.

- `bar` in `struct Foo { bar: u8 }`
- `Foo::Bar::0` in `enum Foo { Bar(u8) }`

§

### LifetimeParam

Lifetime parameter: the `'a` in `struct Foo<'a> { ... }`

§

### GlobalAsm

A use of `global_asm!`.

§

### Impl

#### Fields

§`of_trait: bool`
§

### Closure

A closure, coroutine, or coroutine-closure.

These are all represented with the same `ExprKind::Closure` in the AST and HIR, which makes it difficult to distinguish these during def collection. Therefore, we treat them all the same, and code which needs to distinguish them can match on `hir::ClosureKind` or `type_of`.

§

### SyntheticCoroutineBody

The definition of a synthetic coroutine body created by the lowering of a coroutine-closure, such as an async closure.

## Implementations§

Source§

### impl DefKind

Source

#### pub fn descr(self, def_id: DefId) -> &'static str

Get an English description for the item’s kind.

If you have access to `TyCtxt`, use `TyCtxt::def_descr` or `TyCtxt::def_kind_descr` instead, because they give better information for coroutines and associated functions.

Source

#### pub fn article(&self) -> &'static str

Gets an English article for the definition.

If you have access to `TyCtxt`, use `TyCtxt::def_descr_article` or `TyCtxt::def_kind_descr_article` instead, because they give better information for coroutines and associated functions.

Source

#### pub fn ns(&self) -> Option<Namespace>

Source

#### pub fn def_path_data(self, name: Option<Symbol>) -> DefPathData

Source

#### pub fn is_assoc(self) -> bool

Source

#### pub fn is_module_like(self) -> bool

This is a “module” in name resolution sense.

Source

#### pub fn is_adt(self) -> bool

Source

#### pub fn is_fn_like(self) -> bool

Source

#### pub fn has_generics(self) -> bool

Whether the corresponding item has generic parameters, i.e. the `generics_of` query works.

Source

#### pub fn has_codegen_attrs(self) -> bool

Whether `query get_codegen_attrs` should be used with this definition.

Source

#### pub fn is_typeck_child(self) -> bool

Returns `true` if `self` is a kind of definition that does not have its own type-checking context, i.e. closure, coroutine or inline const.

## Trait Implementations§

Source§

### impl Clone for DefKind

Source§

#### fn clone(&self) -> DefKind

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

#### fn clone_from(&mut self, source: &Self)

Performs copy-assignment from `source`. Read more
Source§

### impl Copy for DefKind

Source§

### impl Debug for DefKind

Source§

#### fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

### impl<__D: SpanDecoder> Decodable<__D> for DefKind

Source§

#### fn decode(__decoder: &mut __D) -> Self

Source§

### impl<__E: SpanEncoder> Encodable<__E> for DefKind

Source§

#### fn encode(&self, __encoder: &mut __E)

Source§

### impl Eq for DefKind

Source§

### impl Hash for DefKind

Source§

#### fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given `Hasher`. Read more
1.3.0 · Source§

#### fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given `Hasher`. Read more
Source§

### impl PartialEq for DefKind

Source§

#### fn eq(&self, other: &DefKind) -> bool

Tests for `self` and `other` values to be equal, and is used by `==`.
1.0.0 (const: unstable) · Source§

#### fn ne(&self, other: &Rhs) -> bool

Tests for `!=`. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

### impl StableHash for DefKind

Source§

#### fn stable_hash<__Hcx: StableHashCtxt>( &self, __hcx: &mut __Hcx, __hasher: &mut StableHasher, )

Source§

### impl StructuralPartialEq for DefKind

## Auto Trait Implementations§

§

### impl DynSend for DefKind

§

### impl DynSync for DefKind

§

### impl Freeze for DefKind

§

### impl RefUnwindSafe for DefKind

§

### impl Send for DefKind

§

### impl Sync for DefKind

§

### impl Unpin for DefKind

§

### impl UnsafeUnpin for DefKind

§

### impl UnwindSafe for DefKind

## Blanket Implementations§

Source§

### impl<T> Aligned for T

Source§

#### const ALIGN: Alignment

Alignment of `Self`.
Source§

### impl<T> Any for T
where T: 'static + ?Sized,

Source§

#### fn type_id(&self) -> TypeId

Gets the `TypeId` of `self`. Read more
Source§

### impl<T> AnyEq for T
where T: Any + PartialEq,

Source§

#### fn equals(&self, other: &(dyn Any + 'static)) -> bool

Source§

#### fn as_any(&self) -> &(dyn Any + 'static)

Source§

### impl<'tcx, T> ArenaAllocatable<'tcx, IsCopy> for T
where T: Copy,

Source§

#### fn allocate_on(self, arena: &'tcx Arena<'tcx>) -> &'tcx mut T

Source§

#### fn allocate_from_iter( arena: &'tcx Arena<'tcx>, iter: impl IntoIterator<Item = T>, ) -> &'tcx mut [T]

Source§

### impl<T> Borrow<T> for T
where T: ?Sized,

Source§

#### fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

### impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

#### fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

### impl<T> CloneToUninit for T
where T: Clone,

Source§

#### unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (`clone_to_uninit`)
Performs copy-assignment from `self` to `dest`. Read more
Source§

### impl<T> DynClone for T
where T: Clone,

Source§

#### fn __clone_box(&self, _: Private) -> *mut ()

Source§

### impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

#### fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

### impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

#### fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

### impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

#### fn equivalent(&self, key: &K) -> bool

Compare self to `key` and return `true` if they are equal.
Source§

### impl<T> ErasedDestructor for T
where T: 'static,

Source§

### impl<T> From<T> for T

Source§

#### fn from(t: T) -> T

Returns the argument unchanged.

Source§

### impl<T> Instrument for T

Source§

#### fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided `Span`, returning an `Instrumented` wrapper. Read more
Source§

#### fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current `Span`, returning an `Instrumented` wrapper. Read more
Source§

### impl<T, U> Into<U> for T
where U: From<T>,

Source§

#### fn into(self) -> U

Calls `U::from(self)`.

That is, this conversion is whatever the implementation of `From<T> for U` chooses to do.

Source§

### impl<T> IntoEither for T

Source§

#### fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts `self` into a `Left` variant of `Either<Self, Self>` if `into_left` is `true`. Converts `self` into a `Right` variant of `Either<Self, Self>` otherwise. Read more
Source§

#### fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts `self` into a `Left` variant of `Either<Self, Self>` if `into_left(&self)` returns `true`. Converts `self` into a `Right` variant of `Either<Self, Self>` otherwise. Read more
Source§

### impl<T> Pointable for T

Source§

#### const ALIGN: usize

The alignment of pointer.
Source§

#### type Init = T

The type for initializers.
Source§

#### unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

#### unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

#### unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

#### unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

### impl<T> Same for T

Source§

#### type Output = T

Should always be `Self`
Source§

### impl<T> ToOwned for T
where T: Clone,

Source§

#### type Owned = T

The resulting type after obtaining ownership.
Source§

#### fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

#### fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

### impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

#### type Error = Infallible

The type returned in the event of a conversion error.
Source§

#### fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

### impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

#### type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

#### fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

### impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

#### fn vzip(self) -> V

Source§

### impl<T> WithSubscriber for T

Source§

#### fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided `Subscriber` to this type, returning a `WithDispatch` wrapper. Read more
Source§

#### fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default `Subscriber` to this type, returning a `WithDispatch` wrapper. Read more

## Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain `repr(...)` attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 3 bytes

Size for each variant:

- `Mod`: 0 bytes
- `Struct`: 0 bytes
- `Union`: 0 bytes
- `Enum`: 0 bytes
- `Variant`: 0 bytes
- `Trait`: 0 bytes
- `TyAlias`: 0 bytes
- `ForeignTy`: 0 bytes
- `TraitAlias`: 0 bytes
- `AssocTy`: 0 bytes
- `TyParam`: 0 bytes
- `Fn`: 0 bytes
- `Const`: 2 bytes
- `ConstParam`: 0 bytes
- `Static`: 3 bytes
- `Ctor`: 3 bytes
- `AssocFn`: 0 bytes
- `AssocConst`: 2 bytes
- `Macro`: 2 bytes
- `ExternCrate`: 0 bytes
- `Use`: 0 bytes
- `ForeignMod`: 0 bytes
- `AnonConst`: 0 bytes
- `InlineConst`: 0 bytes
- `OpaqueTy`: 0 bytes
- `Field`: 0 bytes
- `LifetimeParam`: 0 bytes
- `GlobalAsm`: 0 bytes
- `Impl`: 2 bytes
- `Closure`: 0 bytes
- `SyntheticCoroutineBody`: 0 bytes