Skip to content

transmute size check is wrong for overaligned newtype #155412

@theemathas

Description

@theemathas

I tried this code:

use std::mem::transmute;

#[repr(align(16))]
struct Wrap<T>(T);

fn foo<T: ?Sized>(x: Wrap<*const T>) -> *const T {
    unsafe { transmute(x) }
}

fn main() {
    foo(Wrap(std::ptr::null::<i32>()));
    println!("done");
}

The above code shouldn't compile, since Wrap<*const i32> and *const i32 don't have the same size, and transmute is supposed to check the sizes of the two types to be equal at compile time.

Instead, the code compiles and causes a segmentation fault at run time.

The generated assembly is particularly nonsensical:

example::foo::<i32>:
        mov     byte ptr [rax], 1
        ret

Possibly related to #101084. cc @RalfJung @oli-obk

Meta

Reproducible on the playground with version 1.97.0-nightly (2026-04-15 e8e4541ff19649d95afa)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-alignArea: alignment control (`repr(align(N))` and so on)A-layoutArea: Memory layout of typesA-reprArea: the `#[repr(stuff)]` attributeC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions