Capability is required unexpectedly for constant cast
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 3.4k
- Forks
- 125
- PR merge metrics
- No merged PRs in 30d
Description
I have a following trivial situation:
const K: u8 = 20;
...
let position = 2u32;
let global_y_offset_bits = position * K as u32;
...
Here it should be trivial for compiler to cast u8 to u32 at compile time and make last line effectively this:
let global_y_offset_bits = position * 20u32;
Yet for some reason it doesn't happen and I'm getting this instead:
error: `u8` without `OpCapability Int8`
--> /home/nazar-pc/.rustup/toolchains/nightly-2024-11-22-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/num/mod.rs:1298:5
|
1298 | / uint_impl! {
1299 | | Self = usize,
1300 | | ActualT = u32,
1301 | | SignedT = isize,
... |
1315 | | bound_condition = " on 32-bit targets",
1316 | | }
| |_____^
|
= note: used by unnamed global (%1504)
note: used from within `ab_proof_of_space_gpu::shader::compute_f1::compute_f1`
--> ../src/shader/compute_f1.rs:85:47
|
85 | let global_y_offset_bits = position * K as u32;
| ^^^^^^^^
I think this is a bug. Even if it was a variable that is assigned a literal I'd expect the compiler to inline it with a correct type. And I believe rustc actually does this for CPU architectures like x86-64.
I imagine inability to handle cases like this will result in suboptimal output in many cases, so it'd be nice to address the root cause of it.
UPD: const K_U32: u32 = K as u32; as a workaround helps, but ideally shouldn't be necessary.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the shader compilation failure from src/shader/compute_f1.rs:85 and compare it with the K_U32 workaround. Inspect the generated capability requirements around the u8-to-u32 cast and the related integer implementation in core/src/num/mod.rs:1298; done means the direct constant cast compiles without requiring OpCapability Int8.
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
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100