summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_codegen_ssa/src/mir/constant.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 18:31:44 +0000
commitc23a457e72abe608715ac76f076f47dc42af07a5 (patch)
tree2772049aaf84b5c9d0ed12ec8d86812f7a7904b6 /compiler/rustc_codegen_ssa/src/mir/constant.rs
parentReleasing progress-linux version 1.73.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-c23a457e72abe608715ac76f076f47dc42af07a5.tar.xz
rustc-c23a457e72abe608715ac76f076f47dc42af07a5.zip
Merging upstream version 1.74.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_codegen_ssa/src/mir/constant.rs')
-rw-r--r--compiler/rustc_codegen_ssa/src/mir/constant.rs68
1 files changed, 19 insertions, 49 deletions
diff --git a/compiler/rustc_codegen_ssa/src/mir/constant.rs b/compiler/rustc_codegen_ssa/src/mir/constant.rs
index babcf9bee..fde4e85f9 100644
--- a/compiler/rustc_codegen_ssa/src/mir/constant.rs
+++ b/compiler/rustc_codegen_ssa/src/mir/constant.rs
@@ -2,7 +2,7 @@ use crate::errors;
use crate::mir::operand::OperandRef;
use crate::traits::*;
use rustc_middle::mir;
-use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
+use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::layout::HasTyCtxt;
use rustc_middle::ty::{self, Ty};
use rustc_target::abi::Abi;
@@ -13,74 +13,44 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
pub fn eval_mir_constant_to_operand(
&self,
bx: &mut Bx,
- constant: &mir::Constant<'tcx>,
- ) -> Result<OperandRef<'tcx, Bx::Value>, ErrorHandled> {
- let val = self.eval_mir_constant(constant)?;
+ constant: &mir::ConstOperand<'tcx>,
+ ) -> OperandRef<'tcx, Bx::Value> {
+ let val = self.eval_mir_constant(constant);
let ty = self.monomorphize(constant.ty());
- Ok(OperandRef::from_const(bx, val, ty))
+ OperandRef::from_const(bx, val, ty)
}
- pub fn eval_mir_constant(
- &self,
- constant: &mir::Constant<'tcx>,
- ) -> Result<ConstValue<'tcx>, ErrorHandled> {
- let ct = self.monomorphize(constant.literal);
- let uv = match ct {
- mir::ConstantKind::Ty(ct) => match ct.kind() {
- ty::ConstKind::Unevaluated(uv) => uv.expand(),
- ty::ConstKind::Value(val) => {
- return Ok(self.cx.tcx().valtree_to_const_val((ct.ty(), val)));
- }
- err => span_bug!(
- constant.span,
- "encountered bad ConstKind after monomorphizing: {:?}",
- err
- ),
- },
- mir::ConstantKind::Unevaluated(uv, _) => uv,
- mir::ConstantKind::Val(val, _) => return Ok(val),
- };
-
- self.cx.tcx().const_eval_resolve(ty::ParamEnv::reveal_all(), uv, None).map_err(|err| {
- match err {
- ErrorHandled::Reported(_) => {
- self.cx.tcx().sess.emit_err(errors::ErroneousConstant { span: constant.span });
- }
- ErrorHandled::TooGeneric => {
- self.cx
- .tcx()
- .sess
- .diagnostic()
- .emit_bug(errors::PolymorphicConstantTooGeneric { span: constant.span });
- }
- }
- err
- })
+ pub fn eval_mir_constant(&self, constant: &mir::ConstOperand<'tcx>) -> mir::ConstValue<'tcx> {
+ self.monomorphize(constant.const_)
+ .eval(self.cx.tcx(), ty::ParamEnv::reveal_all(), Some(constant.span))
+ .expect("erroneous constant not captured by required_consts")
}
/// This is a convenience helper for `simd_shuffle_indices`. It has the precondition
- /// that the given `constant` is an `ConstantKind::Unevaluated` and must be convertible to
+ /// that the given `constant` is an `Const::Unevaluated` and must be convertible to
/// a `ValTree`. If you want a more general version of this, talk to `wg-const-eval` on zulip.
+ ///
+ /// Note that this function is cursed, since usually MIR consts should not be evaluated to valtrees!
pub fn eval_unevaluated_mir_constant_to_valtree(
&self,
- constant: &mir::Constant<'tcx>,
+ constant: &mir::ConstOperand<'tcx>,
) -> Result<Option<ty::ValTree<'tcx>>, ErrorHandled> {
- let uv = match self.monomorphize(constant.literal) {
- mir::ConstantKind::Unevaluated(uv, _) => uv.shrink(),
- mir::ConstantKind::Ty(c) => match c.kind() {
+ let uv = match self.monomorphize(constant.const_) {
+ mir::Const::Unevaluated(uv, _) => uv.shrink(),
+ mir::Const::Ty(c) => match c.kind() {
// A constant that came from a const generic but was then used as an argument to old-style
// simd_shuffle (passing as argument instead of as a generic param).
rustc_type_ir::ConstKind::Value(valtree) => return Ok(Some(valtree)),
other => span_bug!(constant.span, "{other:#?}"),
},
- // We should never encounter `ConstantKind::Val` unless MIR opts (like const prop) evaluate
+ // We should never encounter `Const::Val` unless MIR opts (like const prop) evaluate
// a constant and write that value back into `Operand`s. This could happen, but is unlikely.
// Also: all users of `simd_shuffle` are on unstable and already need to take a lot of care
// around intrinsics. For an issue to happen here, it would require a macro expanding to a
// `simd_shuffle` call without wrapping the constant argument in a `const {}` block, but
// the user pass through arbitrary expressions.
// FIXME(oli-obk): replace the magic const generic argument of `simd_shuffle` with a real
- // const generic.
+ // const generic, and get rid of this entire function.
other => span_bug!(constant.span, "{other:#?}"),
};
let uv = self.monomorphize(uv);
@@ -95,7 +65,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
pub fn simd_shuffle_indices(
&mut self,
bx: &Bx,
- constant: &mir::Constant<'tcx>,
+ constant: &mir::ConstOperand<'tcx>,
) -> (Bx::Value, Ty<'tcx>) {
let ty = self.monomorphize(constant.ty());
let val = self