[PATCH v10 2/5] rust: support formatting of foreign types

Tamir Duberstein tamird at gmail.com
Tue May 27 14:57:11 UTC 2025


On Tue, May 27, 2025 at 8:44 AM Alice Ryhl <aliceryhl at google.com> wrote:
>
> On Tue, May 27, 2025 at 12:18 AM Tamir Duberstein <tamird at gmail.com> wrote:
> > > > +}
> > > > +
> > > > +fn make_ident<'a, T: IntoIterator<Item = &'a str>>(
> > > > +    span: Span,
> > > > +    names: T,
> > > > +) -> impl Iterator<Item = TokenTree> + use<'a, T> {
> > > > +    names.into_iter().flat_map(move |name| {
> > > > +        [
> > > > +            TokenTree::Punct(Punct::new(':', Spacing::Joint)),
> > > > +            TokenTree::Punct(Punct::new(':', Spacing::Alone)),
> > > > +            TokenTree::Ident(Ident::new(name, span)),
> > > > +        ]
> > > > +    })
> > > > +}
> > > > diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
> > > > index d31e50c446b0..fa956eaa3ba7 100644
> > > > --- a/rust/macros/lib.rs
> > > > +++ b/rust/macros/lib.rs
> > > > @@ -10,6 +10,7 @@
> > > >  mod quote;
> > > >  mod concat_idents;
> > > >  mod export;
> > > > +mod fmt;
> > > >  mod helpers;
> > > >  mod kunit;
> > > >  mod module;
> > > > @@ -196,6 +197,24 @@ pub fn export(attr: TokenStream, ts: TokenStream) -> TokenStream {
> > > >      export::export(attr, ts)
> > > >  }
> > > >
> > > > +/// Like [`core::format_args!`], but automatically wraps arguments in [`kernel::fmt::Adapter`].
> > > > +///
> > > > +/// This macro allows generating `core::fmt::Arguments` while ensuring that each argument is wrapped
> > > > +/// with `::kernel::fmt::Adapter`, which customizes formatting behavior for kernel logging.
> > > > +///
> > > > +/// Named arguments used in the format string (e.g. `{foo}`) are detected and resolved from local
> > > > +/// bindings. All positional and named arguments are automatically wrapped.
> > > > +///
> > > > +/// This macro is an implementation detail of other kernel logging macros like [`pr_info!`] and
> > > > +/// should not typically be used directly.
> > > > +///
> > > > +/// [`kernel::fmt::Adapter`]: ../kernel/fmt/struct.Adapter.html
> > > > +/// [`pr_info!`]: ../kernel/macro.pr_info.html
> > > > +#[proc_macro]
> > > > +pub fn fmt(input: TokenStream) -> TokenStream {
> > >
> > > I'm wondering if we should name this `format_args` instead in order to
> > > better communicate that it's a replacement for `core::format_args!`.
> >
> > Unfortunately that introduces ambiguity in cases where
> > kernel::prelude::* is imported because core::format_args is in core's
> > prelude.
>
> I'm pretty sure that glob imports are higher priority than the core
> prelude? Or is this because there are macros that now incorrectly use
> kernel::prelude::format_args when they should use the one from core?

compiler says no, e.g.:

error[E0659]: `format_args` is ambiguous
    --> rust/doctests_kernel_generated.rs:8783:25
     |
8783 |     kernel::kunit::info(format_args!("    #
rust_doctest_kernel_workqueue_rs_3.location:
rust/kernel/workqueue.rs:77\n"));
     |                         ^^^^^^^^^^^ ambiguous name
     |
     = note: ambiguous because of a conflict between a name from a
glob import and an outer scope during import or macro resolution
     = note: `format_args` could refer to a macro from prelude
note: `format_args` could also refer to the macro imported here
    --> rust/doctests_kernel_generated.rs:8772:9
     |
8772 |     use kernel::prelude::*;
     |         ^^^^^^^^^^^^^^^^^^
     = help: consider adding an explicit import of `format_args` to disambiguate


More information about the dri-devel mailing list