Rust-GPU / Rust-GPU/rust-gpu

[Migrated] Can not call function with sub-slice as argument

Open
#121 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3.4k
Forks
125
PR merge metrics
No merged PRs in 30d

Description

Issue automatically imported from old repo: https://github.com/EmbarkStudios/rust-gpu/issues/752
Old labels: t: enhancement,s: qptr may fix
Originally creatd by SiebenCorgie on 2021-09-13T18:50:11Z


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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the small Rust example in the issue and compare the behavior with issue 329. Done means calling t2 with &main[10..13] compiles without the reported pointer-cast and pointer-offset errors.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.