Skip to content

Capability is required unexpectedly for constant cast #300

Open
@nazar-pc

Description

@nazar-pc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions