1
1
//! A subset of a mir body used for const evaluatability checking.
2
2
use 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 ,
5
5
} ;
6
6
use rustc_errors:: ErrorGuaranteed ;
7
7
use rustc_hir:: def_id:: DefId ;
@@ -36,7 +36,10 @@ pub type BoundAbstractConst<'tcx> = Result<Option<EarlyBinder<ty::Const<'tcx>>>,
36
36
37
37
impl < ' tcx > TyCtxt < ' tcx > {
38
38
/// 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 > {
40
43
let ac = if let Some ( ( did, param_did) ) = uv. as_const_arg ( ) {
41
44
self . thir_abstract_const_of_const_arg ( ( did, param_did) )
42
45
} else {
@@ -45,70 +48,37 @@ impl<'tcx> TyCtxt<'tcx> {
45
48
Ok ( ac?. map ( |ac| EarlyBinder ( ac) ) )
46
49
}
47
50
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 {
73
52
struct Expander < ' tcx > {
74
53
tcx : TyCtxt < ' tcx > ,
75
- first : bool ,
76
54
}
77
55
78
- impl < ' tcx > FallibleTypeFolder < ' tcx > for Expander < ' tcx > {
79
- type Error = Option < ErrorGuaranteed > ;
56
+ impl < ' tcx > TypeFolder < ' tcx > for Expander < ' tcx > {
80
57
fn tcx ( & self ) -> TyCtxt < ' tcx > {
81
58
self . tcx
82
59
}
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 > {
84
61
if ty. has_type_flags ( ty:: TypeFlags :: HAS_CT_PROJECTION ) {
85
- ty. try_super_fold_with ( self )
62
+ ty. super_fold_with ( self )
86
63
} else {
87
- Ok ( ty )
64
+ ty
88
65
}
89
66
}
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 > {
91
68
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) ) => {
94
72
let substs = self . tcx . erase_regions ( uv. substs ) ;
95
73
bac. subst ( self . tcx , substs)
96
- } else if self . first {
97
- return Err ( None ) ;
98
- } else {
99
- c
100
74
}
101
- }
75
+ Ok ( None ) => c,
76
+ } ,
102
77
_ => c,
103
78
} ;
104
- self . first = false ;
105
- ct. try_super_fold_with ( self )
79
+ ct. super_fold_with ( self )
106
80
}
107
81
}
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 } )
113
83
}
114
84
}
0 commit comments