Skip to content

Uniformative error message when casting a thin raw pointer to a fat one #31511

Closed
@SimonSapin

Description

@SimonSapin

This is fine:

fn cast_thin_to_thin(x: *const ()) {
    x as *const u8;
}

This does not compile, but the error message is not particularly helpful:

fn cast_thin_to_fat(x: *const ()) {
    x as *const [u8];
}
a.rs:8:5: 8:21 error: casting `*const ()` as `*const [u8]` is invalid
a.rs:8     x as *const [u8];
           ^~~~~~~~~~~~~~~~
error: aborting due to previous error

Invalid how? The error message should mention thin vs fat pointers or statically-sized vs dynamically-sized types, and maybe link to https://doc.rust-lang.org/book/unsized-types.html

(To make this a bit less artificial, this came up in FFI code casting *mut c_void to a struct containing an unboxed FnMut closure. The fix was to box the closure to make that struct statically-sized.)

CC @julienw

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions