or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
All your const belong to the type system
TLDR
Duplicate
ty::Const
intoty::Const
andmir::Const
where the former becomes a tree with integer leaves and the latter loses all forms of "optimized" representation and just refers to the plain const allocation backing the constant.Motivation
Currently
ty::Const
has multiple ways of representing the same value, which by itself isn't bad, but we actually do represent the same value in different ways. This breaks the invariant in the compiler that if interned things are equal, their interned address is the same. This is very visible when you have two constants representing a pointer value. Const generic functions that are generic over pointer values can arbitrarily cause linker errors, because code defining the generic function may compute a different symbol than the code calling the generic function.Guts and other gory details
ty::Const
'sval
field is of typeConstKind
, whoseValue
variant contains aConstValue
. ThisConstValue
will be replaced byThis change will shrink the size of
ConstValue
from its current 32 bytes to 24 bytes.A value of struct, tuple or array type will be represented as a
Node
, where all the fields or elements are again encoded asConstValue
. Only integers, bools andchar
are allowed asLeaf
values.A value of enum type will be represented as a
Node
where thevariant
field is set to the active enum variant's index and the enum variant's fields are encoded just like struct fields.A pointer is represented as a
Node
with a single child, so(42,)
and&42
are represented exactly the same, only the type at thety::Const
level will differ. While we could have a separatePointer(&'tcx ConstValue<'tcx>)
variant, there's little use in making this explicit and not having it will reduce code duplication everwhere where the difference is not relevant without affecting the places where the difference is relevant.A lossy conversion from
mir::Const
toty::Const
will be introduced. It will be used for pretty printingmir::Const
during mir dumps. This way we only have to support pretty printingty::Const
, which is trivial to pretty print.mir::Const
will be changed to just become the oldConstValue::ByRef
representation in case ofConstKind::Value
. We can likely also remove other variants from the newmir::ConstKind
- I beliveInfer
,Bound
andPlaceHolder
are only used in typeck and irrelevant for MIR.Future extensions
Function(Instance<'tcx>)'
variant toConstValue
.ConstKind
toConstValue
, allowing things like(42, N)
to be encoded, while currently const generic parameters can only be encoded if the constant is just a const generic parameter directly.mir::Const
->ty::Const
conversion fallible instead of lossy. Whenever there would be a loss of information, resort to verbose printing of themir::Const
.Related reading
Alternative designs
ty::Const
contains anotherty::Const
at every level. This allows types like(42, N)
to be encoded inty::Const
at all.