@@ -41,36 +41,30 @@ impl ConstantCx {
41
41
pub ( crate ) fn check_constants ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > ) -> bool {
42
42
let mut all_constants_ok = true ;
43
43
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,
46
55
ConstantKind :: Val ( ..) => continue ,
47
56
} ;
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) ;
67
66
}
68
67
}
69
- ConstKind :: Param ( _)
70
- | ConstKind :: Infer ( _)
71
- | ConstKind :: Bound ( _, _)
72
- | ConstKind :: Placeholder ( _)
73
- | ConstKind :: Error ( _) => unreachable ! ( "{:?}" , const_) ,
74
68
}
75
69
}
76
70
all_constants_ok
@@ -122,36 +116,28 @@ pub(crate) fn codegen_constant<'tcx>(
122
116
fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
123
117
constant : & Constant < ' tcx > ,
124
118
) -> 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)
132
122
if fx. tcx . is_static ( def. did ) =>
133
123
{
134
124
assert ! ( substs. is_empty( ) ) ;
135
125
assert ! ( promoted. is_none( ) ) ;
136
126
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) ;
138
128
}
139
- ConstKind :: Unevaluated ( unevaluated) => {
129
+ ConstantKind :: Unevaluated ( unevaluated, ty ) => {
140
130
match fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , unevaluated, None ) {
141
- Ok ( const_val) => const_val,
131
+ Ok ( const_val) => ( const_val, ty ) ,
142
132
Err ( _) => {
143
133
span_bug ! ( constant. span, "erroneous constant not captured by required_consts" ) ;
144
134
}
145
135
}
146
136
}
147
- ConstKind :: Param ( _)
148
- | ConstKind :: Infer ( _)
149
- | ConstKind :: Bound ( _, _)
150
- | ConstKind :: Placeholder ( _)
151
- | ConstKind :: Error ( _) => unreachable ! ( "{:?}" , const_) ,
137
+ ConstantKind :: Val ( val, ty) => ( val, ty) ,
152
138
} ;
153
139
154
- codegen_const_value ( fx, const_val, const_ . ty ( ) )
140
+ codegen_const_value ( fx, const_val, ty )
155
141
}
156
142
157
143
pub ( crate ) fn codegen_const_value < ' tcx > (
@@ -496,6 +482,9 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
496
482
. eval_for_mir ( fx. tcx , ParamEnv :: reveal_all ( ) )
497
483
. try_to_value ( fx. tcx ) ,
498
484
ConstantKind :: Val ( val, _) => Some ( val) ,
485
+ ConstantKind :: Unevaluated ( uv, _) => {
486
+ fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , uv, None ) . ok ( )
487
+ }
499
488
} ,
500
489
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
501
490
// inside a temporary before being passed to the intrinsic requiring the const argument.
0 commit comments