@@ -36,22 +36,8 @@ impl ConstantCx {
3636pub ( crate ) fn check_constants ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > ) -> bool {
3737 let mut all_constants_ok = true ;
3838 for constant in & fx. mir . required_consts {
39- let unevaluated = match fx. monomorphize ( constant. literal ) {
40- ConstantKind :: Ty ( _) => unreachable ! ( ) ,
41- ConstantKind :: Unevaluated ( uv, _) => uv,
42- ConstantKind :: Val ( ..) => continue ,
43- } ;
44-
45- if let Err ( err) = fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , unevaluated, None ) {
39+ if eval_mir_constant ( fx, constant) . is_none ( ) {
4640 all_constants_ok = false ;
47- match err {
48- ErrorHandled :: Reported ( _) => {
49- fx. tcx . sess . span_err ( constant. span , "erroneous constant encountered" ) ;
50- }
51- ErrorHandled :: TooGeneric => {
52- span_bug ! ( constant. span, "codegen encountered polymorphic constant: {:?}" , err) ;
53- }
54- }
5541 }
5642 }
5743 all_constants_ok
@@ -78,15 +64,15 @@ pub(crate) fn codegen_tls_ref<'tcx>(
7864}
7965
8066pub ( crate ) fn eval_mir_constant < ' tcx > (
81- fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
67+ fx : & FunctionCx < ' _ , ' _ , ' tcx > ,
8268 constant : & Constant < ' tcx > ,
83- ) -> ( ConstValue < ' tcx > , Ty < ' tcx > ) {
69+ ) -> Option < ( ConstValue < ' tcx > , Ty < ' tcx > ) > {
8470 let constant_kind = fx. monomorphize ( constant. literal ) ;
8571 let uv = match constant_kind {
8672 ConstantKind :: Ty ( const_) => match const_. kind ( ) {
8773 ty:: ConstKind :: Unevaluated ( uv) => uv. expand ( ) ,
8874 ty:: ConstKind :: Value ( val) => {
89- return ( fx. tcx . valtree_to_const_val ( ( const_. ty ( ) , val) ) , const_. ty ( ) ) ;
75+ return Some ( ( fx. tcx . valtree_to_const_val ( ( const_. ty ( ) , val) ) , const_. ty ( ) ) ) ;
9076 }
9177 err => span_bug ! (
9278 constant. span,
@@ -100,22 +86,31 @@ pub(crate) fn eval_mir_constant<'tcx>(
10086 span_bug ! ( constant. span, "MIR constant refers to static" ) ;
10187 }
10288 ConstantKind :: Unevaluated ( uv, _) => uv,
103- ConstantKind :: Val ( val, _) => return ( val, constant_kind. ty ( ) ) ,
89+ ConstantKind :: Val ( val, _) => return Some ( ( val, constant_kind. ty ( ) ) ) ,
10490 } ;
10591
106- (
107- fx. tcx . const_eval_resolve ( ty:: ParamEnv :: reveal_all ( ) , uv, None ) . unwrap_or_else ( |_err| {
108- span_bug ! ( constant. span, "erroneous constant not captured by required_consts" ) ;
109- } ) ,
110- constant_kind. ty ( ) ,
111- )
92+ let val = fx
93+ . tcx
94+ . const_eval_resolve ( ty:: ParamEnv :: reveal_all ( ) , uv, None )
95+ . map_err ( |err| match err {
96+ ErrorHandled :: Reported ( _) => {
97+ fx. tcx . sess . span_err ( constant. span , "erroneous constant encountered" ) ;
98+ }
99+ ErrorHandled :: TooGeneric => {
100+ span_bug ! ( constant. span, "codegen encountered polymorphic constant: {:?}" , err) ;
101+ }
102+ } )
103+ . ok ( ) ;
104+ val. map ( |val| ( val, constant_kind. ty ( ) ) )
112105}
113106
114107pub ( crate ) fn codegen_constant_operand < ' tcx > (
115108 fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
116109 constant : & Constant < ' tcx > ,
117110) -> CValue < ' tcx > {
118- let ( const_val, ty) = eval_mir_constant ( fx, constant) ;
111+ let ( const_val, ty) = eval_mir_constant ( fx, constant) . unwrap_or_else ( || {
112+ span_bug ! ( constant. span, "erroneous constant not captured by required_consts" )
113+ } ) ;
119114
120115 codegen_const_value ( fx, const_val, ty)
121116}
@@ -448,20 +443,13 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
448443 assert ! ( cx. todo. is_empty( ) , "{:?}" , cx. todo) ;
449444}
450445
446+ /// Used only for intrinsic implementations that need a compile-time constant
451447pub ( crate ) fn mir_operand_get_const_val < ' tcx > (
452448 fx : & FunctionCx < ' _ , ' _ , ' tcx > ,
453449 operand : & Operand < ' tcx > ,
454450) -> Option < ConstValue < ' tcx > > {
455451 match operand {
456- Operand :: Constant ( const_) => match fx. monomorphize ( const_. literal ) {
457- ConstantKind :: Ty ( const_) => Some (
458- const_. eval_for_mir ( fx. tcx , ParamEnv :: reveal_all ( ) ) . try_to_value ( fx. tcx ) . unwrap ( ) ,
459- ) ,
460- ConstantKind :: Val ( val, _) => Some ( val) ,
461- ConstantKind :: Unevaluated ( uv, _) => {
462- Some ( fx. tcx . const_eval_resolve ( ParamEnv :: reveal_all ( ) , uv, None ) . unwrap ( ) )
463- }
464- } ,
452+ Operand :: Constant ( const_) => Some ( eval_mir_constant ( fx, const_) . unwrap ( ) . 0 ) ,
465453 // FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
466454 // inside a temporary before being passed to the intrinsic requiring the const argument.
467455 // This code tries to find a single constant defining definition of the referenced local.
0 commit comments