rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Timur Tabi <ttabi@nvidia.com>
To: "lossin@kernel.org" <lossin@kernel.org>,
	"miguel.ojeda.sandonis@gmail.com"
	<miguel.ojeda.sandonis@gmail.com>
Cc: "ojeda@kernel.org" <ojeda@kernel.org>,
	"aliceryhl@google.com" <aliceryhl@google.com>,
	"dakr@kernel.org" <dakr@kernel.org>,
	"rust-for-linux@vger.kernel.org" <rust-for-linux@vger.kernel.org>
Subject: Re: [PATCH v4] rust: introduce sfile macro for succinct code tracing
Date: Wed, 18 Jun 2025 20:05:30 +0000	[thread overview]
Message-ID: <579273832b4accc4cd989d5182f3179c9950966f.camel@nvidia.com> (raw)
In-Reply-To: <CANiq72moqFBWeS9RL6y8wzpxURvhdz4D0J8WfjaOBp0XSgufdg@mail.gmail.com>

On Tue, 2025-06-17 at 09:54 +0200, Miguel Ojeda wrote:
> Yeah, I meant the `const fn`, not the macro, i.e. returning `Option`
> (not `Result`) so that we keep the const block in the macro with an
> `unwrap()` in the macro (so no runtime panics).

I'm having trouble coding this up.  I have this so far, but it doesn't compile:

#[macro_export]
macro_rules! sfile {
    () => {{
        const fn shortname() -> Option<&'static str> {
            const FILE: &str = core::file!();
...

            unsafe { core::str::from_utf8_unchecked(p) }
        };

        shortname().unwrap()
    }};
}

I get a bunch of errors, starting with this:

error[E0308]: mismatched types
   --> /home/ttabi/linux/rust/kernel/print.rs:492:22
    |
456 | macro_rules! sfile {
    | ------------------ in this expansion of `sfile!`
457 |     () => {{
458 |         const fn shortname() -> Option<&'static str> {
    |                                 -------------------- expected `Option<&'static str>` because
of return type
...
492 |             unsafe { core::str::from_utf8_unchecked(p) }
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<&str>`, found `&str`
    |
   ::: samples/rust/rust_minimal.rs:25:37
    |
25  |         const SFILE: &'static str = sfile!();
    |                                     -------- in this macro invocation
    |
    = note:   expected enum `Option<&'static str>`
            found reference `&str`


  reply	other threads:[~2025-06-18 20:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-16 19:34 [PATCH v4] rust: introduce sfile macro for succinct code tracing Timur Tabi
2025-06-16 21:03 ` Miguel Ojeda
2025-06-17  7:29   ` Benno Lossin
2025-06-17  7:54     ` Miguel Ojeda
2025-06-18 20:05       ` Timur Tabi [this message]
2025-06-21 16:59         ` Miguel Ojeda
2025-06-26 16:30           ` Timur Tabi
2025-06-26 20:57             ` Miguel Ojeda
2025-06-18 20:07   ` Timur Tabi
2025-06-21 16:47     ` Miguel Ojeda
2025-06-18 11:20 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=579273832b4accc4cd989d5182f3179c9950966f.camel@nvidia.com \
    --to=ttabi@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=dakr@kernel.org \
    --cc=lossin@kernel.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).