Commit 638dc79
committed
rust: macros: rewrite
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 <benno.lossin@proton.me>module! using syn
1 parent 8938c4f commit 638dc79
3 files changed
+416
-276
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
77 | | - | |
78 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
79 | 92 | | |
80 | 93 | | |
81 | 94 | | |
| |||
0 commit comments