mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Almeida <daniel.almeida@collabora.com>
To: "Daniel Almeida" <daniel.almeida@collabora.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Benno Lossin" <lossin@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>
Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
	 linux-kernel@vger.kernel.org
Subject: [PATCH 09/13] drm/tyr: justify the casts of enums to their integer value
Date: Wed, 25 Nov 2026 19:47:00 -0300	[thread overview]
Message-ID: <20261125-b4-tyr-as-casts-v1-9-2b2f53164d86@collabora.com> (raw)
In-Reply-To: <20261125-b4-tyr-as-casts-v1-0-2b2f53164d86@collabora.com>

An `as` cast silently drops the bits that do not fit the target type,
and nothing at the cast shows whether that can happen. As part of a
larger effort to reduce the number of `as` casts in Tyr, justify the
casts that convert an enum to its integer value. These stay, since
`as` is how Rust reads the value of an enum.

Each of them gets a `// CAST:` comment and an
`#[expect(clippy::as_conversions)]`, so that the lint can be enabled
for the driver.

No functional change intended.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 drivers/gpu/drm/tyr/fw.rs   |  2 ++
 drivers/gpu/drm/tyr/regs.rs | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/drivers/gpu/drm/tyr/fw.rs b/drivers/gpu/drm/tyr/fw.rs
index 14cfe2884d5a..2fa9852e0b83 100644
--- a/drivers/gpu/drm/tyr/fw.rs
+++ b/drivers/gpu/drm/tyr/fw.rs
@@ -98,7 +98,9 @@ fn from(value: Bounded<u32, 2>) -> Self {
 }
 
 impl From<CacheMode> for Bounded<u32, 2> {
+    #[expect(clippy::as_conversions)]
     fn from(value: CacheMode) -> Self {
+        // CAST: `CacheMode` is `#[repr(u8)]`, so the cast is lossless.
         Bounded::try_new(value as u32).unwrap()
     }
 }
diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
index 49e4adf2231d..443e9a1e7a21 100644
--- a/drivers/gpu/drm/tyr/regs.rs
+++ b/drivers/gpu/drm/tyr/regs.rs
@@ -251,7 +251,9 @@ fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
     }
 
     impl From<GpuCommand> for Bounded<u32, 8> {
+        #[expect(clippy::as_conversions)]
         fn from(cmd: GpuCommand) -> Self {
+            // CAST: `GpuCommand` is `#[repr(u8)]`, so the cast is lossless.
             (cmd as u8).into()
         }
     }
@@ -279,7 +281,9 @@ fn try_from(val: Bounded<u32, 4>) -> Result<Self, Self::Error> {
     }
 
     impl From<ResetMode> for Bounded<u32, 4> {
+        #[expect(clippy::as_conversions)]
         fn from(mode: ResetMode) -> Self {
+            // CAST: `ResetMode` is `#[repr(u8)]`, so the cast is lossless.
             Bounded::try_new(mode as u32).unwrap()
         }
     }
@@ -313,7 +317,9 @@ fn try_from(val: Bounded<u32, 4>) -> Result<Self, Self::Error> {
     }
 
     impl From<FlushMode> for Bounded<u32, 4> {
+        #[expect(clippy::as_conversions)]
         fn from(mode: FlushMode) -> Self {
+            // CAST: `FlushMode` is `#[repr(u8)]`, so the cast is lossless.
             Bounded::try_new(mode as u32).unwrap()
         }
     }
@@ -434,7 +440,9 @@ fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
     }
 
     impl From<ExceptionType> for Bounded<u32, 8> {
+        #[expect(clippy::as_conversions)]
         fn from(exc: ExceptionType) -> Self {
+            // CAST: `ExceptionType` is `#[repr(u8)]`, so the cast is lossless.
             (exc as u8).into()
         }
     }
@@ -465,7 +473,9 @@ fn from(val: Bounded<u32, 2>) -> Self {
     }
 
     impl From<AccessType> for Bounded<u32, 2> {
+        #[expect(clippy::as_conversions)]
         fn from(access: AccessType) -> Self {
+            // CAST: `AccessType` is `#[repr(u8)]`, so the cast is lossless.
             Bounded::try_new(access as u32).unwrap()
         }
     }
