E0462: Destructor struct linear field
| Code | Category | Stability |
|---|---|---|
E0462 | Struct and enum | Permanent |
Explanation
A struct with a user-defined destructor declared a field that carries a linear value. Such a field could never be consumed: the destructor may not move a field out of self, no other code may move a field out of a value whose type has a destructor, and the drop glue that runs after the destructor would silently discard it. The only way to dispose of such a value would be to drop it whole, which defeats the obligation the linear field exists to enforce, so the shape is rejected where it is declared.
Likely cause
A drop fn was added to a struct that owns a linear value, or a linear (or linear-carrying) field was added to a struct with a destructor. Remove the destructor and let the code that consumes the struct consume the field explicitly, or give the field an affine type and run its cleanup in the destructor.
Examples
Give a destructor to a struct with a linear field
linear struct Token { value: i32 }
struct Holder { token: Token, n: i32 }
drop fn Holder(self) { @dbg(self.n); }
fn main() -> i32 { 0 }
Drop the destructor and consume the field explicitly
linear struct Token { value: i32 }
struct Holder { token: Token, n: i32 }
fn redeem(token: Token) -> i32 { token.value }
fn main() -> i32 {
let holder = Holder { token: Token { value: 42 }, n: 1 };
redeem(holder.token)
}