Skip to contentSkip to conversation

rust: add Derive macro unit tests

PATCH In review Manos Pitsidianakis 3 participants 8 messages Jul 4, 2025 – Jul 8, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover rust: add Derive macro unit tests

We don't currently test our proc macros and it'd be nice to do so.

Usually this would be done with something like
https://crates.io/crates/trybuild which runs cargo and tries to compile a test input, and checks for success/failure. However we cannot use it with meson directly, plus it would drag in a lot of dependencies anyway.

Instead of compiling, we can easily just expand test input into token streams since we already split macro implementation into separate functions, allowing us to either get a TokenStream back or a compile error message.

You can run the added tests directly with this meson command:

meson test rust-qemu-api-macros-tests

---
Manos Pitsidianakis (2):
rust/qemu-api-macros: normalize TryInto output rust/qemu-api-macros: add unit tests

 rust/qemu-api-macros/meson.build  |   3 +
 rust/qemu-api-macros/src/lib.rs   |   7 +-
 rust/qemu-api-macros/src/tests.rs | 135 ++++++++++++++++++++++++++++++++++++++
 rust/qemu-api-macros/src/utils.rs |   1 +
 4 files changed, 144 insertions(+), 2 deletions(-)

---
base-commit: c77283dd5d79149f4e7e9edd00f65416c648ee59
change-id: 20250704-rust_add_derive_macro_unit_tests-3e7daf905d62

--
γαῖα πυρί μιχθήτω

#1 lore raw

PATCH 1/2 rust/qemu-api-macros: normalize TryInto output

Remove extraneous ; and add missing trailing comma to TryInto derive macro to match rustfmt style. We will add a test in the followup commit and we would like the inlined output in the test body to be properly formatted as well.

No functional changes intended.

---
 rust/qemu-api-macros/src/lib.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
rust/qemu-api-macros/src/lib.rs +2 -2
index c18bb4e036f4e7737f9b95ac300b7d1e8742ef1f..4b30bea9eafc7924bf593113c3f42c5b1010c4b9 100644
@@ -201,7 +201,7 @@ fn derive_tryinto_body(
     let discriminants: Vec<&Ident> = variants.iter().map(|f| &f.ident).collect();
 
     Ok(quote! {
-        #(const #discriminants: #repr = #name::#discriminants as #repr;)*;
+        #(const #discriminants: #repr = #name::#discriminants as #repr;)*
         match value {
             #(#discriminants => core::result::Result::Ok(#name::#discriminants),)*
             _ => core::result::Result::Err(value),
@@ -229,7 +229,7 @@ pub const fn from_bits(value: #repr) -> Self {
                     #body
                 }) {
                     Ok(x) => x,
-                    Err(_) => panic!(#errmsg)
+                    Err(_) => panic!(#errmsg),
                 }
             }
         }
Signature · 1 line
2.47.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] rust/qemu-api-macros: normalize TryInto output

Quoted 10 lines · Manos Pitsidianakis

Date: Fri, 04 Jul 2025 13:26:57 +0300
From: Manos Pitsidianakis <[email protected]>
Subject: [PATCH 1/2] rust/qemu-api-macros: normalize TryInto output
X-Mailer: b4 0.14.2

Remove extraneous ; and add missing trailing comma to TryInto derive macro to match rustfmt style. We will add a test in the followup commit and we would like the inlined output in the test body to be properly formatted as well.

Quoted 6 lines

No functional changes intended.

---
 rust/qemu-api-macros/src/lib.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
#3 lore raw

PATCH 2/2 rust/qemu-api-macros: add unit tests

In reply to Manos Pitsidianakis

Add unit tests to check Derive macro output for expected error messages, or for expected correct codegen output.

---
 rust/qemu-api-macros/meson.build  |   3 +
 rust/qemu-api-macros/src/lib.rs   |   3 +
 rust/qemu-api-macros/src/tests.rs | 135 ++++++++++++++++++++++++++++++++++++++
 rust/qemu-api-macros/src/utils.rs |   1 +
 4 files changed, 142 insertions(+)
rust/qemu-api-macros/meson.build +3 -0
index 8610ce1c8440c4b6e38a8462d4975bf76d72fb05..2152bcb99b30e4bdcc1c5b887b7903a37f6181c3 100644
@@ -17,3 +17,6 @@ _qemu_api_macros_rs = rust.proc_macro(
 qemu_api_macros = declare_dependency(
   link_with: _qemu_api_macros_rs,
 )
+
+rust.test('rust-qemu-api-macros-tests', _qemu_api_macros_rs,
+          suite: ['unit', 'rust'])
rust/qemu-api-macros/src/lib.rs +3 -0
index 4b30bea9eafc7924bf593113c3f42c5b1010c4b9..6c6e9b683f047f79cb377e6d30e23490f66bd711 100644
@@ -15,6 +15,9 @@
 mod bits;
 use bits::BitsConstInternal;
 
+#[cfg(test)]
+mod tests;
+
 fn get_fields<'a>(
     input: &'a DeriveInput,
     msg: &str,
rust/qemu-api-macros/src/tests.rs new file +135 -0

135 lines not on this page · raw message

rust/qemu-api-macros/src/utils.rs +1 -0
index 02c91aed7f6a6d33075bbaa8b4fec4536da94e60..6287d06090b60e7fc1da7fbd563d44effcc0039c 100644
@@ -5,6 +5,7 @@
 use proc_macro2::Span;
 use quote::quote_spanned;
 
+#[derive(Debug)]
 pub enum MacroError {
     Message(String, Span),
     ParseError(syn::Error),
Signature · 1 line
2.47.2
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] rust/qemu-api-macros: add unit tests

Quoted 72 lines · Manos Pitsidianakis

Date: Fri, 04 Jul 2025 13:26:58 +0300
From: Manos Pitsidianakis <[email protected]>
Subject: [PATCH 2/2] rust/qemu-api-macros: add unit tests
X-Mailer: b4 0.14.2

Add unit tests to check Derive macro output for expected error messages, or for expected correct codegen output.

---
 rust/qemu-api-macros/meson.build  |   3 +
 rust/qemu-api-macros/src/lib.rs   |   3 +
 rust/qemu-api-macros/src/tests.rs | 135 ++++++++++++++++++++++++++++++++++++++
 rust/qemu-api-macros/src/utils.rs |   1 +
 4 files changed, 142 insertions(+)
rust/qemu-api-macros/meson.build +3 -0
index 8610ce1c8440c4b6e38a8462d4975bf76d72fb05..2152bcb99b30e4bdcc1c5b887b7903a37f6181c3 100644
@@ -17,3 +17,6 @@ _qemu_api_macros_rs = rust.proc_macro(
 qemu_api_macros = declare_dependency(
   link_with: _qemu_api_macros_rs,
 )
+
+rust.test('rust-qemu-api-macros-tests', _qemu_api_macros_rs,
+          suite: ['unit', 'rust'])
rust/qemu-api-macros/src/lib.rs +3 -0
index 4b30bea9eafc7924bf593113c3f42c5b1010c4b9..6c6e9b683f047f79cb377e6d30e23490f66bd711 100644
@@ -15,6 +15,9 @@
 mod bits;
 use bits::BitsConstInternal;
 
+#[cfg(test)]
+mod tests;
+
 fn get_fields<'a>(
     input: &'a DeriveInput,
     msg: &str,
rust/qemu-api-macros/src/tests.rs new file +25 -0
new file mode 100644
index 0000000000000000000000000000000000000000..dfca7d4838f141783472a4e728312aebeb9b5a8b
@@ -0,0 +1,135 @@
+// Copyright 2025, Linaro Limited
+// Author(s): Manos Pitsidianakis <[email protected]>
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+use super::*;
+use quote::quote;
+
+macro_rules! derive_compile_fail {
+    ($derive_fn:ident, $input:expr, $error_msg:expr) => {{
+        let input: proc_macro2::TokenStream = $input;
+        let error_msg: &str = $error_msg;
+        let derive_fn: fn(input: syn::DeriveInput) -> Result<proc_macro2::TokenStream, MacroError> =
+            $derive_fn;
+
+        let input: syn::DeriveInput = syn::parse2(input).unwrap();
+        let result = derive_fn(input);
+        let MacroError::Message(err, _) = result.unwrap_err() else {
+            panic!()
+        };
+        assert_eq!(err, error_msg);
+    }};
+}
+
+macro_rules! derive_compile {
+    ($derive_fn:ident, $input:expr, $($expected:tt)*) => {{

rust/qemu-api-macros/src/tests.rs line 35

Quoted 10 lines
rust/qemu-api-macros/src/tests.rs new file +10 -0
+        let input: proc_macro2::TokenStream = $input;
+        let expected: proc_macro2::TokenStream = $($expected)*;
+        let derive_fn: fn(input: syn::DeriveInput) -> Result<proc_macro2::TokenStream, MacroError> =
+            $derive_fn;
+
+        let input: syn::DeriveInput = syn::parse2(input).unwrap();
+        let result = derive_fn(input).unwrap();
+        assert_eq!(result.to_string(), expected.to_string());
+    }};
+}

Good examples to test macros. LGTM,

#5 lore raw
In reply to Manos Pitsidianakis
Quoted 3 lines

You can run the added tests directly with this meson command:

meson test rust-qemu-api-macros-tests

Hi Manos, do you meet this error:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/mesonbuild/mesonmain.py", line 146, in run
return options.run_func(options)
File "/usr/lib/python3/dist-packages/mesonbuild/mtest.py", line 2004, in run
b = build.load(options.wd)
File "/usr/lib/python3/dist-packages/mesonbuild/build.py", line 2868, in load
obj = pickle.load(f)
ModuleNotFoundError: No module named 'mesonbuild.options'

ERROR: Unhandled python exception

This is a Meson bug and should be reported!
---

But I've tested this series via make check.

#6 lore raw
Quoted 16 lines · Zhao Liu
Quoted 4 lines

You can run the added tests directly with this meson command:

meson test rust-qemu-api-macros-tests

Hi Manos, do you meet this error:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/mesonbuild/mesonmain.py", line 146, in run
return options.run_func(options)
File "/usr/lib/python3/dist-packages/mesonbuild/mtest.py", line 2004, in run
b = build.load(options.wd)
File "/usr/lib/python3/dist-packages/mesonbuild/build.py", line 2868, in load
obj = pickle.load(f)
ModuleNotFoundError: No module named 'mesonbuild.options'

Quoted 10 lines

ERROR: Unhandled python exception

This is a Meson bug and should be reported!
---

But I've tested this series via make check.

Sounds like it used your global meson installation instead of the one from the virtual environment of your build directory.

Thank you for testing!

Signature · 2 lines
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd
#7 lore raw
In reply to Manos Pitsidianakis
Quoted 12 lines · Manos Pitsidianakis

We don't currently test our proc macros and it'd be nice to do so.

Usually this would be done with something like
https://crates.io/crates/trybuild which runs cargo and tries to compile a test input, and checks for success/failure. However we cannot use it with meson directly, plus it would drag in a lot of dependencies anyway.

Instead of compiling, we can easily just expand test input into token streams since we already split macro implementation into separate functions, allowing us to either get a TokenStream back or a compile error message.

Quoted 5 lines

You can run the added tests directly with this meson command:

meson test rust-qemu-api-macros-tests

Nice. Adjusted for MacroError removal and applied, thanks.

Paolo