rust-gpu icon indicating copy to clipboard operation
rust-gpu copied to clipboard

Can not call function with sub-slice as argument

Open SiebenCorgie opened this issue 4 years ago • 3 comments

When calling a function with a sub-slice of some other slice, the compiler throws a strange error:

Small example:

pub fn t1(main: &[f32]){
    let a = t2(&main[10..13]);
}

fn t2(sub: &[f32]) -> u32{
    1
}

throws

  error: Cannot cast between pointer types
    --> /.../rustup/toolchains/nightly-2021-08-27-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/const_ptr.rs:49:6
     |
  49 |     }
     |      ^
     |
     = note: from: *f32
     = note: to: *struct () {  }

  error: Cannot cast between pointer types
     --> /.../.rustup/toolchains/nightly-2021-08-27-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/metadata.rs:117:14
      |
  117 |     unsafe { PtrRepr { components: PtrComponents { data_address, metadata } }.const_ptr }
      |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |
      = note: from: *[u8; 8]
      = note: to: *struct core::ptr::metadata::PtrComponents<[f32]> { *struct () {  }, u32 }

  error: Cannot cast between pointer types
     --> /.../.rustup/toolchains/nightly-2021-08-27-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/metadata.rs:117:14
      |
  117 |     unsafe { PtrRepr { components: PtrComponents { data_address, metadata } }.const_ptr }
      |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |
      = note: from: *[u8; 8]
      = note: to: *struct *const [f32] { *[f32], u32 }

  error: Cannot offset a pointer to an arbitrary element
     --> /.../.rustup/toolchains/nightly-2021-08-27-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/const_ptr.rs:235:18
      |
  235 |         unsafe { intrinsics::offset(self, count) }
      |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is probably a similar problem like this, however, under different circumstances. If this is viewed as a duplicate, just close the Issue please.

SiebenCorgie avatar Sep 13 '21 18:09 SiebenCorgie

Unfortunately yeah, SPIR-V does not have an easy representation of subslices - we would need to tack on a (offset, length) pair to all regular arrays to support slicing, which then would change the ABI of the generated SPIR-V to be significantly different than what rustc tells us to do, which we've managed to avoid doing so far (it gets really, really complicated).

Hopefully at some point we can support slicing, though!

khyperia avatar Sep 14 '21 07:09 khyperia

I see, I'll track the start and end of my sub-slice via additional arguments, works in glsl too! Good luck with finding a way to do that! I'd help, but I do not understand compilers enough to be a real help to you :(.

SiebenCorgie avatar Sep 14 '21 08:09 SiebenCorgie

We probably won't be supporting slices any time soon

oisyn avatar Nov 17 '22 22:11 oisyn