@@ -41,36 +41,30 @@ impl ConstantCx {
4141pub ( crate ) fn check_constants ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > ) -> bool {
4242 let mut all_constants_ok = true ;
4343 for constant in & fx. mir . required_consts {
44- let const_ = match fx. monomorphize ( constant. literal ) {
45- ConstantKind :: Ty ( ct) => ct,
44+ let unevaluated = match fx. monomorphize ( constant. literal ) {
45+ ConstantKind :: Ty ( ct) => match ct. kind ( ) {
46+ ConstKind :: Unevaluated ( uv) => uv. expand ( ) ,
47+ ConstKind :: Value ( _) => continue ,
48+ ConstKind :: Param ( _)
49+ | ConstKind :: Infer ( _)
50+ | ConstKind :: Bound ( _, _)
51+ | ConstKind :: Placeholder ( _)
52+ | ConstKind :: Error ( _) => unreachable ! ( "{:?}" , ct) ,
53+ } ,
54+ ConstantKind :: Unevaluated ( uv, _) => uv,
4655 ConstantKind :: Val ( ..) => continue ,
4756 } ;
48- match const_. kind ( ) {
49- ConstKind :: Value ( _) => { }
50- ConstKind :: Unevaluated ( unevaluated) => {
51- if let Err ( err) =
52- fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , unevaluated, None )
53- {
54- all_constants_ok = false ;
55- match err {
56- ErrorHandled :: Reported ( _) | ErrorHandled :: Linted => {
57- fx. tcx . sess . span_err ( constant. span , "erroneous constant encountered" ) ;
58- }
59- ErrorHandled :: TooGeneric => {
60- span_bug ! (
61- constant. span,
62- "codegen encountered polymorphic constant: {:?}" ,
63- err
64- ) ;
65- }
66- }
57+
58+ if let Err ( err) = fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , unevaluated, None ) {
59+ all_constants_ok = false ;
60+ match err {
61+ ErrorHandled :: Reported ( _) | ErrorHandled :: Linted => {
62+ fx. tcx . sess . span_err ( constant. span , "erroneous constant encountered" ) ;
63+ }
64+ ErrorHandled :: TooGeneric => {
65+ span_bug ! ( constant. span, "codegen encountered polymorphic constant: {:?}" , err) ;
6766 }
6867 }
69- ConstKind :: Param ( _)
70- | ConstKind :: Infer ( _)
71- | ConstKind :: Bound ( _, _)
72- | ConstKind :: Placeholder ( _)
73- | ConstKind :: Error ( _) => unreachable ! ( "{:?}" , const_) ,
7468 }
7569 }
7670 all_constants_ok
@@ -122,36 +116,28 @@ pub(crate) fn codegen_constant<'tcx>(
122116 fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
123117 constant : & Constant < ' tcx > ,
124118) -> CValue < ' tcx > {
125- let const_ = match fx. monomorphize ( constant. literal ) {
126- ConstantKind :: Ty ( ct) => ct,
127- ConstantKind :: Val ( val, ty) => return codegen_const_value ( fx, val, ty) ,
128- } ;
129- let const_val = match const_. kind ( ) {
130- ConstKind :: Value ( valtree) => fx. tcx . valtree_to_const_val ( ( const_. ty ( ) , valtree) ) ,
131- ConstKind :: Unevaluated ( ty:: Unevaluated { def, substs, promoted } )
119+ let ( const_val, ty) = match fx. monomorphize ( constant. literal ) {
120+ ConstantKind :: Ty ( const_) => unreachable ! ( "{:?}" , const_) ,
121+ ConstantKind :: Unevaluated ( ty:: Unevaluated { def, substs, promoted } , ty)
132122 if fx. tcx . is_static ( def. did ) =>
133123 {
134124 assert ! ( substs. is_empty( ) ) ;
135125 assert ! ( promoted. is_none( ) ) ;
136126
137- return codegen_static_ref ( fx, def. did , fx. layout_of ( const_ . ty ( ) ) ) . to_cvalue ( fx) ;
127+ return codegen_static_ref ( fx, def. did , fx. layout_of ( ty ) ) . to_cvalue ( fx) ;
138128 }
139- ConstKind :: Unevaluated ( unevaluated) => {
129+ ConstantKind :: Unevaluated ( unevaluated, ty ) => {
140130 match fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , unevaluated, None ) {
141- Ok ( const_val) => const_val,
131+ Ok ( const_val) => ( const_val, ty ) ,
142132 Err ( _) => {
143133 span_bug ! ( constant. span, "erroneous constant not captured by required_consts" ) ;
144134 }
145135 }
146136 }
147- ConstKind :: Param ( _)
148- | ConstKind :: Infer ( _)
149- | ConstKind :: Bound ( _, _)
150- | ConstKind :: Placeholder ( _)
151- | ConstKind :: Error ( _) => unreachable ! ( "{:?}" , const_) ,
137+ ConstantKind :: Val ( val, ty) => ( val, ty) ,
152138 } ;
153139
154- codegen_const_value ( fx, const_val, const_ . ty ( ) )
140+ codegen_const_value ( fx, const_val, ty )
155141}
156142
157143pub ( crate ) fn codegen_const_value < ' tcx > (
@@ -496,6 +482,9 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
496482 . eval_for_mir ( fx. tcx , ParamEnv :: reveal_all ( ) )
497483 . try_to_value ( fx. tcx ) ,
498484 ConstantKind :: Val ( val, _) => Some ( val) ,
485+ ConstantKind :: Unevaluated ( uv, _) => {
486+ fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , uv, None ) . ok ( )
487+ }
499488 } ,
500489 // FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
501490 // inside a temporary before being passed to the intrinsic requiring the const argument.
0 commit comments