Skip to content

Commit bc8641a

Browse files
committed
Document valtree
1 parent c30c1be commit bc8641a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

compiler/rustc_middle/src/ty/consts/valtree.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@ use rustc_macros::HashStable;
33

44
#[derive(Copy, Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
55
#[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 must also have equal representation.
10+
/// Since CTFE uses `AllocId` to represent pointers, it often happens that two different
11+
/// `AllocId`s point to equal values. So we may end up with different representations for
12+
/// two constants whose value is `&42`. Furthermore any kind of struct that has padding will
13+
/// have arbitrary values within that padding, even if the values of the struct are the same.
14+
///
15+
/// `ValTree` does not have this problem with representation, as it only contains integers or
16+
/// lists of values of itself.
617
pub enum ValTree<'tcx> {
18+
/// ZSTs, integers, `bool`, `char` are represented as scalars.
19+
/// See the `ScalarInt` documentation for how `ScalarInt` guarantees that equal values
20+
/// of these types have the same representation.
721
Leaf(ScalarInt),
22+
/// The fields of any kind of aggregate. Structs, tuples and arrays are represented by
23+
/// listing their fields' values in order.
24+
/// Enums are represented by storing their discriminant as a field, followed by all
25+
/// the fields of the variant.
826
Branch(&'tcx [ValTree<'tcx>]),
927
}
1028

0 commit comments

Comments
 (0)