E0457: Copy struct with destructor
| Code | Category | Stability |
|---|---|---|
E0457 | Struct and enum | Permanent |
Explanation
A struct declared @copy also has a user-defined destructor. Copy values may be duplicated implicitly and those copies are not tracked as ownership transfers, so running the destructor for every copy could clean up the same logical resource more than once.
Likely cause
A drop fn was added for a struct already marked @copy, or @copy was added to a resource-owning type. Remove @copy so each value has one tracked owner, or remove the destructor when the type is genuinely safe to duplicate and requires no user-defined cleanup.
Examples
Give a Copy struct a destructor
@copy
struct Resource { value: i32 }
drop fn Resource(self) { @dbg(self.value); }
fn main() -> i32 { 0 }
Keep the destructor on a move-only struct
struct Resource { value: i32 }
drop fn Resource(self) { @dbg(self.value); }
fn main() -> i32 {
let resource = Resource { value: 42 };
resource.value
}