Skip to content

Macro syn rewrite #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 27 commits into
base: rust-next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6b2262f
kbuild: rust: apply `CONFIG_WERROR` to hostprogs as well
ojeda Apr 7, 2024
a5f77c5
kbuild: rust: use shared host Rust flags for `macros`
ojeda Apr 7, 2024
e4527ff
kbuild: rust-analyzer: support key-value `cfg`s
ojeda Apr 10, 2024
c3b22c6
rust: proc-macro2: import crate
ojeda Oct 9, 2022
617c120
rust: proc-macro2: add SPDX License Identifiers
ojeda Oct 9, 2022
24954e2
rust: proc-macro2: remove `unicode_ident` dependency
ojeda Oct 9, 2022
925b30e
rust: quote: import crate
ojeda Oct 9, 2022
cdad907
rust: quote: add SPDX License Identifiers
ojeda Oct 9, 2022
328f151
rust: syn: import crate
ojeda Oct 9, 2022
cef2d41
rust: syn: add SPDX License Identifiers
ojeda Oct 9, 2022
82e9a4f
rust: syn: remove `unicode-ident` dependency
ojeda Oct 9, 2022
a59391f
rust: Kbuild: enable `proc-macro2`, `quote` and `syn`
ojeda Oct 9, 2022
1b729e1
rust: macros: fix soundness issue in `module!` macro
y86-dev Apr 1, 2024
ed6e1a8
rust: macros: replace `quote!` with `quote::quote` and use `proc-macro2`
y86-dev Apr 6, 2024
8938c4f
rust: macros: rewrite `#[vtable]` using `syn`
y86-dev Apr 6, 2024
638dc79
rust: macros: rewrite `module!` using `syn`
y86-dev Apr 6, 2024
bdb4cff
rust: macros: rewrite `Zeroable` derive macro using `syn`
y86-dev Apr 6, 2024
2a88e8a
rust: macros: rewrite `#[pin_data]` using `syn`
y86-dev Apr 6, 2024
b8459ad
rust: macros: rewrite `#[pinned_drop]` using `syn`
y86-dev Apr 5, 2024
5d4eb2d
rust: macros: rewrite `__internal_init!` using `syn`
y86-dev Apr 6, 2024
a8dae43
rust: macros: remove helpers
y86-dev Apr 6, 2024
45d057b
rust: init: remove macros.rs
y86-dev Apr 8, 2024
c7790b6
fixup! rust: macros: rewrite `#[pin_data]` using `syn`
y86-dev Apr 16, 2024
66b9b59
fixup! rust: macros: rewrite `#[pinned_drop]` using `syn`
y86-dev Apr 16, 2024
8d21a65
fixup! rust: macros: rewrite `__internal_init!` using `syn`
y86-dev Apr 16, 2024
edd1ce0
fixup! rust: macros: rewrite `Zeroable` derive macro using `syn`
y86-dev Apr 16, 2024
6ad858d
fixup! rust: macros: rewrite `#[vtable]` using `syn`
y86-dev Apr 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rust: macros: rewrite module! using syn
Now that `syn` and `quote` are available in the Kernel use them for the
`module!` proc-macro instead of the current string based proc-macro
approach.

Proc-macros written using `syn` are a lot more readable and thus easier
to maintain than proc-macros written using string manipulation. An
additional advantage of `syn` is the improved error reporting, here is
an example:

    module! {
        type: RustMinimal,
        name: "rust_minimal",
        author: "Rust for Linux Contributors",
        description: "Rust minimal sample",
        // we forgot to add a `license` field.
    }

Prior to this patch, the error reads:

    error: proc macro panicked
      --> samples/rust/rust_minimal.rs:7:1
       |
    7  | / module! {
    8  | |     type: RustMinimal,
    9  | |     name: "rust_minimal",
    10 | |     author: "Rust for Linux Contributors",
    11 | |     description: "Rust minimal sample",
    12 | |     // license: "GPL",
    13 | | }
       | |_^
       |
       = help: message: Missing required key "license".

    error[E0425]: cannot find value `__LOG_PREFIX` in the crate root
      --> samples/rust/rust_minimal.rs:21:9
       |
    21 |         pr_info!("Rust minimal sample (init)\n");
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in the crate root
       |
       = note: this error originates in the macro `$crate::print_macro` which comes from the expansion of the macro `pr_info` (in Nightly builds, run with -Z macro-backtrace for more info)

    error[E0425]: cannot find value `__LOG_PREFIX` in the crate root
      --> samples/rust/rust_minimal.rs:22:9
       |
    22 |         pr_info!("Am I built-in? {}\n", !cfg!(MODULE));
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in the crate root
       |
       = note: this error originates in the macro `$crate::print_macro` which comes from the expansion of the macro `pr_info` (in Nightly builds, run with -Z macro-backtrace for more info)

    error[E0425]: cannot find value `__LOG_PREFIX` in the crate root
      --> samples/rust/rust_minimal.rs:35:9
       |
    35 |         pr_info!("My numbers are {:?}\n", self.numbers);
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in the crate root
       |
       = note: this error originates in the macro `$crate::print_macro` which comes from the expansion of the macro `pr_info` (in Nightly builds, run with -Z macro-backtrace for more info)

    error[E0425]: cannot find value `__LOG_PREFIX` in the crate root
      --> samples/rust/rust_minimal.rs:36:9
       |
    36 |         pr_info!("Rust minimal sample (exit)\n");
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in the crate root
       |
       = note: this error originates in the macro `$crate::print_macro` which comes from the expansion of the macro `pr_info` (in Nightly builds, run with -Z macro-backtrace for more info)

    error: aborting due to 5 previous errors

After this patch, the error reads:

    error: Missing required key "license".
      --> samples/rust/rust_minimal.rs:8:5
       |
    8  | /     type: RustMinimal,
    9  | |     name: "rust_minimal",
    10 | |     author: "Rust for Linux Contributors",
    11 | |     description: "Rust minimal sample",
       | |_______________________________________^

    error: aborting due to 1 previous error

Similar improvements in error quality apply to getting the order of the
fields wrong and giving non-ascii strings as values for fields where it
should be ascii-only.

Signed-off-by: Benno Lossin <[email protected]>
  • Loading branch information
y86-dev committed Apr 10, 2024
commit 638dc7913dc7dda39a5f172bd4c895f96fcb38b4
1 change: 1 addition & 0 deletions rust/macros/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0

#![allow(dead_code)]
use proc_macro2::{token_stream, Group, TokenStream, TokenTree};

pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
Expand Down
17 changes: 15 additions & 2 deletions rust/macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod vtable;
mod zeroable;

use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;

/// Declares a kernel module.
Expand Down Expand Up @@ -74,8 +75,20 @@ use syn::parse_macro_input;
/// - `license`: byte array of the license of the kernel module (required).
/// - `alias`: byte array of alias name of the kernel module.
#[proc_macro]
pub fn module(ts: TokenStream) -> TokenStream {
module::module(ts.into()).into()
pub fn module(input: TokenStream) -> TokenStream {
match syn::parse(input) {
Ok(input) => module::module(input),
Err(err) => {
let err = err.into_compile_error();
quote! {
// Supresses missing `__LOG_PREFIX` errors from printing macros.
const __LOG_PREFIX: &[u8] = b"";
// Due to this error, it will not compile, so an empty `__LOG_PREFIX` is fine.
#err
}
}
}
.into()
}

/// Declares or implements a vtable trait.
Expand Down
Loading