Skip to content

Commit fd15e61

Browse files
committed
Auto merge of #70743 - oli-obk:eager_const_to_pat_conversion, r=eddyb
Fully destructure constants into patterns r? `@varkor` as discussed in https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/constants.20in.20patterns/near/192789924 we should probably crater it once reviewed
2 parents 9e1c436 + daf976f commit fd15e61

File tree

66 files changed

+795
-434
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+795
-434
lines changed

compiler/rustc_middle/src/query/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,14 @@ rustc_queries! {
742742
desc { "destructure constant" }
743743
}
744744

745+
/// Dereference a constant reference or raw pointer and turn the result into a constant
746+
/// again.
747+
query deref_const(
748+
key: ty::ParamEnvAnd<'tcx, &'tcx ty::Const<'tcx>>
749+
) -> &'tcx ty::Const<'tcx> {
750+
desc { "deref constant" }
751+
}
752+
745753
query const_caller_location(key: (rustc_span::Symbol, u32, u32)) -> ConstValue<'tcx> {
746754
desc { "get a &core::panic::Location referring to a span" }
747755
}

compiler/rustc_mir/src/const_eval/mod.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
use std::convert::TryFrom;
44

5+
use rustc_hir::Mutability;
56
use rustc_middle::mir;
67
use rustc_middle::ty::{self, TyCtxt};
78
use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
89

9-
use crate::interpret::{intern_const_alloc_recursive, ConstValue, InternKind, InterpCx};
10+
use crate::interpret::{
11+
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, MemPlaceMeta, Scalar,
12+
};
1013

1114
mod error;
1215
mod eval_queries;
@@ -67,3 +70,39 @@ pub(crate) fn destructure_const<'tcx>(
6770

6871
mir::DestructuredConst { variant, fields }
6972
}
73+
74+
pub(crate) fn deref_const<'tcx>(
75+
tcx: TyCtxt<'tcx>,
76+
param_env: ty::ParamEnv<'tcx>,
77+
val: &'tcx ty::Const<'tcx>,
78+
) -> &'tcx ty::Const<'tcx> {
79+
trace!("deref_const: {:?}", val);
80+
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
81+
let op = ecx.const_to_op(val, None).unwrap();
82+
let mplace = ecx.deref_operand(op).unwrap();
83+
if let Scalar::Ptr(ptr) = mplace.ptr {
84+
assert_eq!(
85+
ecx.memory.get_raw(ptr.alloc_id).unwrap().mutability,
86+
Mutability::Not,
87+
"deref_const cannot be used with mutable allocations as \
88+
that could allow pattern matching to observe mutable statics",
89+
);
90+
}
91+
92+
let ty = match mplace.meta {
93+
MemPlaceMeta::None => mplace.layout.ty,
94+
MemPlaceMeta::Poison => bug!("poison metadata in `deref_const`: {:#?}", mplace),
95+
// In case of unsized types, figure out the real type behind.
96+
MemPlaceMeta::Meta(scalar) => match mplace.layout.ty.kind() {
97+
ty::Str => bug!("there's no sized equivalent of a `str`"),
98+
ty::Slice(elem_ty) => tcx.mk_array(elem_ty, scalar.to_machine_usize(&tcx).unwrap()),
99+
_ => bug!(
100+
"type {} should not have metadata, but had {:?}",
101+
mplace.layout.ty,
102+
mplace.meta
103+
),
104+
},
105+
};
106+
107+
tcx.mk_const(ty::Const { val: ty::ConstKind::Value(op_to_const(&ecx, mplace.into())), ty })
108+
}

compiler/rustc_mir/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,8 @@ pub fn provide(providers: &mut Providers) {
5959
let (param_env, value) = param_env_and_value.into_parts();
6060
const_eval::destructure_const(tcx, param_env, value)
6161
};
62+
providers.deref_const = |tcx, param_env_and_value| {
63+
let (param_env, value) = param_env_and_value.into_parts();
64+
const_eval::deref_const(tcx, param_env, value)
65+
};
6266
}

0 commit comments

Comments
 (0)