You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/1440-drop-types-in-const.md
+11-7
Original file line number
Diff line number
Diff line change
@@ -6,27 +6,27 @@
6
6
# Summary
7
7
[summary]: #summary
8
8
9
-
Allow types with destructors to be used in `static` items and in `const`functions, as long as the destructor never needs to run in const context.
9
+
Allow types with destructors to be used in `static` items, `const`items, and `const` functions.
10
10
11
11
# Motivation
12
12
[motivation]: #motivation
13
13
14
-
Some of the collection types do not allocate any memory when constructed empty (most notably `Vec`). With the change to make leaking safe, the restriction on `static` items with destructors
14
+
Some of the collection types do not allocate any memory when constructed empty (most notably `Vec`). With the change to make leaking safe, the restriction on `static`or `const`items with destructors
15
15
is no longer required to be a hard error (as it is safe and accepted that these destructors may never run).
16
16
17
-
Allowing types with destructors to be directly used in `const` functions and stored in `static`s will remove the need to have
17
+
Allowing types with destructors to be directly used in `const` functions and stored in `static`s or `const`s will remove the need to have
18
18
runtime-initialisation for global variables.
19
19
20
20
# Detailed design
21
21
[design]: #detailed-design
22
22
23
-
- Lift the restriction on types with destructors being used in statics.
23
+
- Lift the restriction on types with destructors being used in `static` or `const` items.
24
24
-`static`s containing Drop-types will not run the destructor upon program/thread exit.
25
+
-`const`s containing Drop-types _will_ run the destructor at the appropriate point in the program.
25
26
- (Optionally adding a lint that warn about the possibility of resource leak)
26
27
- Alloc instantiating structures with destructors in constant expressions,
27
-
- Continue to prevent `const` items from holding types with destructors.
28
28
- Allow `const fn` to return types with destructors.
29
-
- Disallow constant expressions which would result in the destructor being called (if the code were run at runtime).
29
+
- Disallow constant expressions that require destructors to run during compile-time constant evaluation (i.e: a `drop(foo)` in a `const fn`).
30
30
31
31
## Examples
32
32
Assuming that `RwLock` and `Vec` have `const fn new` methods, the following example is possible and avoids runtime validity checks.
Destructors do not run on `static` items (by design), so this can lead to unexpected behavior when a type's destructor has effects outside the program (e.g. a RAII temporary folder handle, which deletes the folder on drop). However, this can already happen using the `lazy_static` crate.
57
59
60
+
A `const` item's destructor _will_ run at each point where the `const` item is used. If a `const` item is never used, its destructor will never run. These behaviors may be unexpected.
0 commit comments