Skip to content

Commit 4b126b8

Browse files
committed
use len on mplace instead of reading immediate, remove dead code
1 parent 8a5273b commit 4b126b8

File tree

3 files changed

+14
-39
lines changed

3 files changed

+14
-39
lines changed

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
1212
use rustc_target::abi::VariantIdx;
1313

1414
use crate::interpret::{
15-
intern_const_alloc_recursive, ConstValue, Immediate, InternKind, InterpCx, InterpResult,
16-
MPlaceTy, MemPlaceMeta, Scalar,
15+
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MPlaceTy,
16+
MemPlaceMeta, Scalar,
1717
};
1818

1919
mod error;
@@ -119,44 +119,19 @@ fn const_to_valtree_inner<'tcx>(
119119
ty::Ref(_, inner_ty, _) => {
120120
match inner_ty.kind() {
121121
ty::Slice(_) | ty::Str => {
122-
match ecx.try_read_immediate_from_mplace(&place) {
123-
Ok(Some(imm)) => {
124-
let derefd = ecx.deref_operand(&place.into()).expect(&format!("couldnt deref {:?}", imm));
125-
debug!(?derefd);
126-
127-
let len = match *imm {
128-
Immediate::ScalarPair(_, b) => {
129-
let len = b.to_machine_usize(&ecx.tcx.tcx).unwrap();
130-
len
131-
}
132-
_ => bug!("expected ScalarPair for &[T] or &str"),
133-
};
134-
debug!(?len);
135-
136-
let valtree = slice_branches(ecx, &derefd, len);
137-
debug!(?valtree);
138-
139-
valtree
140-
}
141-
_ => {
142-
None
143-
}
144-
}
122+
let derefd = ecx.deref_operand(&place.into()).unwrap();
123+
debug!(?derefd);
124+
let len = derefd.len(&ecx.tcx.tcx).unwrap();
125+
let valtree = slice_branches(ecx, &derefd, len);
126+
debug!(?valtree);
127+
128+
valtree
145129
}
146130
_ => {
147-
let imm = ecx.try_read_immediate_from_mplace(&place).unwrap_or_else(|e| bug!("couldnt read immediate from {:?}, error: {:?}", place, e));
148-
149-
match imm {
150-
Some(imm) => {
151-
debug!(?imm);
152-
153-
let derefd_place = ecx.deref_operand(&place.into()).unwrap_or_else(|e| bug!("couldn't deref {:?}, error: {:?}", place, e));
154-
debug!(?derefd_place);
131+
let derefd_place = ecx.deref_operand(&place.into()).unwrap_or_else(|e| bug!("couldn't deref {:?}, error: {:?}", place, e));
132+
debug!(?derefd_place);
155133

156-
const_to_valtree_inner(ecx, &derefd_place)
157-
}
158-
None => bug!("couldn't read immediate from {:?}", place),
159-
}
134+
const_to_valtree_inner(ecx, &derefd_place)
160135
}
161136
}
162137
}

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<'tcx, Tag: Provenance> ImmTy<'tcx, Tag> {
248248
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
249249
/// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
250250
/// Returns `None` if the layout does not permit loading this as a value.
251-
pub(crate) fn try_read_immediate_from_mplace(
251+
fn try_read_immediate_from_mplace(
252252
&self,
253253
mplace: &MPlaceTy<'tcx, M::PointerTag>,
254254
) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::PointerTag>>> {

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl<'tcx, Tag: Provenance> MPlaceTy<'tcx, Tag> {
191191
}
192192

193193
#[inline]
194-
pub(super) fn len(&self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
194+
pub(crate) fn len(&self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
195195
if self.layout.is_unsized() {
196196
// We need to consult `meta` metadata
197197
match self.layout.ty.kind() {

0 commit comments

Comments
 (0)