Skip to content

Commit 9cc5ee3

Browse files
committed
mir: Unsize ConstVal::ByteStr before comparing &[u8] against it.
1 parent ccc5e07 commit 9cc5ee3

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/librustc/middle/ty/sty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ impl<'tcx> TyS<'tcx> {
948948
}
949949
}
950950

951-
fn is_slice(&self) -> bool {
951+
pub fn is_slice(&self) -> bool {
952952
match self.sty {
953953
TyRawPtr(mt) | TyRef(_, mt) => match mt.ty.sty {
954954
TySlice(_) | TyStr => true,

src/librustc_mir/build/matches/test.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,28 @@ impl<'a,'tcx> Builder<'a,'tcx> {
175175
}
176176

177177
TestKind::Eq { ref value, ty } => {
178-
let expect = self.literal_operand(test.span, ty.clone(), Literal::Value {
179-
value: value.clone()
180-
});
178+
// If we're matching against &[u8] with b"...", we need to insert
179+
// an unsizing coercion, as the byte string has type &[u8; N].
180+
let expect = match *value {
181+
ConstVal::ByteStr(ref bytes) if ty.is_slice() => {
182+
let tcx = self.hir.tcx();
183+
let array_ty = tcx.mk_array(tcx.types.u8, bytes.len());
184+
let ref_ty = tcx.mk_imm_ref(tcx.mk_region(ty::ReStatic), array_ty);
185+
let array = self.literal_operand(test.span, ref_ty, Literal::Value {
186+
value: value.clone()
187+
});
188+
189+
let sliced = self.temp(ty);
190+
self.cfg.push_assign(block, test.span, &sliced,
191+
Rvalue::Cast(CastKind::Unsize, array, ty));
192+
Operand::Consume(sliced)
193+
}
194+
_ => {
195+
self.literal_operand(test.span, ty, Literal::Value {
196+
value: value.clone()
197+
})
198+
}
199+
};
181200
let val = Operand::Consume(lvalue.clone());
182201
let fail = self.cfg.start_new_block();
183202
let block = self.compare(block, fail, test.span, BinOp::Eq, expect, val.clone());

0 commit comments

Comments
 (0)