[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251110131049.18e2f5c9@eugeo>
Date: Mon, 10 Nov 2025 13:10:49 +0000
From: Gary Guo <gary@...yguo.net>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Nathan Chancellor <nathan@...nel.org>, Nicolas Schier <nicolas@...sle.eu>,
Boqun Feng <boqun.feng@...il.com>, "Björn Roy Baron"
<bjorn3_gh@...tonmail.com>, Benno Lossin <lossin@...nel.org>, Andreas
Hindborg <a.hindborg@...nel.org>, Trevor Gross <tmgross@...ch.edu>, Danilo
Krummrich <dakr@...nel.org>, rust-for-linux@...r.kernel.org,
linux-kbuild@...r.kernel.org, linux-kernel@...r.kernel.org,
patches@...ts.linux.dev
Subject: Re: [PATCH 09/18] rust: proc-macro2: enable support in kbuild
On Mon, 10 Nov 2025 11:38:04 +0000
Alice Ryhl <aliceryhl@...gle.com> wrote:
> On Mon, Nov 10, 2025 at 10:50:14AM +0100, Miguel Ojeda wrote:
> > With all the new files in place and ready from the new crate, enable
> > the support for it in the build system.
> >
> > `proc_macro_byte_character` and `proc_macro_c_str_literals` were
> > stabilized in Rust 1.79.0 [1] and were implemented earlier than our
> > minimum Rust version (1.78) [2][3]. Thus just enable them instead of using
> > the `cfg` that `proc-macro2` uses to emulate them in older compilers.
> >
> > Link: https://github.com/rust-lang/rust/pull/123431 [1]
> > Link: https://github.com/rust-lang/rust/pull/112711 [2]
> > Link: https://github.com/rust-lang/rust/pull/119651 [3]
> > Signed-off-by: Miguel Ojeda <ojeda@...nel.org>
> > ---
> > Makefile | 3 +++
> > rust/Makefile | 32 +++++++++++++++++++++++++++++--
> > scripts/generate_rust_analyzer.py | 7 +++++++
> > 3 files changed, 40 insertions(+), 2 deletions(-)
> >
> > diff --git a/Makefile b/Makefile
> > index fb4389aa5d5f..6ff887523eee 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -1830,6 +1830,9 @@ PHONY += rustfmt rustfmtcheck
> >
> > rustfmt:
> > $(Q)find $(srctree) $(RCS_FIND_IGNORE) \
> > + \( \
> > + -path $(srctree)/rust/proc-macro2 \
> > + \) -prune -o \
> > -type f -a -name '*.rs' -a ! -name '*generated*' -print \
> > | xargs $(RUSTFMT) $(rustfmt_flags)
> >
> > diff --git a/rust/Makefile b/rust/Makefile
> > index 9eea6563ef35..a614a23023cb 100644
> > --- a/rust/Makefile
> > +++ b/rust/Makefile
> > @@ -27,6 +27,8 @@ endif
> >
> > obj-$(CONFIG_RUST) += exports.o
> >
> > +always-$(CONFIG_RUST) += libproc_macro2.rlib
> > +
> > always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
> > always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
> >
> > @@ -76,6 +78,17 @@ core-flags := \
> > --edition=$(core-edition) \
> > $(call cfgs-to-flags,$(core-cfgs))
> >
> > +proc_macro2-cfgs := \
> > + feature="proc-macro" \
> > + wrap_proc_macro \
> > + $(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
> > +
> > +# Stable since Rust 1.79.0: `feature(proc_macro_byte_character,proc_macro_c_str_literals)`.
> > +proc_macro2-flags := \
> > + --cap-lints=allow \
> > + -Zcrate-attr='feature(proc_macro_byte_character,proc_macro_c_str_literals)' \
> > + $(call cfgs-to-flags,$(proc_macro2-cfgs))
> > +
>
> I don't understand this. We enable the features even on 1.79, but we
> only pass the proc_macro_span_file, proc_macro_span_location cfgs on
> 1.88 and above?
>
> We add the appropriate feature(_) invocations on older compilers, so
> should we not support those things on those compilers?
>
> Alice
For 1.79- it switches to any polyfill for byte_character and
c_str_literals using cfg no_literal_byte_character and
no_literal_c_string. These feature gates are stablised already so I
think turning on features on makes sense for this scenario.
For 1.88+, `Span::file`, `Span::line` etc are stable so proc-macro2 can
stop using polyfills. These APIs are not gated under specific feature
gates before stablising however. They were gated under the generic
`proc_macro_span` feature gate and only renamed to
`proc_macro_span_location` during stabilisation. So in this case
there's no already-stable feature flags to turn on. (proc_macro_span
feature would make these APIs usable in 1.79, but that's a
still-unstable feature flag in all versions of rustc).
So I think the implementation here is sensible. I believe Miguel's
patch is also pretty much replicating the logic in proc-macro2's
build.rs.
Best,
Gary
Powered by blists - more mailing lists