Skip to content
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

Macro syn rewrite #1073

Draft
wants to merge 27 commits into
base: rust-next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
Apr 1, 2024
ed6e1a8
rust: macros: replace `quote!` with `quote::quote` and use `proc-macro2`
Apr 6, 2024
8938c4f
rust: macros: rewrite `#[vtable]` using `syn`
Apr 6, 2024
638dc79
rust: macros: rewrite `module!` using `syn`
Apr 6, 2024
bdb4cff
rust: macros: rewrite `Zeroable` derive macro using `syn`
Apr 6, 2024
2a88e8a
rust: macros: rewrite `#[pin_data]` using `syn`
Apr 6, 2024
b8459ad
rust: macros: rewrite `#[pinned_drop]` using `syn`
Apr 5, 2024
5d4eb2d
rust: macros: rewrite `__internal_init!` using `syn`
Apr 6, 2024
a8dae43
rust: macros: remove helpers
Apr 6, 2024
45d057b
rust: init: remove macros.rs
Apr 8, 2024
c7790b6
fixup! rust: macros: rewrite `#[pin_data]` using `syn`
Apr 16, 2024
66b9b59
fixup! rust: macros: rewrite `#[pinned_drop]` using `syn`
Apr 16, 2024
8d21a65
fixup! rust: macros: rewrite `__internal_init!` using `syn`
Apr 16, 2024
edd1ce0
fixup! rust: macros: rewrite `Zeroable` derive macro using `syn`
Apr 16, 2024
6ad858d
fixup! rust: macros: rewrite `#[vtable]` using `syn`
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
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,9 @@ KBUILD_CFLAGS += $(stackp-flags-y)
KBUILD_RUSTFLAGS-$(CONFIG_WERROR) += -Dwarnings
KBUILD_RUSTFLAGS += $(KBUILD_RUSTFLAGS-y)

KBUILD_HOSTRUSTFLAGS-$(CONFIG_WERROR) += -Dwarnings
KBUILD_HOSTRUSTFLAGS += $(KBUILD_HOSTRUSTFLAGS-y)

ifdef CONFIG_FRAME_POINTER
KBUILD_CFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
KBUILD_RUSTFLAGS += -Cforce-frame-pointers=y
Expand Down Expand Up @@ -1734,8 +1737,14 @@ PHONY += rustfmt rustfmtcheck
rustfmt:
$(Q)find $(abs_srctree) -type f -name '*.rs' \
-o -path $(abs_srctree)/rust/alloc -prune \
-o -path $(abs_srctree)/rust/proc-macro2 -prune \
-o -path $(abs_srctree)/rust/quote -prune \
-o -path $(abs_srctree)/rust/syn -prune \
-o -path $(abs_objtree)/rust/test -prune \
| grep -Fv $(abs_srctree)/rust/alloc \
| grep -Fv $(abs_srctree)/rust/proc-macro2 \
| grep -Fv $(abs_srctree)/rust/quote \
| grep -Fv $(abs_srctree)/rust/syn \
| grep -Fv $(abs_objtree)/rust/test \
| grep -Fv generated \
| xargs $(RUSTFMT) $(rustfmt_flags)
Expand Down
1 change: 1 addition & 0 deletions rust/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0

*.rlib
bindings_generated.rs
bindings_helpers_generated.rs
doctests_kernel_generated.rs
Expand Down
121 changes: 111 additions & 10 deletions rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ always-$(CONFIG_RUST) += exports_core_generated.h
obj-$(CONFIG_RUST) += helpers.o
CFLAGS_REMOVE_helpers.o = -Wmissing-prototypes -Wmissing-declarations

always-$(CONFIG_RUST) += libproc_macro2.rlib libquote.rlib libsyn.rlib
always-$(CONFIG_RUST) += libmacros.so
no-clean-files += libmacros.so

Expand Down Expand Up @@ -71,11 +72,60 @@ alloc-cfgs = \
--cfg no_sync \
--cfg no_thin

proc_macro2-cfgs := \
--cfg 'feature="proc-macro"' \
--cfg wrap_proc_macro

proc_macro2-skip_flags := \
-Drust_2018_idioms \
-Dunreachable_pub

proc_macro2-flags := \
-Amissing_docs \
$(proc_macro2-cfgs)

quote-cfgs := \
--cfg 'feature="proc-macro"'

quote-skip_flags := \
--edition=2021 \
-Drust_2018_idioms

quote-flags := \
--edition=2018 \
-Amissing_docs \
--extern proc_macro2 \
$(quote-cfgs)

syn-cfgs := \
--cfg 'feature="clone-impls"' \
--cfg 'feature="derive"' \
--cfg 'feature="extra-traits"' \
--cfg 'feature="fold"' \
--cfg 'feature="full"' \
--cfg 'feature="parsing"' \
--cfg 'feature="printing"' \
--cfg 'feature="proc-macro"' \
--cfg 'feature="quote"' \
--cfg 'feature="visit"' \
--cfg 'feature="visit-mut"'

syn-skip_flags := \
-Drust_2018_idioms \
-Dunreachable_pub

syn-flags := \
-Amissing_docs \
--extern proc_macro2 \
--extern quote \
$(syn-cfgs)

quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
cmd_rustdoc = \
OBJTREE=$(abspath $(objtree)) \
$(RUSTDOC) $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags)) \
$(rustc_target_flags) -L$(objtree)/$(obj) \
--extern proc_macro2 --extern quote --extern syn \
--output $(rustdoc_output) \
--crate-name $(subst rustdoc-,,$@) \
$(if $(rustdoc_host),,--sysroot=/dev/null) \
Expand Down Expand Up @@ -137,8 +187,9 @@ rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
quiet_cmd_rustc_test_library = RUSTC TL $<
cmd_rustc_test_library = \
OBJTREE=$(abspath $(objtree)) \
$(RUSTC) $(rust_common_flags) \
@$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \
$(RUSTC) \
$(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
@$(objtree)/include/generated/rustc_cfg \
--crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
--out-dir $(objtree)/$(obj)/test --cfg testlib \
--sysroot $(objtree)/$(obj)/test/sysroot \
Expand All @@ -148,9 +199,25 @@ quiet_cmd_rustc_test_library = RUSTC TL $<
rusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE
+$(call if_changed,rustc_test_library)

rusttestlib-macros: private rustc_target_flags = --extern proc_macro
rusttestlib-proc_macro2: private skip_flags = $(proc_macro2-skip_flags)
rusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
rusttestlib-proc_macro2: $(src)/proc-macro2/lib.rs rusttest-prepare FORCE
+$(call if_changed,rustc_test_library)

rusttestlib-quote: private skip_flags = $(quote-skip_flags)
rusttestlib-quote: private rustc_target_flags = $(quote-flags)
rusttestlib-quote: $(src)/quote/lib.rs rusttestlib-proc_macro2 FORCE
+$(call if_changed,rustc_test_library)

rusttestlib-syn: private skip_flags = $(syn-skip_flags)
rusttestlib-syn: private rustc_target_flags = $(syn-flags)
rusttestlib-syn: $(src)/syn/lib.rs rusttestlib-quote FORCE
+$(call if_changed,rustc_test_library)

rusttestlib-macros: private rustc_target_flags = --extern proc_macro \
--extern proc_macro2 --extern quote --extern syn
rusttestlib-macros: private rustc_test_library_proc = yes
rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
rusttestlib-macros: $(src)/macros/lib.rs rusttestlib-syn FORCE
+$(call if_changed,rustc_test_library)

rusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
Expand Down Expand Up @@ -256,9 +323,10 @@ quiet_cmd_rustsysroot = RUSTSYSROOT
rusttest-prepare: FORCE
+$(call if_changed,rustsysroot)

rusttest-macros: private rustc_target_flags = --extern proc_macro
rusttest-macros: private rustc_target_flags = --extern proc_macro \
--extern proc_macro2 --extern quote --extern syn
rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
rusttest-macros: $(src)/macros/lib.rs rusttestlib-syn FORCE
+$(call if_changed,rustc_test)
+$(call if_changed,rustdoc_test)

Expand Down Expand Up @@ -384,19 +452,48 @@ $(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
$(call if_changed,exports)

quiet_cmd_rustc_hostlibrary = $(RUSTC_OR_CLIPPY_QUIET) H $@
cmd_rustc_hostlibrary = \
$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
$(filter-out $(skip_flags),$(KBUILD_HOSTRUSTFLAGS) $(rustc_target_flags)) \
-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
-Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
--emit=dep-info=$(depfile) --emit=link=$@ --crate-type rlib \
--out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \
--crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $<

$(obj)/libproc_macro2.rlib: private skip_clippy = 1
$(obj)/libproc_macro2.rlib: private skip_flags = $(proc_macro2-skip_flags)
$(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
$(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
+$(call if_changed_dep,rustc_hostlibrary)

$(obj)/libquote.rlib: private skip_clippy = 1
$(obj)/libquote.rlib: private skip_flags = $(quote-skip_flags)
$(obj)/libquote.rlib: private rustc_target_flags = $(quote-flags)
$(obj)/libquote.rlib: $(src)/quote/lib.rs $(obj)/libproc_macro2.rlib FORCE
+$(call if_changed_dep,rustc_hostlibrary)

$(obj)/libsyn.rlib: private skip_clippy = 1
$(obj)/libsyn.rlib: private skip_flags = $(syn-skip_flags)
$(obj)/libsyn.rlib: private rustc_target_flags = $(syn-flags)
$(obj)/libsyn.rlib: $(src)/syn/lib.rs $(obj)/libquote.rlib FORCE
+$(call if_changed_dep,rustc_hostlibrary)

quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
cmd_rustc_procmacro = \
$(RUSTC_OR_CLIPPY) $(rust_common_flags) \
$(RUSTC_OR_CLIPPY) $(KBUILD_HOSTRUSTFLAGS) \
-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
-Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
--extern proc_macro2 --extern quote --extern syn \
--crate-type proc-macro \
--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
-L$(objtree)/$(obj) --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<

# Procedural macros can only be used with the `rustc` that compiled it.
# Therefore, to get `libmacros.so` automatically recompiled when the compiler
# version changes, we add `core.o` as a dependency (even if it is not needed).
$(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/core.o FORCE
$(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/libsyn.rlib $(obj)/core.o FORCE
+$(call if_changed_dep,rustc_procmacro)

quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
Expand All @@ -413,7 +510,11 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L

rust-analyzer:
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
--cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
--cfgs='core=$(call escsq,$(core-cfgs))' \
--cfgs='alloc=$(call escsq,$(alloc-cfgs))' \
--cfgs='proc_macro2=$(call escsq,$(proc_macro2-cfgs))' \
--cfgs='quote=$(call escsq,$(quote-cfgs))' \
--cfgs='syn=$(call escsq,$(syn-cfgs))' \
$(realpath $(srctree)) $(realpath $(objtree)) \
$(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
Expand Down
80 changes: 18 additions & 62 deletions rust/kernel/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ use core::{

#[doc(hidden)]
pub mod __internal;
#[doc(hidden)]
pub mod macros;

/// Initialize and pin a type directly on the stack.
///
Expand Down Expand Up @@ -557,16 +555,9 @@ macro_rules! pin_init {
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}) => {
$crate::__init_internal!(
@this($($this)?),
@typ($t $(::<$($generics),*>)?),
@fields($($fields)*),
@error(::core::convert::Infallible),
@data(PinData, use_data),
@has_data(HasPinData, __pin_data),
@construct_closure(pin_init_from_closure),
@munch_fields($($fields)*),
)
$crate::macros::primitive_init!($(&$this in)? pin $t $(::<$($generics),*>)? {
$($fields)*
}? ::core::convert::Infallible)
};
}

Expand Down Expand Up @@ -614,30 +605,16 @@ macro_rules! try_pin_init {
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}) => {
$crate::__init_internal!(
@this($($this)?),
@typ($t $(::<$($generics),*>)? ),
@fields($($fields)*),
@error($crate::error::Error),
@data(PinData, use_data),
@has_data(HasPinData, __pin_data),
@construct_closure(pin_init_from_closure),
@munch_fields($($fields)*),
)
$crate::macros::primitive_init!($(&$this in)? pin $t $(::<$($generics),*>)? {
$($fields)*
}? $crate::error::Error)
};
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}? $err:ty) => {
$crate::__init_internal!(
@this($($this)?),
@typ($t $(::<$($generics),*>)? ),
@fields($($fields)*),
@error($err),
@data(PinData, use_data),
@has_data(HasPinData, __pin_data),
@construct_closure(pin_init_from_closure),
@munch_fields($($fields)*),
)
$crate::macros::primitive_init!($(&$this in)? pin $t $(::<$($generics),*>)? {
$($fields)*
}? $err)
};
}

Expand All @@ -663,16 +640,9 @@ macro_rules! init {
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}) => {
$crate::__init_internal!(
@this($($this)?),
@typ($t $(::<$($generics),*>)?),
@fields($($fields)*),
@error(::core::convert::Infallible),
@data(InitData, /*no use_data*/),
@has_data(HasInitData, __init_data),
@construct_closure(init_from_closure),
@munch_fields($($fields)*),
)
$crate::macros::primitive_init!($(&$this in)? $t $(::<$($generics),*>)? {
$($fields)*
}? ::core::convert::Infallible)
}
}

Expand Down Expand Up @@ -714,30 +684,16 @@ macro_rules! try_init {
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}) => {
$crate::__init_internal!(
@this($($this)?),
@typ($t $(::<$($generics),*>)?),
@fields($($fields)*),
@error($crate::error::Error),
@data(InitData, /*no use_data*/),
@has_data(HasInitData, __init_data),
@construct_closure(init_from_closure),
@munch_fields($($fields)*),
)
$crate::macros::primitive_init!($(&$this in)? $t $(::<$($generics),*>)? {
$($fields)*
}? $crate::error::Error)
};
($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
$($fields:tt)*
}? $err:ty) => {
$crate::__init_internal!(
@this($($this)?),
@typ($t $(::<$($generics),*>)?),
@fields($($fields)*),
@error($err),
@data(InitData, /*no use_data*/),
@has_data(HasInitData, __init_data),
@construct_closure(init_from_closure),
@munch_fields($($fields)*),
)
$crate::macros::primitive_init!($(&$this in)? $t $(::<$($generics),*>)? {
$($fields)*
}? $err)
};
}

Expand Down
Loading