Fix invalid bitcast taking bool out of a union represented as a scalar

As reported in https://github.com/rust-lang/rust/pull/54668#issuecomment-440186476
This commit is contained in:
Olivier Goffart 2018-11-20 12:14:53 +01:00
parent 046e054a99
commit 86d41350c7
2 changed files with 17 additions and 3 deletions

View File

@ -243,14 +243,22 @@ impl<'a, 'tcx: 'a, V: CodegenObject> OperandRef<'tcx, V> {
_ => bug!("OperandRef::extract_field({:?}): not applicable", self)
};
let bitcast = |bx: &mut Bx, val, ty| {
if ty == bx.cx().type_i1() {
bx.trunc(val, ty)
} else {
bx.bitcast(val, ty)
}
};
// HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
match val {
OperandValue::Immediate(ref mut llval) => {
*llval = bx.bitcast(*llval, bx.cx().immediate_backend_type(field));
*llval = bitcast(bx, *llval, bx.cx().immediate_backend_type(field));
}
OperandValue::Pair(ref mut a, ref mut b) => {
*a = bx.bitcast(*a, bx.cx().scalar_pair_element_backend_type(field, 0, true));
*b = bx.bitcast(*b, bx.cx().scalar_pair_element_backend_type(field, 1, true));
*a = bitcast(bx, *a, bx.cx().scalar_pair_element_backend_type(field, 0, true));
*b = bitcast(bx, *b, bx.cx().scalar_pair_element_backend_type(field, 1, true));
}
OperandValue::Ref(..) => bug!()
}

View File

@ -78,3 +78,9 @@ pub union CUnionU128{a:u128}
#[no_mangle]
pub fn test_CUnionU128(_: CUnionU128) { loop {} }
pub union UnionBool { b:bool }
// CHECK: define zeroext i1 @test_UnionBool(i8 %b)
#[no_mangle]
pub fn test_UnionBool(b: UnionBool) -> bool { unsafe { b.b } }
// CHECK: %0 = trunc i8 %b to i1