Skip to content

cannot use mem::offset_of! with unions due to ManuallyDrop interfering #121212

Open
@anderspapitto

Description

@anderspapitto

When a union contains a non-Copy variant, the compiler forces the user to wrap it in ManuallyDrop. ManuallyDrop is guaranteed to have the same layout as the underlying type, but it is opaque to offset_of! because the value field is private.

here is an example that fails to compile

#![feature(offset_of_nested)]
use core::mem;
struct X { foo: usize }
union N { X: mem::ManuallyDrop<X> }
const o: usize = mem::offset_of!(N, X.value.foo);

playground link: (https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=84fadd9f1808d5ff7b52de9e1f0ab6b4)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-offset_of_nested`#![feature(offset_of_nested)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library 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