This repository was archived by the owner on May 28, 2025. It is now read-only.
Commit 7fe6f36
committed
Auto merge of rust-lang#103491 - cjgillot:self-rpit, r=oli-obk
Support using `Self` or projections inside an RPIT/async fn
I reuse the same idea as rust-lang#103449 to use variances to encode whether a lifetime parameter is captured by impl-trait.
The current implementation of async and RPIT replace all lifetimes from the parent generics by `'static`. This PR changes the scheme
```rust
impl<'a> Foo<'a> {
fn foo<'b, T>() -> impl Into<Self> + 'b { ... }
}
opaque Foo::<'_a>::foo::<'_b, T>::opaque<'b>: Into<Foo<'_a>> + 'b;
impl<'a> Foo<'a> {
// OLD
fn foo<'b, T>() -> Foo::<'static>::foo::<'static, T>::opaque::<'b> { ... }
^^^^^^^ the `Self` becomes `Foo<'static>`
// NEW
fn foo<'b, T>() -> Foo::<'a>::foo::<'b, T>::opaque::<'b> { ... }
^^ the `Self` stays `Foo<'a>`
}
```
There is the same issue with projections. In the example, substitute `Self` by `<T as Trait<'b>>::Assoc` in the sugared version, and `Foo<'_a>` by `<T as Trait<'_b>>::Assoc` in the desugared one.
This allows to support `Self` in impl-trait, since we do not replace lifetimes by `'static` any more. The same trick allows to use projections like `T::Assoc` where `Self` is allowed. The feature is gated behind a `impl_trait_projections` feature gate.
The implementation relies on 2 tweaking rules for opaques in 2 places:
- we only relate substs that correspond to captured lifetimes during TypeRelation;
- we only list captured lifetimes in choice region computation.
For simplicity, I encoded the "capturedness" of lifetimes as a variance, `Bivariant` vs `Invariant` for unused vs captured lifetimes. The `variances_of` query used to ICE for opaques.
Impl-trait that do not reference `Self` or projections will have their variances as:
- `o` (invariant) for each parent type or const;
- `*` (bivariant) for each parent lifetime --> will not participate in borrowck;
- `o` (invariant) for each own lifetime.
Impl-trait that does reference `Self` and/or projections will have some parent lifetimes marked as `o` (as the example above), and participate in type relation and borrowck. In the example above, `variances_of(opaque) = ['_a: o, '_b: *, T: o, 'b: o]`.
r? types
cc `@compiler-errors` , as you asked about the issue with `Self` and projections.File tree
33 files changed
+569
-340
lines changed- compiler
- rustc_ast_lowering/src
- rustc_borrowck/src
- region_infer
- rustc_error_codes/src/error_codes
- rustc_feature/src
- rustc_hir_analysis/src
- astconv
- check
- collect
- variance
- rustc_infer/src/infer
- error_reporting
- rustc_metadata/src/rmeta
- rustc_middle/src/ty
- rustc_span/src
- rustc_ty_utils/src
- src/test/ui
- async-await
- in-trait
- issues
- impl-trait
- nll/ty-outlives
- type-alias-impl-trait
33 files changed
+569
-340
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | | - | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| |||
1465 | 1465 | | |
1466 | 1466 | | |
1467 | 1467 | | |
1468 | | - | |
1469 | | - | |
1470 | | - | |
1471 | | - | |
1472 | | - | |
1473 | | - | |
1474 | | - | |
1475 | | - | |
1476 | | - | |
1477 | | - | |
1478 | | - | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
1479 | 1473 | | |
1480 | 1474 | | |
1481 | 1475 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| 35 | + | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
| 131 | + | |
131 | 132 | | |
132 | 133 | | |
133 | 134 | | |
| |||
Lines changed: 37 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
64 | 72 | | |
65 | 73 | | |
66 | 74 | | |
67 | 75 | | |
68 | 76 | | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 77 | + | |
| 78 | + | |
75 | 79 | | |
76 | 80 | | |
77 | 81 | | |
| |||
92 | 96 | | |
93 | 97 | | |
94 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
95 | 119 | | |
| 120 | + | |
| 121 | + | |
96 | 122 | | |
97 | | - | |
98 | | - | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
99 | 126 | | |
100 | 127 | | |
101 | 128 | | |
| |||
106 | 133 | | |
107 | 134 | | |
108 | 135 | | |
109 | | - | |
110 | | - | |
| 136 | + | |
111 | 137 | | |
112 | 138 | | |
113 | 139 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
6 | | - | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
419 | 419 | | |
420 | 420 | | |
421 | 421 | | |
| 422 | + | |
| 423 | + | |
422 | 424 | | |
423 | 425 | | |
424 | 426 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2777 | 2777 | | |
2778 | 2778 | | |
2779 | 2779 | | |
2780 | | - | |
2781 | | - | |
2782 | | - | |
2783 | | - | |
2784 | | - | |
2785 | | - | |
2786 | | - | |
2787 | | - | |
2788 | | - | |
| 2780 | + | |
| 2781 | + | |
| 2782 | + | |
2789 | 2783 | | |
2790 | | - | |
2791 | | - | |
2792 | | - | |
2793 | | - | |
2794 | | - | |
2795 | | - | |
2796 | | - | |
2797 | | - | |
2798 | | - | |
2799 | | - | |
2800 | | - | |
2801 | | - | |
2802 | | - | |
2803 | | - | |
2804 | | - | |
2805 | | - | |
2806 | | - | |
2807 | | - | |
2808 | | - | |
| 2784 | + | |
2809 | 2785 | | |
2810 | 2786 | | |
2811 | 2787 | | |
| |||
2982 | 2958 | | |
2983 | 2959 | | |
2984 | 2960 | | |
| 2961 | + | |
2985 | 2962 | | |
2986 | 2963 | | |
2987 | 2964 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
229 | 230 | | |
230 | 231 | | |
231 | 232 | | |
232 | | - | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
233 | 236 | | |
234 | 237 | | |
235 | 238 | | |
| |||
238 | 241 | | |
239 | 242 | | |
240 | 243 | | |
| 244 | + | |
241 | 245 | | |
242 | 246 | | |
243 | 247 | | |
| |||
249 | 253 | | |
250 | 254 | | |
251 | 255 | | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | 256 | | |
282 | 257 | | |
283 | 258 | | |
284 | | - | |
| 259 | + | |
| 260 | + | |
285 | 261 | | |
286 | 262 | | |
287 | 263 | | |
288 | 264 | | |
289 | 265 | | |
290 | 266 | | |
291 | 267 | | |
292 | | - | |
| 268 | + | |
293 | 269 | | |
294 | 270 | | |
295 | 271 | | |
296 | | - | |
297 | | - | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
298 | 287 | | |
299 | 288 | | |
300 | 289 | | |
| |||
327 | 316 | | |
328 | 317 | | |
329 | 318 | | |
| 319 | + | |
330 | 320 | | |
331 | 321 | | |
332 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
333 | 329 | | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
339 | 333 | | |
340 | 334 | | |
341 | 335 | | |
342 | 336 | | |
343 | 337 | | |
344 | 338 | | |
345 | 339 | | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | 340 | | |
351 | 341 | | |
352 | 342 | | |
| |||
357 | 347 | | |
358 | 348 | | |
359 | 349 | | |
360 | | - | |
361 | | - | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
362 | 353 | | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
367 | 359 | | |
368 | | - | |
369 | 360 | | |
370 | 361 | | |
371 | 362 | | |
| |||
0 commit comments