Skip to content

Commit 135c83c

Browse files
authored
Merge pull request #1817 from SergioBenitez/master
Amend #1440: allow `const` items to contain drop types.
2 parents 4a27a85 + 5a9a2b2 commit 135c83c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

text/1440-drop-types-in-const.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
# Summary
77
[summary]: #summary
88

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.
1010

1111
# Motivation
1212
[motivation]: #motivation
1313

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
1515
is no longer required to be a hard error (as it is safe and accepted that these destructors may never run).
1616

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
1818
runtime-initialisation for global variables.
1919

2020
# Detailed design
2121
[design]: #detailed-design
2222

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.
2424
- `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.
2526
- (Optionally adding a lint that warn about the possibility of resource leak)
2627
- Alloc instantiating structures with destructors in constant expressions,
27-
- Continue to prevent `const` items from holding types with destructors.
2828
- 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`).
3030

3131
## Examples
3232
Assuming that `RwLock` and `Vec` have `const fn new` methods, the following example is possible and avoids runtime validity checks.
@@ -38,12 +38,14 @@ trait LogHandler: Send + Sync {
3838
}
3939
/// List of registered logging handlers
4040
static S_LOGGERS: RwLock<Vec< Box<LogHandler> >> = RwLock::new( Vec::new() );
41+
42+
/// Just an empty byte vector.
43+
const EMPTY_BYTE_VEC: Vec<u8> = Vec::new();
4144
```
4245

4346
Disallowed code
4447
```rust
4548
static VAL: usize = (Vec::<u8>::new(), 0).1; // The `Vec` would be dropped
46-
const EMPTY_BYTE_VEC: Vec<u8> = Vec::new(); // `const` items can't have destructors
4749

4850
const fn sample(_v: Vec<u8>) -> usize {
4951
0 // Discards the input vector, dropping it
@@ -55,6 +57,8 @@ const fn sample(_v: Vec<u8>) -> usize {
5557

5658
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.
5759

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.
61+
5862
# Alternatives
5963
[alternatives]: #alternatives
6064

0 commit comments

Comments
 (0)