@@ -5,7 +5,6 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
5
5
use rustc_middle:: mir:: interpret:: {
6
6
read_target_uint, AllocId , ConstAllocation , ConstValue , ErrorHandled , GlobalAlloc , Scalar ,
7
7
} ;
8
- use rustc_middle:: ty:: ConstKind ;
9
8
use rustc_span:: DUMMY_SP ;
10
9
11
10
use cranelift_codegen:: ir:: GlobalValueData ;
@@ -41,36 +40,22 @@ impl ConstantCx {
41
40
pub ( crate ) fn check_constants ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > ) -> bool {
42
41
let mut all_constants_ok = true ;
43
42
for constant in & fx. mir . required_consts {
44
- let const_ = match fx. monomorphize ( constant. literal ) {
45
- ConstantKind :: Ty ( ct) => ct,
43
+ let unevaluated = match fx. monomorphize ( constant. literal ) {
44
+ ConstantKind :: Ty ( _) => unreachable ! ( ) ,
45
+ ConstantKind :: Unevaluated ( uv, _) => uv,
46
46
ConstantKind :: Val ( ..) => continue ,
47
47
} ;
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
- }
48
+
49
+ if let Err ( err) = fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , unevaluated, None ) {
50
+ all_constants_ok = false ;
51
+ match err {
52
+ ErrorHandled :: Reported ( _) | ErrorHandled :: Linted => {
53
+ fx. tcx . sess . span_err ( constant. span , "erroneous constant encountered" ) ;
54
+ }
55
+ ErrorHandled :: TooGeneric => {
56
+ span_bug ! ( constant. span, "codegen encountered polymorphic constant: {:?}" , err) ;
67
57
}
68
58
}
69
- ConstKind :: Param ( _)
70
- | ConstKind :: Infer ( _)
71
- | ConstKind :: Bound ( _, _)
72
- | ConstKind :: Placeholder ( _)
73
- | ConstKind :: Error ( _) => unreachable ! ( "{:?}" , const_) ,
74
59
}
75
60
}
76
61
all_constants_ok
@@ -122,36 +107,28 @@ pub(crate) fn codegen_constant<'tcx>(
122
107
fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
123
108
constant : & Constant < ' tcx > ,
124
109
) -> 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 } )
110
+ let ( const_val, ty) = match fx. monomorphize ( constant. literal ) {
111
+ ConstantKind :: Ty ( const_) => unreachable ! ( "{:?}" , const_) ,
112
+ ConstantKind :: Unevaluated ( mir:: UnevaluatedConst { def, substs, promoted } , ty)
132
113
if fx. tcx . is_static ( def. did ) =>
133
114
{
134
115
assert ! ( substs. is_empty( ) ) ;
135
116
assert ! ( promoted. is_none( ) ) ;
136
117
137
- return codegen_static_ref ( fx, def. did , fx. layout_of ( const_ . ty ( ) ) ) . to_cvalue ( fx) ;
118
+ return codegen_static_ref ( fx, def. did , fx. layout_of ( ty ) ) . to_cvalue ( fx) ;
138
119
}
139
- ConstKind :: Unevaluated ( unevaluated) => {
120
+ ConstantKind :: Unevaluated ( unevaluated, ty ) => {
140
121
match fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , unevaluated, None ) {
141
- Ok ( const_val) => const_val,
122
+ Ok ( const_val) => ( const_val, ty ) ,
142
123
Err ( _) => {
143
124
span_bug ! ( constant. span, "erroneous constant not captured by required_consts" ) ;
144
125
}
145
126
}
146
127
}
147
- ConstKind :: Param ( _)
148
- | ConstKind :: Infer ( _)
149
- | ConstKind :: Bound ( _, _)
150
- | ConstKind :: Placeholder ( _)
151
- | ConstKind :: Error ( _) => unreachable ! ( "{:?}" , const_) ,
128
+ ConstantKind :: Val ( val, ty) => ( val, ty) ,
152
129
} ;
153
130
154
- codegen_const_value ( fx, const_val, const_ . ty ( ) )
131
+ codegen_const_value ( fx, const_val, ty )
155
132
}
156
133
157
134
pub ( crate ) fn codegen_const_value < ' tcx > (
@@ -496,6 +473,9 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
496
473
. eval_for_mir ( fx. tcx , ParamEnv :: reveal_all ( ) )
497
474
. try_to_value ( fx. tcx ) ,
498
475
ConstantKind :: Val ( val, _) => Some ( val) ,
476
+ ConstantKind :: Unevaluated ( uv, _) => {
477
+ fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , uv, None ) . ok ( )
478
+ }
499
479
} ,
500
480
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
501
481
// inside a temporary before being passed to the intrinsic requiring the const argument.
0 commit comments