Skip to content

Commit 25cbfb7

Browse files
committed
Added text to intro and to conclusion to emphasize that the scope here is unsafe code during const-eval.
1 parent 7f9e711 commit 25cbfb7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

posts/2022-09-15-const-eval-safety-rule-revision.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ In a recent Rust issue ([#99923][]), a developer noted that the upcoming
1212
that is run at compile-time but produces values that may end up embedded in the
1313
final object code that executes at runtime.
1414

15+
Rust's const-eval system supports both safe and unsafe Rust, but the rules for
16+
what unsafe code is allowed to do during const-eval are even more strict than
17+
what is allowed for unsafe code at runtime. This post is going to go into detail
18+
about one of those rules.
19+
20+
(If your `const` code does not use any `unsafe` blocks or call any `const fn`
21+
with an `unsafe` block, then you do not need to worry about this!)
22+
1523
<!--
1624
1725
(This is distinct from procedural macros, which are Rust code that runs at
@@ -63,6 +71,15 @@ to above is trying to reinterpret the memory address `&()` as an integer of type
6371
`usize`. The compiler cannot predict what memory address the `()` would be
6472
associated with at execution time, so it refuses to allow that reinterpretation.
6573

74+
When you write safe Rust, then the compiler is responsible for preventing
75+
undefined behavior. When you write any unsafe code (be it const or non-const), you are
76+
responsible for preventing UB. The Rust compiler will protect itself from being
77+
[adversely affected][const-ub-guide], but other than that there are few
78+
guarantees. Specifically: If you have UB at const-eval time, there is no
79+
guarantee that your code will be accepted from one compiler version to another.
80+
81+
[const-ub-guide]: https://github.com/rust-lang/rfcs/blob/master/text/3016-const-ub.md#guide-level-explanation
82+
6683
## What is new here
6784

6885
You might be thinking: "it *used to be* accepted; therefore, there must be some
@@ -259,6 +276,12 @@ can say Hello in the [miri zulip][].
259276

260277
## Conclusion
261278

279+
When you write safe Rust, then the compiler is responsible for preventing
280+
undefined behavior. When you write any unsafe code, you are responsible for
281+
preventing undefined behavior. If you have undefined behavior at const-eval
282+
time, there is no guarantee that your code will be accepted from one compiler
283+
version to another.
284+
262285
The compiler team is hoping that issue [#99923][] is an exceptional fluke and
263286
that the 1.64 stable release will not encounter any other surprises related to
264287
this change to the const-eval machinery.

0 commit comments

Comments
 (0)