Skip to content

Commit 40e4bd2

Browse files
committed
treat all mir::Constant values as ConstantKind::Val
1 parent 6045c34 commit 40e4bd2

File tree

6 files changed

+56
-10
lines changed

6 files changed

+56
-10
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2554,7 +2554,14 @@ impl<'tcx> Constant<'tcx> {
25542554
impl<'tcx> From<ty::Const<'tcx>> for ConstantKind<'tcx> {
25552555
#[inline]
25562556
fn from(ct: ty::Const<'tcx>) -> Self {
2557-
Self::Ty(ct)
2557+
match ct.val() {
2558+
ty::ConstKind::Value(cv) => {
2559+
// FIXME Once valtrees are introduced we need to convert those
2560+
// into `ConstValue` instances here
2561+
Self::Val(cv, ct.ty())
2562+
}
2563+
_ => Self::Ty(ct),
2564+
}
25582565
}
25592566
}
25602567

@@ -2635,6 +2642,27 @@ impl<'tcx> ConstantKind<'tcx> {
26352642
Self::Val(val, _) => val.try_to_machine_usize(tcx),
26362643
}
26372644
}
2645+
2646+
pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
2647+
let cv = ConstValue::from_bool(v);
2648+
Self::Val(cv, tcx.types.bool)
2649+
}
2650+
2651+
pub fn from_zero_sized(ty: Ty<'tcx>) -> Self {
2652+
let cv = ConstValue::Scalar(Scalar::ZST);
2653+
Self::Val(cv, ty)
2654+
}
2655+
2656+
pub fn from_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
2657+
let ty = tcx.types.usize;
2658+
let size = tcx
2659+
.layout_of(ty::ParamEnv::empty().and(ty))
2660+
.unwrap_or_else(|e| bug!("could not compute layout for {:?}: {:?}", ty, e))
2661+
.size;
2662+
let cv = ConstValue::Scalar(Scalar::from_uint(n as u128, size));
2663+
2664+
Self::Val(cv, ty)
2665+
}
26382666
}
26392667

26402668
/// A collection of projections into user types.

compiler/rustc_mir_build/src/build/cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::build::CFG;
44
use rustc_middle::mir::*;
5-
use rustc_middle::ty::{self, TyCtxt};
5+
use rustc_middle::ty::TyCtxt;
66

77
impl<'tcx> CFG<'tcx> {
88
crate fn block_data(&self, blk: BasicBlock) -> &BasicBlockData<'tcx> {
@@ -73,7 +73,7 @@ impl<'tcx> CFG<'tcx> {
7373
Rvalue::Use(Operand::Constant(Box::new(Constant {
7474
span: source_info.span,
7575
user_ty: None,
76-
literal: ty::Const::zero_sized(tcx, tcx.types.unit).into(),
76+
literal: ConstantKind::from_zero_sized(tcx.types.unit),
7777
}))),
7878
);
7979
}

compiler/rustc_mir_build/src/build/expr/into.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir as hir;
99
use rustc_index::vec::Idx;
1010
use rustc_middle::mir::*;
1111
use rustc_middle::thir::*;
12-
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation};
12+
use rustc_middle::ty::CanonicalUserTypeAnnotation;
1313
use std::iter;
1414

1515
impl<'a, 'tcx> Builder<'a, 'tcx> {
@@ -107,7 +107,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
107107
Constant {
108108
span: expr_span,
109109
user_ty: None,
110-
literal: ty::Const::from_bool(this.tcx, true).into(),
110+
literal: ConstantKind::from_bool(this.tcx, true),
111111
},
112112
);
113113

@@ -118,7 +118,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
118118
Constant {
119119
span: expr_span,
120120
user_ty: None,
121-
literal: ty::Const::from_bool(this.tcx, false).into(),
121+
literal: ConstantKind::from_bool(this.tcx, false),
122122
},
123123
);
124124

@@ -183,8 +183,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
183183
span: expr_span,
184184
user_ty: None,
185185
literal: match op {
186-
LogicalOp::And => ty::Const::from_bool(this.tcx, false).into(),
187-
LogicalOp::Or => ty::Const::from_bool(this.tcx, true).into(),
186+
LogicalOp::And => ConstantKind::from_bool(this.tcx, false),
187+
LogicalOp::Or => ConstantKind::from_bool(this.tcx, true),
188188
},
189189
},
190190
);

compiler/rustc_mir_build/src/build/misc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5454
Constant {
5555
span: source_info.span,
5656
user_ty: None,
57-
literal: ty::Const::from_usize(self.tcx, value).into(),
57+
literal: ConstantKind::from_usize(self.tcx, value),
5858
},
5959
);
6060
temp

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ impl<'tcx> Cx<'tcx> {
569569

570570
hir::ExprKind::ConstBlock(ref anon_const) => {
571571
let anon_const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id);
572+
573+
// FIXME Do we want to use `from_inline_const` once valtrees
574+
// are introduced? This would create `ValTree`s that will never be used...
572575
let value = ty::Const::from_inline_const(self.tcx, anon_const_def_id);
573576

574577
ExprKind::ConstBlock { value }

compiler/rustc_trait_selection/src/traits/query/normalize.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,21 @@ impl<'cx, 'tcx> FallibleTypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
334334
&mut self,
335335
constant: mir::ConstantKind<'tcx>,
336336
) -> Result<mir::ConstantKind<'tcx>, Self::Error> {
337-
constant.try_super_fold_with(self)
337+
let constant_kind = match constant {
338+
mir::ConstantKind::Ty(c) => {
339+
let const_folded = c.try_super_fold_with(self)?;
340+
match const_folded.val() {
341+
ty::ConstKind::Value(cv) => {
342+
// FIXME With Valtrees we need to convert `cv: ValTree`
343+
// to a `ConstValue` here.
344+
mir::ConstantKind::Val(cv, const_folded.ty())
345+
}
346+
_ => mir::ConstantKind::Ty(const_folded),
347+
}
348+
}
349+
mir::ConstantKind::Val(_, _) => constant.try_super_fold_with(self)?,
350+
};
351+
352+
Ok(constant_kind)
338353
}
339354
}

0 commit comments

Comments
 (0)