Skip to content

Packed unsized struct with drop: Confusing error message #47068

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

Closed
RalfJung opened this issue Dec 29, 2017 · 2 comments
Closed

Packed unsized struct with drop: Confusing error message #47068

RalfJung opened this issue Dec 29, 2017 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

RalfJung commented Dec 29, 2017

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.

@RalfJung
Copy link
Member Author

RalfJung commented Dec 29, 2017

Cc @arielb1 who wrote the code emitting the above error in #44884.

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 30, 2018
@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Jun 12, 2020
@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

Current output:

error[[E0277]](https://doc.rust-lang.org/stable/error_codes/E0277.html): the size for values of type `str` cannot be known at compilation time
  --> src/lib.rs:11:8
   |
11 |     y: UnsizedDrop,
   |        ^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: within `UnsizedDrop`, the trait `Sized` is not implemented for `str`
note: required because it appears within the type `UnsizedDrop`
  --> src/lib.rs:1:8
   |
1  | struct UnsizedDrop(str);
   |        ^^^^^^^^^^^
   = note: the last field of a packed struct may only have a dynamically sized type if it does not need drop to be run
   = help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
   |
11 |     y: &UnsizedDrop,
   |        +
help: the `Box` type always has a statically known size and allocates its contents in the heap
   |
11 |     y: Box<UnsizedDrop>,
   |        ++++           +

@estebank estebank closed this as completed Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants