]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_middle/src/ty/consts/valtree.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / compiler / rustc_middle / src / ty / consts / valtree.rs
CommitLineData
6a06907d
XL
1use super::ScalarInt;
2use rustc_macros::HashStable;
3
4#[derive(Copy, Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
5#[derive(HashStable)]
6/// This datastructure is used to represent the value of constants used in the type system.
7///
8/// We explicitly choose a different datastructure from the way values are processed within
9/// CTFE, as in the type system equal values (according to their `PartialEq`) must also have
10/// equal representation (`==` on the rustc data structure, e.g. `ValTree`) and vice versa.
11/// Since CTFE uses `AllocId` to represent pointers, it often happens that two different
12/// `AllocId`s point to equal values. So we may end up with different representations for
13/// two constants whose value is `&42`. Furthermore any kind of struct that has padding will
14/// have arbitrary values within that padding, even if the values of the struct are the same.
15///
16/// `ValTree` does not have this problem with representation, as it only contains integers or
17/// lists of (nested) `ValTree`.
18pub enum ValTree<'tcx> {
19 /// ZSTs, integers, `bool`, `char` are represented as scalars.
20 /// See the `ScalarInt` documentation for how `ScalarInt` guarantees that equal values
21 /// of these types have the same representation.
22 Leaf(ScalarInt),
23 /// The fields of any kind of aggregate. Structs, tuples and arrays are represented by
24 /// listing their fields' values in order.
25 /// Enums are represented by storing their discriminant as a field, followed by all
26 /// the fields of the variant.
27 Branch(&'tcx [ValTree<'tcx>]),
28}
29
30impl ValTree<'tcx> {
31 pub fn zst() -> Self {
32 Self::Branch(&[])
33 }
34}