Skip to content

Commit

Permalink
Add a convenience converter from valtrees to mir constants
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jun 20, 2023
1 parent 41d92c4 commit 246d190
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,12 @@ pub enum ConstantKind<'tcx> {
Val(interpret::ConstValue<'tcx>, Ty<'tcx>),
}

impl<'tcx> From<ty::Const<'tcx>> for ConstantKind<'tcx> {
fn from(v: ty::Const<'tcx>) -> Self {
Self::Ty(v)
}
}

impl<'tcx> Constant<'tcx> {
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
match self.literal.try_to_scalar() {
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_mir_build/src/build/expr/as_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ pub fn as_constant_inner<'tcx>(
let literal =
match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) {
Ok(c) => c,
Err(LitToConstError::Reported(guar)) => {
ConstantKind::Ty(tcx.const_error(ty, guar))
}
Err(LitToConstError::Reported(guar)) => tcx.const_error(ty, guar).into(),
Err(LitToConstError::TypeError) => {
bug!("encountered type error in `lit_to_mir_constant`")
}
Expand Down Expand Up @@ -85,8 +83,7 @@ pub fn as_constant_inner<'tcx>(
Constant { user_ty, span, literal }
}
ExprKind::ConstParam { param, def_id: _ } => {
let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty);
let literal = ConstantKind::Ty(const_param);
let literal = tcx.mk_const(ty::ConstKind::Param(param), expr.ty).into();

Constant { user_ty: None, span, literal }
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl<'tcx> ConstToPat<'tcx> {
ty::Ref(_, pointee_ty, ..) => match *pointee_ty.kind() {
// `&str` is represented as a valtree, let's keep using this
// optimization for now.
ty::Str => PatKind::Constant { value: mir::ConstantKind::Ty(tcx.mk_const(cv, ty)) },
ty::Str => PatKind::Constant { value: tcx.mk_const(cv, ty).into() },
// Backwards compatibility hack: support references to non-structural types,
// but hard error if we aren't behind a double reference. We could just use
// the fallback code path below, but that would allow *more* of this fishy
Expand Down Expand Up @@ -439,7 +439,7 @@ impl<'tcx> ConstToPat<'tcx> {
}
},
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::FnDef(..) => {
PatKind::Constant { value: mir::ConstantKind::Ty(tcx.mk_const(cv, ty)) }
PatKind::Constant { value: tcx.mk_const(cv, ty).into() }
}
ty::FnPtr(..) | ty::RawPtr(..) => unreachable!(),
_ => {
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_index::Idx;
use rustc_middle::mir::interpret::{
ConstValue, ErrorHandled, GlobalId, LitToConstError, LitToConstInput, Scalar,
};
use rustc_middle::mir::{self, ConstantKind, UserTypeProjection};
use rustc_middle::mir::{self, UserTypeProjection};
use rustc_middle::mir::{BorrowKind, Mutability};
use rustc_middle::thir::{Ascription, BindingMode, FieldPat, LocalVarId, Pat, PatKind, PatRange};
use rustc_middle::ty::subst::{GenericArg, SubstsRef};
Expand Down Expand Up @@ -525,7 +525,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
.tcx
.const_eval_global_id_for_typeck(param_env_reveal_all, cid, Some(span))
.map(|val| match val {
Some(valtree) => mir::ConstantKind::Ty(self.tcx.mk_const(valtree, ty)),
Some(valtree) => self.tcx.mk_const(valtree, ty).into(),
None => mir::ConstantKind::Val(
self.tcx
.const_eval_global_id(param_env_reveal_all, cid, Some(span))
Expand Down Expand Up @@ -608,7 +608,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
};
if let Some(lit_input) = lit_input {
match tcx.at(expr.span).lit_to_const(lit_input) {
Ok(c) => return self.const_to_pat(ConstantKind::Ty(c), id, span, None).kind,
Ok(c) => return self.const_to_pat(c.into(), id, span, None).kind,
// If an error occurred, ignore that it's a literal
// and leave reporting the error up to const eval of
// the unevaluated constant below.
Expand All @@ -631,7 +631,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
if let Ok(Some(valtree)) =
self.tcx.const_eval_resolve_for_typeck(self.param_env, ct, Some(span))
{
self.const_to_pat(ConstantKind::Ty(self.tcx.mk_const(valtree, ty)), id, span, None).kind
self.const_to_pat(self.tcx.mk_const(valtree, ty).into(), id, span, None).kind
} else {
// If that fails, convert it to an opaque constant pattern.
match tcx.const_eval_resolve(self.param_env, uneval, None) {
Expand Down Expand Up @@ -671,9 +671,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
let lit_input =
LitToConstInput { lit: &lit.node, ty: self.typeck_results.expr_ty(expr), neg };
match self.tcx.at(expr.span).lit_to_const(lit_input) {
Ok(constant) => {
self.const_to_pat(ConstantKind::Ty(constant), expr.hir_id, lit.span, None).kind
}
Ok(constant) => self.const_to_pat(constant.into(), expr.hir_id, lit.span, None).kind,
Err(LitToConstError::Reported(_)) => PatKind::Wild,
Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"),
}
Expand Down

0 comments on commit 246d190

Please sign in to comment.