33use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
44use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
55use rustc_middle:: mir:: interpret:: {
6- read_target_uint, AllocId , ConstAllocation , ConstValue , ErrorHandled , GlobalAlloc , Scalar ,
6+ read_target_uint, AllocId , ConstAllocation , ConstValue , GlobalAlloc , Scalar ,
77} ;
88
99use cranelift_module:: * ;
@@ -33,16 +33,6 @@ impl ConstantCx {
3333 }
3434}
3535
36- pub ( crate ) fn check_constants ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > ) -> bool {
37- let mut all_constants_ok = true ;
38- for constant in & fx. mir . required_consts {
39- if eval_mir_constant ( fx, constant) . is_none ( ) {
40- all_constants_ok = false ;
41- }
42- }
43- all_constants_ok
44- }
45-
4636pub ( crate ) fn codegen_static ( tcx : TyCtxt < ' _ > , module : & mut dyn Module , def_id : DefId ) {
4737 let mut constants_cx = ConstantCx :: new ( ) ;
4838 constants_cx. todo . push ( TodoItem :: Static ( def_id) ) ;
@@ -76,13 +66,13 @@ pub(crate) fn codegen_tls_ref<'tcx>(
7666pub ( crate ) fn eval_mir_constant < ' tcx > (
7767 fx : & FunctionCx < ' _ , ' _ , ' tcx > ,
7868 constant : & Constant < ' tcx > ,
79- ) -> Option < ( ConstValue < ' tcx > , Ty < ' tcx > ) > {
69+ ) -> ( ConstValue < ' tcx > , Ty < ' tcx > ) {
8070 let constant_kind = fx. monomorphize ( constant. literal ) ;
8171 let uv = match constant_kind {
8272 ConstantKind :: Ty ( const_) => match const_. kind ( ) {
8373 ty:: ConstKind :: Unevaluated ( uv) => uv. expand ( ) ,
8474 ty:: ConstKind :: Value ( val) => {
85- return Some ( ( fx. tcx . valtree_to_const_val ( ( const_. ty ( ) , val) ) , const_. ty ( ) ) ) ;
75+ return ( fx. tcx . valtree_to_const_val ( ( const_. ty ( ) , val) ) , const_. ty ( ) ) ;
8676 }
8777 err => span_bug ! (
8878 constant. span,
@@ -96,32 +86,22 @@ pub(crate) fn eval_mir_constant<'tcx>(
9686 span_bug ! ( constant. span, "MIR constant refers to static" ) ;
9787 }
9888 ConstantKind :: Unevaluated ( uv, _) => uv,
99- ConstantKind :: Val ( val, _) => return Some ( ( val, constant_kind. ty ( ) ) ) ,
89+ ConstantKind :: Val ( val, _) => return ( val, constant_kind. ty ( ) ) ,
10090 } ;
10191
92+ // This cannot fail because we checked all required_consts in advance.
10293 let val = fx
10394 . tcx
104- . const_eval_resolve ( ty:: ParamEnv :: reveal_all ( ) , uv, None )
105- . map_err ( |err| match err {
106- ErrorHandled :: Reported ( _) => {
107- fx. tcx . sess . span_err ( constant. span , "erroneous constant encountered" ) ;
108- }
109- ErrorHandled :: TooGeneric => {
110- span_bug ! ( constant. span, "codegen encountered polymorphic constant: {:?}" , err) ;
111- }
112- } )
113- . ok ( ) ;
114- val. map ( |val| ( val, constant_kind. ty ( ) ) )
95+ . const_eval_resolve ( ty:: ParamEnv :: reveal_all ( ) , uv, Some ( constant. span ) )
96+ . expect ( "erroneous constant not captured by required_consts" ) ;
97+ ( val, constant_kind. ty ( ) )
11598}
11699
117100pub ( crate ) fn codegen_constant_operand < ' tcx > (
118101 fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
119102 constant : & Constant < ' tcx > ,
120103) -> CValue < ' tcx > {
121- let ( const_val, ty) = eval_mir_constant ( fx, constant) . unwrap_or_else ( || {
122- span_bug ! ( constant. span, "erroneous constant not captured by required_consts" )
123- } ) ;
124-
104+ let ( const_val, ty) = eval_mir_constant ( fx, constant) ;
125105 codegen_const_value ( fx, const_val, ty)
126106}
127107
@@ -479,7 +459,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
479459 operand : & Operand < ' tcx > ,
480460) -> Option < ConstValue < ' tcx > > {
481461 match operand {
482- Operand :: Constant ( const_) => Some ( eval_mir_constant ( fx, const_) . unwrap ( ) . 0 ) ,
462+ Operand :: Constant ( const_) => Some ( eval_mir_constant ( fx, const_) . 0 ) ,
483463 // FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
484464 // inside a temporary before being passed to the intrinsic requiring the const argument.
485465 // This code tries to find a single constant defining definition of the referenced local.
0 commit comments