@@ -772,7 +782,9 @@ fn try_from(val: Bounded<u32, 32>) -> Result<Self, Self::Error> {
     }
 
     impl From<CoherencyMode> for Bounded<u32, 32> {
+        #[expect(clippy::as_conversions)]
         fn from(mode: CoherencyMode) -> Self {
+            // CAST: `CoherencyMode` is `#[repr(u8)]`, so the cast is lossless.
             (mode as u8).into()
         }
     }
@@ -814,7 +826,9 @@ fn try_from(val: Bounded<u32, 2>) -> Result<Self, Self::Error> {
     }
 
     impl From<McuControlMode> for Bounded<u32, 2> {
+        #[expect(clippy::as_conversions)]
         fn from(mode: McuControlMode) -> Self {
+            // CAST: `McuControlMode` is `#[repr(u8)]`, so the cast is lossless.
             Bounded::try_new(mode as u32).unwrap()
         }
     }
@@ -857,7 +871,9 @@ fn from(val: Bounded<u32, 2>) -> Self {
     }
 
     impl From<McuStatus> for Bounded<u32, 2> {
+        #[expect(clippy::as_conversions)]
         fn from(status: McuStatus) -> Self {
+            // CAST: `McuStatus` is `#[repr(u8)]`, so the cast is lossless.
             Bounded::try_new(status as u32).unwrap()
         }
     }
@@ -1050,7 +1066,9 @@ fn try_from(val: Bounded<u8, 2>) -> Result<Self, Self::Error> {
         }
 
         impl From<AllocPolicySelect> for Bounded<u8, 2> {
+            #[expect(clippy::as_conversions)]
             fn from(val: AllocPolicySelect) -> Self {
+                // CAST: `AllocPolicySelect` is `#[repr(u8)]`, so the cast is lossless.
                 Bounded::try_new(val as u8).unwrap()
             }
         }
@@ -1095,7 +1113,9 @@ fn try_from(val: Bounded<u8, 2>) -> Result<Self, Self::Error> {
         }
 
         impl From<Coherency> for Bounded<u8, 2> {
+            #[expect(clippy::as_conversions)]
             fn from(val: Coherency) -> Self {
+                // CAST: `Coherency` is `#[repr(u8)]`, so the cast is lossless.
                 Bounded::try_new(val as u8).unwrap()
             }
         }
@@ -1126,7 +1146,9 @@ fn from(val: Bounded<u8, 2>) -> Self {
         }
 
         impl From<MemoryType> for Bounded<u8, 2> {
+            #[expect(clippy::as_conversions)]
             fn from(val: MemoryType) -> Self {
+                // CAST: `MemoryType` is `#[repr(u8)]`, so the cast is lossless.
                 Bounded::try_new(val as u8).unwrap()
             }
         }
@@ -1379,7 +1401,9 @@ fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
         }
 
         impl From<MmuCommand> for Bounded<u32, 8> {
+            #[expect(clippy::as_conversions)]
             fn from(cmd: MmuCommand) -> Self {
+                // CAST: `MmuCommand` is `#[repr(u8)]`, so the cast is lossless.
                 (cmd as u8).into()
             }
         }
@@ -1476,7 +1500,9 @@ fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
         }
 
         impl From<MmuExceptionType> for Bounded<u32, 8> {
+            #[expect(clippy::as_conversions)]
             fn from(exc: MmuExceptionType) -> Self {
+                // CAST: `MmuExceptionType` is `#[repr(u8)]`, so the cast is lossless.
                 (exc as u8).into()
             }
         }
@@ -1508,7 +1534,9 @@ fn from(val: Bounded<u32, 2>) -> Self {
         }
 
         impl From<MmuAccessType> for Bounded<u32, 2> {
+            #[expect(clippy::as_conversions)]
             fn from(access: MmuAccessType) -> Self {
+                // CAST: `MmuAccessType` is `#[repr(u8)]`, so the cast is lossless.
                 Bounded::try_new(access as u32).unwrap()
             }
         }
@@ -1575,7 +1603,9 @@ fn try_from(val: Bounded<u64, 4>) -> Result<Self, Self::Error> {
         }
 
         impl From<AddressSpaceMode> for Bounded<u64, 4> {
+            #[expect(clippy::as_conversions)]
             fn from(mode: AddressSpaceMode) -> Self {
+                // CAST: `AddressSpaceMode` is `#[repr(u8)]`, so the cast is lossless.
                 Bounded::try_new(mode as u64).unwrap()
             }
         }
@@ -1672,7 +1702,9 @@ fn try_from(val: Bounded<u64, 5>) -> Result<Self, Self::Error> {
         }
 
         impl From<InaBits> for Bounded<u64, 5> {
+            #[expect(clippy::as_conversions)]
             fn from(bits: InaBits) -> Self {
+                // CAST: `InaBits` is `#[repr(u8)]`, so the cast is lossless.
                 Bounded::try_new(bits as u64).unwrap()
             }
         }
@@ -1703,7 +1735,9 @@ fn try_from(val: Bounded<u64, 2>) -> Result<Self, Self::Error> {
         }
 
         impl From<PtwMemattr> for Bounded<u64, 2> {
+            #[expect(clippy::as_conversions)]
             fn from(attr: PtwMemattr) -> Self {
+                // CAST: `PtwMemattr` is `#[repr(u8)]`, so the cast is lossless.
                 Bounded::try_new(attr as u64).unwrap()
             }
         }
@@ -1735,7 +1769,9 @@ fn try_from(val: Bounded<u64, 2>) -> Result<Self, Self::Error> {
         }
 
         impl From<PtwShareability> for Bounded<u64, 2> {
+            #[expect(clippy::as_conversions)]
             fn from(sh: PtwShareability) -> Self {
+                // CAST: `PtwShareability` is `#[repr(u8)]`, so the cast is lossless.
                 Bounded::try_new(sh as u64).unwrap()
             }
         }

-- 
2.56.0


  parent reply	other threads:[~2026-11-25 22:48 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
2026-11-25 22:46 ` [PATCH 01/13] drm/tyr: convert SeatInfo::slot from u8 to usize Daniel Almeida
2026-11-25 22:46 ` [PATCH 02/13] drm/tyr: convert VmAsData::va_bits from u8 to u32 Daniel Almeida
2026-11-25 22:46 ` [PATCH 03/13] drm/tyr: convert the lock region exponents " Daniel Almeida
2026-11-25 22:46 ` [PATCH 04/13] drm/tyr: convert EntryHeader to `bitfield!` Daniel Almeida
2026-11-25 22:46 ` [PATCH 05/13] drm/tyr: use SizeConstants for the u32 and u64 size constants Daniel Almeida
2026-11-25 22:46 ` [PATCH 06/13] drm/tyr: replace lossless `as` casts with From and FromSafeCast Daniel Almeida
2026-11-25 22:46 ` [PATCH 07/13] drm/tyr: use `cv!` for constant conversions Daniel Almeida
2026-11-25 22:46 ` [PATCH 08/13] drm/tyr: use VmMapFlags::all_bits() to validate map flags Daniel Almeida
2026-11-25 22:47 ` Daniel Almeida [this message]
2026-11-25 22:47 ` [PATCH 10/13] drm/tyr: add split_u64() for the 64-bit register values Daniel Almeida
2026-11-25 22:47 ` [PATCH 11/13] drm/tyr: return usize from get_pgsize() Daniel Almeida
2026-11-25 22:47 ` [PATCH 12/13] drm/tyr: justify the physical address cast in pt_map() Daniel Almeida
2026-11-25 22:47 ` [PATCH 13/13] drm/tyr: deny `clippy::as_conversions` Daniel Almeida

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=20261125-b4-tyr-as-casts-v1-9-2b2f53164d86@collabora.com \
    --to=daniel.almeida@collabora.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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

all inboxes | Powered by JetHome®