rustc_hir::def

Enum Res

Source
pub enum Res<Id = HirId> {
    Def(DefKind, DefId),
    PrimTy(PrimTy),
    SelfTyParam {
        trait_: DefId,
    },
    SelfTyAlias {
        alias_to: DefId,
        forbid_generic: bool,
        is_trait_impl: bool,
    },
    SelfCtor(DefId),
    Local(Id),
    ToolMod,
    NonMacroAttr(NonMacroAttrKind),
    Err,
}
Expand description

The resolution of a path or export.

For every path or identifier in Rust, the compiler must determine what the path refers to. This process is called name resolution, and Res is the primary result of name resolution.

For example, everything prefixed with /* Res */ in this example has an associated Res:

fn str_to_string(s: & /* Res */ str) -> /* Res */ String {
    /* Res */ String::from(/* Res */ s)
}

/* Res */ str_to_string("hello");

The associated Ress will be:

  • str will resolve to Res::PrimTy;
  • String will resolve to Res::Def, and the Res will include the DefId for String as defined in the standard library;
  • String::from will also resolve to Res::Def, with the DefId pointing to String::from;
  • s will resolve to Res::Local;
  • the call to str_to_string will resolve to Res::Def, with the DefId pointing to the definition of str_to_string in the current crate.

Variants§

§

Def(DefKind, DefId)

Definition having a unique ID (DefId), corresponds to something defined in user code.

Not bound to a specific namespace.

§

PrimTy(PrimTy)

A primitive type such as i32 or str.

Belongs to the type namespace.

§

SelfTyParam

The Self type, as used within a trait.

Belongs to the type namespace.

See the examples on Res::SelfTyAlias for details.

Fields

§trait_: DefId

The trait this Self is a generic parameter for.

§

SelfTyAlias

The Self type, as used somewhere other than within a trait.

Belongs to the type namespace.

Examples:

struct Bar(Box<Self>); // SelfTyAlias

trait Foo {
    fn foo() -> Box<Self>; // SelfTyParam
}

impl Bar {
    fn blah() {
        let _: Self; // SelfTyAlias
    }
}

impl Foo for Bar {
    fn foo() -> Box<Self> { // SelfTyAlias
        let _: Self;        // SelfTyAlias

        todo!()
    }
}

See also Res::SelfCtor.

Fields

§alias_to: DefId

The item introducing the Self type alias. Can be used in the type_of query to get the underlying type.

§forbid_generic: bool

Whether the Self type is disallowed from mentioning generics (i.e. when used in an anonymous constant).

HACK(min_const_generics): self types also have an optional requirement to not mention any generic parameters to allow the following with min_const_generics:

impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }

struct Bar([u8; baz::<Self>()]);
const fn baz<T>() -> usize { 10 }

We do however allow Self in repeat expression even if it is generic to not break code which already works on stable while causing the const_evaluatable_unchecked future compat lint:

fn foo<T>() {
    let _bar = [1_u8; std::mem::size_of::<*mut T>()];
}
§is_trait_impl: bool

Is this within an impl Foo for bar?

§

SelfCtor(DefId)

The Self constructor, along with the DefId of the impl it is associated with.

Belongs to the value namespace.

See also Res::SelfTyParam and Res::SelfTyAlias.

§

Local(Id)

A local variable or function parameter.

Belongs to the value namespace.

§

ToolMod

A tool attribute module; e.g., the rustfmt in #[rustfmt::skip].

Belongs to the type namespace.

§

NonMacroAttr(NonMacroAttrKind)

An attribute that is not implemented via macro. E.g., #[inline] and #[rustfmt::skip], which are essentially directives, as opposed to #[test], which is a builtin macro.

Belongs to the macro namespace.

§

Err

Name resolution failed. We use a dummy Res variant so later phases of the compiler won’t crash and can instead report more errors.

Not bound to a specific namespace.

Implementations§

Source§

impl<Id> Res<Id>

Source

pub fn def_id(&self) -> DefId
where Id: Debug,

Return the DefId of this Def if it has an ID, else panic.

Source

pub fn opt_def_id(&self) -> Option<DefId>

Return Some(..) with the DefId of this Res if it has a ID, else None.

Source

pub fn mod_def_id(&self) -> Option<DefId>

Return the DefId of this Res if it represents a module.

Source

pub fn descr(&self) -> &'static str

A human readable name for the res kind (“function”, “module”, etc.).

Source

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

Gets an English article for the Res.

Source

pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Res<R>

Source

pub fn apply_id<R, E>( self, map: impl FnMut(Id) -> Result<R, E>, ) -> Result<Res<R>, E>

Source

pub fn expect_non_local<OtherId>(self) -> Res<OtherId>

Source

pub fn macro_kind(self) -> Option<MacroKind>

Source

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

Returns None if this is Res::Err

Source

pub fn matches_ns(&self, ns: Namespace) -> bool

Always returns true if self is Res::Err

Source

pub fn expected_in_tuple_struct_pat(&self) -> bool

Returns whether such a resolved path can occur in a tuple struct/variant pattern

Source

pub fn expected_in_unit_struct_pat(&self) -> bool

Returns whether such a resolved path can occur in a unit struct/variant pattern

Trait Implementations§

Source§

impl<Id: Clone> Clone for Res<Id>

Source§

fn clone(&self) -> Res<Id>

Returns a copy of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl<Id: Debug> Debug for Res<Id>

Source§

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

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

impl<Id, __D: SpanDecoder> Decodable<__D> for Res<Id>
where Id: Decodable<__D>,

Source§

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

Source§

impl<Id, __E: SpanEncoder> Encodable<__E> for Res<Id>
where Id: Encodable<__E>,

Source§

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

Source§

impl<Id: Hash> Hash for Res<Id>

Source§

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

Feeds this value into the given Hasher. Read more
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<Id, __CTX> HashStable<__CTX> for Res<Id>
where __CTX: HashStableContext, Id: HashStable<__CTX>,

Source§

fn hash_stable(&self, __hcx: &mut __CTX, __hasher: &mut StableHasher)

Source§

impl<Id: PartialEq> PartialEq for Res<Id>

Source§

fn eq(&self, other: &Res<Id>) -> bool

Tests for self and other values to be equal, and is used by ==.
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<Id: Copy> Copy for Res<Id>

Source§

impl<Id: Eq> Eq for Res<Id>

Source§

impl<Id> StructuralPartialEq for Res<Id>

Auto Trait Implementations§

§

impl<Id> DynSend for Res<Id>
where Id: DynSend,

§

impl<Id> DynSync for Res<Id>
where Id: DynSync,

§

impl<Id> Freeze for Res<Id>
where Id: Freeze,

§

impl<Id> RefUnwindSafe for Res<Id>
where Id: RefUnwindSafe,

§

impl<Id> Send for Res<Id>
where Id: Send,

§

impl<Id> Sync for Res<Id>
where Id: Sync,

§

impl<Id> Unpin for Res<Id>
where Id: Unpin,

§

impl<Id> UnwindSafe for Res<Id>
where Id: UnwindSafe,

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<'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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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

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> 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
Source§

impl<'a, T> Captures<'a> for T
where T: ?Sized,

Layout§

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.