Skip to content

Insufficient diagnostic info when casting usize to fat pointer #92125

Closed
@Noratrieb

Description

@Noratrieb

Given the following code: playground

fn main() {
    maybe_sized_t::<()>();
}

fn maybe_sized_t<T: ?Sized>() {
    let _ptr = 0 as *mut T; 
}

The current output is:

error[E0606]: casting `usize` as `*mut T` is invalid
 --> src/main.rs:6:16
  |
6 |     let _ptr = 0 as *mut T; 
  |                ^^^^^^^^^^^

For more information about this error, try `rustc --explain E0606`.
error: could not compile `playground` due to previous error
error[E0606]: casting `usize` as `*mut T` is invalid
 --> src/main.rs:6:16
  |
6 |     let _ptr = 0 as *mut T; 
  |                ^^^^^^^^^^^

For more information about this error, try `rustc --explain E0606`.
error: could not compile `playground` due to previous error

note: `*mut T` is a fat pointer, and can therefore not be built from a `usize`

Casting a usize to a regular pointer is possible, so it's a bit confusing at first that this doesn't work for fat pointers, especially if it's not obvious that *mut T is a fat pointer here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions