3
3
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
4
4
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
5
5
use rustc_middle:: mir:: interpret:: {
6
- read_target_uint, AllocId , ConstAllocation , ConstValue , ErrorHandled , GlobalAlloc , Scalar ,
6
+ read_target_uint, AllocId , ConstAllocation , ConstValue , GlobalAlloc , Scalar ,
7
7
} ;
8
8
9
9
use cranelift_module:: * ;
@@ -33,16 +33,6 @@ impl ConstantCx {
33
33
}
34
34
}
35
35
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
-
46
36
pub ( crate ) fn codegen_static ( tcx : TyCtxt < ' _ > , module : & mut dyn Module , def_id : DefId ) {
47
37
let mut constants_cx = ConstantCx :: new ( ) ;
48
38
constants_cx. todo . push ( TodoItem :: Static ( def_id) ) ;
@@ -76,13 +66,13 @@ pub(crate) fn codegen_tls_ref<'tcx>(
76
66
pub ( crate ) fn eval_mir_constant < ' tcx > (
77
67
fx : & FunctionCx < ' _ , ' _ , ' tcx > ,
78
68
constant : & Constant < ' tcx > ,
79
- ) -> Option < ( ConstValue < ' tcx > , Ty < ' tcx > ) > {
69
+ ) -> ( ConstValue < ' tcx > , Ty < ' tcx > ) {
80
70
let constant_kind = fx. monomorphize ( constant. literal ) ;
81
71
let uv = match constant_kind {
82
72
ConstantKind :: Ty ( const_) => match const_. kind ( ) {
83
73
ty:: ConstKind :: Unevaluated ( uv) => uv. expand ( ) ,
84
74
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 ( ) ) ;
86
76
}
87
77
err => span_bug ! (
88
78
constant. span,
@@ -96,32 +86,22 @@ pub(crate) fn eval_mir_constant<'tcx>(
96
86
span_bug ! ( constant. span, "MIR constant refers to static" ) ;
97
87
}
98
88
ConstantKind :: Unevaluated ( uv, _) => uv,
99
- ConstantKind :: Val ( val, _) => return Some ( ( val, constant_kind. ty ( ) ) ) ,
89
+ ConstantKind :: Val ( val, _) => return ( val, constant_kind. ty ( ) ) ,
100
90
} ;
101
91
92
+ // This cannot fail because we checked all required_consts in advance.
102
93
let val = fx
103
94
. 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 ( ) )
115
98
}
116
99
117
100
pub ( crate ) fn codegen_constant_operand < ' tcx > (
118
101
fx : & mut FunctionCx < ' _ , ' _ , ' tcx > ,
119
102
constant : & Constant < ' tcx > ,
120
103
) -> 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) ;
125
105
codegen_const_value ( fx, const_val, ty)
126
106
}
127
107
@@ -479,7 +459,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
479
459
operand : & Operand < ' tcx > ,
480
460
) -> Option < ConstValue < ' tcx > > {
481
461
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 ) ,
483
463
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
484
464
// inside a temporary before being passed to the intrinsic requiring the const argument.
485
465
// This code tries to find a single constant defining definition of the referenced local.
0 commit comments