Skip to content

Commit

Permalink
use len on mplace instead of reading immediate, remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
b-naber committed Apr 12, 2022
1 parent 8a5273b commit 4b126b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 39 deletions.
49 changes: 12 additions & 37 deletions compiler/rustc_const_eval/src/const_eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use rustc_span::{source_map::DUMMY_SP, symbol::Symbol};
use rustc_target::abi::VariantIdx;

use crate::interpret::{
intern_const_alloc_recursive, ConstValue, Immediate, InternKind, InterpCx, InterpResult,
MPlaceTy, MemPlaceMeta, Scalar,
intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, InterpResult, MPlaceTy,
MemPlaceMeta, Scalar,
};

mod error;
Expand Down Expand Up @@ -119,44 +119,19 @@ fn const_to_valtree_inner<'tcx>(
ty::Ref(_, inner_ty, _) => {
match inner_ty.kind() {
ty::Slice(_) | ty::Str => {
match ecx.try_read_immediate_from_mplace(&place) {
Ok(Some(imm)) => {
let derefd = ecx.deref_operand(&place.into()).expect(&format!("couldnt deref {:?}", imm));
debug!(?derefd);

let len = match *imm {
Immediate::ScalarPair(_, b) => {
let len = b.to_machine_usize(&ecx.tcx.tcx).unwrap();
len
}
_ => bug!("expected ScalarPair for &[T] or &str"),
};
debug!(?len);

let valtree = slice_branches(ecx, &derefd, len);
debug!(?valtree);

valtree
}
_ => {
None
}
}
let derefd = ecx.deref_operand(&place.into()).unwrap();
debug!(?derefd);
let len = derefd.len(&ecx.tcx.tcx).unwrap();
let valtree = slice_branches(ecx, &derefd, len);
debug!(?valtree);

valtree
}
_ => {
let imm = ecx.try_read_immediate_from_mplace(&place).unwrap_or_else(|e| bug!("couldnt read immediate from {:?}, error: {:?}", place, e));

match imm {
Some(imm) => {
debug!(?imm);

let derefd_place = ecx.deref_operand(&place.into()).unwrap_or_else(|e| bug!("couldn't deref {:?}, error: {:?}", place, e));
debug!(?derefd_place);
let derefd_place = ecx.deref_operand(&place.into()).unwrap_or_else(|e| bug!("couldn't deref {:?}, error: {:?}", place, e));
debug!(?derefd_place);

const_to_valtree_inner(ecx, &derefd_place)
}
None => bug!("couldn't read immediate from {:?}", place),
}
const_to_valtree_inner(ecx, &derefd_place)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl<'tcx, Tag: Provenance> ImmTy<'tcx, Tag> {
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
/// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
/// Returns `None` if the layout does not permit loading this as a value.
pub(crate) fn try_read_immediate_from_mplace(
fn try_read_immediate_from_mplace(
&self,
mplace: &MPlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::PointerTag>>> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<'tcx, Tag: Provenance> MPlaceTy<'tcx, Tag> {
}

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

0 comments on commit 4b126b8

Please sign in to comment.