Skip to content

refactor Rust proc macros with `syn`

Gary Guo linux-kselftest linux-modules lkml rust-for-linux 3 versions – Applied

Status from this message: Applied to `rust-next` -- thanks everyone!

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest12 patches 8 repliesApplied
  2. v211 patches 11 repliesSuperseded
  3. v111 patches 29 repliesSuperseded

Changes in v3 from the cover letter

- Changes are limited to #[vtable] and module! macros.
- #[vtable] macro is reworked per Tamir's suggestion. This makes it easy
to support #[cfg] properly, and hence a new patch is added to do that.
- Avoided variable renames related to param_name/param_type between
patches in module! macro
- CString is now interpolated directly without adding extra Literal::c_string.
- Fixed an typo that affects only CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
builds.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/

Patches in v3 Applied

#Subject and filesSizeRepliesApplied
1/12rust: pin-init: internal: remove proc-macro[2] and quote workaroundsrust/Makefile rust/pin-init/internal/src/helpers.rs rust/pin-init/internal/src/lib.rs +4 more+25 -500 replies
2/12rust: macros: use `quote!` from vendored craterust/macros/concat_idents.rs rust/macros/export.rs rust/macros/fmt.rs +7 more+32 -2051 reply
3/12rust: macros: convert `#[vtable]` macro to use `syn`rust/macros/lib.rs rust/macros/vtable.rs+98 -801 reply
4/12rust: macros: use `syn` to parse `module!` macrorust/macros/helpers.rs rust/macros/lib.rs rust/macros/module.rs+280 -2341 reply
5/12rust: macros: use `quote!` for `module!` macrorust/macros/module.rs+286 -2570 replies
6/12rust: macros: convert `#[export]` to use `syn`rust/macros/export.rs rust/macros/helpers.rs rust/macros/lib.rs+12 -350 replies
7/12rust: macros: convert `concat_idents!` to use `syn`rust/macros/concat_idents.rs rust/macros/helpers.rs rust/macros/lib.rs+29 -280 replies
8/12rust: macros: convert `#[kunit_tests]` macro to use `syn`rust/macros/kunit.rs rust/macros/lib.rs+123 -1572 replies
9/12rust: macros: allow arbitrary types to be used in `module!` macrorust/macros/module.rs+7 -60 replies
10/12rust: macros: rearrange `#[doc(hidden)]` in `module!` macrorust/macros/module.rs+1 -90 replies
11/12rust: kunit: use `pin_init::zeroed` instead of custom null valuerust/kernel/kunit.rs rust/macros/kunit.rs+3 -271 reply
12/12rust: macros: support `#[cfg]` properly in `#[vtable]` macro.rust/macros/helpers.rs rust/macros/vtable.rs+13 -97 replies

Reviews trailers per patch

11 of 12 patches have a Reviewed-by or Acked-by.

PatchTamir DubersteinBenno LossinDavid Gow
1/12R-b
2/12R-b R-b A-b
3/12R-b R-b
4/12R-b R-b
5/12
6/12R-b R-b
7/12R-b R-b
8/12R-b R-b R-b
9/12R-b
10/12R-b R-b
11/12R-b R-b R-b
12/12R-b R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.

Read the v3 thread

Cover letter [PATCH v3 00/12] refactor Rust proc macros with `syn`

From: Gary Guo <[email protected]>

This series convert Rust proc macros that we have with syn, and replace the custom quote! macro that we have with the vendored quote! macro. The pin-init macros are not converted syn yet; Benno has a work in progress in converting them. They're however converted to use quote and proc-macro2 crate so our custom quote! macro can be removed.

Overall this improves the robustness of the macros as we have precise parsing of the AST rather than relying on heuristics to extract needed information from there. This is also a quality-of-life improvement to those using language servers (e.g. Rust analyzer) as the span information of the proc macros are now preserved which allows the "jump-to-definition" feature to work, even when used on completely custom macros such as module!.

Miguel gave a very good explaination on why syn is a good idea in the patch series that introduced it [1], which I shall not repeat here.

Changes since v2:
- Changes are limited to #[vtable] and module! macros.
- #[vtable] macro is reworked per Tamir's suggestion. This makes it easy
to support #[cfg] properly, and hence a new patch is added to do that.
- Avoided variable renames related to param_name/param_type between
patches in module! macro
- CString is now interpolated directly without adding extra Literal::c_string.
- Fixed an typo that affects only CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
builds.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/

Changes since v1:
- Fixed clippy warnings when moving from proc-macro to proc-macro2
(extra trait impl in proc-macro2 causes some .to_string() to be unnecessary).
- A Makefile change to pin-init-internal that is mistakenly included in
patch 5/11 is moved to the correct patch 1/11.
- module! parsing has been completely reworked to support imports_ns
and params. As there're two structs that need to parse ordered fields (ModuleInfo and Parameter), a parse_ordered_field macro is added
and it is used to implement the parsing instead of ModInfoField and
custom keywords.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/

Benno Lossin (1):
rust: pin-init: internal: remove proc-macro[2] and quote workarounds

Gary Guo (11):
rust: macros: use quote! from vendored crate
rust: macros: convert #[vtable] macro to use syn
rust: macros: use syn to parse module! macro
rust: macros: use quote! for module! macro
rust: macros: convert #[export] to use syn
rust: macros: convert concat_idents! to use syn
rust: macros: convert #[kunit_tests] macro to use syn
rust: macros: allow arbitrary types to be used in module! macro
rust: macros: rearrange #[doc(hidden)] in module! macro
rust: kunit: use pin_init::zeroed instead of custom null value
rust: macros: support #[cfg] properly in #[vtable] macro.

 rust/Makefile                             |  16 +-
 rust/kernel/kunit.rs                      |  26 +-
 rust/macros/concat_idents.rs              |  39 +-
 rust/macros/export.rs                     |  26 +-
 rust/macros/fmt.rs                        |   4 +-
 rust/macros/helpers.rs                    | 130 +---
 rust/macros/kunit.rs                      | 275 +++----
 rust/macros/lib.rs                        |  41 +-
 rust/macros/module.rs                     | 907 ++++++++++++----------
 rust/macros/paste.rs                      |   2 +-
 rust/macros/quote.rs                      | 182 -----
 rust/macros/vtable.rs                     | 165 ++--
 rust/pin-init/internal/src/helpers.rs     |   7 +-
 rust/pin-init/internal/src/lib.rs         |  16 -
 rust/pin-init/internal/src/pin_data.rs    |  18 +-
 rust/pin-init/internal/src/pinned_drop.rs |  10 +-
 rust/pin-init/internal/src/zeroable.rs    |   6 +-
 scripts/generate_rust_analyzer.py         |   2 +-
 18 files changed, 842 insertions(+), 1030 deletions(-)
 delete mode 100644 rust/macros/quote.rs

base-commit: 9ace4753a5202b02191d54e9fdf7f9e3d02b85eb

Signature · 1 line
2.51.2