Skip to content

Commit 4dcf988

Browse files
committed
Specialize try_destructure_mir_constant for its sole user
1 parent dfe0683 commit 4dcf988

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::interpret::{
66
};
77
use rustc_middle::mir;
88
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
9-
use rustc_middle::ty::{self, TyCtxt};
9+
use rustc_middle::ty::{self, Ty, TyCtxt};
1010
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
1111

1212
mod error;
@@ -89,14 +89,15 @@ pub(crate) fn eval_to_valtree<'tcx>(
8989
#[instrument(skip(tcx), level = "debug")]
9090
pub(crate) fn try_destructure_mir_constant<'tcx>(
9191
tcx: TyCtxt<'tcx>,
92-
param_env: ty::ParamEnv<'tcx>,
93-
val: mir::ConstantKind<'tcx>,
92+
val: ConstValue<'tcx>,
93+
ty: Ty<'tcx>,
9494
) -> InterpResult<'tcx, mir::DestructuredConstant<'tcx>> {
95+
let param_env = ty::ParamEnv::reveal_all();
9596
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No);
96-
let op = ecx.eval_mir_constant(&val, None, None)?;
97+
let op = ecx.const_val_to_op(val, ty, None)?;
9798

9899
// We go to `usize` as we cannot allocate anything bigger anyway.
99-
let (field_count, variant, down) = match val.ty().kind() {
100+
let (field_count, variant, down) = match ty.kind() {
100101
ty::Array(_, len) => (len.eval_target_usize(tcx, param_env) as usize, None, op),
101102
ty::Adt(def, _) if def.variants().is_empty() => {
102103
throw_ub!(Unreachable)

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
633633
}
634634
}
635635

636-
pub(super) fn const_val_to_op(
636+
pub(crate) fn const_val_to_op(
637637
&self,
638638
val_val: ConstValue<'tcx>,
639639
ty: Ty<'tcx>,

compiler/rustc_const_eval/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ pub fn provide(providers: &mut Providers) {
5252
let (param_env, raw) = param_env_and_value.into_parts();
5353
const_eval::eval_to_valtree(tcx, param_env, raw)
5454
};
55-
providers.try_destructure_mir_constant = |tcx, param_env_and_value| {
56-
let (param_env, value) = param_env_and_value.into_parts();
57-
const_eval::try_destructure_mir_constant(tcx, param_env, value).ok()
58-
};
55+
providers.try_destructure_mir_constant =
56+
|tcx, (cv, ty)| const_eval::try_destructure_mir_constant(tcx, cv, ty).ok();
5957
providers.valtree_to_const_val = |tcx, (ty, valtree)| {
6058
const_eval::valtree_to_const_value(tcx, ty::ParamEnv::empty().and(ty), valtree)
6159
};

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,15 +2882,10 @@ fn pretty_print_const_value<'tcx>(
28822882
// introducing ICEs (e.g. via `layout_of`) from missing bounds.
28832883
// E.g. `transmute([0usize; 2]): (u8, *mut T)` needs to know `T: Sized`
28842884
// to be able to destructure the tuple into `(0u8, *mut T)`
2885-
//
2886-
// FIXME(eddyb) for `--emit=mir`/`-Z dump-mir`, we should provide the
2887-
// correct `ty::ParamEnv` to allow printing *all* constant values.
28882885
(_, ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) if !ty.has_non_region_param() => {
28892886
let ct = tcx.lift(ct).unwrap();
28902887
let ty = tcx.lift(ty).unwrap();
2891-
if let Some(contents) = tcx.try_destructure_mir_constant(
2892-
ty::ParamEnv::reveal_all().and(ConstantKind::Val(ct, ty)),
2893-
) {
2888+
if let Some(contents) = tcx.try_destructure_mir_constant((ct, ty)) {
28942889
let fields = contents.fields.to_vec();
28952890
match *ty.kind() {
28962891
ty::Array(..) => {

compiler/rustc_middle/src/query/keys.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use crate::infer::canonical::Canonical;
44
use crate::mir;
5+
use crate::mir::interpret::ConstValue;
56
use crate::traits;
67
use crate::ty::fast_reject::SimplifiedType;
78
use crate::ty::layout::{TyAndLayout, ValidityRequirement};
@@ -333,6 +334,14 @@ impl<'tcx> Key for (ty::Const<'tcx>, FieldIdx) {
333334
}
334335
}
335336

337+
impl<'tcx> Key for (ConstValue<'tcx>, Ty<'tcx>) {
338+
type CacheSelector = DefaultCacheSelector<Self>;
339+
340+
fn default_span(&self, _: TyCtxt<'_>) -> Span {
341+
DUMMY_SP
342+
}
343+
}
344+
336345
impl<'tcx> Key for mir::interpret::ConstAlloc<'tcx> {
337346
type CacheSelector = DefaultCacheSelector<Self>;
338347

compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,11 +1087,13 @@ rustc_queries! {
10871087
}
10881088

10891089
/// Tries to destructure an `mir::ConstantKind` ADT or array into its variant index
1090-
/// and its field values.
1090+
/// and its field values. This should only be used for pretty printing.
10911091
query try_destructure_mir_constant(
1092-
key: ty::ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>>
1092+
key: (ConstValue<'tcx>, Ty<'tcx>)
10931093
) -> Option<mir::DestructuredConstant<'tcx>> {
10941094
desc { "destructuring MIR constant"}
1095+
no_hash
1096+
eval_always
10951097
}
10961098

10971099
query const_caller_location(key: (rustc_span::Symbol, u32, u32)) -> ConstValue<'tcx> {

0 commit comments

Comments
 (0)