Skip to content

mem::transmute is too restrictive #27570

@Diggsey

Description

@Diggsey

Because mem::transmute checks its constraints before monomorphization, it fails for types containing fields whose type is determined by a generic parameter:

cannot transmute to or from a type that contains unsubstituted type parameters

It's great that it attempts to catch errors as early as possible, but if it's unable to prove that the 'to' and 'from' types have the same or different representations before monomorphization, it should fall back to checking them after, when it can get a definitive answer.

There's a slightly convoluted work-around which involves using ptr::read followed by mem::forget on the original to prevent it being dropped twice, but there's no guarantee that will compile into the same zero-cost operation as transmute, and it won't perform any checks that the two types have the same representation.

An alternative would be to leave the restrictions on mem::transmute unchanged and add a new intrinsic instead.

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