Commit c107313
committed
Auto merge of #141980 - beetrees:va-list-proposal, r=workingjubilee
Rework `c_variadic`
tracking issue: #44930
related PR: #144529
On some platforms, the C `va_list` type is actually a single-element array of a struct (on other platforms it is just a pointer). In C, arrays passed as function arguments expirience array-to-pointer decay, which means that C will pass a pointer to the array in the caller instead of the array itself, and modifications to the array in the callee will be visible to the caller (this does not match Rust by-value semantics). However, for `va_list`, the C standard explicitly states that it is undefined behaviour to use a `va_list` after it has been passed by value to a function (in Rust parlance, the `va_list` is moved, not copied). This matches Rust's pass-by-value semantics, meaning that when the C `va_list` type is a single-element array of a struct, the ABI will match C as long as the Rust type is always be passed indirectly.
In the old implementation, this ABI was achieved by having two separate types: `VaList` was the type that needed to be used when passing a `VaList` as a function parameter, whereas `VaListImpl` was the actual `va_list` type that was correct everywhere else. This however is quite confusing, as there are lots of footguns: it is easy to cause bugs by mixing them up (e.g. the C function `void foo(va_list va)` was equivalent to the Rust `fn foo(va: VaList)` whereas the C function `void bar(va_list* va)` was equivalent to the Rust `fn foo(va: *mut VaListImpl)`, not `fn foo(va: *mut VaList)` as might be expected); also converting from `VaListImpl` to `VaList` with `as_va_list()` had platform specific behaviour: on single-element array of a struct platforms it would return a `VaList` referencing the original `VaListImpl`, whereas on other platforms it would return a cioy,
In this PR, there is now just a single `VaList` type (renamed from `VaListImpl`) which represents the C `va_list` type and will just work in all positions. Instead of having a separate type just to make the ABI work, #144529 adds a `#[rustc_pass_indirectly_in_non_rustic_abis]` attribute, which when applied to a struct will force the struct to be passed indirectly by non-Rustic calling conventions. This PR then implements the `VaList` rework, making use of the new attribute on all platforms where the C `va_list` type is a single-element array of a struct.
Cleanup of the `VaList` API and implementation is also included in this PR: since it was decided it was OK to experiment with Rust requiring that not calling `va_end` is not undefined behaviour (#141524 (comment)), I've removed the `with_copy` method as it was redundant to the `Clone` impl (the `Drop` impl of `VaList` is a no-op as `va_end` is a no-op on all known platforms).
Previous discussion: #141524 and [t-compiler > c_variadic API and ABI](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/c_variadic.20API.20and.20ABI)
Tracking issue: #44930
r? `@joshtriplett`File tree
22 files changed
+631
-290
lines changed- compiler/rustc_codegen_ssa/src/traits
- library
- core/src
- ffi
- intrinsics
- std/src/ffi
- src/tools/compiletest/src/directives
- tests
- auxiliary
- codegen-llvm/cffi
- run-make/c-link-to-rust-va-list-fn
- ui
- abi
- c-variadic
- parser
- macro
- thir-print
22 files changed
+631
-290
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | | - | |
10 | | - | |
11 | | - | |
| 8 | + | |
| 9 | + | |
12 | 10 | | |
13 | | - | |
| 11 | + | |
| 12 | + | |
14 | 13 | | |
15 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
16 | 34 | | |
17 | 35 | | |
18 | 36 | | |
19 | 37 | | |
20 | 38 | | |
21 | 39 | | |
22 | 40 | | |
23 | | - | |
24 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
25 | 44 | | |
26 | 45 | | |
27 | 46 | | |
28 | 47 | | |
29 | 48 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
35 | 53 | | |
36 | 54 | | |
37 | | - | |
38 | 55 | | |
39 | 56 | | |
40 | 57 | | |
41 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
42 | 65 | | |
43 | 66 | | |
44 | | - | |
45 | | - | |
| 67 | + | |
| 68 | + | |
46 | 69 | | |
47 | 70 | | |
48 | 71 | | |
49 | | - | |
50 | | - | |
51 | | - | |
| 72 | + | |
| 73 | + | |
52 | 74 | | |
53 | 75 | | |
54 | 76 | | |
55 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
56 | 83 | | |
57 | 84 | | |
58 | | - | |
59 | | - | |
| 85 | + | |
| 86 | + | |
60 | 87 | | |
61 | 88 | | |
62 | | - | |
63 | | - | |
64 | | - | |
| 89 | + | |
| 90 | + | |
65 | 91 | | |
66 | 92 | | |
67 | 93 | | |
68 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
69 | 100 | | |
70 | 101 | | |
71 | | - | |
72 | | - | |
| 102 | + | |
| 103 | + | |
73 | 104 | | |
74 | 105 | | |
75 | | - | |
76 | | - | |
77 | | - | |
| 106 | + | |
| 107 | + | |
78 | 108 | | |
79 | 109 | | |
80 | 110 | | |
81 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
82 | 117 | | |
83 | 118 | | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
88 | 123 | | |
89 | | - | |
90 | 124 | | |
91 | 125 | | |
92 | 126 | | |
93 | 127 | | |
94 | 128 | | |
95 | 129 | | |
96 | 130 | | |
| 131 | + | |
97 | 132 | | |
98 | | - | |
| 133 | + | |
99 | 134 | | |
100 | 135 | | |
101 | 136 | | |
102 | 137 | | |
103 | 138 | | |
104 | 139 | | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | 140 | | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
| 141 | + | |
| 142 | + | |
169 | 143 | | |
170 | 144 | | |
171 | 145 | | |
172 | 146 | | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
180 | 153 | | |
181 | 154 | | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
186 | 159 | | |
187 | 160 | | |
188 | 161 | | |
| |||
203 | 176 | | |
204 | 177 | | |
205 | 178 | | |
206 | | - | |
| 179 | + | |
207 | 180 | | |
208 | 181 | | |
209 | 182 | | |
| |||
238 | 211 | | |
239 | 212 | | |
240 | 213 | | |
241 | | - | |
| 214 | + | |
242 | 215 | | |
243 | 216 | | |
244 | 217 | | |
| |||
258 | 231 | | |
259 | 232 | | |
260 | 233 | | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | 234 | | |
276 | 235 | | |
277 | | - | |
| 236 | + | |
278 | 237 | | |
279 | 238 | | |
280 | 239 | | |
281 | | - | |
| 240 | + | |
282 | 241 | | |
283 | 242 | | |
284 | 243 | | |
285 | 244 | | |
286 | 245 | | |
287 | 246 | | |
288 | 247 | | |
289 | | - | |
| 248 | + | |
290 | 249 | | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
301 | 253 | | |
302 | 254 | | |
303 | 255 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| |||
3447 | 3447 | | |
3448 | 3448 | | |
3449 | 3449 | | |
3450 | | - | |
| 3450 | + | |
3451 | 3451 | | |
3452 | 3452 | | |
3453 | 3453 | | |
| |||
3465 | 3465 | | |
3466 | 3466 | | |
3467 | 3467 | | |
3468 | | - | |
| 3468 | + | |
3469 | 3469 | | |
3470 | 3470 | | |
3471 | 3471 | | |
| |||
3475 | 3475 | | |
3476 | 3476 | | |
3477 | 3477 | | |
3478 | | - | |
| 3478 | + | |
0 commit comments