@@ -12,6 +12,14 @@ In a recent Rust issue ([#99923][]), a developer noted that the upcoming
12
12
that is run at compile-time but produces values that may end up embedded in the
13
13
final object code that executes at runtime.
14
14
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
+
15
23
<!--
16
24
17
25
(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
63
71
` usize ` . The compiler cannot predict what memory address the ` () ` would be
64
72
associated with at execution time, so it refuses to allow that reinterpretation.
65
73
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
+
66
83
## What is new here
67
84
68
85
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][].
259
276
260
277
## Conclusion
261
278
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
+
262
285
The compiler team is hoping that issue [ #99923 ] [ ] is an exceptional fluke and
263
286
that the 1.64 stable release will not encounter any other surprises related to
264
287
this change to the const-eval machinery.
0 commit comments