Skip to content

Missing unsizing coercions for raw slice pointers #74679

Open
@RalfJung

Description

@RalfJung

With #73986 having landed, one would expect something like this to work:

#![feature(raw_ref_op)]
#![feature(slice_ptr_get)]

struct S {
    keys: [i32; 2],
}

unsafe fn get_last(s: *const S) -> i32 {
    let keys = &raw const (*s).keys;
    *keys.get_unchecked(1)
}

fn main() {
    let s = S { keys: [24, 42] };
    let p = &raw const s;
    assert_eq!(unsafe { get_last(p) }, 42);
}

However, this fails saying

error[E0599]: no method named `get_unchecked` found for raw pointer `*const [i32; 2]` in the current scope
  --> src/main.rs:11:11
   |
11 |     *keys.get_unchecked(1)
   |           ^^^^^^^^^^^^^ method not found in `*const [i32; 2]`
   |
   = note: try using `<*const T>::as_ref()` to get a reference to the type behind the pointer: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref
   = note: using `<*const T>::as_ref()` on a pointer which is unaligned or points to invalid or uninitialized memory is undefined behavior

One has to add a *const [_] type annotation to force an unsizing coercion for this to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions