Skip to content

Commit 6182582

Browse files
trueptolemybonzini
authored andcommitted
rust/vmstate: Fix type check for varray in vmstate_struct
When pass a varray to vmstate_struct, the `type` parameter should be the type of the element in the varray, for example: vmstate_struct!(HPETState, timers, [0 .. num_timers], VMSTATE_HPET_TIMER, BqlRefCell<HPETTimer>).with_version_id(0) But this breaks current type check, because it checks the type of `field`, which is an array type (for the above example, type of timers is [BqlRefCell<HPETTimer>; 32], not BqlRefCell<HPETTimer>). But the current assert_field_type() can no longer be extended to include new arguments, so a variant of it (a second macro containing the `num = $num:ident` parameter) had to be added to handle array cases. In this new macro, it not only checks the type of element, but also checks whether the `num` (number of elements in varray) is out of range. Signed-off-by: Zhao Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 2079706 commit 6182582

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

rust/qemu-api/src/assertions.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ macro_rules! assert_field_type {
9191
}
9292
};
9393
};
94+
95+
($t:ty, $i:tt, $ti:ty, num = $num:ident) => {
96+
const _: () = {
97+
#[allow(unused)]
98+
fn assert_field_type(v: $t) {
99+
fn types_must_be_equal<T, U>(_: T)
100+
where
101+
T: $crate::assertions::EqType<Itself = U>,
102+
{
103+
}
104+
let index: usize = v.$num.try_into().unwrap();
105+
types_must_be_equal::<_, &$ti>(&v.$i[index]);
106+
}
107+
};
108+
};
94109
}
95110

96111
/// Assert that an expression matches a pattern. This can also be

rust/qemu-api/src/vmstate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ macro_rules! vmstate_struct {
447447
.as_ptr() as *const ::std::os::raw::c_char,
448448
$(num_offset: $crate::offset_of!($struct_name, $num),)?
449449
offset: {
450-
$crate::assert_field_type!($struct_name, $field_name, $type);
450+
$crate::assert_field_type!($struct_name, $field_name, $type $(, num = $num)?);
451451
$crate::offset_of!($struct_name, $field_name)
452452
},
453453
size: ::core::mem::size_of::<$type>(),

0 commit comments

Comments
 (0)