You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We had included a reference desugaring of `use<..>` in RPIT using
ATPIT. Let's add a similar desugaring for `use<..>` in RPITIT.
In doing this, we'll make some changes to the RPIT desugaring so that
it better parallels the RPITIT one. In particular, we want to
turbofish all generic parameters for clarity, and we want to eliminate
all function arguments for conciseness. Doing this means that all of
the lifetimes become early bound. This seems fine, since it's rather
orthogonal to the semantics we're trying to demonstrate here.
We also want to demonstrate using const generics in the hidden type.
We could do this using arrays, e.g. `[(); N]`, but it seems more clear
to just define a type constructor that uses all of the generics, so
we'll sacrifice a bit of conciseness to do it that way.
Associated type position `impl Trait` (ATPIT) can also be used, more verbosely, to control capturing of generic parameters in opaque types. We can use this to describe the semantics of `use<..>`. If we consider the following code:
210
+
The desugarings that follow can be used to answer questions about how `use<..>` is expected to work with respect to the capturing of generic parameters in opaque types.
211
+
212
+
### Reference desugaring for `use<..>` in RPIT
213
+
214
+
Associated type position `impl Trait` (ATPIT) can be used, more verbosely, to control capturing of generic parameters in opaque types. We can use this to describe the semantics of `use<..>`. If we consider the following code:
211
215
212
216
```rust
213
-
structTy<'u, U, constCU:u8>(&'u (), U);
217
+
usecore::marker::PhantomData;
214
218
215
-
impl<'u, U, constCU:u8> Ty<'u, U, CU> {
219
+
structC<'s, 't, S, T, constCS:u8, constCT:u8> {
220
+
_p:PhantomData<(&'s (), &'t (), S, T)>,
221
+
}
222
+
223
+
structTy<'s, S, constCS:u8>(&'s (), S);
224
+
impl<'s, S, constCS:u8> Ty<'s, S, CS> {
216
225
pubfnf<'t, T, constCT:u8>(
217
-
self, x:&'t (), y:T,
218
-
) ->impluse<'u, 't, U, T, CU, CT> Sized {
219
-
(self, x, y, CU, CT)
226
+
) ->impluse<'s, 't, S, T, CS, CT> Sized {
227
+
// ^^^^^^^^^^^^^^^^^^^^^^^^^
228
+
// This is the `use<..>` specifier to desugar.
229
+
C::<'s, 't, S, T, CS, CT> { _p:PhantomData }
220
230
}
221
231
}
222
232
```
223
233
224
-
Then, using ATPIT, we could desugar this as followswhile preserving equivalent semantics:
234
+
Thenwe can desugar this as follows, without the use of a `use<..>` specifier, while preserving equivalent semantics with respect to the capturing of generic parameters:
225
235
226
236
```rust
227
-
structTy<'u, U, constCU:u8>(&'u (), U);
237
+
usecore::marker::PhantomData;
228
238
229
-
impl<'u, U, constCU:u8> Ty<'u, U, CU> {
239
+
structC<'s, 't, S, T, constCS:u8, constCT:u8> {
240
+
_p:PhantomData<(&'s (), &'t (), S, T)>,
241
+
}
242
+
243
+
structTy<'s, S, constCS:u8>(&'s (), S);
244
+
impl<'s, S, constCS:u8> Ty<'s, S, CS> {
230
245
pubfnf<'t, T, constCT:u8>(
231
-
self, x:&'t (), y:T,
232
-
) -> <() as_0::H>::Opaque<'u, 't, U, T, CU, CT> {
233
-
<() as_0::H>::f(self, x, y)
246
+
) -> <() as_0::H>::Opaque<'s, 't, S, T, CS, CT> {
247
+
// ^^^^^^^^^^^^^^^^^^^^
248
+
// These are the arguments given to the `use<..>` specifier.
249
+
//
250
+
// Reducing what is captured by removing arguments from
251
+
// `use<..>` is equivalent to removing arguments from this
252
+
// list and as needed below.
253
+
<() as_0::H>::f::<'s, 't, S, T, CS, CT>()
234
254
}
235
255
}
236
256
237
257
mod_0 {
238
258
usesuper::*;
239
259
pubtraitH {
240
-
typeOpaque<'u, 't, U, T, constCU:u8, constCT:u8>;
241
-
fnf<'u, 't, U, T, constCU:u8, constCT:u8>(
242
-
s:Ty<'u, U, CU>, x:&'t (), y:T,
243
-
) ->Self::Opaque<'u, 't, U, T, CU, CT>;
260
+
typeOpaque<'s, 't, S, T, constCS:u8, constCT:u8>;
261
+
fnf<'s, 't, S, T, constCS:u8, constCT:u8>(
262
+
) ->Self::Opaque<'s, 't, S, T, CS, CT>;
244
263
}
245
264
implHfor () {
246
-
typeOpaque<'u, 't, U, T, constCU:u8, constCT:u8>
265
+
typeOpaque<'s, 't, S, T, constCS:u8, constCT:u8>
247
266
=implSized;
248
267
#[inline(always)]
249
-
fnf<'u, 't, U, T, constCU:u8, constCT:u8>(
250
-
s:Ty<'u, U, CU>, x:&'t (), y:T,
251
-
) ->Self::Opaque<'u, 't, U, T, CU, CT> {
252
-
(s, x, y, CU, CT)
268
+
fnf<'s, 't, S, T, constCS:u8, constCT:u8>(
269
+
) ->Self::Opaque<'s, 't, S, T, CS, CT> {
270
+
C::<'s, 't, S, T, CS, CT> { _p:PhantomData }
271
+
}
272
+
}
273
+
}
274
+
```
275
+
276
+
### Reference desugaring for `use<..>` in RPITIT
277
+
278
+
Similarly, we can describe the semantics of `use<..>` in return position `impl Trait` in trait (RPITIT) using anonymous associated types and ATPIT. If we consider the following code:
Then we can desugar this as follows, without the use of `use<..>` specifiers, while preserving equivalent semantics with respect to the capturing of generic parameters:
0 commit comments