From 86062ef930a756cb993832fbfce2467500050268 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Sat, 5 Nov 2016 02:53:02 +0000 Subject: [PATCH] Read discriminant as a signed integer if specified by layout. This ensures it gets sign extended correctly. Fixes #78 --- src/interpreter/terminator/mod.rs | 7 ++++++- tests/run-pass/negative_discriminant.rs | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/run-pass/negative_discriminant.rs diff --git a/src/interpreter/terminator/mod.rs b/src/interpreter/terminator/mod.rs index fdd703e940b..414fdeb936d 100644 --- a/src/interpreter/terminator/mod.rs +++ b/src/interpreter/terminator/mod.rs @@ -252,11 +252,16 @@ fn read_discriminant_value(&self, adt_ptr: Pointer, adt_ty: Ty<'tcx>) -> EvalRes let adt_layout = self.type_layout(adt_ty); let discr_val = match *adt_layout { - General { discr, .. } | CEnum { discr, .. } => { + General { discr, .. } | CEnum { discr, signed: false, .. } => { let discr_size = discr.size().bytes(); self.memory.read_uint(adt_ptr, discr_size as usize)? } + CEnum { discr, signed: true, .. } => { + let discr_size = discr.size().bytes(); + self.memory.read_int(adt_ptr, discr_size as usize)? as u64 + } + RawNullablePointer { nndiscr, .. } => { self.read_nonnull_discriminant_value(adt_ptr, nndiscr)? } diff --git a/tests/run-pass/negative_discriminant.rs b/tests/run-pass/negative_discriminant.rs new file mode 100644 index 00000000000..16f175e7dfc --- /dev/null +++ b/tests/run-pass/negative_discriminant.rs @@ -0,0 +1,13 @@ +enum AB { A = -1, B = 1 } + +fn main() { + match AB::A { + AB::A => (), + AB::B => panic!(), + } + + match AB::B { + AB::A => panic!(), + AB::B => (), + } +}