diff options
author | Zhao Liu <zhao1.liu@intel.com> | 2025-03-18 21:02:09 +0800 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2025-03-21 12:56:00 +0100 |
commit | 618258256e6c60957100c5a01ab70f5473020cb9 (patch) | |
tree | 8c1573075dd7bbc83243d7c883da2d95e9dfbb3c /rust/qemu-api/src/assertions.rs | |
parent | 20797069c71a90582078448b81de28f227a8403b (diff) | |
download | qemu-618258256e6c60957100c5a01ab70f5473020cb9.zip qemu-618258256e6c60957100c5a01ab70f5473020cb9.tar.gz qemu-618258256e6c60957100c5a01ab70f5473020cb9.tar.bz2 |
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 <zhao1.liu@intel.com>
Link: https://lore.kernel.org/r/20250318130219.1799170-6-zhao1.liu@intel.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'rust/qemu-api/src/assertions.rs')
-rw-r--r-- | rust/qemu-api/src/assertions.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/rust/qemu-api/src/assertions.rs b/rust/qemu-api/src/assertions.rs index bba38cf..eb12e94 100644 --- a/rust/qemu-api/src/assertions.rs +++ b/rust/qemu-api/src/assertions.rs @@ -91,6 +91,21 @@ macro_rules! assert_field_type { } }; }; + + ($t:ty, $i:tt, $ti:ty, num = $num:ident) => { + const _: () = { + #[allow(unused)] + fn assert_field_type(v: $t) { + fn types_must_be_equal<T, U>(_: T) + where + T: $crate::assertions::EqType<Itself = U>, + { + } + let index: usize = v.$num.try_into().unwrap(); + types_must_be_equal::<_, &$ti>(&v.$i[index]); + } + }; + }; } /// Assert that an expression matches a pattern. This can also be |