Skip to content

unsized generic field not allowed in repr(packed) struct #49903

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

Open
z33ky opened this issue Apr 12, 2018 · 1 comment
Open

unsized generic field not allowed in repr(packed) struct #49903

z33ky opened this issue Apr 12, 2018 · 1 comment
Labels
A-repr-packed Area: the naughtiest repr A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@z33ky
Copy link

z33ky commented Apr 12, 2018

For some reason, using a generic T: ?Sized does not work in a repr(packed) struct, while a non-generic unsized type is OK.

#[repr(packed)]
struct Bar([u8]); // ok

#[repr(packed)]
struct Foo<T: ?Sized>(T): // err

An non-packed is also OK:
struct Foo<T: ?Sized>(T); // ok

The full error:

error[E0277]: the trait bound `T: std::marker::Sized` is not satisfied
  |
5 | struct Foo<T: ?Sized>(T); // err
  |                       ^^ `T` does not have a constant size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `T`
  = help: consider adding a `where T: std::marker::Sized` bound
  = note: only the last field of a struct may have a dynamically sized type

This may be related to #47068?

Meta

$ rust --version --verbose
rustc 1.25.0 (84203cac6 2018-03-25)
binary: rustc
commit-hash: 84203cac67e65ca8640b8392348411098c856985
commit-date: 2018-03-25
host: x86_64-unknown-linux-gnu
release: 1.25.0
LLVM version: 6.0
@XAMPPRocky XAMPPRocky added A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. A-attributes Area: Attributes (`#[…]`, `#![…]`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 7, 2018
@rectang
Copy link

rectang commented Aug 21, 2023

The full error has changed in recent years, and now contains a note which suggests a partial workaround:

  = note: the last field of a packed struct may only have a dynamically sized type if it does not need drop to be run

Thus prompted, I discovered that wrapping the DST field in ManuallyDrop allows compilation to complete successfully:

  #[repr(packed)]
- struct Foo<T: ?Sized>(T): // err
+ struct Foo<T: ?Sized>(std::mem::ManuallyDrop<T>); // no err

@workingjubilee workingjubilee added A-repr-packed Area: the naughtiest repr and removed A-attributes Area: Attributes (`#[…]`, `#![…]`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 1, 2024
@fmease fmease added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-repr-packed Area: the naughtiest repr A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants