Skip to content

Clarify cast rules (RFC 401) wrt. fat pointers #1046

@arielb1

Description

@arielb1

The RFC 401 cast rules are (my comments in italic):

A cast e as U is valid if one of the following holds:

  • e has type T and T coerces to U; coerce-cast
  • e has type *T and U is *U_0 (i.e., between any raw pointers); ptr-ptr-cast
  • e has type *T and U is uint , or vice versa; ptr-addr-cast
  • e has type T and T and U are any numeric types; numeric-cast
  • e is a C-like enum and U is any integer type, bool; enum-cast
  • e has type T and T == u8 and U == char; char-cast
  • e has type T and T == &[V, ..n] or T == &V and U == *const V, and
    similarly for the mutable variants to either *const V or *mut V. ptr-ref-cast

The raw pointer casts (ptr-ptr-cast, ptr-addr-cast) don't make sense when only one of the pointers involved is fat (i.e. 0xb8000 as *mut [u8], let v=5; let u=&⁠v; u as *const [Vec<u32>]) – where does the vtable come from?

The casts also don't make much sense if the vtables have distinct types (say, casting *const [u8] → *const fmt::Write – does the length turn into a vtable pointer? while this does make some sense, it is still somewhat weird).

Currently, rustc ICEs in all these casts. Some (but not all) of these ICEs are fixed in rust-lang/rust#24158.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions