Skip to contentSkip to conversation

rust/qemu-api-macros: use syn::Error directly

PATCH New Manos Pitsidianakis 1 participant 1 message Jul 3, 2025 Rust

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust/qemu-api-macros: use syn::Error directly

Our MacroError type wraps syn::Error as a variant, and uses another variant for custom errors. Fortunately syn::Error can be used directly, avoiding extra code on our side, so change the proc macro crate to use it.

---
 docs/devel/rust.rst               | 11 ++---
 rust/qemu-api-macros/src/bits.rs  | 58 ++++++++++----------------
 rust/qemu-api-macros/src/lib.rs   | 86 +++++++++++++++++++--------------------
 rust/qemu-api-macros/src/utils.rs | 26 ------------
 4 files changed, 70 insertions(+), 111 deletions(-)
docs/devel/rust.rst +6 -5
index dc8c44109e1c17dde0707439dc6255257532cb84..b6737536c694fd54fd0d3204c0dd1145a9b0e973 100644
@@ -351,7 +351,7 @@ Writing procedural macros
 '''''''''''''''''''''''''
 
 By conventions, procedural macros are split in two functions, one
-returning ``Result<proc_macro2::TokenStream, MacroError>`` with the body of
+returning ``Result<proc_macro2::TokenStream, syn::Error>`` with the body of
 the procedural macro, and the second returning ``proc_macro::TokenStream``
 which is the actual procedural macro.  The former's name is the same as
 the latter with the ``_or_error`` suffix.  The code for the latter is more
@@ -361,18 +361,19 @@ from the type after ``as`` in the invocation of ``parse_macro_input!``::
     #[proc_macro_derive(Object)]
     pub fn derive_object(input: TokenStream) -> TokenStream {
         let input = parse_macro_input!(input as DeriveInput);
-        let expanded = derive_object_or_error(input).unwrap_or_else(Into::into);
 
-        TokenStream::from(expanded)
+        derive_object_or_error(input)
+            .unwrap_or_else(syn::Error::into_compile_error)
+            .into()
     }
 
 The ``qemu_api_macros`` crate has utility functions to examine a
 ``DeriveInput`` and perform common checks (e.g. looking for a struct
-with named fields).  These functions return ``Result<..., MacroError>``
+with named fields).  These functions return ``Result<..., syn::Error>``
 and can be used easily in the procedural macro function::
 
     fn derive_object_or_error(input: DeriveInput) ->
-        Result<proc_macro2::TokenStream, MacroError>
+        Result<proc_macro2::TokenStream, Error>
     {
         is_c_repr(&input, "#[derive(Object)]")?;
 
rust/qemu-api-macros/src/bits.rs +21 -37

159 lines not on this page · raw message

rust/qemu-api-macros/src/lib.rs +43 -43

203 lines not on this page · raw message

rust/qemu-api-macros/src/utils.rs deleted +0 -26
deleted file mode 100644
index 02c91aed7f6a6d33075bbaa8b4fec4536da94e60..0000000000000000000000000000000000000000
@@ -1,26 +0,0 @@
-// Procedural macro utilities.
-// Author(s): Paolo Bonzini <[email protected]>
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-use proc_macro2::Span;
-use quote::quote_spanned;
-
-pub enum MacroError {
-    Message(String, Span),
-    ParseError(syn::Error),
-}
-
-impl From<syn::Error> for MacroError {
-    fn from(err: syn::Error) -> Self {
-        MacroError::ParseError(err)
-    }
-}
-
-impl From<MacroError> for proc_macro2::TokenStream {
-    fn from(err: MacroError) -> Self {
-        match err {
-            MacroError::Message(msg, span) => quote_spanned! { span => compile_error!(#msg); },
-            MacroError::ParseError(err) => err.into_compile_error(),
-        }
-    }
-}

---
base-commit: c77283dd5d79149f4e7e9edd00f65416c648ee59
change-id: 20250703-rust_macros-53d9e6a0b802

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