[RFC PATCH 0/4] rust: use attrs crate to parse #[property]

Paolo Bonzini posted 4 patches 4 months ago
Failed in applying to current master (apply log)
rust/Cargo.lock                               | 19 ++++-
rust/meson.build                              |  2 +
rust/qemu-api-macros/Cargo.toml               |  1 +
rust/qemu-api-macros/meson.build              |  1 +
rust/qemu-api-macros/src/lib.rs               | 84 +++++++------------
rust/qemu-api-macros/src/tests.rs             | 14 ++--
scripts/archive-source.sh                     |  2 +-
scripts/make-release                          |  2 +-
subprojects/.gitignore                        |  5 +-
subprojects/attrs-0.2-rs.wrap                 |  7 ++
.../packagefiles/attrs-0.2-rs/meson.build     | 32 +++++++
.../packagefiles/proc-macro2-1-rs/meson.build |  2 +-
subprojects/packagefiles/syn-2-rs/meson.build |  2 +-
subprojects/proc-macro2-1-rs.wrap             |  8 +-
subprojects/syn-2-rs.wrap                     |  8 +-
15 files changed, 111 insertions(+), 78 deletions(-)
create mode 100644 subprojects/attrs-0.2-rs.wrap
create mode 100644 subprojects/packagefiles/attrs-0.2-rs/meson.build
[RFC PATCH 0/4] rust: use attrs crate to parse #[property]
Posted by Paolo Bonzini 4 months ago
This needs a bit of preparation, but it simplifies the handwritten parsing
code down to approximately 10 lines, and slightly improves the error
messages too.  All the scaffolding will be shared by future uses of
procedural macros.

Thanks to the author of the attrs crate, Aatif Syed, for accepting to lower
his crate's minimum supported Rust version to 1.83.

Paolo

Paolo Bonzini (4):
  subprojects: update proc-macro2 and syn
  subprojects: add attrs crate
  rust: qemu-api-macros: support matching more than one error
  rust: qemu-api-macros: switch #[property] parsing to use combinators

 rust/Cargo.lock                               | 19 ++++-
 rust/meson.build                              |  2 +
 rust/qemu-api-macros/Cargo.toml               |  1 +
 rust/qemu-api-macros/meson.build              |  1 +
 rust/qemu-api-macros/src/lib.rs               | 84 +++++++------------
 rust/qemu-api-macros/src/tests.rs             | 14 ++--
 scripts/archive-source.sh                     |  2 +-
 scripts/make-release                          |  2 +-
 subprojects/.gitignore                        |  5 +-
 subprojects/attrs-0.2-rs.wrap                 |  7 ++
 .../packagefiles/attrs-0.2-rs/meson.build     | 32 +++++++
 .../packagefiles/proc-macro2-1-rs/meson.build |  2 +-
 subprojects/packagefiles/syn-2-rs/meson.build |  2 +-
 subprojects/proc-macro2-1-rs.wrap             |  8 +-
 subprojects/syn-2-rs.wrap                     |  8 +-
 15 files changed, 111 insertions(+), 78 deletions(-)
 create mode 100644 subprojects/attrs-0.2-rs.wrap
 create mode 100644 subprojects/packagefiles/attrs-0.2-rs/meson.build

-- 
2.50.1
Re: [RFC PATCH 0/4] rust: use attrs crate to parse #[property]
Posted by Manos Pitsidianakis 4 months ago
On Thu, Jul 17, 2025 at 9:27 AM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> This needs a bit of preparation, but it simplifies the handwritten parsing
> code down to approximately 10 lines, and slightly improves the error
> messages too.  All the scaffolding will be shared by future uses of
> procedural macros.
>
> Thanks to the author of the attrs crate, Aatif Syed, for accepting to lower
> his crate's minimum supported Rust version to 1.83.
>
> Paolo
>
> Paolo Bonzini (4):
>   subprojects: update proc-macro2 and syn
>   subprojects: add attrs crate
>   rust: qemu-api-macros: support matching more than one error
>   rust: qemu-api-macros: switch #[property] parsing to use combinators
>
>  rust/Cargo.lock                               | 19 ++++-
>  rust/meson.build                              |  2 +
>  rust/qemu-api-macros/Cargo.toml               |  1 +
>  rust/qemu-api-macros/meson.build              |  1 +
>  rust/qemu-api-macros/src/lib.rs               | 84 +++++++------------
>  rust/qemu-api-macros/src/tests.rs             | 14 ++--
>  scripts/archive-source.sh                     |  2 +-
>  scripts/make-release                          |  2 +-
>  subprojects/.gitignore                        |  5 +-
>  subprojects/attrs-0.2-rs.wrap                 |  7 ++
>  .../packagefiles/attrs-0.2-rs/meson.build     | 32 +++++++
>  .../packagefiles/proc-macro2-1-rs/meson.build |  2 +-
>  subprojects/packagefiles/syn-2-rs/meson.build |  2 +-
>  subprojects/proc-macro2-1-rs.wrap             |  8 +-
>  subprojects/syn-2-rs.wrap                     |  8 +-
>  15 files changed, 111 insertions(+), 78 deletions(-)
>  create mode 100644 subprojects/attrs-0.2-rs.wrap
>  create mode 100644 subprojects/packagefiles/attrs-0.2-rs/meson.build
>
> --
> 2.50.1
>

Looks OK to me but are we sure it's necessary? This dependency is just
a syn::parse wrapper under the hood. Even serde's derive macros use
syn directly: https://github.com/serde-rs/serde/blob/babafa54d283fb087fa94f50a2cf82fa9e582a7c/serde_derive/src/internals/attr.rs#L276

I'm not against dependencies mind you, but this replaces just a few
lines of code.

-- 
Manos Pitsidianakis
Emulation and Virtualization Engineer at Linaro Ltd
Re: [RFC PATCH 0/4] rust: use attrs crate to parse #[property]
Posted by Paolo Bonzini 4 months ago
Il gio 17 lug 2025, 11:18 Manos Pitsidianakis <
manos.pitsidianakis@linaro.org> ha scritto:

> Looks OK to me but are we sure it's necessary? This dependency is just
> a syn::parse wrapper under the hood. Even serde's derive macros use
> syn directly:
> https://github.com/serde-rs/serde/blob/babafa54d283fb087fa94f50a2cf82fa9e582a7c/serde_derive/src/internals/attr.rs#L276
>
> I'm not against dependencies mind you, but this replaces just a few
> lines of code.
>

It depends on how many copies of the procedural macros parsing code we have
(hence RFC).

If you introduced a #[device] or #[object] attribute, for example, it
probably would make a lot more sense than for #[property] only. Besides the
shorter code it would also provide more consistent error messages across
the attributes.

For just one macro, what you have now is fine. I did this series mostly to
have it ready and check that attrs respects the same MSRV as QEMU (it
didn't :)).

Paolo



> --
> Manos Pitsidianakis
> Emulation and Virtualization Engineer at Linaro Ltd
>
>