@@ -36,22 +36,8 @@ impl ConstantCx {
36
36
pub ( crate ) fn check_constants ( fx : & mut FunctionCx < ' _ , ' _ , ' _ > ) -> bool {
37
37
let mut all_constants_ok = true ;
38
38
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 ( ) {
46
40
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
- }
55
41
}
56
42
}
57
43
all_constants_ok
@@ -78,15 +64,15 @@ pub(crate) fn codegen_tls_ref<'tcx>(
78
64
}
79
65
80
66
pub ( crate ) fn eval_mir_constant < ' tcx > (
81
- fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
67
+ fx : & FunctionCx < ' _ , ' _ , ' tcx > ,
82
68
constant : & Constant < ' tcx > ,
83
- ) -> ( ConstValue < ' tcx > , Ty < ' tcx > ) {
69
+ ) -> Option < ( ConstValue < ' tcx > , Ty < ' tcx > ) > {
84
70
let constant_kind = fx. monomorphize ( constant. literal ) ;
85
71
let uv = match constant_kind {
86
72
ConstantKind :: Ty ( const_) => match const_. kind ( ) {
87
73
ty:: ConstKind :: Unevaluated ( uv) => uv. expand ( ) ,
88
74
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 ( ) ) ) ;
90
76
}
91
77
err => span_bug ! (
92
78
constant. span,
@@ -100,22 +86,31 @@ pub(crate) fn eval_mir_constant<'tcx>(
100
86
span_bug ! ( constant. span, "MIR constant refers to static" ) ;
101
87
}
102
88
ConstantKind :: Unevaluated ( uv, _) => uv,
103
- ConstantKind :: Val ( val, _) => return ( val, constant_kind. ty ( ) ) ,
89
+ ConstantKind :: Val ( val, _) => return Some ( ( val, constant_kind. ty ( ) ) ) ,
104
90
} ;
105
91
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 ( ) ) )
112
105
}
113
106
114
107
pub ( crate ) fn codegen_constant_operand < ' tcx > (
115
108
fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
116
109
constant : & Constant < ' tcx > ,
117
110
) -> 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
+ } ) ;
119
114
120
115
codegen_const_value ( fx, const_val, ty)
121
116
}
@@ -448,20 +443,13 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
448
443
assert ! ( cx. todo. is_empty( ) , "{:?}" , cx. todo) ;
449
444
}
450
445
446
+ /// Used only for intrinsic implementations that need a compile-time constant
451
447
pub ( crate ) fn mir_operand_get_const_val < ' tcx > (
452
448
fx : & FunctionCx < ' _ , ' _ , ' tcx > ,
453
449
operand : & Operand < ' tcx > ,
454
450
) -> Option < ConstValue < ' tcx > > {
455
451
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 ) ,
465
453
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
466
454
// inside a temporary before being passed to the intrinsic requiring the const argument.
467
455
// This code tries to find a single constant defining definition of the referenced local.
0 commit comments