add Reddit link
[web.git] / personal / _posts / 2024-08-14-places.md
1 ---
2 title: "What is a place expression?"
3 categories: programming rust
4 reddit: /rust/comments/1esavn3/what_is_a_place_expression/
5 ---
6
7 One of the more subtle aspects of the Rust language is the fact that there are actually two kinds of expressions:
8 *value expressions* and *place expressions*.
9 Most of the time, programmers do not have to think much about that distinction, as Rust will helpfully insert automatic conversions when one kind of expression is encountered but the other was expected.
10 However, when it comes to unsafe code, a proper understanding of this dichotomy of expressions can be required.
11 Consider the following [example](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=9a8802d20da16d6569510124c5827794):
12
13 ```rust
14 #[repr(packed)]
15 struct MyStruct {
16   field: i32
17 }
18
19 let x = MyStruct { field: 42 };
20 let ptr = &raw const x.field;
21 // This line is fine.
22 let ptr_copy = &raw const *ptr;
23 // But this line has UB!
24 let val = *ptr;
25 ```
26
27 Here I am using the unstable but soon-to-be-stabilized "raw borrow" operator, `&raw const`.
28 You may know it in its stable form as a macro, `ptr::addr_of!`, but the `&` syntax makes the interplay of places and values more explicit so we will use it here.
29
30 The last line has Undefined Behavior (UB) because `ptr` points to a field of a packed struct, which is not sufficiently aligned.
31 But how can it be the case that evaluating `*ptr` is UB, but evaluating `&raw const *ptr` is fine?
32 Evaluating an expression should proceed by first evaluating the sub-expressions and then doing something with the result.
33 However, `*ptr` is a sub-expression of `&raw const *ptr`, and we just said that `*ptr` is UB, so shouldn't `&raw const *ptr` also be UB?
34 That is the topic of this post.
35
36 <!-- MORE -->
37
38 ### Making the implicit explicit
39
40 The main reason why this dichotomy of place expressions and value expressions is so elusive is that it is entirely implicit.
41 Therefore, to understand what actually happens in code like the above, the first step is to add some new syntax that lets us make this implicit distinction explicit in the code.
42
43 Normally, we may think of (a fragment of) the grammar of Rust expressions roughly as follows:
44
45 > _Expr_ ::= \
46 > &nbsp;&nbsp; _Literal_ | _LocalVar_ | _Expr_ `+` _Expr_ | `&` _BorMod_ _Expr_ | `*` _Expr_ | \
47 > &nbsp;&nbsp; _Expr_ `.` _Field_ | _Expr_ `=` _Expr_ | ... \
48 > _BorMod_ ::= `​` | `mut` | `raw` `const` | `raw` `mut` \
49 > _Statement_ ::= \
50 > &nbsp;&nbsp; `let` _LocalVar_ `=` _Expr_ `;` | ...
51
52 This directly explains why we can write expressions like `*ptr = *other_ptr + my_var`.
53
54 However, to understand places and values, it is instructive to consider a different grammar that explicitly has two kinds of expressions.
55 I will first give the grammar, and then explain it with some examples:
56
57 > _ValueExpr_ ::= \
58 > &nbsp;&nbsp; _Literal_ | _ValueExpr_ `+` _ValueExpr_ | `&` _BorMod_ _PlaceExpr_ | \
59 > &nbsp;&nbsp; _PlaceExpr_ `=` _ValueExpr_ | `load` _PlaceExpr_ \
60 > _PlaceExpr_ ::= \
61 > &nbsp;&nbsp; _LocalVar_ | `*` _ValueExpr_ | _PlaceExpr_ `.` _Field_ \
62 > _Statement_ ::= \
63 > &nbsp;&nbsp; `let` _LocalVar_ `=` _ValueExpr_ `;` | ...
64
65 *Value expressions* are those expressions that compute a value: literals like `5`, computations like `5 + 7`,
66 but also expressions that compute values of pointer type like `&my_var`.
67 However, the expression `my_var` (referencing a local variable), according to this grammar, is *not* a value expression, it is a *place expression*.
68 This is because `my_var` actually denotes a place in memory, and there's multiple things one can do with a place:
69 one can load the contents of the place from memory (which produces a value), one can create a pointer to the place (which also produces a value, but does not access memory at all),
70 or one can store a value into this place (which in Rust produces the `()` value, but the side-effect of changing the contents of memory is more relevant).
71 Besides local variable, the other main example of a place expression is the result of the `*` operator, which takes a *value* (of pointer type) and turns it into a place.
72 Furthermore, given a place of struct type, we can use a field projection to obtain a place just for that field.
73
74 This may sound odd, because it means that `let new_var = my_var;` is not actually a valid statement in our grammar!
75 To accept this code, the Rust compiler will automatically convert this statement into a form that fits the grammar by adding `load` whenever needed.[^desugar]
76 `load` takes a place and, as the name indicates, performs a load from memory to obtain the value currently stored in this place.
77 The desugared form of the statement therefore is `let new_var = load my_var;`.
78
79 To consider a more complicated example, the assignment expression `*ptr = *other_ptr + my_var` mentioned above desugars to `*(load ptr) = load *(load other_ptr) + load my_var`.
80 That's a lot of `load` expressions!
81 It is instructive to convince yourself that every one of them is necessary to make this term fit the grammar.
82 In particular, `*` works on a value expression (so we need `load other_ptr` to obtain the value stored in this place) and produces a place expression (so we need to `load` again to obtain a value expression that we can use with `+`).
83 However, the left-hand side of `=` is a place expression, so we do not `load` the result of the `*` there.
84
85 [^desugar]: The Rust compiler does not actually explicitly do such a desugaring, but this happens implicitly as part of compiling the program into MIR form.
86
87 Since the `load` operator is introduced implicitly, it is sometimes referred to a "place-to-value coercion".
88 Understanding where place-to-value coercions or `load` expressions are introduced is the key to understanding the example at the top of this blog post.
89 So let us write the relevant part of that example again, using our more explicit grammar:
90 ```rust
91 let ptr = &raw const x.field;
92 // This line is fine.
93 let ptr_copy = &raw const *(load ptr);
94 // But this line has UB!
95 let val = load *(load ptr);
96 ```
97
98 Suddenly, it makes perfect sense why the last line has UB but the previous one does not!
99 The expression `&raw const *(load ptr)` merely computes the place `*(load ptr)` *without ever loading from it*, and then uses `&raw const` to turn that place into a value.
100 This is worth repeating: the `*` operator, usually referred to as "dereferencing a pointer", *does not access memory in any way*.
101 All it does is take a value of pointer type, and convert it into a place.
102 This is a pure operation that can never fail.
103 In the last line, there is an extra `load` applied to the result of the `*`, and *that* is where a memory access happens---and in this case, UB occurs since the place is not sufficiently aligned.
104
105 It is completely legal to evaluate a place expression that produces an unaligned place, and it is also legal to then turn that unaligned place into a raw pointer value.
106 Generally, in terms of UB, you should think of places as being pretty much like raw pointers: there is no requirement that they point to valid values, or even to existing memory.[^field]
107 However, it is *not* legal to load from (or store to) an unaligned place, which is why `load *(load ptr)` is UB.
108
109 [^field]: One subtlety, however, is that the _PlaceExpr_ `.` _Field_ expression performs *in-bounds* pointer arithmetic using the rules of the [`offset` method](https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.offset). This is the one case where a place expression does care about pointing to existing memory. This is unfortunate, but optimizations greatly benefit from this rule and since the introduction of the `offset_of!` macro, it should be extremely rare that unsafe code would want to do a field projection on a dangling pointer.
110
111 In other words, when `*ptr` is used as a value expression (as it is in our example), then it is *not* a sub-expression of `&raw const *ptr` because the implicit place-to-value coercion adds an extra `load` around `*ptr` that is not added in `&raw const *ptr`.
112
113 ### Other examples of place expression surprises
114
115 The other main example where place expressions can lead to surprising behavior is in combination with the `_` pattern.
116 For instance:
117 ```rust
118 let ptr = std::ptr::null::<i32>();
119 let _ = *ptr; // This is fine!
120 let _val = *ptr; // This is UB.
121 ```
122
123 The reason for this is that the `_` pattern does *not* incur a place-to-value coercion.
124 The desugared form of the relevant part of this code is:
125 ```rust
126 let _ = *(load ptr); // This is fine!
127 let _val = load *(load ptr); // This is UB.
128 ```
129 As you can see, the first line does not actually load from the pointer (the only `load` is there to load the pointer itself from the local variable that stores it).
130 No value is ever constructed when a place expression is used with the `_` pattern.
131 In contrast, the last line actually creates a new local variable, and therefore a place-to-value coercion is inserted to compute the initial value for that variable.
132
133 The same also happens with `match` statements:
134 ```rust
135 let ptr = std::ptr::null::<i32>();
136 match *ptr { _ => "happy" } // This is fine!
137 match *ptr { _val => "not happy" } // This is UB.
138 ```
139 The scrutinee of a `match` expression is a place expression, and if the pattern is `_` then a value is never constructed.
140 However, when an actual binder is present, this introduces a local variable and a place-to-value coercion is inserted to compute the value that will be stored in that local variable.
141
142 ### Are there also value-to-place coercions?
143
144 So far, we have discussed what happens when a place expression is encountered in a spot where a value expression was expected.
145 But what about the opposite case?
146 Consider:
147 ```rust
148 let x = &mut 15;
149 ```
150 According to our grammar, `&` (in this case with the `mut` modifier) needs a place expression, but `15` is a value expression.
151 How can the Rust compiler accept such code?
152
153 In this case, the desugaring involves introducing new "temporary" local variables:
154 ```rust
155 let mut _tmp = 15;
156 let x = &mut _tmp;
157 ```
158 The exact scope in which this temporary is introduced is defined by [non-trivial rules](https://github.com/rust-lang/lang-team/blob/master/design-meeting-minutes/2023-03-15-temporary-lifetimes.md) that are outside the scope of this blog post;
159 the key point is that this transformation again makes the program valid according to the more explicit grammar.
160
161 There is one exception to this rule, which is the left-hand side of an assignment operator: if you write something like `15 = 12 + 19`, the value `15` is not turned into a temporary place, and instead the program is rejected.
162 Introducing temporaries here is very unlikely to produce a meaningful result, so there's no good reason to accept such code.
163
164 ### Conclusion
165
166 Whenever we are using a place expression where a value is expected, or a value expression where a place is expected, the Rust compiler implicitly transforms our program into a form that matches the grammar given above.
167 If you are only writing safe code, you can almost always entirely forget about this transformation.
168 However, if you are writing unsafe code and want to understand why some programs have UB and others do not, it can be crucial to understand what exactly happens.
169 If you only remember one thing from this blog post, then remember that `*` dereferences a pointer but *does not load from memory*; instead, all it does is turn the pointer into a place---it is the subsequent implicit place-to-value conversion that performs the actual load.
170 I hope that giving a name to this implicit `load` operator can help demystify the topic of places and values. :)