Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 34c62e0

Browse files
committed
Add a query for dereferencing constants of reference type
1 parent b54f122 commit 34c62e0

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,14 @@ rustc_queries! {
749749
desc { "destructure constant" }
750750
}
751751

752+
/// Dereference a constant reference or raw pointer and turn the result into a constant
753+
/// again.
754+
query deref_const(
755+
key: ty::ParamEnvAnd<'tcx, &'tcx ty::Const<'tcx>>
756+
) -> &'tcx ty::Const<'tcx> {
757+
desc { "deref constant" }
758+
}
759+
752760
query const_caller_location(key: (rustc_span::Symbol, u32, u32)) -> ConstValue<'tcx> {
753761
desc { "get a &core::panic::Location referring to a span" }
754762
}

compiler/rustc_mir/src/const_eval/mod.rs

Lines changed: 41 additions & 1 deletion
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,40 @@ 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::Dynamic(..) => ecx.read_drop_type_from_vtable(scalar).unwrap().1,
98+
ty::Str => bug!("there's no sized equivalent of a `str`"),
99+
ty::Slice(elem_ty) => tcx.mk_array(elem_ty, scalar.to_machine_usize(&tcx).unwrap()),
100+
_ => bug!(
101+
"type {} should not have metadata, but had {:?}",
102+
mplace.layout.ty,
103+
mplace.meta
104+
),
105+
},
106+
};
107+
108+
tcx.mk_const(ty::Const { val: ty::ConstKind::Value(op_to_const(&ecx, mplace.into())), ty })
109+
}

compiler/rustc_mir/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,8 @@ pub fn provide(providers: &mut Providers) {
6060
let (param_env, value) = param_env_and_value.into_parts();
6161
const_eval::destructure_const(tcx, param_env, value)
6262
};
63+
providers.deref_const = |tcx, param_env_and_value| {
64+
let (param_env, value) = param_env_and_value.into_parts();
65+
const_eval::deref_const(tcx, param_env, value)
66+
};
6367
}

0 commit comments

Comments
 (0)