]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0493.md
Auto merge of #66396 - smmalis37:pythontest, r=alexcrichton
[rust.git] / src / librustc_error_codes / error_codes / E0493.md
1 A type with a `Drop` implementation was destructured when trying to initialize
2 a static item.
3
4 Erroneous code example:
5
6 ```compile_fail,E0493
7 enum DropType {
8     A,
9 }
10
11 impl Drop for DropType {
12     fn drop(&mut self) {}
13 }
14
15 struct Foo {
16     field1: DropType,
17 }
18
19 static FOO: Foo = Foo { ..Foo { field1: DropType::A } }; // error!
20 ```
21
22 The problem here is that if the given type or one of its fields implements the
23 `Drop` trait, this `Drop` implementation cannot be called during the static
24 type initialization which might cause a memory leak. To prevent this issue,
25 you need to instantiate all the static type's fields by hand.
26
27 ```
28 enum DropType {
29     A,
30 }
31
32 impl Drop for DropType {
33     fn drop(&mut self) {}
34 }
35
36 struct Foo {
37     field1: DropType,
38 }
39
40 static FOO: Foo = Foo { field1: DropType::A }; // We initialize all fields
41                                                // by hand.
42 ```