Skip to content

Commit c94fea0

Browse files
committed
Move get_slice_bytes to rustc::mir::interpret so it can be reused.
1 parent 875fa72 commit c94fea0

File tree

4 files changed

+26
-42
lines changed

4 files changed

+26
-42
lines changed

src/librustc/mir/interpret/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub use self::error::{
101101
InvalidProgramInfo, ResourceExhaustionInfo, UndefinedBehaviorInfo,
102102
};
103103

104-
pub use self::value::{Scalar, ScalarMaybeUndef, RawConst, ConstValue};
104+
pub use self::value::{Scalar, ScalarMaybeUndef, RawConst, ConstValue, get_slice_bytes};
105105

106106
pub use self::allocation::{Allocation, AllocationExtra, Relocations, UndefMask};
107107

src/librustc/mir/interpret/value.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,18 @@ impl_stable_hash_for!(enum crate::mir::interpret::ScalarMaybeUndef {
611611
Scalar(v),
612612
Undef
613613
});
614+
615+
/// Gets the bytes of a constant slice value.
616+
pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] {
617+
if let ConstValue::Slice { data, start, end } = val {
618+
let len = end - start;
619+
data.get_bytes(
620+
cx,
621+
// invent a pointer, only the offset is relevant anyway
622+
Pointer::new(AllocId(0), Size::from_bytes(start as u64)),
623+
Size::from_bytes(len as u64),
624+
).unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
625+
} else {
626+
bug!("expected const slice, but found another const value");
627+
}
628+
}

src/librustc/ty/relate.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
77
use crate::hir::def_id::DefId;
88
use crate::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
9-
use crate::ty::{self, layout::Size, Ty, TyCtxt, TypeFoldable};
9+
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
1010
use crate::ty::error::{ExpectedFound, TypeError};
11-
use crate::mir::interpret::{AllocId, ConstValue, Pointer, Scalar};
11+
use crate::mir::interpret::{ConstValue, get_slice_bytes, Scalar};
1212
use std::rc::Rc;
1313
use std::iter;
1414
use rustc_target::spec::abi;
@@ -585,22 +585,8 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
585585
// saying that we're not handling it intentionally.
586586

587587
(a_val @ ConstValue::Slice { .. }, b_val @ ConstValue::Slice { .. }) => {
588-
fn get_slice_bytes<'tcx>(tcx: TyCtxt<'tcx>, val: ConstValue<'tcx>) -> &'tcx [u8] {
589-
if let ConstValue::Slice { data, start, end } = val {
590-
let len = end - start;
591-
data.get_bytes(
592-
&tcx,
593-
// invent a pointer, only the offset is relevant anyway
594-
Pointer::new(AllocId(0), Size::from_bytes(start as u64)),
595-
Size::from_bytes(len as u64),
596-
).unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
597-
} else {
598-
unreachable!();
599-
}
600-
}
601-
602-
let a_bytes = get_slice_bytes(tcx, a_val);
603-
let b_bytes = get_slice_bytes(tcx, b_val);
588+
let a_bytes = get_slice_bytes(&tcx, a_val);
589+
let b_bytes = get_slice_bytes(&tcx, b_val);
604590
if a_bytes == b_bytes {
605591
Ok(tcx.mk_const(ty::Const {
606592
val: a_val,

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use crate::hair::constant::*;
1313
use rustc::lint;
1414
use rustc::mir::{Field, BorrowKind, Mutability};
1515
use rustc::mir::{UserTypeProjection};
16-
use rustc::mir::interpret::{GlobalId, ConstValue, sign_extend, AllocId, Pointer};
16+
use rustc::mir::interpret::{GlobalId, ConstValue, get_slice_bytes, sign_extend};
1717
use rustc::traits::{ObligationCause, PredicateObligation};
1818
use rustc::ty::{self, Region, TyCtxt, AdtDef, Ty, UserType, DefIdTree};
1919
use rustc::ty::{CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations};
2020
use rustc::ty::subst::{SubstsRef, GenericArg};
21-
use rustc::ty::layout::{VariantIdx, Size};
21+
use rustc::ty::layout::VariantIdx;
2222
use rustc::hir::{self, RangeEnd};
2323
use rustc::hir::def::{CtorOf, Res, DefKind, CtorKind};
2424
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
@@ -1526,27 +1526,10 @@ pub fn compare_const_vals<'tcx>(
15261526

15271527
if let ty::Str = ty.kind {
15281528
match (a.val, b.val) {
1529-
(
1530-
ConstValue::Slice { data: alloc_a, start: offset_a, end: end_a },
1531-
ConstValue::Slice { data: alloc_b, start: offset_b, end: end_b },
1532-
) => {
1533-
let len_a = end_a - offset_a;
1534-
let len_b = end_b - offset_b;
1535-
let a = alloc_a.get_bytes(
1536-
&tcx,
1537-
// invent a pointer, only the offset is relevant anyway
1538-
Pointer::new(AllocId(0), Size::from_bytes(offset_a as u64)),
1539-
Size::from_bytes(len_a as u64),
1540-
);
1541-
let b = alloc_b.get_bytes(
1542-
&tcx,
1543-
// invent a pointer, only the offset is relevant anyway
1544-
Pointer::new(AllocId(0), Size::from_bytes(offset_b as u64)),
1545-
Size::from_bytes(len_b as u64),
1546-
);
1547-
if let (Ok(a), Ok(b)) = (a, b) {
1548-
return from_bool(a == b);
1549-
}
1529+
(ConstValue::Slice { .. }, ConstValue::Slice { .. }) => {
1530+
let a_bytes = get_slice_bytes(&tcx, a.val);
1531+
let b_bytes = get_slice_bytes(&tcx, b.val);
1532+
return from_bool(a_bytes == b_bytes);
15501533
}
15511534
_ => (),
15521535
}

0 commit comments

Comments
 (0)