pub enum RawConstantExpr {
Literal(Literal),
Adt(Option<VariantId>, Vec<ConstantExpr>),
Global(GlobalDeclRef),
TraitConst(TraitRef, TraitItemName),
Ref(Box<ConstantExpr>),
MutPtr(Box<ConstantExpr>),
Var(ConstGenericVarId),
FnPtr(FnPtr),
}
Expand description
A constant expression.
Only the Literal and Var cases are left in the final LLBC.
The other cases come from a straight translation from the MIR:
[Adt] case: It is a bit annoying, but rustc treats some ADT and tuple instances as constants when generating MIR:
- an enumeration with one variant and no fields is a constant.
- a structure with no field is a constant.
- sometimes, Rust stores the initialization of an ADT as a constant (if all the fields are constant) rather than as an aggregated value We later desugar those to regular ADTs, see [regularize_constant_adts.rs].
[Global] case: access to a global variable. We later desugar it to a separate statement.
[Ref] case: reference to a constant value. We later desugar it to a separate statement.
FnPtr case: a function pointer (to a top-level function).
Remark:
MIR seems to forbid more complex expressions like paths. For instance,
reading the constant a.b
is translated to { _1 = const a; _2 = (_1.0) }
.
Variants§
Literal(Literal)
Adt(Option<VariantId>, Vec<ConstantExpr>)
In most situations: Enumeration with one variant with no fields, structure with no fields, unit (encoded as a 0-tuple).
Less frequently: arbitrary ADT values.
We eliminate this case in a micro-pass.
Global(GlobalDeclRef)
The value is a top-level constant/static.
We eliminate this case in a micro-pass.
Remark: constants can actually have generic parameters.
struct V<const N: usize, T> {
x: [T; N],
}
impl<const N: usize, T> V<N, T> {
const LEN: usize = N; // This has generics <N, T>
}
fn use_v<const N: usize, T>(v: V<N, T>) {
let l = V::<N, T>::LEN; // We need to provided a substitution here
}
TraitConst(TraitRef, TraitItemName)
A trait constant.
Ex.:
impl Foo for Bar {
const C : usize = 32; // <-
}
Remark: trait constants can not be used in types, they are necessarily values.
Ref(Box<ConstantExpr>)
A shared reference to a constant value.
We eliminate this case in a micro-pass.
MutPtr(Box<ConstantExpr>)
A mutable pointer to a mutable static.
We eliminate this case in a micro-pass.
Var(ConstGenericVarId)
A const generic var
FnPtr(FnPtr)
Function pointer
Implementations§
source§impl RawConstantExpr
impl RawConstantExpr
pub fn variant_name(&self) -> &'static str
source§impl RawConstantExpr
impl RawConstantExpr
pub fn as_literal(&self) -> Option<&Literal>
pub fn as_adt(&self) -> Option<(&Option<VariantId>, &Vec<ConstantExpr>)>
pub fn as_global(&self) -> Option<&GlobalDeclRef>
pub fn as_trait_const(&self) -> Option<(&TraitRef, &TraitItemName)>
pub fn as_ref(&self) -> Option<&Box<ConstantExpr>>
pub fn as_mut_ptr(&self) -> Option<&Box<ConstantExpr>>
pub fn as_var(&self) -> Option<&ConstGenericVarId>
pub fn as_fn_ptr(&self) -> Option<&FnPtr>
source§impl RawConstantExpr
impl RawConstantExpr
pub fn as_literal_mut(&mut self) -> Option<&mut Literal>
pub fn as_adt_mut( &mut self, ) -> Option<(&mut Option<VariantId>, &mut Vec<ConstantExpr>)>
pub fn as_global_mut(&mut self) -> Option<&mut GlobalDeclRef>
pub fn as_trait_const_mut( &mut self, ) -> Option<(&mut TraitRef, &mut TraitItemName)>
pub fn as_ref_mut(&mut self) -> Option<&mut Box<ConstantExpr>>
pub fn as_mut_ptr_mut(&mut self) -> Option<&mut Box<ConstantExpr>>
pub fn as_var_mut(&mut self) -> Option<&mut ConstGenericVarId>
pub fn as_fn_ptr_mut(&mut self) -> Option<&mut FnPtr>
Trait Implementations§
source§impl Clone for RawConstantExpr
impl Clone for RawConstantExpr
source§fn clone(&self) -> RawConstantExpr
fn clone(&self) -> RawConstantExpr
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for RawConstantExpr
impl Debug for RawConstantExpr
source§impl<'de> Deserialize<'de> for RawConstantExpr
impl<'de> Deserialize<'de> for RawConstantExpr
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl DriveMut for RawConstantExpr
impl DriveMut for RawConstantExpr
source§impl<C: AstFormatter> FmtWithCtx<C> for RawConstantExpr
impl<C: AstFormatter> FmtWithCtx<C> for RawConstantExpr
source§impl PartialEq for RawConstantExpr
impl PartialEq for RawConstantExpr
source§impl Serialize for RawConstantExpr
impl Serialize for RawConstantExpr
impl Eq for RawConstantExpr
impl StructuralPartialEq for RawConstantExpr
Auto Trait Implementations§
impl Freeze for RawConstantExpr
impl RefUnwindSafe for RawConstantExpr
impl Send for RawConstantExpr
impl Sync for RawConstantExpr
impl Unpin for RawConstantExpr
impl UnwindSafe for RawConstantExpr
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<I, T> ExtractContext<I, ()> for T
impl<I, T> ExtractContext<I, ()> for T
§fn extract_context(self, _original_input: I)
fn extract_context(self, _original_input: I)
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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