Enum rustc_hir::def::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

Fields

§trait_: DefId

The trait this Self is a generic parameter for.

The Self type, as used within a trait.

Belongs to the type namespace.

See the examples on Res::SelfTyAlias for details.

§

SelfTyAlias

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?

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.

§

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
1.0.0 · 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
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<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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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> 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<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut T

source§

fn allocate_from_iter<'a>( arena: &'a Arena<'tcx>, iter: impl IntoIterator<Item = T> ) -> &'a 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<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
§

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

§

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

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

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

§

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> Same for T

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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>,

§

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

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

§

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.