Rust-GPU / Rust-GPU/rust-gpu

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

オープン
#537 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Rust
スター
3.4k
フォーク
126
PR マージ指標
30日以内にマージされた PR はありません

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

シェーダーのコンパイル失敗を再現し、その後 core/src/num/mod.rs から始めて、usize::unchecked_mul を assert_unsafe_precondition! と overflowing_mul を通じて追跡します。その経路を Layout::repeat と比較し、checked multiplication のサポートまたは unsafe preconditions の無効化によって報告されたエラーが解消されるかを判断します。reproducer が正常にコンパイルできれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
rust
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。