Skip to content

Commit 246d190

Browse files
committed
Add a convenience converter from valtrees to mir constants
1 parent 41d92c4 commit 246d190

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

compiler/rustc_middle/src/mir/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,12 @@ pub enum ConstantKind<'tcx> {
22592259
Val(interpret::ConstValue<'tcx>, Ty<'tcx>),
22602260
}
22612261

2262+
impl<'tcx> From<ty::Const<'tcx>> for ConstantKind<'tcx> {
2263+
fn from(v: ty::Const<'tcx>) -> Self {
2264+
Self::Ty(v)
2265+
}
2266+
}
2267+
22622268
impl<'tcx> Constant<'tcx> {
22632269
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
22642270
match self.literal.try_to_scalar() {

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ pub fn as_constant_inner<'tcx>(
5252
let literal =
5353
match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) {
5454
Ok(c) => c,
55-
Err(LitToConstError::Reported(guar)) => {
56-
ConstantKind::Ty(tcx.const_error(ty, guar))
57-
}
55+
Err(LitToConstError::Reported(guar)) => tcx.const_error(ty, guar).into(),
5856
Err(LitToConstError::TypeError) => {
5957
bug!("encountered type error in `lit_to_mir_constant`")
6058
}
@@ -85,8 +83,7 @@ pub fn as_constant_inner<'tcx>(
8583
Constant { user_ty, span, literal }
8684
}
8785
ExprKind::ConstParam { param, def_id: _ } => {
88-
let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty);
89-
let literal = ConstantKind::Ty(const_param);
86+
let literal = tcx.mk_const(ty::ConstKind::Param(param), expr.ty).into();
9087

9188
Constant { user_ty: None, span, literal }
9289
}

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl<'tcx> ConstToPat<'tcx> {
380380
ty::Ref(_, pointee_ty, ..) => match *pointee_ty.kind() {
381381
// `&str` is represented as a valtree, let's keep using this
382382
// optimization for now.
383-
ty::Str => PatKind::Constant { value: mir::ConstantKind::Ty(tcx.mk_const(cv, ty)) },
383+
ty::Str => PatKind::Constant { value: tcx.mk_const(cv, ty).into() },
384384
// Backwards compatibility hack: support references to non-structural types,
385385
// but hard error if we aren't behind a double reference. We could just use
386386
// the fallback code path below, but that would allow *more* of this fishy
@@ -439,7 +439,7 @@ impl<'tcx> ConstToPat<'tcx> {
439439
}
440440
},
441441
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::FnDef(..) => {
442-
PatKind::Constant { value: mir::ConstantKind::Ty(tcx.mk_const(cv, ty)) }
442+
PatKind::Constant { value: tcx.mk_const(cv, ty).into() }
443443
}
444444
ty::FnPtr(..) | ty::RawPtr(..) => unreachable!(),
445445
_ => {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_index::Idx;
2020
use rustc_middle::mir::interpret::{
2121
ConstValue, ErrorHandled, GlobalId, LitToConstError, LitToConstInput, Scalar,
2222
};
23-
use rustc_middle::mir::{self, ConstantKind, UserTypeProjection};
23+
use rustc_middle::mir::{self, UserTypeProjection};
2424
use rustc_middle::mir::{BorrowKind, Mutability};
2525
use rustc_middle::thir::{Ascription, BindingMode, FieldPat, LocalVarId, Pat, PatKind, PatRange};
2626
use rustc_middle::ty::subst::{GenericArg, SubstsRef};
@@ -525,7 +525,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
525525
.tcx
526526
.const_eval_global_id_for_typeck(param_env_reveal_all, cid, Some(span))
527527
.map(|val| match val {
528-
Some(valtree) => mir::ConstantKind::Ty(self.tcx.mk_const(valtree, ty)),
528+
Some(valtree) => self.tcx.mk_const(valtree, ty).into(),
529529
None => mir::ConstantKind::Val(
530530
self.tcx
531531
.const_eval_global_id(param_env_reveal_all, cid, Some(span))
@@ -608,7 +608,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
608608
};
609609
if let Some(lit_input) = lit_input {
610610
match tcx.at(expr.span).lit_to_const(lit_input) {
611-
Ok(c) => return self.const_to_pat(ConstantKind::Ty(c), id, span, None).kind,
611+
Ok(c) => return self.const_to_pat(c.into(), id, span, None).kind,
612612
// If an error occurred, ignore that it's a literal
613613
// and leave reporting the error up to const eval of
614614
// the unevaluated constant below.
@@ -631,7 +631,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
631631
if let Ok(Some(valtree)) =
632632
self.tcx.const_eval_resolve_for_typeck(self.param_env, ct, Some(span))
633633
{
634-
self.const_to_pat(ConstantKind::Ty(self.tcx.mk_const(valtree, ty)), id, span, None).kind
634+
self.const_to_pat(self.tcx.mk_const(valtree, ty).into(), id, span, None).kind
635635
} else {
636636
// If that fails, convert it to an opaque constant pattern.
637637
match tcx.const_eval_resolve(self.param_env, uneval, None) {
@@ -671,9 +671,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
671671
let lit_input =
672672
LitToConstInput { lit: &lit.node, ty: self.typeck_results.expr_ty(expr), neg };
673673
match self.tcx.at(expr.span).lit_to_const(lit_input) {
674-
Ok(constant) => {
675-
self.const_to_pat(ConstantKind::Ty(constant), expr.hir_id, lit.span, None).kind
676-
}
674+
Ok(constant) => self.const_to_pat(constant.into(), expr.hir_id, lit.span, None).kind,
677675
Err(LitToConstError::Reported(_)) => PatKind::Wild,
678676
Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"),
679677
}

0 commit comments

Comments
 (0)