@@ -97,21 +97,28 @@ here."
97
97
But such reasoning would be based on an imprecise view of what the Rust compiler
98
98
was doing here.
99
99
100
- The const-eval machinery of the Rust compiler is built upon the MIR-interpreter
101
- [ Miri ] [ ] , which uses an * abstract model* of a hypothetical machine as the
100
+ The const-eval machinery of the Rust compiler (also known as " the [ CTFE ] [ ] engine")
101
+ is built upon a [ MIR ] [ ] interpreter which uses an * abstract model* of a hypothetical machine as the
102
102
foundation for evaluating such expressions. This abstract model doesn't have to
103
- represent memory addresses as mere integers; in fact, to support Miri's
103
+ represent memory addresses as mere integers; in fact, to support
104
104
fine-grained checking for UB, it uses a much richer datatype for
105
105
the values that are held in the abstract memory store.
106
106
107
+ (The aforementioned MIR interpreter is also the basis for [ Miri] [ ] , a research
108
+ tool that interprets * non-const* Rust code, with a focus on
109
+ explicit detection of undefined behavior. The Miri developers are the primary
110
+ contributors to the CTFE engine in the Rust compiler.)
111
+
112
+ [ CTFE ] : https://rustc-dev-guide.rust-lang.org/const-eval.html
113
+ [ MIR ] : https://rustc-dev-guide.rust-lang.org/mir/index.html
107
114
[ Miri ] : https://github.com/rust-lang/miri#readme
108
115
109
- The details of Miri 's value representation do not matter too much for our
116
+ The details of the CTFE engine 's value representation do not matter too much for our
110
117
discussion here. We merely note that earlier versions of the compiler silently
111
118
accepted expressions that * seemed to* transmute memory addresses into integers,
112
119
copied them around, and then transmuted them back into addresses; but that was
113
120
not what was acutally happening under the hood. Instead, what was happening was
114
- that the Miri values were passed around blindly (after all, the whole point of
121
+ that the values were passed around blindly (after all, the whole point of
115
122
transmute is that it does no transformation on its input value, so it is a no-op
116
123
in terms of its operational semantics).
117
124
@@ -153,12 +160,12 @@ In fact, *all* of the examples provided above are exhibiting *undefined
153
160
behavior* according to the semantics of Rust's const-eval system.
154
161
155
162
The first example with ` _copy ` was accepted in Rust versions 1.46 through 1.63
156
- because of Miri implementation artifacts. Miri puts considerable effort into
163
+ because of CTFE implementation artifacts. The CTFE engine puts considerable effort into
157
164
detecting UB, but does not catch all instances of it. Furthermore, by default,
158
- Miri's detection can be delayed to a point far after where the actual
165
+ such detection can be delayed to a point far after where the actual
159
166
problematic expression is found.
160
167
161
- But with nightly Rust, we can opt into extra checks for UB that Miri provides,
168
+ But with nightly Rust, we can opt into extra checks for UB that the engine provides,
162
169
by passing the unstable flag ` -Z extra-const-ub-checks ` . If we do that, then for
163
170
* all* of the above examples we get the same result:
164
171
@@ -207,7 +214,7 @@ though with less discussion than what you see in the stabilization report -->.)
207
214
[ doc for transmute ] : https://doc.rust-lang.org/std/mem/fn.transmute.html
208
215
209
216
Thus, we can see that the classification of the above examples as UB during const evaluation
210
- is not a new thing at all. The only change here was that Miri had some internal
217
+ is not a new thing at all. The only change here was that the CTFE engine had some internal
211
218
changes that made it start detecting the UB rather than silently ignoring it.
212
219
213
220
This means the Rust compiler has a shifting notion of what UB it will
@@ -228,7 +235,7 @@ strived to mitigate such breakage *whenever feasible*, via things like
228
235
229
236
[ future-incompat ] : https://doc.rust-lang.org/rustc/lints/index.html#future-incompatible-lints
230
237
231
- Today, with our current const-eval architecture layered atop Miri , it is not
238
+ Today, with our current const-eval architecture, it is not
232
239
feasible to ensure that changes such as the [ one that injected] [ PR #97684 ] issue
233
240
[ #99923 ] [ ] go through a future-incompat warning cycle.
234
241
The compiler team plans to keep our eye on issues in this space. If we see
@@ -288,7 +295,7 @@ As you might imagine, a lot of us are pretty interested in questions such as
288
295
289
296
See for example Ralf Jung's excellent blog series on why pointers are
290
297
complicated (parts [ I] [ ralf1 ] , [ II] [ ralf2 ] , [ III] [ ralf3 ] ), which contain some of
291
- the details elided above about Miri's representation, and spell out reasons why
298
+ the details elided above about the representation of pointer values , and spell out reasons why
292
299
you might want to be concerned about pointer-to-usize transmutes even * outside*
293
300
of const-eval.
294
301
@@ -322,6 +329,6 @@ that the 1.64 stable release will not encounter any other surprises related to
322
329
the aforementioned change to the const-eval machinery.
323
330
324
331
But fluke or not, the issue provided excellent motivation to spend some time
325
- exploring facets of Rust's const-eval architecture, and the Miri interpreter
326
- that underlies it. We hope you enjoyed reading this as much as we did writing
327
- it.
332
+ exploring facets of Rust's const-eval architecture and the interpreter
333
+ that underlies it.
334
+ We hope you enjoyed reading this as much as we did writing it.
0 commit comments