Skip to content

Commit 5268567

Browse files
committed
ctfe, const_to_op only for mir constants
1 parent 638b612 commit 5268567

File tree

6 files changed

+40
-49
lines changed

6 files changed

+40
-49
lines changed

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
103103
) -> InterpResult<'tcx, mir::DestructuredMirConstant<'tcx>> {
104104
trace!("destructure_mir_constant: {:?}", val);
105105
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
106-
let op = ecx.mir_const_to_op(&val, None)?;
106+
let op = ecx.const_to_op(&val, None)?;
107107

108108
// We go to `usize` as we cannot allocate anything bigger anyway.
109109
let (field_count, variant, down) = match val.ty().kind() {
@@ -139,7 +139,7 @@ pub(crate) fn deref_mir_constant<'tcx>(
139139
val: mir::ConstantKind<'tcx>,
140140
) -> mir::ConstantKind<'tcx> {
141141
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
142-
let op = ecx.mir_const_to_op(&val, None).unwrap();
142+
let op = ecx.const_to_op(&val, None).unwrap();
143143
let mplace = ecx.deref_operand(&op).unwrap();
144144
if let Some(alloc_id) = mplace.ptr.provenance {
145145
assert_eq!(

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
683683
self.stack_mut().push(frame);
684684

685685
// Make sure all the constants required by this frame evaluate successfully (post-monomorphization check).
686-
for const_ in &body.required_consts {
687-
let span = const_.span;
688-
let const_ =
689-
self.subst_from_current_frame_and_normalize_erasing_regions(const_.literal)?;
690-
self.mir_const_to_op(&const_, None).map_err(|err| {
686+
for ct in &body.required_consts {
687+
let span = ct.span;
688+
let ct = self.subst_from_current_frame_and_normalize_erasing_regions(ct.literal)?;
689+
self.const_to_op(&ct, None).map_err(|err| {
691690
// If there was an error, set the span of the current frame to this constant.
692691
// Avoiding doing this when evaluation succeeds.
693692
self.frame_mut().loc = Err(span);

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
534534
// * During ConstProp, with `TooGeneric` or since the `required_consts` were not all
535535
// checked yet.
536536
// * During CTFE, since promoteds in `const`/`static` initializer bodies can fail.
537-
self.mir_const_to_op(&val, layout)?
537+
self.const_to_op(&val, layout)?
538538
}
539539
};
540540
trace!("{:?}: {:?}", mir_op, *op);
@@ -549,50 +549,42 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
549549
ops.iter().map(|op| self.eval_operand(op, None)).collect()
550550
}
551551

552-
// Used when the miri-engine runs into a constant and for extracting information from constants
553-
// in patterns via the `const_eval` module
554-
/// The `val` and `layout` are assumed to already be in our interpreter
555-
/// "universe" (param_env).
556552
pub fn const_to_op(
557-
&self,
558-
c: ty::Const<'tcx>,
559-
layout: Option<TyAndLayout<'tcx>>,
560-
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
561-
match c.kind() {
562-
ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => throw_inval!(TooGeneric),
563-
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => {
564-
throw_inval!(AlreadyReported(reported))
565-
}
566-
ty::ConstKind::Unevaluated(uv) => {
567-
// NOTE: We evaluate to a `ValTree` here as a check to ensure
568-
// we're working with valid constants, even though we never need it.
569-
let instance = self.resolve(uv.def, uv.substs)?;
570-
let cid = GlobalId { instance, promoted: None };
571-
let _valtree = self
572-
.tcx
573-
.eval_to_valtree(self.param_env.and(cid))?
574-
.unwrap_or_else(|| bug!("unable to create ValTree for {:?}", uv));
575-
576-
Ok(self.eval_to_allocation(cid)?.into())
577-
}
578-
ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => {
579-
span_bug!(self.cur_span(), "const_to_op: Unexpected ConstKind {:?}", c)
580-
}
581-
ty::ConstKind::Value(valtree) => {
582-
let ty = c.ty();
583-
let const_val = self.tcx.valtree_to_const_val((ty, valtree));
584-
self.const_val_to_op(const_val, ty, layout)
585-
}
586-
}
587-
}
588-
589-
pub fn mir_const_to_op(
590553
&self,
591554
val: &mir::ConstantKind<'tcx>,
592555
layout: Option<TyAndLayout<'tcx>>,
593556
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
594557
match val {
595-
mir::ConstantKind::Ty(ct) => self.const_to_op(*ct, layout),
558+
mir::ConstantKind::Ty(ct) => {
559+
match ct.kind() {
560+
ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => {
561+
throw_inval!(TooGeneric)
562+
}
563+
ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => {
564+
throw_inval!(AlreadyReported(reported))
565+
}
566+
ty::ConstKind::Unevaluated(uv) => {
567+
// NOTE: We evaluate to a `ValTree` here as a check to ensure
568+
// we're working with valid constants, even though we never need it.
569+
let instance = self.resolve(uv.def, uv.substs)?;
570+
let cid = GlobalId { instance, promoted: None };
571+
let _valtree = self
572+
.tcx
573+
.eval_to_valtree(self.param_env.and(cid))?
574+
.unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"));
575+
576+
Ok(self.eval_to_allocation(cid)?.into())
577+
}
578+
ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => {
579+
span_bug!(self.cur_span(), "unexpected ConstKind in ctfe: {ct:?}")
580+
}
581+
ty::ConstKind::Value(valtree) => {
582+
let ty = ct.ty();
583+
let const_val = self.tcx.valtree_to_const_val((ty, valtree));
584+
self.const_val_to_op(const_val, ty, layout)
585+
}
586+
}
587+
}
596588
mir::ConstantKind::Val(val, ty) => self.const_val_to_op(*val, *ty, layout),
597589
mir::ConstantKind::Unevaluated(uv, _) => {
598590
let instance = self.resolve(uv.def, uv.substs)?;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ macro_rules! ClonePatternFoldableImpls {
679679
}
680680

681681
ClonePatternFoldableImpls! { <'tcx>
682-
Span, Field, Mutability, Symbol, LocalVarId, usize, ty::Const<'tcx>,
682+
Span, Field, Mutability, Symbol, LocalVarId, usize,
683683
Region<'tcx>, Ty<'tcx>, BindingMode, AdtDef<'tcx>,
684684
SubstsRef<'tcx>, &'tcx GenericArg<'tcx>, UserType<'tcx>,
685685
UserTypeProjection, CanonicalUserTypeAnnotation<'tcx>

compiler/rustc_mir_transform/src/const_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
471471
return None;
472472
}
473473

474-
self.ecx.mir_const_to_op(&c.literal, None).ok()
474+
self.ecx.const_to_op(&c.literal, None).ok()
475475
}
476476

477477
/// Returns the value, if any, of evaluating `place`.

compiler/rustc_mir_transform/src/const_prop_lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
292292
return None;
293293
}
294294

295-
match self.ecx.mir_const_to_op(&c.literal, None) {
295+
match self.ecx.const_to_op(&c.literal, None) {
296296
Ok(op) => Some(op),
297297
Err(error) => {
298298
let tcx = self.ecx.tcx.at(c.span);

0 commit comments

Comments
 (0)