Skip to content

Require opting in to repr-less types? #358

Open
@joshlf

Description

@joshlf

Some of our derives support types without reprs because they're not required for soundness. This can be useful if you don't care about layout stability (and perhaps you care about allowing the compiler to make layout optimizations), but it's a big footgun: it makes it easy to accidentally write code which depends upon type layout without realizing it.

Perhaps we should require users who wish to not use reprs to opt-in to this behavior; something like:

#[derive(FromZeroes, FromBytes)]
#[allow(zerocopy::missing_repr)] // Is this legal? Maybe we need the syntax below instead.
struct Foo;

#[derive(FromZeroes, FromBytes)]
#[zerocopy(allow(missing_repr))]
struct Bar;

This would be a semver-breaking change, but a minor one, and our error message could suggest the allow to steer users in the right direction.

Note one important subtlety: On its own, this isn't sufficient: if a struct contains another type with a non-stable representation, then even if the outer struct is, e.g., repr(C), it isn't sufficient to guarantee the stability of the outer type's layout.

It seems unlikely, but perhaps we could also have zerocopy-derive emit a warning that users can allow using rustc's built-in machinery. According to this documentation, warnings are not currently supported except on nightly, so this may be a non-starter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions