Skip to main content

rustc_middle/ty/consts/
valtree.rs

1use std::fmt;
2use std::ops::Deref;
3
4use rustc_abi::{FIRST_VARIANT, VariantIdx};
5use rustc_data_structures::intern::Interned;
6use rustc_hir::def::Namespace;
7use rustc_macros::{
8    HashStable, Lift, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable, extension,
9};
10
11use super::ScalarInt;
12use crate::mir::interpret::{ErrorHandled, Scalar};
13use crate::ty::print::{FmtPrinter, PrettyPrinter};
14use crate::ty::{self, Ty, TyCtxt, ValTreeKind};
15
16impl<'tcx> ValTreeKindExt<'tcx> for ty::ValTreeKind<TyCtxt<'tcx>> {
    fn try_to_scalar(&self) -> Option<Scalar> {
        self.try_to_leaf().map(Scalar::Int)
    }
}#[extension(pub trait ValTreeKindExt<'tcx>)]
17impl<'tcx> ty::ValTreeKind<TyCtxt<'tcx>> {
18    fn try_to_scalar(&self) -> Option<Scalar> {
19        self.try_to_leaf().map(Scalar::Int)
20    }
21}
22
23/// An interned valtree. Use this rather than `ValTreeKind`, whenever possible.
24///
25/// See the docs of [`ty::ValTreeKind`] or the [dev guide] for an explanation of this type.
26///
27/// [dev guide]: https://rustc-dev-guide.rust-lang.org/mir/index.html#valtrees
28#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for ValTree<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for ValTree<'tcx> {
    #[inline]
    fn clone(&self) -> ValTree<'tcx> {
        let _:
                ::core::clone::AssertParamIsClone<Interned<'tcx,
                ty::ValTreeKind<TyCtxt<'tcx>>>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for ValTree<'tcx> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.0, state)
    }
}Hash, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for ValTree<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _:
                ::core::cmp::AssertParamIsEq<Interned<'tcx,
                ty::ValTreeKind<TyCtxt<'tcx>>>>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for ValTree<'tcx> {
    #[inline]
    fn eq(&self, other: &ValTree<'tcx>) -> bool { self.0 == other.0 }
}PartialEq)]
29#[derive(const _: () =
    {
        impl<'tcx, '__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
            for ValTree<'tcx> {
            #[inline]
            fn hash_stable(&self,
                __hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    ValTree(ref __binding_0) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable)]
30// FIXME(mgca): Try not interning here. We already intern `ty::Const` which `ValTreeKind`
31// recurses through
32pub struct ValTree<'tcx>(pub(crate) Interned<'tcx, ty::ValTreeKind<TyCtxt<'tcx>>>);
33
34impl<'tcx> ValTree<'tcx> {
35    /// Returns the zero-sized valtree: `Branch([])`.
36    pub fn zst(tcx: TyCtxt<'tcx>) -> Self {
37        tcx.consts.valtree_zst
38    }
39
40    pub fn is_zst(self) -> bool {
41        #[allow(non_exhaustive_omitted_patterns)] match *self {
    ty::ValTreeKind::Branch(consts) if consts.is_empty() => true,
    _ => false,
}matches!(*self, ty::ValTreeKind::Branch(consts) if consts.is_empty())
42    }
43
44    pub fn from_raw_bytes(tcx: TyCtxt<'tcx>, bytes: &[u8]) -> Self {
45        let branches = bytes.iter().map(|&b| {
46            ty::Const::new_value(tcx, Self::from_scalar_int(tcx, b.into()), tcx.types.u8)
47        });
48        Self::from_branches(tcx, branches)
49    }
50
51    pub fn from_branches(
52        tcx: TyCtxt<'tcx>,
53        branches: impl IntoIterator<Item = ty::Const<'tcx>>,
54    ) -> Self {
55        tcx.intern_valtree(ty::ValTreeKind::Branch(
56            tcx.mk_const_list_from_iter(branches.into_iter()),
57        ))
58    }
59
60    pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt) -> Self {
61        tcx.intern_valtree(ty::ValTreeKind::Leaf(i))
62    }
63}
64
65impl<'tcx> Deref for ValTree<'tcx> {
66    type Target = &'tcx ty::ValTreeKind<TyCtxt<'tcx>>;
67
68    #[inline]
69    fn deref(&self) -> &&'tcx ty::ValTreeKind<TyCtxt<'tcx>> {
70        &self.0.0
71    }
72}
73
74impl fmt::Debug for ValTree<'_> {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        (**self).fmt(f)
77    }
78}
79
80impl<'tcx> rustc_type_ir::inherent::IntoKind for ty::ValTree<'tcx> {
81    type Kind = ty::ValTreeKind<TyCtxt<'tcx>>;
82
83    fn kind(self) -> Self::Kind {
84        *self.0
85    }
86}
87
88/// `Ok(Err(ty))` indicates the constant was fine, but the valtree couldn't be constructed
89/// because the value contains something of type `ty` that is not valtree-compatible.
90/// The caller can then show an appropriate error; the query does not have the
91/// necessary context to give good user-facing errors for this case.
92pub type ConstToValTreeResult<'tcx> = Result<Result<ValTree<'tcx>, Ty<'tcx>>, ErrorHandled>;
93
94/// A type-level constant value.
95///
96/// Represents a typed, fully evaluated constant.
97/// Note that this is also used by pattern elaboration to represent values which cannot occur in types,
98/// such as raw pointers and floats.
99#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for Value<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for Value<'tcx> {
    #[inline]
    fn clone(&self) -> Value<'tcx> {
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<ValTree<'tcx>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for Value<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "Value", "ty",
            &self.ty, "valtree", &&self.valtree)
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for Value<'tcx> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.ty, state);
        ::core::hash::Hash::hash(&self.valtree, state)
    }
}Hash, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for Value<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<ValTree<'tcx>>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for Value<'tcx> {
    #[inline]
    fn eq(&self, other: &Value<'tcx>) -> bool {
        self.ty == other.ty && self.valtree == other.valtree
    }
}PartialEq)]
100#[derive(const _: () =
    {
        impl<'tcx, '__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
            for Value<'tcx> {
            #[inline]
            fn hash_stable(&self,
                __hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self {
                    Value { ty: ref __binding_0, valtree: ref __binding_1 } => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable, const _: () =
    {
        impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
            ::rustc_serialize::Encodable<__E> for Value<'tcx> {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    Value { ty: ref __binding_0, valtree: ref __binding_1 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                    }
                }
            }
        }
    };TyEncodable, const _: () =
    {
        impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
            ::rustc_serialize::Decodable<__D> for Value<'tcx> {
            fn decode(__decoder: &mut __D) -> Self {
                Value {
                    ty: ::rustc_serialize::Decodable::decode(__decoder),
                    valtree: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };TyDecodable, const _: () =
    {
        impl<'tcx>
            ::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
            for Value<'tcx> {
            fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
                __folder: &mut __F) -> Result<Self, __F::Error> {
                Ok(match self {
                        Value { ty: __binding_0, valtree: __binding_1 } => {
                            Value {
                                ty: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
                                        __folder)?,
                                valtree: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
                                        __folder)?,
                            }
                        }
                    })
            }
            fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
                __folder: &mut __F) -> Self {
                match self {
                    Value { ty: __binding_0, valtree: __binding_1 } => {
                        Value {
                            ty: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
                                __folder),
                            valtree: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
                                __folder),
                        }
                    }
                }
            }
        }
    };TypeFoldable, const _: () =
    {
        impl<'tcx>
            ::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
            for Value<'tcx> {
            fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
                __visitor: &mut __V) -> __V::Result {
                match *self {
                    Value { ty: ref __binding_0, valtree: ref __binding_1 } => {
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                    }
                }
                <__V::Result as ::rustc_middle::ty::VisitorResult>::output()
            }
        }
    };TypeVisitable, const _: () =
    {
        impl<'tcx, '__lifted>
            ::rustc_middle::ty::Lift<::rustc_middle::ty::TyCtxt<'__lifted>>
            for Value<'tcx> {
            type Lifted = Value<'__lifted>;
            fn lift_to_interner(self,
                __tcx: ::rustc_middle::ty::TyCtxt<'__lifted>)
                -> Option<Value<'__lifted>> {
                Some(match self {
                        Value { ty: __binding_0, valtree: __binding_1 } => {
                            Value {
                                ty: __tcx.lift(__binding_0)?,
                                valtree: __tcx.lift(__binding_1)?,
                            }
                        }
                    })
            }
        }
    };Lift)]
101pub struct Value<'tcx> {
102    pub ty: Ty<'tcx>,
103    pub valtree: ValTree<'tcx>,
104}
105
106impl<'tcx> Value<'tcx> {
107    /// Attempts to extract the raw bits from the constant.
108    ///
109    /// Fails if the value can't be represented as bits (e.g. because it is a reference
110    /// or an aggregate).
111    #[inline]
112    pub fn try_to_bits(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> Option<u128> {
113        let (ty::Bool | ty::Char | ty::Uint(_) | ty::Int(_) | ty::Float(_)) = self.ty.kind() else {
114            return None;
115        };
116        let scalar = self.try_to_leaf()?;
117        let input = typing_env.with_post_analysis_normalized(tcx).as_query_input(self.ty);
118        let size = tcx.layout_of(input).ok()?.size;
119        Some(scalar.to_bits(size))
120    }
121
122    pub fn try_to_bool(self) -> Option<bool> {
123        if !self.ty.is_bool() {
124            return None;
125        }
126        self.try_to_leaf()?.try_to_bool().ok()
127    }
128
129    pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> {
130        if !self.ty.is_usize() {
131            return None;
132        }
133        self.try_to_leaf().map(|s| s.to_target_usize(tcx))
134    }
135
136    /// Get the values inside the ValTree as a slice of bytes. This only works for
137    /// constants with types &str, &[u8], or [u8; _].
138    pub fn try_to_raw_bytes(self, tcx: TyCtxt<'tcx>) -> Option<&'tcx [u8]> {
139        match self.ty.kind() {
140            ty::Ref(_, inner_ty, _) => match inner_ty.kind() {
141                // `&str` can be interpreted as raw bytes
142                ty::Str => {}
143                // `&[T]` can be interpreted as raw bytes if elements are `u8`
144                ty::Slice(_) => {}
145                // other `&_` can't be interpreted as raw bytes
146                _ => return None,
147            },
148            // `[T; N]` can be interpreted as raw bytes if elements are `u8`
149            ty::Array(_, _) => {}
150            // Otherwise, type cannot be interpreted as raw bytes
151            _ => return None,
152        }
153
154        // We create an iterator that yields `Option<u8>`
155        let iterator = self.to_branch().into_iter().map(|ct| {
156            (*ct)
157                .try_to_value()
158                .and_then(|value| (value.ty == tcx.types.u8).then_some(value))
159                .and_then(|value| value.try_to_leaf().map(|leaf| leaf.to_u8()))
160        });
161        // If there is `None` in the iterator, then the array is not a valid array of u8s and we return `None`
162        let bytes: Vec<u8> = iterator.collect::<Option<Vec<u8>>>()?;
163
164        Some(tcx.arena.alloc_from_iter(bytes))
165    }
166
167    /// Converts to a `ValTreeKind::Leaf` value, `panic`'ing
168    /// if this constant is some other kind.
169    #[inline]
170    pub fn to_leaf(self) -> ScalarInt {
171        match &**self.valtree {
172            ValTreeKind::Leaf(s) => *s,
173            ValTreeKind::Branch(..) => crate::util::bug::bug_fmt(format_args!("expected leaf, got {0:?}", self))bug!("expected leaf, got {:?}", self),
174        }
175    }
176
177    /// Converts to a `ValTreeKind::Branch` value, `panic`'ing
178    /// if this constant is some other kind.
179    #[inline]
180    pub fn to_branch(self) -> &'tcx [ty::Const<'tcx>] {
181        match &**self.valtree {
182            ValTreeKind::Branch(branch) => &**branch,
183            ValTreeKind::Leaf(..) => crate::util::bug::bug_fmt(format_args!("expected branch, got {0:?}", self))bug!("expected branch, got {:?}", self),
184        }
185    }
186
187    /// Attempts to convert to a `ValTreeKind::Leaf` value.
188    pub fn try_to_leaf(self) -> Option<ScalarInt> {
189        match &**self.valtree {
190            ValTreeKind::Leaf(s) => Some(*s),
191            ValTreeKind::Branch(_) => None,
192        }
193    }
194
195    /// Attempts to convert to a `ValTreeKind::Leaf` value.
196    pub fn try_to_scalar(&self) -> Option<Scalar> {
197        self.try_to_leaf().map(Scalar::Int)
198    }
199
200    /// Attempts to convert to a `ValTreeKind::Branch` value.
201    pub fn try_to_branch(self) -> Option<&'tcx [ty::Const<'tcx>]> {
202        match &**self.valtree {
203            ValTreeKind::Branch(branch) => Some(&**branch),
204            ValTreeKind::Leaf(_) => None,
205        }
206    }
207
208    /// Destructures ADT constants into the constants of their fields.
209    pub fn destructure_adt_const(&self) -> ty::DestructuredAdtConst<'tcx> {
210        let fields = self.to_branch();
211
212        let (variant, fields) = match self.ty.kind() {
213            ty::Adt(def, _) if def.variants().is_empty() => {
214                crate::util::bug::bug_fmt(format_args!("unreachable"))bug!("unreachable")
215            }
216            ty::Adt(def, _) if def.is_enum() => {
217                let (head, rest) = fields.split_first().unwrap();
218                (VariantIdx::from_u32(head.to_leaf().to_u32()), rest)
219            }
220            ty::Adt(_, _) => (FIRST_VARIANT, fields),
221            _ => crate::util::bug::bug_fmt(format_args!("destructure_adt_const called on non-ADT type: {0:?}",
        self.ty))bug!("destructure_adt_const called on non-ADT type: {:?}", self.ty),
222        };
223
224        ty::DestructuredAdtConst { variant, fields }
225    }
226}
227
228impl<'tcx> rustc_type_ir::inherent::ValueConst<TyCtxt<'tcx>> for Value<'tcx> {
229    fn ty(self) -> Ty<'tcx> {
230        self.ty
231    }
232
233    fn valtree(self) -> ValTree<'tcx> {
234        self.valtree
235    }
236}
237
238impl<'tcx> fmt::Display for Value<'tcx> {
239    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240        ty::tls::with(move |tcx| {
241            let cv = tcx.lift(*self).unwrap();
242            let mut p = FmtPrinter::new(tcx, Namespace::ValueNS);
243            p.pretty_print_const_valtree(cv, /*print_ty*/ true)?;
244            f.write_str(&p.into_buffer())
245        })
246    }
247}