@@ -11,7 +11,7 @@ use crate::mir::interpret::ConstValue;
11
11
use crate :: mir:: interpret:: Scalar ;
12
12
use crate :: mir:: Promoted ;
13
13
use crate :: ty:: layout:: VariantIdx ;
14
- use crate :: ty:: subst:: { GenericArgKind , InternalSubsts , Subst , SubstsRef } ;
14
+ use crate :: ty:: subst:: { GenericArg , InternalSubsts , Subst , SubstsRef } ;
15
15
use crate :: ty:: {
16
16
self , AdtDef , DefIdTree , Discr , Ty , TyCtxt , TypeFlags , TypeFoldable , WithConstness ,
17
17
} ;
@@ -364,23 +364,21 @@ pub struct ClosureSubsts<'tcx> {
364
364
/// Struct returned by `split()`. Note that these are subslices of the
365
365
/// parent slice and not canonical substs themselves.
366
366
struct SplitClosureSubsts < ' tcx > {
367
- // FIXME(eddyb) maybe replace these with `GenericArg` to avoid having
368
- // `GenericArg::expect_ty` called on all of them when only one is used.
369
- closure_kind_ty : Ty < ' tcx > ,
370
- closure_sig_as_fn_ptr_ty : Ty < ' tcx > ,
371
- tupled_upvars_ty : Ty < ' tcx > ,
367
+ closure_kind_ty : GenericArg < ' tcx > ,
368
+ closure_sig_as_fn_ptr_ty : GenericArg < ' tcx > ,
369
+ tupled_upvars_ty : GenericArg < ' tcx > ,
372
370
}
373
371
374
372
impl < ' tcx > ClosureSubsts < ' tcx > {
375
373
/// Divides the closure substs into their respective
376
374
/// components. Single source of truth with respect to the
377
375
/// ordering.
378
376
fn split ( self ) -> SplitClosureSubsts < ' tcx > {
379
- let parent_len = self . substs . len ( ) - 3 ;
380
- SplitClosureSubsts {
381
- closure_kind_ty : self . substs . type_at ( parent_len ) ,
382
- closure_sig_as_fn_ptr_ty : self . substs . type_at ( parent_len + 1 ) ,
383
- tupled_upvars_ty : self . substs . type_at ( parent_len + 2 ) ,
377
+ match self . substs [ .. ] {
378
+ [ .. , closure_kind_ty , closure_sig_as_fn_ptr_ty , tupled_upvars_ty ] => {
379
+ SplitClosureSubsts { closure_kind_ty, closure_sig_as_fn_ptr_ty , tupled_upvars_ty }
380
+ }
381
+ _ => bug ! ( "closure substs missing synthetics" ) ,
384
382
}
385
383
}
386
384
@@ -390,29 +388,19 @@ impl<'tcx> ClosureSubsts<'tcx> {
390
388
/// Used primarily by `ty::print::pretty` to be able to handle closure
391
389
/// types that haven't had their synthetic types substituted in.
392
390
pub fn is_valid ( self ) -> bool {
393
- self . substs . len ( ) >= 3 && matches ! ( self . split( ) . tupled_upvars_ty. kind, Tuple ( _) )
391
+ self . substs . len ( ) >= 3 && matches ! ( self . split( ) . tupled_upvars_ty. expect_ty ( ) . kind, Tuple ( _) )
394
392
}
395
393
396
394
#[ inline]
397
395
pub fn upvar_tys ( self ) -> impl Iterator < Item = Ty < ' tcx > > + ' tcx {
398
- let upvars = match self . split ( ) . tupled_upvars_ty . kind {
399
- Tuple ( upvars) => upvars,
400
- _ => bug ! ( "upvars should be tupled" ) ,
401
- } ;
402
- upvars. iter ( ) . map ( |t| {
403
- if let GenericArgKind :: Type ( ty) = t. unpack ( ) {
404
- ty
405
- } else {
406
- bug ! ( "upvar should be type" )
407
- }
408
- } )
396
+ self . split ( ) . tupled_upvars_ty . expect_ty ( ) . tuple_fields ( )
409
397
}
410
398
411
399
/// Returns the closure kind for this closure; may return a type
412
400
/// variable during inference. To get the closure kind during
413
401
/// inference, use `infcx.closure_kind(substs)`.
414
402
pub fn kind_ty ( self ) -> Ty < ' tcx > {
415
- self . split ( ) . closure_kind_ty
403
+ self . split ( ) . closure_kind_ty . expect_ty ( )
416
404
}
417
405
418
406
/// Returns the `fn` pointer type representing the closure signature for this
@@ -421,7 +409,7 @@ impl<'tcx> ClosureSubsts<'tcx> {
421
409
// type is known at the time of the creation of `ClosureSubsts`,
422
410
// see `rustc_typeck::check::closure`.
423
411
pub fn sig_as_fn_ptr_ty ( self ) -> Ty < ' tcx > {
424
- self . split ( ) . closure_sig_as_fn_ptr_ty
412
+ self . split ( ) . closure_sig_as_fn_ptr_ty . expect_ty ( )
425
413
}
426
414
427
415
/// Returns the closure kind for this closure; only usable outside
@@ -430,7 +418,7 @@ impl<'tcx> ClosureSubsts<'tcx> {
430
418
///
431
419
/// If you have an inference context, use `infcx.closure_kind()`.
432
420
pub fn kind ( self ) -> ty:: ClosureKind {
433
- self . split ( ) . closure_kind_ty . to_opt_closure_kind ( ) . unwrap ( )
421
+ self . kind_ty ( ) . to_opt_closure_kind ( ) . unwrap ( )
434
422
}
435
423
436
424
/// Extracts the signature from the closure.
@@ -450,24 +438,20 @@ pub struct GeneratorSubsts<'tcx> {
450
438
}
451
439
452
440
struct SplitGeneratorSubsts < ' tcx > {
453
- // FIXME(eddyb) maybe replace these with `GenericArg` to avoid having
454
- // `GenericArg::expect_ty` called on all of them when only one is used.
455
- resume_ty : Ty < ' tcx > ,
456
- yield_ty : Ty < ' tcx > ,
457
- return_ty : Ty < ' tcx > ,
458
- witness : Ty < ' tcx > ,
459
- tupled_upvars_ty : Ty < ' tcx > ,
441
+ resume_ty : GenericArg < ' tcx > ,
442
+ yield_ty : GenericArg < ' tcx > ,
443
+ return_ty : GenericArg < ' tcx > ,
444
+ witness : GenericArg < ' tcx > ,
445
+ tupled_upvars_ty : GenericArg < ' tcx > ,
460
446
}
461
447
462
448
impl < ' tcx > GeneratorSubsts < ' tcx > {
463
449
fn split ( self ) -> SplitGeneratorSubsts < ' tcx > {
464
- let parent_len = self . substs . len ( ) - 5 ;
465
- SplitGeneratorSubsts {
466
- resume_ty : self . substs . type_at ( parent_len) ,
467
- yield_ty : self . substs . type_at ( parent_len + 1 ) ,
468
- return_ty : self . substs . type_at ( parent_len + 2 ) ,
469
- witness : self . substs . type_at ( parent_len + 3 ) ,
470
- tupled_upvars_ty : self . substs . type_at ( parent_len + 4 ) ,
450
+ match self . substs [ ..] {
451
+ [ .., resume_ty, yield_ty, return_ty, witness, tupled_upvars_ty] => {
452
+ SplitGeneratorSubsts { resume_ty, yield_ty, return_ty, witness, tupled_upvars_ty }
453
+ }
454
+ _ => bug ! ( "generator substs missing synthetics" ) ,
471
455
}
472
456
}
473
457
@@ -477,7 +461,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
477
461
/// Used primarily by `ty::print::pretty` to be able to handle generator
478
462
/// types that haven't had their synthetic types substituted in.
479
463
pub fn is_valid ( self ) -> bool {
480
- self . substs . len ( ) >= 5 && matches ! ( self . split( ) . tupled_upvars_ty. kind, Tuple ( _) )
464
+ self . substs . len ( ) >= 5 && matches ! ( self . split( ) . tupled_upvars_ty. expect_ty ( ) . kind, Tuple ( _) )
481
465
}
482
466
483
467
/// This describes the types that can be contained in a generator.
@@ -486,37 +470,27 @@ impl<'tcx> GeneratorSubsts<'tcx> {
486
470
/// The state transformation MIR pass may only produce layouts which mention types
487
471
/// in this tuple. Upvars are not counted here.
488
472
pub fn witness ( self ) -> Ty < ' tcx > {
489
- self . split ( ) . witness
473
+ self . split ( ) . witness . expect_ty ( )
490
474
}
491
475
492
476
#[ inline]
493
477
pub fn upvar_tys ( self ) -> impl Iterator < Item = Ty < ' tcx > > + ' tcx {
494
- let upvars = match self . split ( ) . tupled_upvars_ty . kind {
495
- Tuple ( upvars) => upvars,
496
- _ => bug ! ( "upvars should be tupled" ) ,
497
- } ;
498
- upvars. iter ( ) . map ( |t| {
499
- if let GenericArgKind :: Type ( ty) = t. unpack ( ) {
500
- ty
501
- } else {
502
- bug ! ( "upvar should be type" )
503
- }
504
- } )
478
+ self . split ( ) . tupled_upvars_ty . expect_ty ( ) . tuple_fields ( )
505
479
}
506
480
507
481
/// Returns the type representing the resume type of the generator.
508
482
pub fn resume_ty ( self ) -> Ty < ' tcx > {
509
- self . split ( ) . resume_ty
483
+ self . split ( ) . resume_ty . expect_ty ( )
510
484
}
511
485
512
486
/// Returns the type representing the yield type of the generator.
513
487
pub fn yield_ty ( self ) -> Ty < ' tcx > {
514
- self . split ( ) . yield_ty
488
+ self . split ( ) . yield_ty . expect_ty ( )
515
489
}
516
490
517
491
/// Returns the type representing the return type of the generator.
518
492
pub fn return_ty ( self ) -> Ty < ' tcx > {
519
- self . split ( ) . return_ty
493
+ self . split ( ) . return_ty . expect_ty ( )
520
494
}
521
495
522
496
/// Returns the "generator signature", which consists of its yield
@@ -645,17 +619,7 @@ impl<'tcx> UpvarSubsts<'tcx> {
645
619
UpvarSubsts :: Closure ( substs) => substs. as_closure ( ) . split ( ) . tupled_upvars_ty ,
646
620
UpvarSubsts :: Generator ( substs) => substs. as_generator ( ) . split ( ) . tupled_upvars_ty ,
647
621
} ;
648
- let upvars = match tupled_upvars_ty. kind {
649
- Tuple ( upvars) => upvars,
650
- _ => bug ! ( "upvars should be tupled" ) ,
651
- } ;
652
- upvars. iter ( ) . map ( |t| {
653
- if let GenericArgKind :: Type ( ty) = t. unpack ( ) {
654
- ty
655
- } else {
656
- bug ! ( "upvar should be type" )
657
- }
658
- } )
622
+ tupled_upvars_ty. expect_ty ( ) . tuple_fields ( )
659
623
}
660
624
}
661
625
0 commit comments