Skip to content

Uninformative error message when casting usize to pointer to generic ?Sized type #74756

@rodrimati1992

Description

@rodrimati1992

Trying to compile this code

pub fn bogus_ptr<U: ?Sized>() -> *const U {
    0usize as *const U
}

produces this error message

error[E0606]: casting `usize` as `*const U` is invalid
  --> <source>:2:5
  |
2 |     0usize as *const U
  |     ^^^^^^^^^^^^^^^^^^

It would be better if the error message stated that usize cannot be cast to a pointer to a ?Sized type because it's potentially unsized.

Meta

Tried the code in these Rust versions and they all produced the same error message:

  • 1.37.0 stable

  • 1.45.0 stable

  • 1.46.0-beta.1 (2020-07-15 e51b714)

  • 1.47.0-nightly (2020-07-22 bbebe73)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.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