]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0493.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0493.md
1 A value with a custom `Drop` implementation may be dropped during const-eval.
2
3 Erroneous code example:
4
5 ```compile_fail,E0493
6 enum DropType {
7     A,
8 }
9
10 impl Drop for DropType {
11     fn drop(&mut self) {}
12 }
13
14 struct Foo {
15     field1: DropType,
16 }
17
18 static FOO: Foo = Foo { field1: (DropType::A, DropType::A).1 }; // error!
19 ```
20
21 The problem here is that if the given type or one of its fields implements the
22 `Drop` trait, this `Drop` implementation cannot be called within a const
23 context since it may run arbitrary, non-const-checked code. To prevent this
24 issue, ensure all values with a custom `Drop` implementation escape the
25 initializer.
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 ```