Rust-GPU / Rust-GPU/rust-gpu

Can't use `usize::unchecked_mul()` because "checked mul is not supported yet"

Open
#537 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Rust
Stars
3.4k
Forks
125
PR merge metrics
No merged PRs in 30d

Description

I've stumbled upon this while trying to use core::alloc::Layout type in my shader code.
You see, Layout::repeat() function uses checked math to return an error, but in my case this should not be possible, so I've reimplemented it using usize::unchecked_mul().
I expected my code to compile, but got this compiler error instead:

error: checked mul is not supported yet
      --> ***\.rustup\toolchains\nightly-2025-06-30-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\num\mod.rs:1224:5
       |
  1224 | /     uint_impl! {
  1225 | |         Self = usize,
  1226 | |         ActualT = u32,
  1227 | |         SignedT = isize,
  ...    |
  1241 | |         bound_condition = " on 32-bit targets",
  1242 | |     }
       | |_____^
       |
  note: used from within `<usize>::overflowing_mul`
      --> ***\.rustup\toolchains\nightly-2025-06-30-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\num\mod.rs:1224:5
       |
  1224 | /     uint_impl! {
  1225 | |         Self = usize,
  1226 | |         ActualT = u32,
  1227 | |         SignedT = isize,
  ...    |
  1241 | |         bound_condition = " on 32-bit targets",
  1242 | |     }
       | |_____^
  note: called by `<usize>::unchecked_mul::precondition_check`
      --> ***\.rustup\toolchains\nightly-2025-06-30-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\num\mod.rs:1224:5
       |
  1224 | /     uint_impl! {
  1225 | |         Self = usize,
  1226 | |         ActualT = u32,
  1227 | |         SignedT = isize,
  ...    |
  1241 | |         bound_condition = " on 32-bit targets",
  1242 | |     }
       | |_____^
  note: called by `<usize>::unchecked_mul`
      --> ***\.rustup\toolchains\nightly-2025-06-30-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\num\mod.rs:1224:5
       |
  1224 | /     uint_impl! {
  1225 | |         Self = usize,
  1226 | |         ActualT = u32,
  1227 | |         SignedT = isize,
  ...    |
  1241 | |         bound_condition = " on 32-bit targets",
  1242 | |     }
       | |_____^
  note: 

I guess it tries to compile assert_unsafe_precondition! macro call which contains a call to usize::overflowing_mul() (which is used to do checked multiplication), and fails to do so.
I thought it should be compiled out because I'm compiling my shader in release mode (as by default) which suggests that debug_assertions are set to false (which should disable unsafe preconditions), but it seems this is not the case here.

I think it will be best to resolve such issue by implementing checked mul (as it is already implemented in Zig SPIR-V compiler, which I guess was the inspiration for rust-gpu's checked math functions)...
But if it is possible to disable unsafe preconditions completely, I can do just that.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the shader compilation failure, then start in core/src/num/mod.rs and trace usize::unchecked_mul through assert_unsafe_precondition! and overflowing_mul. Compare that path with Layout::repeat and determine whether checked multiplication support or disabling unsafe preconditions resolves the reported error; done means the reproducer compiles successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.