Skip to content

Packed unsized struct with drop: Confusing error message #47068

Closed
@RalfJung

Description

@RalfJung

The following type

struct UnsizedDrop(str);

impl Drop for UnsizedDrop {
    fn drop(&mut self) {
    }
}

#[repr(packed)]
struct Foo {
    x: u32,
    y: UnsizedDrop,
}

raises the following error

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied in `UnsizedDrop`
  --> src/main.rs:11:5
   |
11 |     y: UnsizedDrop,
   |     ^^^^^^^^^^^^^^ `str` does not have a constant size known at compile-time
   |
   = help: within `UnsizedDrop`, the trait `std::marker::Sized` is not implemented for `str`
   = note: required because it appears within the type `UnsizedDrop`
   = note: only the last field of a struct may have a dynamically sized type

The last line of the note here is pretty confusing, because this is the last field! The actual problem is that a packed struct implementing Drop copies things around for dropping to get the right alignment, and that requires the field to be sized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler 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