@@ -7,7 +7,6 @@ use rustc_middle::mir::interpret::{
7
7
} ;
8
8
use rustc_span:: DUMMY_SP ;
9
9
10
- use cranelift_codegen:: ir:: GlobalValueData ;
11
10
use cranelift_module:: * ;
12
11
13
12
use crate :: prelude:: * ;
@@ -81,53 +80,46 @@ pub(crate) fn codegen_tls_ref<'tcx>(
81
80
CValue :: by_val ( tls_ptr, layout)
82
81
}
83
82
84
- fn codegen_static_ref < ' tcx > (
85
- fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
86
- def_id : DefId ,
87
- layout : TyAndLayout < ' tcx > ,
88
- ) -> CPlace < ' tcx > {
89
- let data_id = data_id_for_static ( fx. tcx , fx. module , def_id, false ) ;
90
- let local_data_id = fx. module . declare_data_in_func ( data_id, & mut fx. bcx . func ) ;
91
- if fx. clif_comments . enabled ( ) {
92
- fx. add_comment ( local_data_id, format ! ( "{:?}" , def_id) ) ;
93
- }
94
- let global_ptr = fx. bcx . ins ( ) . global_value ( fx. pointer_type , local_data_id) ;
95
- assert ! ( !layout. is_unsized( ) , "unsized statics aren't supported" ) ;
96
- assert ! (
97
- matches!(
98
- fx. bcx. func. global_values[ local_data_id] ,
99
- GlobalValueData :: Symbol { tls: false , .. }
100
- ) ,
101
- "tls static referenced without Rvalue::ThreadLocalRef"
102
- ) ;
103
- CPlace :: for_ptr ( crate :: pointer:: Pointer :: new ( global_ptr) , layout)
104
- }
105
-
106
- pub ( crate ) fn codegen_constant < ' tcx > (
83
+ pub ( crate ) fn eval_mir_constant < ' tcx > (
107
84
fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
108
85
constant : & Constant < ' tcx > ,
109
- ) -> CValue < ' tcx > {
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)
86
+ ) -> ( ConstValue < ' tcx > , Ty < ' tcx > ) {
87
+ let constant_kind = fx. monomorphize ( constant. literal ) ;
88
+ let uv = match constant_kind {
89
+ ConstantKind :: Ty ( const_) => match const_. kind ( ) {
90
+ ty:: ConstKind :: Unevaluated ( uv) => uv. expand ( ) ,
91
+ ty:: ConstKind :: Value ( val) => {
92
+ return ( fx. tcx . valtree_to_const_val ( ( const_. ty ( ) , val) ) , const_. ty ( ) ) ;
93
+ }
94
+ err => span_bug ! (
95
+ constant. span,
96
+ "encountered bad ConstKind after monomorphizing: {:?}" ,
97
+ err
98
+ ) ,
99
+ } ,
100
+ ConstantKind :: Unevaluated ( mir:: UnevaluatedConst { def, .. } , _)
113
101
if fx. tcx . is_static ( def. did ) =>
114
102
{
115
- assert ! ( substs. is_empty( ) ) ;
116
- assert ! ( promoted. is_none( ) ) ;
117
-
118
- return codegen_static_ref ( fx, def. did , fx. layout_of ( ty) ) . to_cvalue ( fx) ;
119
- }
120
- ConstantKind :: Unevaluated ( unevaluated, ty) => {
121
- match fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , unevaluated, None ) {
122
- Ok ( const_val) => ( const_val, ty) ,
123
- Err ( _) => {
124
- span_bug ! ( constant. span, "erroneous constant not captured by required_consts" ) ;
125
- }
126
- }
103
+ span_bug ! ( constant. span, "MIR constant refers to static" ) ;
127
104
}
128
- ConstantKind :: Val ( val, ty) => ( val, ty) ,
105
+ ConstantKind :: Unevaluated ( uv, _) => uv,
106
+ ConstantKind :: Val ( val, _) => return ( val, constant_kind. ty ( ) ) ,
129
107
} ;
130
108
109
+ (
110
+ fx. tcx . const_eval_resolve ( ty:: ParamEnv :: reveal_all ( ) , uv, None ) . unwrap_or_else ( |_err| {
111
+ span_bug ! ( constant. span, "erroneous constant not captured by required_consts" ) ;
112
+ } ) ,
113
+ constant_kind. ty ( ) ,
114
+ )
115
+ }
116
+
117
+ pub ( crate ) fn codegen_constant_operand < ' tcx > (
118
+ fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
119
+ constant : & Constant < ' tcx > ,
120
+ ) -> CValue < ' tcx > {
121
+ let ( const_val, ty) = eval_mir_constant ( fx, constant) ;
122
+
131
123
codegen_const_value ( fx, const_val, ty)
132
124
}
133
125
0 commit comments