Closed
Description
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.