Skip to content

Commit a9c7047

Browse files
committed
notes on dynamic checks
1 parent a18aafc commit a9c7047

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

const.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ at type `&Option<Cell<i32>>` would be rejected by the naive analysis above, but
5454
is actually accepted by the compiler because we know that there is no
5555
`UnsafeCell` here that would permit interior mutability.
5656

57+
*Dynamic check.* The Miri engine could check this dynamically by ensuring that
58+
the new data that is interned for a constant is all marked as
59+
immutable. (Constants referring to already existing mutable data are not
60+
inherently problematic.)
61+
5762
### 3. `Sync`
5863

5964
Finally, the same constant reference is actually shared across threads. This is
@@ -66,6 +71,8 @@ ecosystem that would break if we just started enforcing this now. See
6671
[this issue](https://github.com/rust-lang/rust/issues/49206) and the
6772
[PR attempting to fix this](https://github.com/rust-lang/rust/pull/54424/).
6873

74+
*Dynamic check.* It is unclear how the Miri engine could dynamically check this.
75+
6976
### 4. Drop
7077

7178
`Drop` is actually not an issue, at least not more so than for statics:
@@ -97,3 +104,6 @@ This is distinct to the concern about interior mutability above: That concern
97104
was about first computing a `&Cell<i32>` and then using it at run-time (and
98105
observing the fact that it has been "deduplicated"), this here is about using
99106
such a value at compile-time even though it might be changed at run-time.
107+
108+
*Dynamic check.* The Miri engine could check this dynamically by refusing to
109+
access mutable global memory when computing a const.

promotion.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ earlier version of miri used to panic on arithmetic overflow even in release
5151
mode. This breaks promotion, because now promoting code that would work (and
5252
could not panic!) at run-time leads to a compile-time CTFE error.
5353

54+
*Dynamic check.* The Miri engine already dynamically detects panics, but the
55+
main point of promoteds is ruling them out statically.
56+
5457
### 2. Const safety
5558

5659
We have explained what happens when evaluating a promoted panics, but what about
@@ -94,6 +97,9 @@ but to abort compilation of a program that would have compiled fine if we would
9497
not have decided to promote. It is the responsibility of `foo` to not fail this
9598
way when working with const-safe arguments.
9699

100+
*Dynamic check.* The Miri engine already dynamically detects const safety
101+
violations, but the main point of promoteds is ruling them out statically.
102+
97103
### 3. Drop
98104

99105
Expressions returning "needs drop" types can never be promoted. If such an
@@ -110,6 +116,9 @@ it is unlikely to be the desired behavior in most cases and very likey to be con
110116
to the user. If such behavior is desired, the user can still use an explicit `static`
111117
or `const` item and refer to that.
112118

119+
*Dynamic check.* The Miri engine could dynamically check this by ensuring that
120+
the result of computing a promoted is a value that does not need dropping.
121+
113122
## `&` in `const` and `static`
114123

115124
Promotion is also responsible for making code like this work:

static.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ static FOOO: Foo = Foo; // Ok, drop is never run
3030
// Not ok, cannot run `Foo::drop` because it's not a const fn
3131
static BAR: i32 = (Foo, 42).1;
3232
```
33+
34+
*Dynamic check.* The Miri engine dynamically checks that this is done correctly
35+
by not permitting calls of non-`const` functions.

0 commit comments

Comments
 (0)