public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gpu: nova-core: bitfield: fix broken Default implementation
@ 2026-03-31  7:56 Eliot Courtney
  2026-03-31 13:21 ` Gary Guo
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Eliot Courtney @ 2026-03-31  7:56 UTC (permalink / raw)
  To: Danilo Krummrich, Alexandre Courbot, Alice Ryhl, David Airlie,
	Simona Vetter
  Cc: John Hubbard, Alistair Popple, Joel Fernandes, Timur Tabi,
	rust-for-linux, dri-devel, linux-kernel, Eliot Courtney

The current implementation does not actually set the default values for
the fields in the bitfield. Set it and add a test.

Signed-off-by: Eliot Courtney <ecourtney@nvidia.com>
---
It doesn't actually set the default values for each field. I know that
bitfields are being moved soon, so maybe this fix is unnecessary but I
saw it and it's a simple fix.
---
 drivers/gpu/nova-core/bitfield.rs | 50 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/nova-core/bitfield.rs b/drivers/gpu/nova-core/bitfield.rs
index 16e143658c51..22d68fc21a1f 100644
--- a/drivers/gpu/nova-core/bitfield.rs
+++ b/drivers/gpu/nova-core/bitfield.rs
@@ -4,6 +4,8 @@
 //!
 //! Support for defining bitfields in Rust structures. Also used by the [`register!`] macro.
 
+use kernel::prelude::*;
+
 /// Defines a struct with accessors to access bits within an inner unsigned integer.
 ///
 /// # Syntax
@@ -314,12 +316,11 @@ fn fmt(&self, f: &mut ::kernel::fmt::Formatter<'_>) -> ::kernel::fmt::Result {
         /// Returns a value for the bitfield where all fields are set to their default value.
         impl ::core::default::Default for $name {
             fn default() -> Self {
-                #[allow(unused_mut)]
-                let mut value = Self(Default::default());
+                let value = Self(Default::default());
 
                 ::kernel::macros::paste!(
                 $(
-                value.[<set_ $field>](Default::default());
+                let value = value.[<set_ $field>](Default::default());
                 )*
                 );
 
@@ -328,3 +329,46 @@ fn default() -> Self {
         }
     };
 }
+
+#[kunit_tests(nova_core_bitfield)]
+mod tests {
+    use super::*;
+
+    #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
+    enum State {
+        Inactive = 0,
+        #[default]
+        Active = 1,
+    }
+
+    impl From<bool> for State {
+        fn from(value: bool) -> Self {
+            if value {
+                State::Active
+            } else {
+                State::Inactive
+            }
+        }
+    }
+
+    impl From<State> for bool {
+        fn from(state: State) -> bool {
+            match state {
+                State::Inactive => false,
+                State::Active => true,
+            }
+        }
+    }
+
+    bitfield! {
+        struct TestBitfield(u32) {
+            0:0 state as bool => State;
+        }
+    }
+
+    #[test]
+    fn default_impl() -> Result {
+        assert_eq!(TestBitfield::default().state(), State::Active);
+        Ok(())
+    }
+}

---
base-commit: 7c50d748b4a635bc39802ea3f6b120e66b1b9067
change-id: 20260331-fix-bitfield-a03f6d1b9e00

Best regards,
--  
Eliot Courtney <ecourtney@nvidia.com>


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

end of thread, other threads:[~2026-03-31 22:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-31  7:56 [PATCH] gpu: nova-core: bitfield: fix broken Default implementation Eliot Courtney
2026-03-31 13:21 ` Gary Guo
2026-03-31 13:36 ` Danilo Krummrich
2026-03-31 20:53   ` John Hubbard
2026-03-31 22:32     ` Eliot Courtney
2026-03-31 13:47 ` Joel Fernandes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox