Skip to contentSkip to conversation

rust: kernel: drop repetition in offset_of macro

PATCH In review Wei Liu 4 participants 7 messages Dec 16, 2022 Rust

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

PATCH 1/1 rust: kernel: drop repetition in offset_of macro

It doesn't make sense to allow multiple fields to be specified in offset_of.

No functional change.

---
Cc: Miguel Ojeda <[email protected]>
Cc: Alex Gaynor <[email protected]>
Cc: Wedson Almeida Filho <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: Björn Roy Baron <[email protected]>

---
 rust/kernel/lib.rs | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
rust/kernel/lib.rs +6 -4
index 6a322effa60c..2f3601e4e27e 100644
@@ -208,7 +208,7 @@ impl<'a> Drop for KParamGuard<'a> {
 /// ```
 #[macro_export]
 macro_rules! offset_of {
-    ($type:ty, $($f:tt)*) => {{
+    ($type:ty, $f:tt) => {{
         let tmp = core::mem::MaybeUninit::<$type>::uninit();
         let outer = tmp.as_ptr();
         // To avoid warnings when nesting `unsafe` blocks.
@@ -216,12 +216,14 @@ macro_rules! offset_of {
         // SAFETY: The pointer is valid and aligned, just not initialised; `addr_of` ensures that
         // we don't actually read from `outer` (which would be UB) nor create an intermediate
         // reference.
-        let inner = unsafe { core::ptr::addr_of!((*outer).$($f)*) } as *const u8;
+        let inner = unsafe { core::ptr::addr_of!((*outer).$f) } as *const u8;
         // To avoid warnings when nesting `unsafe` blocks.
         #[allow(unused_unsafe)]
         // SAFETY: The two pointers are within the same allocation block.
-        unsafe { inner.offset_from(outer as *const u8) }
-    }}
+        unsafe {
+            inner.offset_from(outer as *const u8)
+        }
+    }};
 }
 
 /// Produces a pointer to an object from a pointer to one of its fields.
Signature · 1 line
2.35.1
#1 lore raw
Quoted 3 lines · Wei Liu

It doesn't make sense to allow multiple fields to be specified in offset_of.

Why do you say it doesn't make sense?

Here's what I had in mind:

```
    struct Y {
        z: u32
    }
    struct X {
        y: Y
    }
    offset_of!(X, y.z)
```

Which is something very plausible.

Quoted 48 lines

No functional change.

---
Cc: Miguel Ojeda <[email protected]>
Cc: Alex Gaynor <[email protected]>
Cc: Wedson Almeida Filho <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: Björn Roy Baron <[email protected]>

---
 rust/kernel/lib.rs | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
rust/kernel/lib.rs +6 -5
index 6a322effa60c..2f3601e4e27e 100644
@@ -208,7 +208,7 @@ impl<'a> Drop for KParamGuard<'a> {
 /// ```
 #[macro_export]
 macro_rules! offset_of {
-    ($type:ty, $($f:tt)*) => {{
+    ($type:ty, $f:tt) => {{
         let tmp = core::mem::MaybeUninit::<$type>::uninit();
         let outer = tmp.as_ptr();
         // To avoid warnings when nesting `unsafe` blocks.
@@ -216,12 +216,14 @@ macro_rules! offset_of {
         // SAFETY: The pointer is valid and aligned, just not initialised; `addr_of` ensures that
         // we don't actually read from `outer` (which would be UB) nor create an intermediate
         // reference.
-        let inner = unsafe { core::ptr::addr_of!((*outer).$($f)*) } as *const u8;
+        let inner = unsafe { core::ptr::addr_of!((*outer).$f) } as *const u8;
         // To avoid warnings when nesting `unsafe` blocks.
         #[allow(unused_unsafe)]
         // SAFETY: The two pointers are within the same allocation block.
-        unsafe { inner.offset_from(outer as *const u8) }
-    }}
+        unsafe {
+            inner.offset_from(outer as *const u8)
+        }
+    }};
 }
 
 /// Produces a pointer to an object from a pointer to one of its fields.
--

2.35.1

#2 lore raw
Quoted 9 lines · Wedson Almeida Filho
Quoted 3 lines · Wei Liu

It doesn't make sense to allow multiple fields to be specified in offset_of.

Why do you say it doesn't make sense?

Here's what I had in mind:
```

Quoted 7 lines
    struct Y {
        z: u32
    }
    struct X {
        y: Y
    }
    offset_of!(X, y.z)

For me, it's not very obvious that "y.z" is multiples of token trees rather a single token tree ;-)

Maybe some examples of the match pattern of macros can help people catch up faster? Like

	"y.z" => tt [y], tt [.], tt [z]

I will defer to Gary or Bjorn for a better quick guide of Rust macros ;-)

Regards,
Boqun

Quoted 52 lines

```

Which is something very plausible.

Quoted 48 lines

No functional change.

---
Cc: Miguel Ojeda <[email protected]>
Cc: Alex Gaynor <[email protected]>
Cc: Wedson Almeida Filho <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: Björn Roy Baron <[email protected]>

---
 rust/kernel/lib.rs | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
rust/kernel/lib.rs +6 -5
index 6a322effa60c..2f3601e4e27e 100644
@@ -208,7 +208,7 @@ impl<'a> Drop for KParamGuard<'a> {
 /// ```
 #[macro_export]
 macro_rules! offset_of {
-    ($type:ty, $($f:tt)*) => {{
+    ($type:ty, $f:tt) => {{
         let tmp = core::mem::MaybeUninit::<$type>::uninit();
         let outer = tmp.as_ptr();
         // To avoid warnings when nesting `unsafe` blocks.
@@ -216,12 +216,14 @@ macro_rules! offset_of {
         // SAFETY: The pointer is valid and aligned, just not initialised; `addr_of` ensures that
         // we don't actually read from `outer` (which would be UB) nor create an intermediate
         // reference.
-        let inner = unsafe { core::ptr::addr_of!((*outer).$($f)*) } as *const u8;
+        let inner = unsafe { core::ptr::addr_of!((*outer).$f) } as *const u8;
         // To avoid warnings when nesting `unsafe` blocks.
         #[allow(unused_unsafe)]
         // SAFETY: The two pointers are within the same allocation block.
-        unsafe { inner.offset_from(outer as *const u8) }
-    }}
+        unsafe {
+            inner.offset_from(outer as *const u8)
+        }
+    }};
 }
 
 /// Produces a pointer to an object from a pointer to one of its fields.
--

2.35.1

author
#3 lore raw
Quoted 21 lines · Boqun Feng
Quoted 9 lines · Wedson Almeida Filho
Quoted 3 lines · Wei Liu

It doesn't make sense to allow multiple fields to be specified in offset_of.

Why do you say it doesn't make sense?

Here's what I had in mind:
```

Quoted 7 lines
    struct Y {
        z: u32
    }
    struct X {
        y: Y
    }
    offset_of!(X, y.z)

For me, it's not very obvious that "y.z" is multiples of token trees rather a single token tree ;-)

Quoted 8 lines

Maybe some examples of the match pattern of macros can help people catch up faster? Like

	"y.z" => tt [y], tt [.], tt [z]

I will defer to Gary or Bjorn for a better quick guide of Rust macros ;-)

What will be even better is someone please contribute such a macro to libcore so that I don't have to replicate the code snippet everywhere. :-)

I have a version somewhere, the second argument matches against ident, which was definitely not as flexible as tt.

Thanks,
Wei.

#4 lore raw
Quoted 4 lines · Wei Liu

What will be even better is someone please contribute such a macro to libcore so that I don't have to replicate the code snippet everywhere. :-)

It is happening! :-) See https://github.com/rust-lang/rfcs/pull/3308,
currently at the end of the FCP ("final comment period"), i.e. the RFC is likely getting accepted soon.

We track it at https://github.com/Rust-for-Linux/linux/issues/514 (one of the sub-lists in issue #2).

Cheers,
Miguel

author
#5 lore raw
In reply to Wedson Almeida Filho
Quoted 18 lines · Wedson Almeida Filho
Quoted 3 lines · Wei Liu

It doesn't make sense to allow multiple fields to be specified in offset_of.

Why do you say it doesn't make sense?

Here's what I had in mind:

```
    struct Y {
        z: u32
    }
    struct X {
        y: Y
    }
    offset_of!(X, y.z)
```
Quoted 1 line

Which is something very plausible.

You're right. I didn't consider that use case. This patch can be ignored.

Thanks,
Wei.

author
#6 lore raw
In reply to Wedson Almeida Filho
Quoted 34 lines · Wedson Almeida Filho
Quoted 3 lines · Wei Liu

It doesn't make sense to allow multiple fields to be specified in offset_of.

Why do you say it doesn't make sense?

Here's what I had in mind:

```
    struct Y {
        z: u32
    }
    struct X {
        y: Y
    }
    offset_of!(X, y.z)
```

Which is something very plausible.

Quoted 14 lines

No functional change.

---
Cc: Miguel Ojeda <[email protected]>
Cc: Alex Gaynor <[email protected]>
Cc: Wedson Almeida Filho <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Gary Guo <[email protected]>
Cc: Björn Roy Baron <[email protected]>

---
 rust/kernel/lib.rs | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
Quoted 9 lines

rust/kernel/lib.rs impl<'a> Drop for KParamGuard<'a> { · line 211

Quoted 9 lines
rust/kernel/lib.rs +0 -1
index 6a322effa60c..2f3601e4e27e 100644
@@ -208,7 +208,7 @@ impl<'a> Drop for KParamGuard<'a> {
 /// ```
 #[macro_export]
 macro_rules! offset_of {
-    ($type:ty, $($f:tt)*) => {{

Shouldn't this be + instead of *?

offset_of!(X,) is valid according to this pattern.

Thanks,
Wei.