@@ -35,30 +35,34 @@ in which the constant is evaluated (e.g. the function within which the constant
35
35
and a [ ` GlobalId ` ] . The ` GlobalId ` is made up of an ` Instance ` referring to a constant
36
36
or static or of an ` Instance ` of a function and an index into the function's ` Promoted ` table.
37
37
38
- Constant evaluation returns a [ ` EvalToValTreeResult ` ] (for type system constants) or [ ` EvalToConstValueResult ` ] with either the error, or a
39
- representation of the constant.
38
+ Constant evaluation returns a [ ` EvalToValTreeResult ` ] (for type system constants) or
39
+ [ ` EvalToConstValueResult ` ] with either the error, or a representation of the constant.
40
40
41
41
Constants for the type system are encoded in "valtree representation". The ` ValTree ` datastructure
42
42
allows us to represent arrays, many structs, tuples, enums and most primitives. The basic rule for
43
- being permitted in the type system is that every value must be uniquely represented. In other words:
44
- a specific value must only be representable in one specific way. For example: there is only one way
45
- to represent an array of two integers as a ` ValTree ` : ` ValTree::Branch(&[ValTree::Leaf(first_int), ValTree;:Leaf(second_int)]) ` .
46
- Even though theoretically a ` [u32; 2] ` could be encoded in a ` u64 ` and thus just be a ` ValTree::Leaf(bits_of_two_u32) ` , that
47
- is not a legal construction of ` ValTree ` (and is so complex to do, so it is unlikely anyone is tempted to do so).
43
+ being permitted in the type system is that every value must be uniquely represented. In other
44
+ words: a specific value must only be representable in one specific way. For example: there is only
45
+ one way to represent an array of two integers as a ` ValTree ` :
46
+ ` ValTree::Branch(&[ValTree::Leaf(first_int), ValTree;:Leaf(second_int)]) ` .
47
+ Even though theoretically a ` [u32; 2] ` could be encoded in a ` u64 ` and thus just be a
48
+ ` ValTree::Leaf(bits_of_two_u32) ` , that is not a legal construction of ` ValTree `
49
+ (and is so complex to do, so it is unlikely anyone is tempted to do so).
48
50
49
- These rules also mean that some values are not representable. There can be no ` union ` s in type level
50
- constants, as it is not clear how they should be represented, because their active variant is unknown.
51
- Similarly there is no way to represent pointers, as addresses are unknown at compile-time and thus we
52
- cannot make any assumptions about them. References on the other hand can be represented, as equality
53
- for references is defined as equality on their value, so we ignore their address and just look at the
54
- backing value. This means that there is no difference in encoding for ` &42 ` and ` 42 ` .
51
+ These rules also mean that some values are not representable. There can be no ` union ` s in type
52
+ level constants, as it is not clear how they should be represented, because their active variant
53
+ is unknown. Similarly there is no way to represent pointers, as addresses are unknown at
54
+ compile-time and thus we cannot make any assumptions about them. References on the other hand
55
+ can be represented, as equality for references is defined as equality on their value, so we
56
+ ignore their address and just look at the backing value.
57
+ This means that there is no difference in encoding for ` &42 ` and ` 42 ` .
55
58
56
- As a consequence, all decoding of ` ValTree ` must happen by matching on the type first and making decisions
57
- depending on that. The value itself gives no useful information without the type that belongs to it.
58
- One notable oddity is ` &str ` representation. There is no sized equivalent of it, so unlike slices we cannot
59
- choose to represent them as their sized variant (slices are represented as arrays). ` &str ` thus has
60
- its own ` ValTree ` variant ` Str ` . The advantage of using a custom variant is that we are able to translate
61
- parser/AST/HIR string literals without any conversion as we use the same (` Symbol ` ) representation.
59
+ As a consequence, all decoding of ` ValTree ` must happen by matching on the type first and making
60
+ decisions depending on that. The value itself gives no useful information without the type that
61
+ belongs to it. One notable oddity is ` &str ` representation. There is no sized equivalent of it,
62
+ so unlike slices we cannot choose to represent them as their sized variant (slices are represented
63
+ as arrays). ` &str ` thus has its own ` ValTree ` variant ` Str ` . The advantage of using a custom
64
+ variant is that we are able to translate parser/AST/HIR string literals without any conversion
65
+ as we use the same (` Symbol ` ) representation.
62
66
63
67
Other constants get represented as [ ` ConstValue::Scalar ` ]
64
68
or [ ` ConstValue::Slice ` ] if possible. This means that the ` const_eval_* `
0 commit comments