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

Clarify that #[repr(transparent)] is valid without a non-ZST field #1283

Closed
wants to merge 1 commit into from

Conversation

kupiakos
Copy link

@kupiakos kupiakos commented Oct 13, 2022

The following is valid, but the prior language suggests it is not:

#[repr(transparent)]
enum Banana {
    Phone,
}

#[repr(transparent)]
struct Blooey(());

#[repr(transparent)]
struct Orange;

Of note: it's still invalid to have a zero-sized field in a transparent
struct that has an alignment requirement higher than 1:

#[repr(align(2))]
struct Inner;

// error[E0691]: zero-sized field in transparent struct has alignment
//               larger than 1
#[repr(transparent)]
struct Fizz(Inner);

The following is valid, but the prior language suggests it is not:

```rust
#[repr(transparent)]
enum Banana {
    Phone,
}

#[repr(transparent)]
struct Blooey(());

#[repr(transparent)]
struct TheVicePresidentDoesNotHaveUnilateralPowerOverFederalElections;
```

Of note: it's still invalid to have a zero-sized field in a transparent
struct that has an alignment requirement higher than 1:
```rust
#[repr(align(2))]
struct Inner;

// error[E0691]: zero-sized field in transparent struct has alignment
//               larger than 1
#[repr(transparent)]
struct Fizz(Inner);
```
@kupiakos
Copy link
Author

An extension on that align-2 weirdness: it's valid to express in generic code, but not concretely. Rustc bug?

// This is just fine, because it's generic?
#[repr(transparent)]
struct Fizz<T = Inner>(T, [(); 10]);

@kupiakos
Copy link
Author

Looks like rust-lang/rust#100954 is already tracking the inconsistencies with #[repr(transparent)]

@traviscross
Copy link
Contributor

Thanks @kupiakos for this. We're going to close as we merged #1568 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants