Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One can put repr(align) into repr(packed) with generics #83672

Closed
RalfJung opened this issue Mar 30, 2021 · 3 comments
Closed

One can put repr(align) into repr(packed) with generics #83672

RalfJung opened this issue Mar 30, 2021 · 3 comments
Labels
A-layout Area: Memory layout of types C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented Mar 30, 2021

Usually, one cannot put repr(align) fields into repr(packed) structs:

#[repr(align(8))]
struct Align8(u32);

#[repr(packed)]
struct Packed(Align8); // error

However, this check can be circumvented using generics:

#[repr(align(8))]
pub struct Align8(u32);

#[repr(packed)]
pub struct Packed<T>(T);

pub type PackedAlign8 = Packed<Align8>;

pub fn foo(_x: PackedAlign8) {}

I don't know why this restriction exists in the first place, but I think we have one of two situations here:

  • The restriction is truly needed, as parts of the compiler rely on it, and we have a problem here.
  • The restriction is not really needed, and we could relax this check.

Cc @eddyb @nagisa

@RalfJung
Copy link
Member Author

This is the exact same problem we had with repr(transparent) where we also had a check that could be circumvented with generics... we should probably do a proper audit of all these checks as that seems to be a weak spot.

Cc @nikomatsakis

@jonas-schievink jonas-schievink added A-layout Area: Memory layout of types C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Mar 30, 2021
@ghost
Copy link

ghost commented Mar 31, 2021

Is this a duplicate of #80926?

@RalfJung
Copy link
Member Author

It is, good point, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-layout Area: Memory layout of types C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants