rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: macros: update 'paste!' macro to accept string literals
@ 2023-10-08  9:48 Trevor Gross
  2023-10-08 12:27 ` Martin Rodriguez Reboredo
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Trevor Gross @ 2023-10-08  9:48 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Martin Rodriguez Reboredo,
	rust-for-linux, linux-kernel, FUJITA Tomonori, Trevor Gross

Enable combining identifiers with string literals in the 'paste!' macro.
This allows combining user-specified strings with affixes to create
namespaced identifiers.

This sample code:

    macro_rules! m {
        ($name:lit) => {
            paste!(struct [<_some_ $name _struct_>];)
        }
    }

    m!("foo_bar");

Would previously cause a compilation error. It will now generate:

    struct _some_foo_bar_struct_;

Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Signed-off-by: Trevor Gross <tmgross@umich.edu>
---

Original mention of this problem in [1]

[1]: https://lore.kernel.org/rust-for-linux/20231008.164906.1151622782836568538.fujita.tomonori@gmail.com/

 rust/macros/paste.rs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/rust/macros/paste.rs b/rust/macros/paste.rs
index 385a78434224..f40d42b35b58 100644
--- a/rust/macros/paste.rs
+++ b/rust/macros/paste.rs
@@ -9,7 +9,15 @@ fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree {
     loop {
         match tokens.next() {
             None => break,
-            Some(TokenTree::Literal(lit)) => segments.push((lit.to_string(), lit.span())),
+            Some(TokenTree::Literal(lit)) => {
+                // Allow us to concat string literals by stripping quotes
+                let mut value = lit.to_string();
+                if value.starts_with('"') && value.ends_with('"') {
+                    value.remove(0);
+                    value.pop();
+                }
+                segments.push((value, lit.span()));
+            }
             Some(TokenTree::Ident(ident)) => {
                 let mut value = ident.to_string();
                 if value.starts_with("r#") {
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-08  9:48 [PATCH] rust: macros: update 'paste!' macro to accept string literals Trevor Gross
@ 2023-10-08 12:27 ` Martin Rodriguez Reboredo
  2023-10-09  3:03   ` Trevor Gross
  2023-10-09  8:44 ` Vincenzo Palazzo
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Martin Rodriguez Reboredo @ 2023-10-08 12:27 UTC (permalink / raw)
  To: Trevor Gross, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, rust-for-linux, linux-kernel,
	FUJITA Tomonori

On 10/8/23 06:48, Trevor Gross wrote:
> Enable combining identifiers with string literals in the 'paste!' macro.
> This allows combining user-specified strings with affixes to create
> namespaced identifiers.
> 
> This sample code:
> 
>      macro_rules! m {
>          ($name:lit) => {
>              paste!(struct [<_some_ $name _struct_>];)
>          }
>      }
> 
>      m!("foo_bar");
> 
> Would previously cause a compilation error. It will now generate:
> 
>      struct _some_foo_bar_struct_;
> 
> Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> Signed-off-by: Trevor Gross <tmgross@umich.edu>
> ---
> 
> Original mention of this problem in [1]
> 
> [1]: https://lore.kernel.org/rust-for-linux/20231008.164906.1151622782836568538.fujita.tomonori@gmail.com/

Next time I think you should put this in `Fixes:`.

> [...]
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-08 12:27 ` Martin Rodriguez Reboredo
@ 2023-10-09  3:03   ` Trevor Gross
  2023-10-09 10:49     ` Miguel Ojeda
  0 siblings, 1 reply; 11+ messages in thread
From: Trevor Gross @ 2023-10-09  3:03 UTC (permalink / raw)
  To: Martin Rodriguez Reboredo
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, rust-for-linux, linux-kernel, FUJITA Tomonori

On Sun, Oct 8, 2023 at 8:27 AM Martin Rodriguez Reboredo
<yakoyoku@gmail.com> wrote:
>
> Next time I think you should put this in `Fixes:`.
>
> > [...]
> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
>

Good point, thanks! I'll add that if there is a v2 (or Miguel can
probably add it if not)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-08  9:48 [PATCH] rust: macros: update 'paste!' macro to accept string literals Trevor Gross
  2023-10-08 12:27 ` Martin Rodriguez Reboredo
@ 2023-10-09  8:44 ` Vincenzo Palazzo
  2023-10-09 10:02 ` Alice Ryhl
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Vincenzo Palazzo @ 2023-10-09  8:44 UTC (permalink / raw)
  To: Trevor Gross, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Martin Rodriguez Reboredo,
	rust-for-linux, linux-kernel, FUJITA Tomonori

> This allows combining user-specified strings with affixes to create
> namespaced identifiers.
>
> This sample code:
>
>     macro_rules! m {
>         ($name:lit) => {
>             paste!(struct [<_some_ $name _struct_>];)
>         }
>     }
>
>     m!("foo_bar");
>
> Would previously cause a compilation error. It will now generate:
>
>     struct _some_foo_bar_struct_;
>
> Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> Signed-off-by: Trevor Gross <tmgross@umich.edu>
> ---

Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-08  9:48 [PATCH] rust: macros: update 'paste!' macro to accept string literals Trevor Gross
  2023-10-08 12:27 ` Martin Rodriguez Reboredo
  2023-10-09  8:44 ` Vincenzo Palazzo
@ 2023-10-09 10:02 ` Alice Ryhl
  2023-10-09 14:51 ` Benno Lossin
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Alice Ryhl @ 2023-10-09 10:02 UTC (permalink / raw)
  To: Trevor Gross
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Martin Rodriguez Reboredo, rust-for-linux, linux-kernel,
	FUJITA Tomonori

On Sun, Oct 8, 2023 at 11:51 AM Trevor Gross <tmgross@umich.edu> wrote:
> Enable combining identifiers with string literals in the 'paste!' macro.
> This allows combining user-specified strings with affixes to create
> namespaced identifiers.
>
> This sample code:
>
>     macro_rules! m {
>         ($name:lit) => {
>             paste!(struct [<_some_ $name _struct_>];)
>         }
>     }
>
>     m!("foo_bar");
>
> Would previously cause a compilation error. It will now generate:
>
>     struct _some_foo_bar_struct_;
>
> Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> Signed-off-by: Trevor Gross <tmgross@umich.edu>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-09  3:03   ` Trevor Gross
@ 2023-10-09 10:49     ` Miguel Ojeda
  2023-10-09 19:14       ` Trevor Gross
  0 siblings, 1 reply; 11+ messages in thread
From: Miguel Ojeda @ 2023-10-09 10:49 UTC (permalink / raw)
  To: Trevor Gross
  Cc: Martin Rodriguez Reboredo, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, rust-for-linux,
	linux-kernel, FUJITA Tomonori

On Mon, Oct 9, 2023 at 5:04 AM Trevor Gross <tmgross@umich.edu> wrote:
>
> Good point, thanks! I'll add that if there is a v2 (or Miguel can
> probably add it if not)

Yes, I add them myself when I notice they are missing (e.g. most
recently 2 of the ones in `rust-fixes`), but patches should definitely
come with the `Fixes: ` tag themselves, i.e. it should be the
exceptional case.

However, is this actually a fix? The title and commit message make it
sound like it is a new feature rather than a fix. And the docs of the
macro says literals are not supported, right?

So this probably needs to update those docs too (and ideally add an
example with the newly supported construct too). Or am I
misunderstanding?

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-08  9:48 [PATCH] rust: macros: update 'paste!' macro to accept string literals Trevor Gross
                   ` (2 preceding siblings ...)
  2023-10-09 10:02 ` Alice Ryhl
@ 2023-10-09 14:51 ` Benno Lossin
  2023-10-24 12:54 ` Gary Guo
  2023-10-24 16:51 ` Boqun Feng
  5 siblings, 0 replies; 11+ messages in thread
From: Benno Lossin @ 2023-10-09 14:51 UTC (permalink / raw)
  To: Trevor Gross, Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Andreas Hindborg,
	Alice Ryhl, Martin Rodriguez Reboredo, rust-for-linux,
	linux-kernel, FUJITA Tomonori

On 08.10.23 11:48, Trevor Gross wrote:
> Enable combining identifiers with string literals in the 'paste!' macro.
> This allows combining user-specified strings with affixes to create
> namespaced identifiers.
> 
> This sample code:
> 
>      macro_rules! m {
>          ($name:lit) => {
>              paste!(struct [<_some_ $name _struct_>];)
>          }
>      }
> 
>      m!("foo_bar");
> 
> Would previously cause a compilation error. It will now generate:
> 
>      struct _some_foo_bar_struct_;
> 
> Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> Signed-off-by: Trevor Gross <tmgross@umich.edu>

Reviewed-by: Benno Lossin <benno.lossin@proton.me>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-09 10:49     ` Miguel Ojeda
@ 2023-10-09 19:14       ` Trevor Gross
  2023-10-12 20:45         ` Miguel Ojeda
  0 siblings, 1 reply; 11+ messages in thread
From: Trevor Gross @ 2023-10-09 19:14 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Martin Rodriguez Reboredo, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, rust-for-linux,
	linux-kernel, FUJITA Tomonori

On Mon, Oct 9, 2023 at 6:49 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Mon, Oct 9, 2023 at 5:04 AM Trevor Gross <tmgross@umich.edu> wrote:
> >
> > Good point, thanks! I'll add that if there is a v2 (or Miguel can
> > probably add it if not)
>
> Yes, I add them myself when I notice they are missing (e.g. most
> recently 2 of the ones in `rust-fixes`), but patches should definitely
> come with the `Fixes: ` tag themselves, i.e. it should be the
> exceptional case.
>
> However, is this actually a fix? The title and commit message make it
> sound like it is a new feature rather than a fix. And the docs of the
> macro says literals are not supported, right?

I suppose it is something that augments current behavior and "fixes"
the linked use case by making it possible. I am not sure what
qualifies as a fix.

> So this probably needs to update those docs too (and ideally add an
> example with the newly supported construct too). Or am I
> misunderstanding?
>
> Cheers,
> Miguel

I will update the documentation, thanks for the catch.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-09 19:14       ` Trevor Gross
@ 2023-10-12 20:45         ` Miguel Ojeda
  0 siblings, 0 replies; 11+ messages in thread
From: Miguel Ojeda @ 2023-10-12 20:45 UTC (permalink / raw)
  To: Trevor Gross
  Cc: Martin Rodriguez Reboredo, Miguel Ojeda, Alex Gaynor,
	Wedson Almeida Filho, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, rust-for-linux,
	linux-kernel, FUJITA Tomonori

On Mon, Oct 9, 2023 at 9:14 PM Trevor Gross <tmgross@umich.edu> wrote:
>
> I suppose it is something that augments current behavior and "fixes"
> the linked use case by making it possible. I am not sure what
> qualifies as a fix.

`Fixes` is meant for issues/bugs. So if the macro was broken, i.e. it
does not do what it says it would, it would be a fix.

But if I understand correctly, the docs say this was not supported, so
it is not a fix, it is just expanding the feature set.

Similarly, `Reported-by` is not meant for feature requests.

> I will update the documentation, thanks for the catch.

Thanks!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-08  9:48 [PATCH] rust: macros: update 'paste!' macro to accept string literals Trevor Gross
                   ` (3 preceding siblings ...)
  2023-10-09 14:51 ` Benno Lossin
@ 2023-10-24 12:54 ` Gary Guo
  2023-10-24 16:51 ` Boqun Feng
  5 siblings, 0 replies; 11+ messages in thread
From: Gary Guo @ 2023-10-24 12:54 UTC (permalink / raw)
  To: Trevor Gross
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Martin Rodriguez Reboredo, rust-for-linux, linux-kernel,
	FUJITA Tomonori

On Sun,  8 Oct 2023 05:48:18 -0400
Trevor Gross <tmgross@umich.edu> wrote:

> Enable combining identifiers with string literals in the 'paste!' macro.
> This allows combining user-specified strings with affixes to create
> namespaced identifiers.
> 
> This sample code:
> 
>     macro_rules! m {
>         ($name:lit) => {
>             paste!(struct [<_some_ $name _struct_>];)
>         }
>     }
> 
>     m!("foo_bar");
> 
> Would previously cause a compilation error. It will now generate:
> 
>     struct _some_foo_bar_struct_;
> 
> Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> Signed-off-by: Trevor Gross <tmgross@umich.edu>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
> 
> Original mention of this problem in [1]
> 
> [1]: https://lore.kernel.org/rust-for-linux/20231008.164906.1151622782836568538.fujita.tomonori@gmail.com/
> 
>  rust/macros/paste.rs | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/macros/paste.rs b/rust/macros/paste.rs
> index 385a78434224..f40d42b35b58 100644
> --- a/rust/macros/paste.rs
> +++ b/rust/macros/paste.rs
> @@ -9,7 +9,15 @@ fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree {
>      loop {
>          match tokens.next() {
>              None => break,
> -            Some(TokenTree::Literal(lit)) => segments.push((lit.to_string(), lit.span())),
> +            Some(TokenTree::Literal(lit)) => {
> +                // Allow us to concat string literals by stripping quotes
> +                let mut value = lit.to_string();
> +                if value.starts_with('"') && value.ends_with('"') {
> +                    value.remove(0);
> +                    value.pop();
> +                }
> +                segments.push((value, lit.span()));
> +            }
>              Some(TokenTree::Ident(ident)) => {
>                  let mut value = ident.to_string();
>                  if value.starts_with("r#") {


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] rust: macros: update 'paste!' macro to accept string literals
  2023-10-08  9:48 [PATCH] rust: macros: update 'paste!' macro to accept string literals Trevor Gross
                   ` (4 preceding siblings ...)
  2023-10-24 12:54 ` Gary Guo
@ 2023-10-24 16:51 ` Boqun Feng
  5 siblings, 0 replies; 11+ messages in thread
From: Boqun Feng @ 2023-10-24 16:51 UTC (permalink / raw)
  To: Trevor Gross
  Cc: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Martin Rodriguez Reboredo, rust-for-linux, linux-kernel,
	FUJITA Tomonori

On Sun, Oct 08, 2023 at 05:48:18AM -0400, Trevor Gross wrote:
> Enable combining identifiers with string literals in the 'paste!' macro.
> This allows combining user-specified strings with affixes to create
> namespaced identifiers.
> 
> This sample code:
> 
>     macro_rules! m {
>         ($name:lit) => {
>             paste!(struct [<_some_ $name _struct_>];)
>         }
>     }
> 
>     m!("foo_bar");
> 
> Would previously cause a compilation error. It will now generate:
> 
>     struct _some_foo_bar_struct_;
> 
> Reported-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> Signed-off-by: Trevor Gross <tmgross@umich.edu>

This looks good to me, but could you (in a follow-up patch mabye) add an
example demonstrating the usage, which could also serve as a test if we
can run doctest for macro doc. Thanks!

Regards,
Boqun

> ---
> 
> Original mention of this problem in [1]
> 
> [1]: https://lore.kernel.org/rust-for-linux/20231008.164906.1151622782836568538.fujita.tomonori@gmail.com/
> 
>  rust/macros/paste.rs | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/macros/paste.rs b/rust/macros/paste.rs
> index 385a78434224..f40d42b35b58 100644
> --- a/rust/macros/paste.rs
> +++ b/rust/macros/paste.rs
> @@ -9,7 +9,15 @@ fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree {
>      loop {
>          match tokens.next() {
>              None => break,
> -            Some(TokenTree::Literal(lit)) => segments.push((lit.to_string(), lit.span())),
> +            Some(TokenTree::Literal(lit)) => {
> +                // Allow us to concat string literals by stripping quotes
> +                let mut value = lit.to_string();
> +                if value.starts_with('"') && value.ends_with('"') {
> +                    value.remove(0);
> +                    value.pop();
> +                }
> +                segments.push((value, lit.span()));
> +            }
>              Some(TokenTree::Ident(ident)) => {
>                  let mut value = ident.to_string();
>                  if value.starts_with("r#") {
> -- 
> 2.34.1
> 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-10-24 16:51 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-08  9:48 [PATCH] rust: macros: update 'paste!' macro to accept string literals Trevor Gross
2023-10-08 12:27 ` Martin Rodriguez Reboredo
2023-10-09  3:03   ` Trevor Gross
2023-10-09 10:49     ` Miguel Ojeda
2023-10-09 19:14       ` Trevor Gross
2023-10-12 20:45         ` Miguel Ojeda
2023-10-09  8:44 ` Vincenzo Palazzo
2023-10-09 10:02 ` Alice Ryhl
2023-10-09 14:51 ` Benno Lossin
2023-10-24 12:54 ` Gary Guo
2023-10-24 16:51 ` Boqun Feng

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).