1
Fork 0

treat all mir::Constant values as ConstantKind::Val

This commit is contained in:
b-naber 2022-02-16 10:54:36 +01:00
parent 6045c34f15
commit 40e4bd2d02
6 changed files with 56 additions and 10 deletions

View file

@ -2554,7 +2554,14 @@ impl<'tcx> Constant<'tcx> {
impl<'tcx> From<ty::Const<'tcx>> for ConstantKind<'tcx> { impl<'tcx> From<ty::Const<'tcx>> for ConstantKind<'tcx> {
#[inline] #[inline]
fn from(ct: ty::Const<'tcx>) -> Self { 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 @@ impl<'tcx> ConstantKind<'tcx> {
Self::Val(val, _) => val.try_to_machine_usize(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. /// A collection of projections into user types.

View file

@ -2,7 +2,7 @@
use crate::build::CFG; use crate::build::CFG;
use rustc_middle::mir::*; use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::TyCtxt;
impl<'tcx> CFG<'tcx> { impl<'tcx> CFG<'tcx> {
crate fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'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 { Rvalue::Use(Operand::Constant(Box::new(Constant {
span: source_info.span, span: source_info.span,
user_ty: None, user_ty: None,
literal: ty::Const::zero_sized(tcx, tcx.types.unit).into(), literal: ConstantKind::from_zero_sized(tcx.types.unit),
}))), }))),
); );
} }

View file

@ -9,7 +9,7 @@ use rustc_hir as hir;
use rustc_index::vec::Idx; use rustc_index::vec::Idx;
use rustc_middle::mir::*; use rustc_middle::mir::*;
use rustc_middle::thir::*; use rustc_middle::thir::*;
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation}; use rustc_middle::ty::CanonicalUserTypeAnnotation;
use std::iter; use std::iter;
impl<'a, 'tcx> Builder<'a, 'tcx> { impl<'a, 'tcx> Builder<'a, 'tcx> {
@ -107,7 +107,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant { Constant {
span: expr_span, span: expr_span,
user_ty: None, 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 { Constant {
span: expr_span, span: expr_span,
user_ty: None, 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, span: expr_span,
user_ty: None, user_ty: None,
literal: match op { literal: match op {
LogicalOp::And => ty::Const::from_bool(this.tcx, false).into(), LogicalOp::And => ConstantKind::from_bool(this.tcx, false),
LogicalOp::Or => ty::Const::from_bool(this.tcx, true).into(), LogicalOp::Or => ConstantKind::from_bool(this.tcx, true),
}, },
}, },
); );

View file

@ -54,7 +54,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant { Constant {
span: source_info.span, span: source_info.span,
user_ty: None, user_ty: None,
literal: ty::Const::from_usize(self.tcx, value).into(), literal: ConstantKind::from_usize(self.tcx, value),
}, },
); );
temp temp

View file

@ -569,6 +569,9 @@ impl<'tcx> Cx<'tcx> {
hir::ExprKind::ConstBlock(ref anon_const) => { hir::ExprKind::ConstBlock(ref anon_const) => {
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id); 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); let value = ty::Const::from_inline_const(self.tcx, anon_const_def_id);
ExprKind::ConstBlock { value } ExprKind::ConstBlock { value }

View file

@ -334,6 +334,21 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
&mut self, &mut self,
constant: mir::ConstantKind<'tcx>, constant: mir::ConstantKind<'tcx>,
) -> Result<mir::ConstantKind<'tcx>, Self::Error> { ) -> 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)
} }
} }