@@ -63,7 +63,9 @@ use rustc_index::bit_set::{BitMatrix, BitSet, GrowableBitSet};
6363use rustc_index:: { Idx , IndexVec } ;
6464use rustc_middle:: mir:: visit:: { MutVisitor , PlaceContext , Visitor } ;
6565use 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+ } ;
6769use rustc_middle:: { bug, span_bug} ;
6870use rustc_mir_dataflow:: impls:: {
6971 MaybeBorrowedLocals , MaybeLiveLocals , MaybeRequiresStorage , MaybeStorageLive ,
@@ -210,14 +212,10 @@ impl<'tcx> TransformVisitor<'tcx> {
210212 // `gen` continues return `None`
211213 CoroutineKind :: Desugared ( CoroutineDesugaring :: Gen , _) => {
212214 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 ( ) ] ) ,
221219 IndexVec :: new ( ) ,
222220 )
223221 }
@@ -266,64 +264,28 @@ impl<'tcx> TransformVisitor<'tcx> {
266264 is_return : bool ,
267265 statements : & mut Vec < Statement < ' tcx > > ,
268266 ) {
267+ const ZERO : VariantIdx = VariantIdx :: ZERO ;
268+ const ONE : VariantIdx = VariantIdx :: from_usize ( 1 ) ;
269269 let rvalue = match self . coroutine_kind {
270270 CoroutineKind :: Desugared ( CoroutineDesugaring :: Async , _) => {
271271 let poll_def_id = self . tcx . require_lang_item ( LangItem :: Poll , None ) ;
272272 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)
285275 } 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)
298279 }
299280 CoroutineKind :: Desugared ( CoroutineDesugaring :: Gen , _) => {
300281 let option_def_id = self . tcx . require_lang_item ( LangItem :: Option , None ) ;
301282 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
314285 } 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)
327289 }
328290 CoroutineKind :: Desugared ( CoroutineDesugaring :: AsyncGen , _) => {
329291 if is_return {
@@ -349,31 +311,17 @@ impl<'tcx> TransformVisitor<'tcx> {
349311 let coroutine_state_def_id =
350312 self . tcx . require_lang_item ( LangItem :: CoroutineState , None ) ;
351313 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)
364316 } 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+ )
377325 }
378326 } ;
379327
@@ -509,6 +457,15 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> {
509457 }
510458}
511459
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+
512469fn make_coroutine_state_argument_indirect < ' tcx > ( tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
513470 let coroutine_ty = body. local_decls . raw [ 1 ] . ty ;
514471
0 commit comments