@@ -14,6 +14,7 @@ use crate::ty::{DelaySpanBugEmitted, List, ParamEnv, TyS};
14
14
use polonius_engine:: Atom ;
15
15
use rustc_ast as ast;
16
16
use rustc_data_structures:: captures:: Captures ;
17
+ use rustc_errors:: ErrorReported ;
17
18
use rustc_hir as hir;
18
19
use rustc_hir:: def_id:: DefId ;
19
20
use rustc_index:: vec:: Idx ;
@@ -388,15 +389,23 @@ impl<'tcx> ClosureSubsts<'tcx> {
388
389
self . split ( ) . parent_substs
389
390
}
390
391
392
+ /// Returns an iterator that iterates the types of paths captured by a closure.
393
+ /// Note it's possible that there was a type error that prevented us from figuring out
394
+ /// the types of the upvars captured by the closure.
395
+ ///
396
+ /// This function can be safely called if `self.tupled_upvars_ty().is_ok()` is true.
391
397
#[ inline]
392
398
pub fn upvar_tys ( self ) -> impl Iterator < Item = Ty < ' tcx > > + ' tcx {
393
- self . tupled_upvars_ty ( ) . tuple_fields ( )
399
+ self . split ( ) . tupled_upvars_ty . expect_ty ( ) . tuple_fields ( )
394
400
}
395
401
396
- /// Returns the tuple type representing the upvars for this closure.
402
+ /// Returns a tuple type containing the types of paths captured by the closure.
403
+ /// Returns Err(ErrorReported) if a type error prevented us from figuring out
404
+ /// the types of the upvars for this closure.
397
405
#[ inline]
398
- pub fn tupled_upvars_ty ( self ) -> Ty < ' tcx > {
399
- self . split ( ) . tupled_upvars_ty . expect_ty ( )
406
+ pub fn tupled_upvars_ty ( self ) -> Result < Ty < ' tcx > , ErrorReported > {
407
+ let tupled_ty = self . split ( ) . tupled_upvars_ty . expect_ty ( ) ;
408
+ if let TyKind :: Error ( _) = tupled_ty. kind ( ) { Err ( ErrorReported ) } else { Ok ( tupled_ty) }
400
409
}
401
410
402
411
/// Returns the closure kind for this closure; may return a type
@@ -515,15 +524,23 @@ impl<'tcx> GeneratorSubsts<'tcx> {
515
524
self . split ( ) . witness . expect_ty ( )
516
525
}
517
526
527
+ /// Returns an iterator that iterates the types of paths captured by a generator.
528
+ /// Note it's possible that there was a type error that prevented us from figuring out
529
+ /// the types of the upvars captured by the generator.
530
+ ///
531
+ /// This function can be safely called if `self.tupled_upvars_ty().is_ok()` is true.
518
532
#[ inline]
519
533
pub fn upvar_tys ( self ) -> impl Iterator < Item = Ty < ' tcx > > + ' tcx {
520
- self . tupled_upvars_ty ( ) . tuple_fields ( )
534
+ self . split ( ) . tupled_upvars_ty . expect_ty ( ) . tuple_fields ( )
521
535
}
522
536
523
- /// Returns the tuple type representing the upvars for this generator.
537
+ /// Returns a tuple type containing the types of paths captured by the generator.
538
+ /// Returns Err(ErrorReported) if a type error prevented us from figuring out
539
+ /// the types of the upvars for this generator.
524
540
#[ inline]
525
- pub fn tupled_upvars_ty ( self ) -> Ty < ' tcx > {
526
- self . split ( ) . tupled_upvars_ty . expect_ty ( )
541
+ pub fn tupled_upvars_ty ( self ) -> Result < Ty < ' tcx > , ErrorReported > {
542
+ let tupled_ty = self . split ( ) . tupled_upvars_ty . expect_ty ( ) ;
543
+ if let TyKind :: Error ( _) = tupled_ty. kind ( ) { Err ( ErrorReported ) } else { Ok ( tupled_ty) }
527
544
}
528
545
529
546
/// Returns the type representing the resume type of the generator.
@@ -660,6 +677,11 @@ pub enum UpvarSubsts<'tcx> {
660
677
}
661
678
662
679
impl < ' tcx > UpvarSubsts < ' tcx > {
680
+ /// Returns an iterator that iterates the types of paths captured by a closure/generator.
681
+ /// Note it's possible that there was a type error that prevented us from figuring out
682
+ /// the types of the upvars captured by the closure/generator.
683
+ ///
684
+ /// This function can be safely called if `self.tupled_upvars_ty().is_ok()` is true.
663
685
#[ inline]
664
686
pub fn upvar_tys ( self ) -> impl Iterator < Item = Ty < ' tcx > > + ' tcx {
665
687
let tupled_upvars_ty = match self {
@@ -669,8 +691,11 @@ impl<'tcx> UpvarSubsts<'tcx> {
669
691
tupled_upvars_ty. expect_ty ( ) . tuple_fields ( )
670
692
}
671
693
694
+ /// Returns a tuple type containing the types of paths captured by a closure/generator.
695
+ /// Returns Err(ErrorReported) if a type error prevented us from figuring out
696
+ /// the types of the upvars for this closure/generator.
672
697
#[ inline]
673
- pub fn tupled_upvars_ty ( self ) -> Ty < ' tcx > {
698
+ pub fn tupled_upvars_ty ( self ) -> Result < Ty < ' tcx > , ErrorReported > {
674
699
match self {
675
700
UpvarSubsts :: Closure ( substs) => substs. as_closure ( ) . tupled_upvars_ty ( ) ,
676
701
UpvarSubsts :: Generator ( substs) => substs. as_generator ( ) . tupled_upvars_ty ( ) ,
0 commit comments