hax icon indicating copy to clipboard operation
hax copied to clipboard

Error when casting mut to const pointer

Open maximebuyse opened this issue 1 year ago • 4 comments

use std::ptr::NonNull;


pub fn g(x: * const i32) -> i32 {
    1
}


unsafe fn f(x: NonNull<i32>) -> i32 {
    g(x.as_ptr())
}

Open this code snippet in the playground

This gives:

error: [HAX0002] (AST import) Fatal error: something we considered as impossible occurred! Please report this by submitting an issue on GitHub!
Details: Pointer, with [cast] being Types.MutToConstPointer
  --> src/lib.rs:10:7
   |
10 |     g(x.as_ptr())

and

error: [HAX0002] (RefMut) Fatal error: something we considered as impossible occurred! Please report this by submitting an issue on GitHub!
Details: expected an arrow type here
  --> src/lib.rs:10:1
   |
10 |     g(x.as_ptr())

I also found the same kind of error with Details: Pointer, with [cast] being Types.UnsafeFnPointer

maximebuyse avatar Oct 28 '24 16:10 maximebuyse

Thanks, that's the engine side of #1057 I believe!

W95Psp avatar Oct 28 '24 16:10 W95Psp

There are no constants involved here though

Nadrieril avatar Oct 28 '24 19:10 Nadrieril

This issue has been marked as stale due to a lack of activity for 60 days. If you believe this issue is still relevant, please provide an update or comment to keep it open. Otherwise, it will be closed in 7 days.

github-actions[bot] avatar Feb 06 '25 01:02 github-actions[bot]

There is no unsafe needed here:

use std::ptr::NonNull;

fn g(x: * const i32) -> i32 {
    1
}

fn f(x: NonNull<i32>) -> i32 {
    g(x.as_ptr())
}

Open this code snippet in the playground

Also, here is a further minimization:

fn g(x: *const ()) { }
fn f(x: *mut ()) {
    g(x)
}

Open this code snippet in the playground

We should implement everything from https://doc.rust-lang.org/beta/nightly-rustc/rustc_middle/ty/adjustment/enum.PointerCoercion.html#variant.ClosureFnPointer, via calls to dedicated Rust_primitive cast functions.

W95Psp avatar Feb 10 '25 08:02 W95Psp