Skip to content

Commit b062668

Browse files
committed
rust: pin-init: add syn dependency and remove proc-macro[2] and quote workarounds
`syn` makes parsing Rust from proc-macros a lot simpler. `pin-init` has not used `syn` up until now, because the we did not support it. That changed in commit 54e3eae ("Merge patch series "`syn` support""), so we can finally utilize the added ergonomics of parsing proc-macro input with `syn`. Previously we only had the `proc-macro` library available, whereas the user-space version also used `proc-macro2` and `quote`. Now both are available, so remove the workarounds. Due to these changes, clippy emits warnings about unnecessary `.to_string()` as `proc-macro2` provides an additional `PartialEq` impl on `Ident`, so the warnings are fixed. [ Adjusted wording from upstream version and added build system changes for the kernel - Benno ] Co-developed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Gary Guo <gary@garyguo.net> Reviewed-by: Tamir Duberstein <tamird@gmail.com> Signed-off-by: Benno Lossin <lossin@kernel.org>
1 parent 79deef1 commit b062668

7 files changed

Lines changed: 25 additions & 50 deletions

File tree

rust/Makefile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
212212

213213
rustdoc-pin_init_internal: private rustdoc_host = yes
214214
rustdoc-pin_init_internal: private rustc_target_flags = --cfg kernel \
215-
--extern proc_macro --crate-type proc-macro
215+
--extern proc_macro --extern proc_macro2 --extern quote --extern syn \
216+
--crate-type proc-macro
216217
rustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
217-
rustdoc-clean FORCE
218+
rustdoc-clean rustdoc-proc_macro2 rustdoc-quote rustdoc-syn FORCE
218219
+$(call if_changed,rustdoc)
219220

220221
rustdoc-pin_init: private rustdoc_host = yes
@@ -273,9 +274,10 @@ rusttestlib-macros: $(src)/macros/lib.rs \
273274
+$(call if_changed,rustc_test_library)
274275

275276
rusttestlib-pin_init_internal: private rustc_target_flags = --cfg kernel \
276-
--extern proc_macro
277+
--extern proc_macro --extern proc_macro2 --extern quote --extern syn
277278
rusttestlib-pin_init_internal: private rustc_test_library_proc = yes
278-
rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs FORCE
279+
rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
280+
rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
279281
+$(call if_changed,rustc_test_library)
280282

281283
rusttestlib-pin_init: private rustc_target_flags = --extern pin_init_internal \
@@ -547,8 +549,10 @@ $(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \
547549
$(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
548550
+$(call if_changed_dep,rustc_procmacro)
549551

550-
$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel
551-
$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs FORCE
552+
$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel \
553+
--extern proc_macro2 --extern quote --extern syn
554+
$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
555+
$(obj)/libproc_macro2.rlib $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
552556
+$(call if_changed_dep,rustc_procmacro)
553557

554558
quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@

rust/pin-init/internal/src/helpers.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
22

3-
#[cfg(not(kernel))]
4-
use proc_macro2 as proc_macro;
5-
6-
use proc_macro::{TokenStream, TokenTree};
3+
use proc_macro2::{TokenStream, TokenTree};
74

85
/// Parsed generics.
96
///
@@ -101,7 +98,7 @@ pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec<TokenTree>) {
10198
1 => {
10299
// Here depending on the token, it might be a generic variable name.
103100
match tt.clone() {
104-
TokenTree::Ident(i) if at_start && i.to_string() == "const" => {
101+
TokenTree::Ident(i) if at_start && i == "const" => {
105102
let Some(name) = toks.next() else {
106103
// Parsing error.
107104
break;

rust/pin-init/internal/src/lib.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,11 @@
77
//! `pin-init` proc macros.
88
99
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
10-
// Allow `.into()` to convert
11-
// - `proc_macro2::TokenStream` into `proc_macro::TokenStream` in the user-space version.
12-
// - `proc_macro::TokenStream` into `proc_macro::TokenStream` in the kernel version.
13-
// Clippy warns on this conversion, but it's required by the user-space version.
14-
//
15-
// Remove once we have `proc_macro2` in the kernel.
16-
#![allow(clippy::useless_conversion)]
1710
// Documentation is done in the pin-init crate instead.
1811
#![allow(missing_docs)]
1912

2013
use proc_macro::TokenStream;
2114

22-
#[cfg(kernel)]
23-
#[path = "../../../macros/quote.rs"]
24-
#[macro_use]
25-
#[cfg_attr(not(kernel), rustfmt::skip)]
26-
mod quote;
27-
#[cfg(not(kernel))]
28-
#[macro_use]
29-
extern crate quote;
30-
3115
mod helpers;
3216
mod pin_data;
3317
mod pinned_drop;

rust/pin-init/internal/src/pin_data.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
22

3-
#[cfg(not(kernel))]
4-
use proc_macro2 as proc_macro;
5-
63
use crate::helpers::{parse_generics, Generics};
7-
use proc_macro::{Group, Punct, Spacing, TokenStream, TokenTree};
4+
use proc_macro2::{Group, Punct, Spacing, TokenStream, TokenTree};
5+
use quote::quote;
86

97
pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
108
// This proc-macro only does some pre-parsing and then delegates the actual parsing to
@@ -28,7 +26,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
2826
// The name of the struct with ty_generics.
2927
let struct_name = rest
3028
.iter()
31-
.skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if i.to_string() == "struct"))
29+
.skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if i == "struct"))
3230
.nth(1)
3331
.and_then(|tt| match tt {
3432
TokenTree::Ident(_) => {
@@ -65,7 +63,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {
6563
.into_iter()
6664
.flat_map(|tt| {
6765
// We ignore top level `struct` tokens, since they would emit a compile error.
68-
if matches!(&tt, TokenTree::Ident(i) if i.to_string() == "struct") {
66+
if matches!(&tt, TokenTree::Ident(i) if i == "struct") {
6967
vec![tt]
7068
} else {
7169
replace_self_and_deny_type_defs(&struct_name, tt, &mut errs)
@@ -98,11 +96,7 @@ fn replace_self_and_deny_type_defs(
9896
) -> Vec<TokenTree> {
9997
match tt {
10098
TokenTree::Ident(ref i)
101-
if i.to_string() == "enum"
102-
|| i.to_string() == "trait"
103-
|| i.to_string() == "struct"
104-
|| i.to_string() == "union"
105-
|| i.to_string() == "impl" =>
99+
if i == "enum" || i == "trait" || i == "struct" || i == "union" || i == "impl" =>
106100
{
107101
errs.extend(
108102
format!(
@@ -119,7 +113,7 @@ fn replace_self_and_deny_type_defs(
119113
);
120114
vec![tt]
121115
}
122-
TokenTree::Ident(i) if i.to_string() == "Self" => struct_name.clone(),
116+
TokenTree::Ident(i) if i == "Self" => struct_name.clone(),
123117
TokenTree::Literal(_) | TokenTree::Punct(_) | TokenTree::Ident(_) => vec![tt],
124118
TokenTree::Group(g) => vec![TokenTree::Group(Group::new(
125119
g.delimiter(),

rust/pin-init/internal/src/pinned_drop.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
22

3-
#[cfg(not(kernel))]
4-
use proc_macro2 as proc_macro;
5-
6-
use proc_macro::{TokenStream, TokenTree};
3+
use proc_macro2::{TokenStream, TokenTree};
4+
use quote::quote;
75

86
pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
97
let mut toks = input.into_iter().collect::<Vec<_>>();
108
assert!(!toks.is_empty());
119
// Ensure that we have an `impl` item.
12-
assert!(matches!(&toks[0], TokenTree::Ident(i) if i.to_string() == "impl"));
10+
assert!(matches!(&toks[0], TokenTree::Ident(i) if i == "impl"));
1311
// Ensure that we are implementing `PinnedDrop`.
1412
let mut nesting: usize = 0;
1513
let mut pinned_drop_idx = None;
@@ -27,7 +25,7 @@ pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream
2725
if i >= 1 && nesting == 0 {
2826
// Found the end of the generics, this should be `PinnedDrop`.
2927
assert!(
30-
matches!(tt, TokenTree::Ident(i) if i.to_string() == "PinnedDrop"),
28+
matches!(tt, TokenTree::Ident(i) if i == "PinnedDrop"),
3129
"expected 'PinnedDrop', found: '{tt:?}'"
3230
);
3331
pinned_drop_idx = Some(i);

rust/pin-init/internal/src/zeroable.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
#[cfg(not(kernel))]
4-
use proc_macro2 as proc_macro;
5-
63
use crate::helpers::{parse_generics, Generics};
7-
use proc_macro::{TokenStream, TokenTree};
4+
use proc_macro2::{TokenStream, TokenTree};
5+
use quote::quote;
86

97
pub(crate) fn parse_zeroable_derive_input(
108
input: TokenStream,

scripts/generate_rust_analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def append_sysroot_crate(
123123
append_crate(
124124
"pin_init_internal",
125125
srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
126-
[],
126+
["std", "proc_macro", "proc_macro2", "quote", "syn"],
127127
cfg=["kernel"],
128128
is_proc_macro=True,
129129
)

0 commit comments

Comments
 (0)