|
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,52 +66,20 @@ 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>)> { |
80 |
| - let constant_kind = fx.monomorphize(constant.literal); |
81 |
| - let uv = match constant_kind { |
82 |
| - ConstantKind::Ty(const_) => match const_.kind() { |
83 |
| - ty::ConstKind::Unevaluated(uv) => uv.expand(), |
84 |
| - ty::ConstKind::Value(val) => { |
85 |
| - return Some((fx.tcx.valtree_to_const_val((const_.ty(), val)), const_.ty())); |
86 |
| - } |
87 |
| - err => span_bug!( |
88 |
| - constant.span, |
89 |
| - "encountered bad ConstKind after monomorphizing: {:?}", |
90 |
| - err |
91 |
| - ), |
92 |
| - }, |
93 |
| - ConstantKind::Unevaluated(mir::UnevaluatedConst { def, .. }, _) |
94 |
| - if fx.tcx.is_static(def) => |
95 |
| - { |
96 |
| - span_bug!(constant.span, "MIR constant refers to static"); |
97 |
| - } |
98 |
| - ConstantKind::Unevaluated(uv, _) => uv, |
99 |
| - ConstantKind::Val(val, _) => return Some((val, constant_kind.ty())), |
100 |
| - }; |
101 |
| - |
102 |
| - let val = fx |
103 |
| - .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())) |
| 69 | +) -> (ConstValue<'tcx>, Ty<'tcx>) { |
| 70 | + let cv = fx.monomorphize(constant.literal); |
| 71 | + // This cannot fail because we checked all required_consts in advance. |
| 72 | + let val = cv |
| 73 | + .eval(fx.tcx, ty::ParamEnv::reveal_all(), Some(constant.span)) |
| 74 | + .expect("erroneous constant not captured by required_consts"); |
| 75 | + (val, cv.ty()) |
115 | 76 | }
|
116 | 77 |
|
117 | 78 | pub(crate) fn codegen_constant_operand<'tcx>(
|
118 | 79 | fx: &mut FunctionCx<'_, '_, 'tcx>,
|
119 | 80 | constant: &Constant<'tcx>,
|
120 | 81 | ) -> 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 |
| - |
| 82 | + let (const_val, ty) = eval_mir_constant(fx, constant); |
125 | 83 | codegen_const_value(fx, const_val, ty)
|
126 | 84 | }
|
127 | 85 |
|
@@ -479,7 +437,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
|
479 | 437 | operand: &Operand<'tcx>,
|
480 | 438 | ) -> Option<ConstValue<'tcx>> {
|
481 | 439 | match operand {
|
482 |
| - Operand::Constant(const_) => Some(eval_mir_constant(fx, const_).unwrap().0), |
| 440 | + Operand::Constant(const_) => Some(eval_mir_constant(fx, const_).0), |
483 | 441 | // FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
|
484 | 442 | // inside a temporary before being passed to the intrinsic requiring the const argument.
|
485 | 443 | // This code tries to find a single constant defining definition of the referenced local.
|
|
0 commit comments