Skip to content

Commit 5414b48

Browse files
authored
fix: handle panics for bit_vector (#2334)
1 parent 4018af0 commit 5414b48

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

source/rust_verify_test/tests/bitvector.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,32 @@ test_verify_one_bv_file! {
245245
} => Ok(())
246246
}
247247

248+
test_verify_one_bv_file! {
249+
#[test] strslice_len_not_supported_in_by_bit_vector verus_code! {
250+
proof fn test() {
251+
use verus_builtin::*;
252+
assert(strslice_len::<u64>(7u64) == 0int) by(bit_vector);
253+
}
254+
} => Err(err) => assert_vir_error_msg(err, "string slice length not supported in bit_vector assert")
255+
}
256+
257+
test_verify_one_bv_file! {
258+
#[test] float_to_bits_not_supported_in_by_bit_vector verus_code! {
259+
proof fn test() {
260+
use verus_builtin::*;
261+
assert(f32_to_bits(0.0f32) == 0u32) by(bit_vector);
262+
}
263+
} => Err(err) => assert_vir_error_msg(err, "float-to-bits coercion not supported in bit_vector assert")
264+
}
265+
266+
test_verify_one_bv_file! {
267+
#[test] real_to_int_not_supported_in_by_bit_vector verus_code! {
268+
proof fn test() {
269+
assert(1.0real.floor() == 1int) by(bit_vector);
270+
}
271+
} => Err(err) => assert_vir_error_msg(err, "real-to-int coercion not supported in bit_vector assert")
272+
}
273+
248274
test_verify_one_bv_file! {
249275
#[test] usize_cast_in_by_bit_vector verus_code! {
250276
proof fn test_usize(x: u64) {

source/vir/src/bitvector_to_air.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,24 @@ fn bv_exp_to_expr(ctx: &Ctx, state: &mut State, exp: &Exp) -> Result<BvExpr, Vir
442442
let bv_typ = if is_bool { BvTyp::Bool } else { bv_typ };
443443
Ok(BvExpr { expr: Arc::new(ExprX::Unary(op, expr)), bv_typ })
444444
}
445-
UnaryOp::IntToReal => panic!("internal error: unexpected int to real coercion"),
446-
UnaryOp::RealToInt => panic!("internal error: unexpected real to int coercion"),
447-
UnaryOp::FloatToBits => panic!("internal error: unexpected float to bits coercion"),
445+
UnaryOp::IntToReal => {
446+
return Err(error(
447+
&exp.span,
448+
"int-to-real coercion not supported in bit_vector assert",
449+
));
450+
}
451+
UnaryOp::RealToInt => {
452+
return Err(error(
453+
&exp.span,
454+
"real-to-int coercion not supported in bit_vector assert",
455+
));
456+
}
457+
UnaryOp::FloatToBits => {
458+
return Err(error(
459+
&exp.span,
460+
"float-to-bits coercion not supported in bit_vector assert",
461+
));
462+
}
448463
UnaryOp::HeightTrigger => panic!("internal error: unexpected HeightTrigger"),
449464
UnaryOp::Trigger(_) => bv_exp_to_expr(ctx, state, arg),
450465
UnaryOp::CoerceMode { .. } => {
@@ -453,11 +468,17 @@ fn bv_exp_to_expr(ctx: &Ctx, state: &mut State, exp: &Exp) -> Result<BvExpr, Vir
453468
UnaryOp::MustBeFinalized | UnaryOp::MustBeElaborated => {
454469
panic!("internal error: Exp not finalized: {:?}", arg)
455470
}
456-
UnaryOp::StrLen => panic!(
457-
"internal error: matching for bit vector ops on this match should be impossible"
458-
),
471+
UnaryOp::StrLen => {
472+
return Err(error(
473+
&exp.span,
474+
"string slice length not supported in bit_vector assert",
475+
));
476+
}
459477
UnaryOp::InferSpecForLoopIter { .. } => {
460-
panic!("internal error: unexpected Option type (from InferSpecForLoopIter)")
478+
return Err(error(
479+
&exp.span,
480+
"loop-iterator inference hint not supported in bit_vector assert",
481+
));
461482
}
462483
UnaryOp::CastToInteger => {
463484
panic!("internal error: unexpected CastToInteger")

0 commit comments

Comments
 (0)