mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-28 20:16:58 +03:00
treat all mir::Constant values as ConstantKind::Val
This commit is contained in:
@@ -2554,7 +2554,14 @@ pub fn ty(&self) -> Ty<'tcx> {
|
||||
impl<'tcx> From<ty::Const<'tcx>> for ConstantKind<'tcx> {
|
||||
#[inline]
|
||||
fn from(ct: ty::Const<'tcx>) -> Self {
|
||||
Self::Ty(ct)
|
||||
match ct.val() {
|
||||
ty::ConstKind::Value(cv) => {
|
||||
// FIXME Once valtrees are introduced we need to convert those
|
||||
// into `ConstValue` instances here
|
||||
Self::Val(cv, ct.ty())
|
||||
}
|
||||
_ => Self::Ty(ct),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2635,6 +2642,27 @@ pub fn try_eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -
|
||||
Self::Val(val, _) => val.try_to_machine_usize(tcx),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
|
||||
let cv = ConstValue::from_bool(v);
|
||||
Self::Val(cv, tcx.types.bool)
|
||||
}
|
||||
|
||||
pub fn from_zero_sized(ty: Ty<'tcx>) -> Self {
|
||||
let cv = ConstValue::Scalar(Scalar::ZST);
|
||||
Self::Val(cv, ty)
|
||||
}
|
||||
|
||||
pub fn from_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
|
||||
let ty = tcx.types.usize;
|
||||
let size = tcx
|
||||
.layout_of(ty::ParamEnv::empty().and(ty))
|
||||
.unwrap_or_else(|e| bug!("could not compute layout for {:?}: {:?}", ty, e))
|
||||
.size;
|
||||
let cv = ConstValue::Scalar(Scalar::from_uint(n as u128, size));
|
||||
|
||||
Self::Val(cv, ty)
|
||||
}
|
||||
}
|
||||
|
||||
/// A collection of projections into user types.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::build::CFG;
|
||||
use rustc_middle::mir::*;
|
||||
use rustc_middle::ty::{self, TyCtxt};
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
|
||||
impl<'tcx> CFG<'tcx> {
|
||||
crate fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> {
|
||||
@@ -73,7 +73,7 @@ impl<'tcx> CFG<'tcx> {
|
||||
Rvalue::Use(Operand::Constant(Box::new(Constant {
|
||||
span: source_info.span,
|
||||
user_ty: None,
|
||||
literal: ty::Const::zero_sized(tcx, tcx.types.unit).into(),
|
||||
literal: ConstantKind::from_zero_sized(tcx.types.unit),
|
||||
}))),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
use rustc_index::vec::Idx;
|
||||
use rustc_middle::mir::*;
|
||||
use rustc_middle::thir::*;
|
||||
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation};
|
||||
use rustc_middle::ty::CanonicalUserTypeAnnotation;
|
||||
use std::iter;
|
||||
|
||||
impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
@@ -107,7 +107,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
Constant {
|
||||
span: expr_span,
|
||||
user_ty: None,
|
||||
literal: ty::Const::from_bool(this.tcx, true).into(),
|
||||
literal: ConstantKind::from_bool(this.tcx, true),
|
||||
},
|
||||
);
|
||||
|
||||
@@ -118,7 +118,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
Constant {
|
||||
span: expr_span,
|
||||
user_ty: None,
|
||||
literal: ty::Const::from_bool(this.tcx, false).into(),
|
||||
literal: ConstantKind::from_bool(this.tcx, false),
|
||||
},
|
||||
);
|
||||
|
||||
@@ -183,8 +183,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
span: expr_span,
|
||||
user_ty: None,
|
||||
literal: match op {
|
||||
LogicalOp::And => ty::Const::from_bool(this.tcx, false).into(),
|
||||
LogicalOp::Or => ty::Const::from_bool(this.tcx, true).into(),
|
||||
LogicalOp::And => ConstantKind::from_bool(this.tcx, false),
|
||||
LogicalOp::Or => ConstantKind::from_bool(this.tcx, true),
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
@@ -54,7 +54,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
Constant {
|
||||
span: source_info.span,
|
||||
user_ty: None,
|
||||
literal: ty::Const::from_usize(self.tcx, value).into(),
|
||||
literal: ConstantKind::from_usize(self.tcx, value),
|
||||
},
|
||||
);
|
||||
temp
|
||||
|
||||
@@ -569,6 +569,9 @@ fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx>
|
||||
|
||||
hir::ExprKind::ConstBlock(ref anon_const) => {
|
||||
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
|
||||
|
||||
// FIXME Do we want to use `from_inline_const` once valtrees
|
||||
// are introduced? This would create `ValTree`s that will never be used...
|
||||
let value = ty::Const::from_inline_const(self.tcx, anon_const_def_id);
|
||||
|
||||
ExprKind::ConstBlock { value }
|
||||
|
||||
@@ -334,6 +334,21 @@ fn try_fold_mir_const(
|
||||
&mut self,
|
||||
constant: mir::ConstantKind<'tcx>,
|
||||
) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
|
||||
constant.try_super_fold_with(self)
|
||||
let constant_kind = match constant {
|
||||
mir::ConstantKind::Ty(c) => {
|
||||
let const_folded = c.try_super_fold_with(self)?;
|
||||
match const_folded.val() {
|
||||
ty::ConstKind::Value(cv) => {
|
||||
// FIXME With Valtrees we need to convert `cv: ValTree`
|
||||
// to a `ConstValue` here.
|
||||
mir::ConstantKind::Val(cv, const_folded.ty())
|
||||
}
|
||||
_ => mir::ConstantKind::Ty(const_folded),
|
||||
}
|
||||
}
|
||||
mir::ConstantKind::Val(_, _) => constant.try_super_fold_with(self)?,
|
||||
};
|
||||
|
||||
Ok(constant_kind)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user