Commit 496296e
committed
[stdlib] Fix RawSpan initializer lifetime dependencies.
Two are fixes needed in most of the `RawSpan` and `Span` initializers. For example:
```
let baseAddress = buffer.baseAddress
let span = RawSpan(_unchecked: baseAddress, byteCount: buffer.count)
// As a trivial value, 'baseAddress' does not formally depend on the
// lifetime of 'buffer'. Make the dependence explicit.
self = _overrideLifetime(span, borrowing: buffer)
```
Fix swiftlang#1. baseAddress needs to be a variable
`span` has a lifetime dependence on `baseAddress` via its
initializer. Therefore, the lifetime of `baseAddress` needs to include the call
to `_overrideLifetime`. The override sets the lifetime dependency of its result,
not its argument. It's argument still needs to be non-escaping when it is passed
in.
Alternatives:
- Make the RawSpan initializer `@_unsafeNonescapableResult`.
Any occurrence of `@_unsafeNonescapableResult` actually signals a bug. We never
want to expose this annotation.
In addition to being gross, it would totally disable enforcement of the
initialized span. But we really don't want to side-step `_overrideLifetime`
where it makes sense. We want the library author to explicitly indicate that
they understand exactly which dependence is unsafe. And we do want to
eventually expose the `_overrideLifetime` API, which needs to be well
understood, supported, and tested.
- Add lifetime annotations to a bunch of `UnsafePointer`-family APIs so the
compiler can see that the resulting pointer is derived from self, where self is
an incoming `Unsafe[Buffer]Pointer`. This would create a massive lifetime
annotation burden on the `UnsafePointer`-family APIs, which don't really have
anything to do with lifetime dependence. It makes more sense for the author of
`Span`-like APIs to reason about pointer lifetimes.
Fix swiftlang#2. `_overrideLifetime` changes the lifetime dependency of span to be on an
incoming argument rather than a local variable.
This makes it legal to escape the function (by assigning it to self). Remember
that self is implicitly returned, so the `@lifetime(borrow buffer)` tells the
compiler that `self` is valid within `buffer`'s borrow scope.1 parent df655e8 commit 496296e
2 files changed
+126
-38
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | | - | |
101 | | - | |
102 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
103 | 105 | | |
104 | 106 | | |
105 | 107 | | |
| |||
115 | 117 | | |
116 | 118 | | |
117 | 119 | | |
118 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
119 | 125 | | |
120 | 126 | | |
121 | 127 | | |
| |||
131 | 137 | | |
132 | 138 | | |
133 | 139 | | |
134 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
135 | 145 | | |
136 | 146 | | |
137 | 147 | | |
138 | 148 | | |
139 | 149 | | |
140 | 150 | | |
141 | 151 | | |
142 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
143 | 158 | | |
144 | 159 | | |
145 | 160 | | |
| |||
175 | 190 | | |
176 | 191 | | |
177 | 192 | | |
178 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
179 | 198 | | |
180 | 199 | | |
181 | 200 | | |
| |||
191 | 210 | | |
192 | 211 | | |
193 | 212 | | |
194 | | - | |
195 | | - | |
196 | | - | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
197 | 218 | | |
198 | 219 | | |
199 | 220 | | |
| |||
209 | 230 | | |
210 | 231 | | |
211 | 232 | | |
212 | | - | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
213 | 238 | | |
214 | 239 | | |
215 | 240 | | |
| |||
225 | 250 | | |
226 | 251 | | |
227 | 252 | | |
228 | | - | |
229 | | - | |
230 | | - | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
231 | 259 | | |
232 | 260 | | |
233 | 261 | | |
| |||
344 | 372 | | |
345 | 373 | | |
346 | 374 | | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
351 | 378 | | |
352 | 379 | | |
353 | 380 | | |
| |||
457 | 484 | | |
458 | 485 | | |
459 | 486 | | |
460 | | - | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
461 | 492 | | |
462 | 493 | | |
463 | 494 | | |
| |||
677 | 708 | | |
678 | 709 | | |
679 | 710 | | |
680 | | - | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
681 | 715 | | |
682 | 716 | | |
683 | 717 | | |
| |||
700 | 734 | | |
701 | 735 | | |
702 | 736 | | |
703 | | - | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
704 | 741 | | |
705 | 742 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
110 | 113 | | |
111 | 114 | | |
112 | 115 | | |
| |||
122 | 125 | | |
123 | 126 | | |
124 | 127 | | |
125 | | - | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
126 | 133 | | |
127 | 134 | | |
128 | 135 | | |
| |||
142 | 149 | | |
143 | 150 | | |
144 | 151 | | |
145 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
146 | 157 | | |
147 | 158 | | |
148 | 159 | | |
| |||
162 | 173 | | |
163 | 174 | | |
164 | 175 | | |
165 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
166 | 181 | | |
167 | 182 | | |
168 | 183 | | |
| |||
178 | 193 | | |
179 | 194 | | |
180 | 195 | | |
181 | | - | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
182 | 201 | | |
183 | 202 | | |
184 | 203 | | |
| |||
214 | 233 | | |
215 | 234 | | |
216 | 235 | | |
217 | | - | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
218 | 240 | | |
219 | 241 | | |
220 | 242 | | |
| |||
234 | 256 | | |
235 | 257 | | |
236 | 258 | | |
237 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
238 | 264 | | |
239 | 265 | | |
240 | 266 | | |
| |||
258 | 284 | | |
259 | 285 | | |
260 | 286 | | |
261 | | - | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
262 | 292 | | |
263 | 293 | | |
264 | 294 | | |
| |||
278 | 308 | | |
279 | 309 | | |
280 | 310 | | |
281 | | - | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
282 | 316 | | |
283 | 317 | | |
284 | 318 | | |
| |||
298 | 332 | | |
299 | 333 | | |
300 | 334 | | |
301 | | - | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
302 | 340 | | |
303 | 341 | | |
304 | 342 | | |
| |||
309 | 347 | | |
310 | 348 | | |
311 | 349 | | |
312 | | - | |
313 | | - | |
314 | | - | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
315 | 356 | | |
316 | 357 | | |
317 | 358 | | |
| |||
481 | 522 | | |
482 | 523 | | |
483 | 524 | | |
484 | | - | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
485 | 530 | | |
486 | 531 | | |
487 | 532 | | |
| |||
704 | 749 | | |
705 | 750 | | |
706 | 751 | | |
707 | | - | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
708 | 756 | | |
709 | 757 | | |
710 | 758 | | |
| |||
728 | 776 | | |
729 | 777 | | |
730 | 778 | | |
731 | | - | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
732 | 783 | | |
733 | 784 | | |
0 commit comments