@@ -63,7 +63,9 @@ use rustc_index::bit_set::{BitMatrix, BitSet, GrowableBitSet};
63
63
use rustc_index:: { Idx , IndexVec } ;
64
64
use rustc_middle:: mir:: visit:: { MutVisitor , PlaceContext , Visitor } ;
65
65
use rustc_middle:: mir:: * ;
66
- use rustc_middle:: ty:: { self , CoroutineArgs , CoroutineArgsExt , InstanceKind , Ty , TyCtxt } ;
66
+ use rustc_middle:: ty:: {
67
+ self , CoroutineArgs , CoroutineArgsExt , GenericArgsRef , InstanceKind , Ty , TyCtxt ,
68
+ } ;
67
69
use rustc_middle:: { bug, span_bug} ;
68
70
use rustc_mir_dataflow:: impls:: {
69
71
MaybeBorrowedLocals , MaybeLiveLocals , MaybeRequiresStorage , MaybeStorageLive ,
@@ -210,14 +212,10 @@ impl<'tcx> TransformVisitor<'tcx> {
210
212
// `gen` continues return `None`
211
213
CoroutineKind :: Desugared ( CoroutineDesugaring :: Gen , _) => {
212
214
let option_def_id = self . tcx . require_lang_item ( LangItem :: Option , None ) ;
213
- Rvalue :: Aggregate (
214
- Box :: new ( AggregateKind :: Adt (
215
- option_def_id,
216
- VariantIdx :: ZERO ,
217
- self . tcx . mk_args ( & [ self . old_yield_ty . into ( ) ] ) ,
218
- None ,
219
- None ,
220
- ) ) ,
215
+ make_aggregate_adt (
216
+ option_def_id,
217
+ VariantIdx :: ZERO ,
218
+ self . tcx . mk_args ( & [ self . old_yield_ty . into ( ) ] ) ,
221
219
IndexVec :: new ( ) ,
222
220
)
223
221
}
@@ -266,64 +264,28 @@ impl<'tcx> TransformVisitor<'tcx> {
266
264
is_return : bool ,
267
265
statements : & mut Vec < Statement < ' tcx > > ,
268
266
) {
267
+ const ZERO : VariantIdx = VariantIdx :: ZERO ;
268
+ const ONE : VariantIdx = VariantIdx :: from_usize ( 1 ) ;
269
269
let rvalue = match self . coroutine_kind {
270
270
CoroutineKind :: Desugared ( CoroutineDesugaring :: Async , _) => {
271
271
let poll_def_id = self . tcx . require_lang_item ( LangItem :: Poll , None ) ;
272
272
let args = self . tcx . mk_args ( & [ self . old_ret_ty . into ( ) ] ) ;
273
- if is_return {
274
- // Poll::Ready(val)
275
- Rvalue :: Aggregate (
276
- Box :: new ( AggregateKind :: Adt (
277
- poll_def_id,
278
- VariantIdx :: ZERO ,
279
- args,
280
- None ,
281
- None ,
282
- ) ) ,
283
- IndexVec :: from_raw ( vec ! [ val] ) ,
284
- )
273
+ let ( variant_idx, operands) = if is_return {
274
+ ( ZERO , IndexVec :: from_raw ( vec ! [ val] ) ) // Poll::Ready(val)
285
275
} else {
286
- // Poll::Pending
287
- Rvalue :: Aggregate (
288
- Box :: new ( AggregateKind :: Adt (
289
- poll_def_id,
290
- VariantIdx :: from_usize ( 1 ) ,
291
- args,
292
- None ,
293
- None ,
294
- ) ) ,
295
- IndexVec :: new ( ) ,
296
- )
297
- }
276
+ ( ONE , IndexVec :: new ( ) ) // Poll::Pending
277
+ } ;
278
+ make_aggregate_adt ( poll_def_id, variant_idx, args, operands)
298
279
}
299
280
CoroutineKind :: Desugared ( CoroutineDesugaring :: Gen , _) => {
300
281
let option_def_id = self . tcx . require_lang_item ( LangItem :: Option , None ) ;
301
282
let args = self . tcx . mk_args ( & [ self . old_yield_ty . into ( ) ] ) ;
302
- if is_return {
303
- // None
304
- Rvalue :: Aggregate (
305
- Box :: new ( AggregateKind :: Adt (
306
- option_def_id,
307
- VariantIdx :: ZERO ,
308
- args,
309
- None ,
310
- None ,
311
- ) ) ,
312
- IndexVec :: new ( ) ,
313
- )
283
+ let ( variant_idx, operands) = if is_return {
284
+ ( ZERO , IndexVec :: new ( ) ) // None
314
285
} else {
315
- // Some(val)
316
- Rvalue :: Aggregate (
317
- Box :: new ( AggregateKind :: Adt (
318
- option_def_id,
319
- VariantIdx :: from_usize ( 1 ) ,
320
- args,
321
- None ,
322
- None ,
323
- ) ) ,
324
- IndexVec :: from_raw ( vec ! [ val] ) ,
325
- )
326
- }
286
+ ( ONE , IndexVec :: from_raw ( vec ! [ val] ) ) // Some(val)
287
+ } ;
288
+ make_aggregate_adt ( option_def_id, variant_idx, args, operands)
327
289
}
328
290
CoroutineKind :: Desugared ( CoroutineDesugaring :: AsyncGen , _) => {
329
291
if is_return {
@@ -349,31 +311,17 @@ impl<'tcx> TransformVisitor<'tcx> {
349
311
let coroutine_state_def_id =
350
312
self . tcx . require_lang_item ( LangItem :: CoroutineState , None ) ;
351
313
let args = self . tcx . mk_args ( & [ self . old_yield_ty . into ( ) , self . old_ret_ty . into ( ) ] ) ;
352
- if is_return {
353
- // CoroutineState::Complete(val)
354
- Rvalue :: Aggregate (
355
- Box :: new ( AggregateKind :: Adt (
356
- coroutine_state_def_id,
357
- VariantIdx :: from_usize ( 1 ) ,
358
- args,
359
- None ,
360
- None ,
361
- ) ) ,
362
- IndexVec :: from_raw ( vec ! [ val] ) ,
363
- )
314
+ let variant_idx = if is_return {
315
+ ONE // CoroutineState::Complete(val)
364
316
} else {
365
- // CoroutineState::Yielded(val)
366
- Rvalue :: Aggregate (
367
- Box :: new ( AggregateKind :: Adt (
368
- coroutine_state_def_id,
369
- VariantIdx :: ZERO ,
370
- args,
371
- None ,
372
- None ,
373
- ) ) ,
374
- IndexVec :: from_raw ( vec ! [ val] ) ,
375
- )
376
- }
317
+ ZERO // CoroutineState::Yielded(val)
318
+ } ;
319
+ make_aggregate_adt (
320
+ coroutine_state_def_id,
321
+ variant_idx,
322
+ args,
323
+ IndexVec :: from_raw ( vec ! [ val] ) ,
324
+ )
377
325
}
378
326
} ;
379
327
@@ -509,6 +457,15 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> {
509
457
}
510
458
}
511
459
460
+ fn make_aggregate_adt < ' tcx > (
461
+ def_id : DefId ,
462
+ variant_idx : VariantIdx ,
463
+ args : GenericArgsRef < ' tcx > ,
464
+ operands : IndexVec < FieldIdx , Operand < ' tcx > > ,
465
+ ) -> Rvalue < ' tcx > {
466
+ Rvalue :: Aggregate ( Box :: new ( AggregateKind :: Adt ( def_id, variant_idx, args, None , None ) ) , operands)
467
+ }
468
+
512
469
fn make_coroutine_state_argument_indirect < ' tcx > ( tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
513
470
let coroutine_ty = body. local_decls . raw [ 1 ] . ty ;
514
471
0 commit comments