pub enum Res<Id = HirId> {
Def(DefKind, DefId),
PrimTy(PrimTy),
SelfTy {
trait_: Option<DefId>,
alias_to: Option<(DefId, bool)>,
},
ToolMod,
SelfCtor(DefId),
Local(Id),
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 Res
s will be:
str
will resolve toRes::PrimTy
;String
will resolve toRes::Def
, and theRes
will include theDefId
forString
as defined in the standard library;String::from
will also resolve toRes::Def
, with theDefId
pointing toString::from
;s
will resolve toRes::Local
;- the call to
str_to_string
will resolve toRes::Def
, with theDefId
pointing to the definition ofstr_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.
SelfTy
Fields
The Self
type, optionally with the DefId
of the trait it belongs to and
optionally with the DefId
of the item introducing the Self
type alias.
Belongs to the type namespace.
Examples:
struct Bar(Box<Self>);
// `Res::SelfTy { trait_: None, alias_of: Some(Bar) }`
trait Foo {
fn foo() -> Box<Self>;
// `Res::SelfTy { trait_: Some(Foo), alias_of: None }`
}
impl Bar {
fn blah() {
let _: Self;
// `Res::SelfTy { trait_: None, alias_of: Some(::{impl#0}) }`
}
}
impl Foo for Bar {
fn foo() -> Box<Self> {
// `Res::SelfTy { trait_: Some(Foo), alias_of: Some(::{impl#1}) }`
let _: Self;
// `Res::SelfTy { trait_: Some(Foo), alias_of: Some(::{impl#1}) }`
todo!()
}
}
See also Res::SelfCtor
.
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>()];
}
ToolMod
A tool attribute module; e.g., the rustfmt
in #[rustfmt::skip]
.
Belongs to the type namespace.
SelfCtor(DefId)
The Self
constructor, along with the DefId
of the impl it is associated with.
Belongs to the value namespace.
See also Res::SelfTy
.
Local(Id)
A local variable or function parameter.
Belongs to the value 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
sourceimpl<Id> Res<Id>
impl<Id> Res<Id>
sourcepub fn def_id(&self) -> DefIdwhere
Id: Debug,
pub fn def_id(&self) -> DefIdwhere
Id: Debug,
Return the DefId
of this Def
if it has an ID, else panic.
sourcepub fn opt_def_id(&self) -> Option<DefId>
pub fn opt_def_id(&self) -> Option<DefId>
Return Some(..)
with the DefId
of this Res
if it has a ID, else None
.
sourcepub fn mod_def_id(&self) -> Option<DefId>
pub fn mod_def_id(&self) -> Option<DefId>
Return the DefId
of this Res
if it represents a module.
sourcepub fn descr(&self) -> &'static str
pub fn descr(&self) -> &'static str
A human readable name for the res kind (“function”, “module”, etc.).
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Res<R>
pub fn apply_id<R, E>(
self,
map: impl FnMut(Id) -> Result<R, E>
) -> Result<Res<R>, E>
pub fn expect_non_local<OtherId>(self) -> Res<OtherId>
pub fn macro_kind(self) -> Option<MacroKind>
sourcepub fn matches_ns(&self, ns: Namespace) -> bool
pub fn matches_ns(&self, ns: Namespace) -> bool
Always returns true
if self
is Res::Err
sourcepub fn expected_in_tuple_struct_pat(&self) -> bool
pub fn expected_in_tuple_struct_pat(&self) -> bool
Returns whether such a resolved path can occur in a tuple struct/variant pattern
sourcepub fn expected_in_unit_struct_pat(&self) -> bool
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
sourceimpl<Id, __CTX> HashStable<__CTX> for Res<Id>where
__CTX: HashStableContext,
Id: HashStable<__CTX>,
impl<Id, __CTX> HashStable<__CTX> for Res<Id>where
__CTX: HashStableContext,
Id: HashStable<__CTX>,
fn hash_stable(&self, __hcx: &mut __CTX, __hasher: &mut StableHasher)
impl<Id: Copy> Copy for Res<Id>
impl<Id: Eq> Eq for Res<Id>
impl<Id> StructuralEq for Res<Id>
impl<Id> StructuralPartialEq for Res<Id>
Auto Trait Implementations
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
sourceimpl<'tcx, T> ArenaAllocatable<'tcx, IsCopy> for Twhere
T: Copy,
impl<'tcx, T> ArenaAllocatable<'tcx, IsCopy> for Twhere
T: Copy,
fn allocate_on(self, arena: &'a Arena<'tcx>) -> &'a mut T
fn allocate_from_iter(
arena: &'a Arena<'tcx>,
iter: impl IntoIterator<Item = T>
) -> &'a mut [T]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
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.