11//! A subset of a mir body used for const evaluatability checking.
22use crate :: ty:: {
3- self , subst :: SubstsRef , Const , EarlyBinder , FallibleTypeFolder , Ty , TyCtxt , TypeFoldable ,
4- TypeSuperFoldable , TypeVisitable ,
3+ self , Const , EarlyBinder , Ty , TyCtxt , TypeFoldable , TypeFolder , TypeSuperFoldable ,
4+ TypeVisitable ,
55} ;
66use rustc_errors:: ErrorGuaranteed ;
77use rustc_hir:: def_id:: DefId ;
@@ -36,7 +36,10 @@ pub type BoundAbstractConst<'tcx> = Result<Option<EarlyBinder<ty::Const<'tcx>>>,
3636
3737impl < ' tcx > TyCtxt < ' tcx > {
3838 /// Returns a const without substs applied
39- fn bound_abstract_const ( self , uv : ty:: WithOptConstParam < DefId > ) -> BoundAbstractConst < ' tcx > {
39+ pub fn bound_abstract_const (
40+ self ,
41+ uv : ty:: WithOptConstParam < DefId > ,
42+ ) -> BoundAbstractConst < ' tcx > {
4043 let ac = if let Some ( ( did, param_did) ) = uv. as_const_arg ( ) {
4144 self . thir_abstract_const_of_const_arg ( ( did, param_did) )
4245 } else {
@@ -45,70 +48,37 @@ impl<'tcx> TyCtxt<'tcx> {
4548 Ok ( ac?. map ( |ac| EarlyBinder ( ac) ) )
4649 }
4750
48- pub fn expand_abstract_consts < T : TypeFoldable < ' tcx > > (
49- self ,
50- ac : T ,
51- ) -> Result < Option < T > , ErrorGuaranteed > {
52- self . _expand_abstract_consts ( ac, true )
53- }
54-
55- pub fn expand_unevaluated_abstract_const (
56- self ,
57- did : ty:: WithOptConstParam < DefId > ,
58- substs : SubstsRef < ' tcx > ,
59- ) -> Result < Option < ty:: Const < ' tcx > > , ErrorGuaranteed > {
60- let Some ( ac) = self . bound_abstract_const ( did) ? else {
61- return Ok ( None ) ;
62- } ;
63- let substs = self . erase_regions ( substs) ;
64- let ac = ac. subst ( self , substs) ;
65- self . _expand_abstract_consts ( ac, false )
66- }
67-
68- fn _expand_abstract_consts < T : TypeFoldable < ' tcx > > (
69- self ,
70- ac : T ,
71- first : bool ,
72- ) -> Result < Option < T > , ErrorGuaranteed > {
51+ pub fn expand_abstract_consts < T : TypeFoldable < ' tcx > > ( self , ac : T ) -> T {
7352 struct Expander < ' tcx > {
7453 tcx : TyCtxt < ' tcx > ,
75- first : bool ,
7654 }
7755
78- impl < ' tcx > FallibleTypeFolder < ' tcx > for Expander < ' tcx > {
79- type Error = Option < ErrorGuaranteed > ;
56+ impl < ' tcx > TypeFolder < ' tcx > for Expander < ' tcx > {
8057 fn tcx ( & self ) -> TyCtxt < ' tcx > {
8158 self . tcx
8259 }
83- fn try_fold_ty ( & mut self , ty : Ty < ' tcx > ) -> Result < Ty < ' tcx > , Self :: Error > {
60+ fn fold_ty ( & mut self , ty : Ty < ' tcx > ) -> Ty < ' tcx > {
8461 if ty. has_type_flags ( ty:: TypeFlags :: HAS_CT_PROJECTION ) {
85- ty. try_super_fold_with ( self )
62+ ty. super_fold_with ( self )
8663 } else {
87- Ok ( ty )
64+ ty
8865 }
8966 }
90- fn try_fold_const ( & mut self , c : Const < ' tcx > ) -> Result < Const < ' tcx > , Self :: Error > {
67+ fn fold_const ( & mut self , c : Const < ' tcx > ) -> Const < ' tcx > {
9168 let ct = match c. kind ( ) {
92- ty:: ConstKind :: Unevaluated ( uv) => {
93- if let Some ( bac) = self . tcx . bound_abstract_const ( uv. def ) ? {
69+ ty:: ConstKind :: Unevaluated ( uv) => match self . tcx . bound_abstract_const ( uv. def ) {
70+ Err ( e) => self . tcx . const_error_with_guaranteed ( c. ty ( ) , e) ,
71+ Ok ( Some ( bac) ) => {
9472 let substs = self . tcx . erase_regions ( uv. substs ) ;
9573 bac. subst ( self . tcx , substs)
96- } else if self . first {
97- return Err ( None ) ;
98- } else {
99- c
10074 }
101- }
75+ Ok ( None ) => c,
76+ } ,
10277 _ => c,
10378 } ;
104- self . first = false ;
105- ct. try_super_fold_with ( self )
79+ ct. super_fold_with ( self )
10680 }
10781 }
108- match ac. try_fold_with ( & mut Expander { tcx : self , first } ) {
109- Ok ( c) => Ok ( Some ( c) ) ,
110- Err ( None ) => Ok ( None ) ,
111- Err ( Some ( e) ) => Err ( e) ,
112- }
82+ ac. fold_with ( & mut Expander { tcx : self } )
11383 }
11484}
0 commit comments