Skip to content

Commit 875fa72

Browse files
committed
Add inline function get_slice_bytes to remove code duplication.
1 parent 22b87a5 commit 875fa72

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/librustc/ty/relate.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -584,32 +584,26 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
584584
// FIXME(const_generics): we should either handle `Scalar::Ptr` or add a comment
585585
// saying that we're not handling it intentionally.
586586

587-
(
588-
ConstValue::Slice { data: alloc_a, start: offset_a, end: end_a },
589-
ConstValue::Slice { data: alloc_b, start: offset_b, end: end_b },
590-
) => {
591-
let len_a = end_a - offset_a;
592-
let len_b = end_b - offset_b;
593-
let a_bytes = alloc_a
594-
.get_bytes(
595-
&tcx,
596-
// invent a pointer, only the offset is relevant anyway
597-
Pointer::new(AllocId(0), Size::from_bytes(offset_a as u64)),
598-
Size::from_bytes(len_a as u64),
599-
)
600-
.unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err));
601-
602-
let b_bytes = alloc_b
603-
.get_bytes(
604-
&tcx,
605-
// invent a pointer, only the offset is relevant anyway
606-
Pointer::new(AllocId(0), Size::from_bytes(offset_b as u64)),
607-
Size::from_bytes(len_b as u64),
608-
)
609-
.unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err));
587+
(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);
610604
if a_bytes == b_bytes {
611605
Ok(tcx.mk_const(ty::Const {
612-
val: ConstValue::Slice { data: alloc_a, start: offset_a, end: end_a },
606+
val: a_val,
613607
ty: a.ty,
614608
}))
615609
} else {

0 commit comments

Comments
 (0)