Fix ICE caused by truncating a negative ZST enum discriminant
This commit is contained in:
parent
f509b26a77
commit
2fa550b85d
@ -293,7 +293,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
|
||||
if let Some(discr) =
|
||||
operand.layout.ty.discriminant_for_variant(bx.tcx(), index)
|
||||
{
|
||||
let discr_val = bx.cx().const_uint_big(ll_t_out, discr.val);
|
||||
let discr_layout = bx.cx().layout_of(discr.ty);
|
||||
let discr_t = bx.cx().immediate_backend_type(discr_layout);
|
||||
let discr_val = bx.cx().const_uint_big(discr_t, discr.val);
|
||||
let discr_val =
|
||||
bx.intcast(discr_val, ll_t_out, discr.ty.is_signed());
|
||||
|
||||
return (
|
||||
bx,
|
||||
OperandRef {
|
||||
|
@ -3,6 +3,7 @@ use rustc::ty::layout::{self, Size, TyLayout};
|
||||
use rustc::ty::{self, Ty, TypeAndMut, TypeFoldable};
|
||||
use rustc_ast::ast::FloatTy;
|
||||
use rustc_span::symbol::sym;
|
||||
use rustc_target::abi::LayoutOf;
|
||||
|
||||
use rustc::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
|
||||
use rustc::mir::CastKind;
|
||||
@ -134,7 +135,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||
layout::Variants::Single { index } => {
|
||||
if let Some(discr) = src.layout.ty.discriminant_for_variant(*self.tcx, index) {
|
||||
assert!(src.layout.is_zst());
|
||||
return Ok(Scalar::from_uint(discr.val, dest_layout.size).into());
|
||||
let discr_layout = self.layout_of(discr.ty)?;
|
||||
return Ok(self.cast_from_int(discr.val, discr_layout, dest_layout)?.into());
|
||||
}
|
||||
}
|
||||
layout::Variants::Multiple { .. } => {}
|
||||
|
47
src/test/ui/consts/cast-discriminant-zst-enum.rs
Normal file
47
src/test/ui/consts/cast-discriminant-zst-enum.rs
Normal file
@ -0,0 +1,47 @@
|
||||
// run-pass
|
||||
// Test a ZST enum whose dicriminant is ~0i128. This caused an ICE when casting to a i32.
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
enum Nums {
|
||||
NegOne = -1,
|
||||
}
|
||||
|
||||
const NEG_ONE_I8: i8 = Nums::NegOne as i8;
|
||||
const NEG_ONE_I16: i16 = Nums::NegOne as i16;
|
||||
const NEG_ONE_I32: i32 = Nums::NegOne as i32;
|
||||
const NEG_ONE_I64: i64 = Nums::NegOne as i64;
|
||||
const NEG_ONE_I128: i128 = Nums::NegOne as i128;
|
||||
|
||||
#[inline(never)]
|
||||
fn identity<T>(t: T) -> T { t }
|
||||
|
||||
fn test_as_arg(n: Nums) {
|
||||
assert_eq!(-1i8, n as i8);
|
||||
assert_eq!(-1i16, n as i16);
|
||||
assert_eq!(-1i32, n as i32);
|
||||
assert_eq!(-1i64, n as i64);
|
||||
assert_eq!(-1i128, n as i128);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let kind = Nums::NegOne;
|
||||
assert_eq!(-1i8, kind as i8);
|
||||
assert_eq!(-1i16, kind as i16);
|
||||
assert_eq!(-1i32, kind as i32);
|
||||
assert_eq!(-1i64, kind as i64);
|
||||
assert_eq!(-1i128, kind as i128);
|
||||
|
||||
assert_eq!(-1i8, identity(kind) as i8);
|
||||
assert_eq!(-1i16, identity(kind) as i16);
|
||||
assert_eq!(-1i32, identity(kind) as i32);
|
||||
assert_eq!(-1i64, identity(kind) as i64);
|
||||
assert_eq!(-1i128, identity(kind) as i128);
|
||||
|
||||
test_as_arg(Nums::NegOne);
|
||||
|
||||
assert_eq!(-1i8, NEG_ONE_I8);
|
||||
assert_eq!(-1i16, NEG_ONE_I16);
|
||||
assert_eq!(-1i32, NEG_ONE_I32);
|
||||
assert_eq!(-1i64, NEG_ONE_I64);
|
||||
assert_eq!(-1i128, NEG_ONE_I128);
|
||||
}
|
Loading…
Reference in New Issue
Block a user