Skip to content

Union cast failed #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
simensgreen opened this issue Apr 23, 2025 · 5 comments
Open

Union cast failed #241

simensgreen opened this issue Apr 23, 2025 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@simensgreen
Copy link

Expected Behaviour

Can cast [f32; 5] as Data { a: f32, b: [f32; 3], c: f32 }

Example & Steps To Reproduce

#[repr(C)]
#[derive(Clone, Copy)]
struct Data {
    a: f32,
    b: [f32; 3],
    c: f32,
}

union DataOrArray {
    arr: [f32; 5],
    str: Data,
}

impl DataOrArray {
    fn arr(self) -> [f32; 5] { unsafe { self.arr } }
    fn new(arr: [f32; 5]) -> Self { Self { arr } }
}

in shader:

let dora = DataOrArray::new([0.0, 0.0, 0.0, 0.0, 0.0]);
let arr = dora.arr();

Produces:

error: cannot cast between pointer types
         from `*struct types::Data { a: f32, b: [f32; 3], c: f32 }`
           to `*[f32; 5]`

System Info

  • Rust: rustc 1.84.0-nightly (b19329a37 2024-11-21)
  • OS: MacOS 15.3.2
  • GPU: M2 Max
  • rust-gpu: 698f10a
@simensgreen simensgreen added the bug Something isn't working label Apr 23, 2025
@LegNeato
Copy link
Collaborator

LegNeato commented Apr 23, 2025

Can you try latest master? I landed 2f915a0 which fixed issues with a similar message (#46) and I think this is fixed by it too.

@simensgreen
Copy link
Author

Nope, even at commit 2f915a0 error still appears

@simensgreen
Copy link
Author

I tried trick it with core::mem::transmute:

    pub fn arr(self) -> [f32; 5] {
        unsafe { core::mem::transmute(self) }
    }

Error:

  error: cannot memcpy dynamically sized data
    --> solver/src/types/data_or_array.rs:16:18
     |
  16 |         unsafe { core::mem::transmute(self) }
     |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^

@simensgreen
Copy link
Author

I created repo to test this error, but it failed to compile #242

@LegNeato
Copy link
Collaborator

Ok great, I'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants