Rust-GPU / Rust-GPU/rust-gpu

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

Abierto
#537 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug
Lenguaje dominante
Rust
Estrellas
3.4k
Forks
126
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce el fallo de compilación del shader y, después, empieza en core/src/num/mod.rs y sigue usize::unchecked_mul a través de assert_unsafe_precondition! y overflowing_mul. Compara ese recorrido con Layout::repeat y determina si la compatibilidad con la multiplicación comprobada o la desactivación de las precondiciones unsafe resuelve el error notificado; se considera terminado cuando el reproductor compila correctamente.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
rust
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.