Skip to content

Commit 5438ae9

Browse files
adpaco-awstedinski
authored andcommitted
Use signed types for signed scalars (rust-lang#1063)
* Use signed types for signed scalars * Remove outdated comment
1 parent cdb401a commit 5438ae9

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/kani-compiler/src/codegen_cprover_gotoc/codegen/operand.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,13 @@ impl<'tcx> GotocCtx<'tcx> {
157157
debug! {"codegen_scalar\n{:?}\n{:?}\n{:?}\n{:?}",s, ty, span, &ty.kind()};
158158
match (s, &ty.kind()) {
159159
(Scalar::Int(_), ty::Int(it)) => match it {
160-
// We treat the data as bit vector. Thus, we extract the value as unsigned and set
161-
// the type to signed int.
162-
IntTy::I8 => Expr::int_constant(s.to_u8().unwrap(), Type::signed_int(8)),
163-
IntTy::I16 => Expr::int_constant(s.to_u16().unwrap(), Type::signed_int(16)),
164-
IntTy::I32 => Expr::int_constant(s.to_u32().unwrap(), Type::signed_int(32)),
165-
IntTy::I64 => Expr::int_constant(s.to_u64().unwrap(), Type::signed_int(64)),
166-
IntTy::I128 => Expr::int_constant(s.to_u128().unwrap(), Type::signed_int(128)),
160+
IntTy::I8 => Expr::int_constant(s.to_i8().unwrap(), Type::signed_int(8)),
161+
IntTy::I16 => Expr::int_constant(s.to_i16().unwrap(), Type::signed_int(16)),
162+
IntTy::I32 => Expr::int_constant(s.to_i32().unwrap(), Type::signed_int(32)),
163+
IntTy::I64 => Expr::int_constant(s.to_i64().unwrap(), Type::signed_int(64)),
164+
IntTy::I128 => Expr::int_constant(s.to_i128().unwrap(), Type::signed_int(128)),
167165
IntTy::Isize => {
168-
Expr::int_constant(s.to_machine_usize(self).unwrap(), Type::ssize_t())
166+
Expr::int_constant(s.to_machine_isize(self).unwrap(), Type::ssize_t())
169167
}
170168
},
171169
(Scalar::Int(_), ty::Uint(it)) => match it {

src/kani-compiler/src/codegen_cprover_gotoc/codegen/statement.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,18 @@ impl<'tcx> GotocCtx<'tcx> {
611611
TagEncoding::Direct => {
612612
let discr = def.discriminant_for_variant(self.tcx, *variant_index);
613613
let discr_t = self.codegen_enum_discr_typ(pt);
614+
// The constant created below may not fit into the type.
615+
// https://github.com/model-checking/kani/issues/996
616+
//
617+
// It doesn't matter if the type comes from `self.codegen_enum_discr_typ(pt)`
618+
// or `discr.ty`. It looks like something is wrong with `discriminat_for_variant`
619+
// because when it tries to codegen `std::cmp::Ordering` (which should produce
620+
// discriminant values -1, 0 and 1) it produces values 255, 0 and 1 with i8 types:
621+
//
622+
// debug!("DISCRIMINANT - val:{:?} ty:{:?}", discr.val, discr.ty);
623+
// DISCRIMINANT - val:255 ty:i8
624+
// DISCRIMINANT - val:0 ty:i8
625+
// DISCRIMINANT - val:1 ty:i8
614626
let discr = Expr::int_constant(discr.val, self.codegen_ty(discr_t));
615627
self.codegen_place(place)
616628
.goto_expr

0 commit comments

Comments
 (0)