From 734ab29dd0000478e1835330a099df761558847b Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 13 Dec 2016 04:02:10 -0800 Subject: [PATCH 1/3] Amend #1440: allow `const` items to contain drop types. --- text/1440-drop-types-in-const.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/text/1440-drop-types-in-const.md b/text/1440-drop-types-in-const.md index 4455d580b38..ba0cd0b1b87 100644 --- a/text/1440-drop-types-in-const.md +++ b/text/1440-drop-types-in-const.md @@ -6,7 +6,7 @@ # Summary [summary]: #summary -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. +Allow types with destructors to be used in `static` items, `const` items, and `const` functions. # Motivation [motivation]: #motivation @@ -14,19 +14,19 @@ Allow types with destructors to be used in `static` items and in `const` functio 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 is no longer required to be a hard error (as it is safe and accepted that these destructors may never run). -Allowing types with destructors to be directly used in `const` functions and stored in `static`s will remove the need to have +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 runtime-initialisation for global variables. # Detailed design [design]: #detailed-design -- Lift the restriction on types with destructors being used in statics. +- Lift the restriction on types with destructors being used in `static` or `const` items. - `static`s containing Drop-types will not run the destructor upon program/thread exit. + - `const`s containing Drop-types _will_ run the destructor at the appropriate point in the program. - (Optionally adding a lint that warn about the possibility of resource leak) - Alloc instantiating structures with destructors in constant expressions, -- Continue to prevent `const` items from holding types with destructors. - Allow `const fn` to return types with destructors. -- Disallow constant expressions which would result in the destructor being called (if the code were run at runtime). +- Disallow constant expressions resulting in destructors being called at runtime (i.e: a `drop(foo)` in a `const fn`). ## Examples 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 { } /// List of registered logging handlers static S_LOGGERS: RwLock >> = RwLock::new( Vec::new() ); + +/// Just an empty byte vector. +const EMPTY_BYTE_VEC: Vec = Vec::new(); ``` Disallowed code ```rust static VAL: usize = (Vec::::new(), 0).1; // The `Vec` would be dropped -const EMPTY_BYTE_VEC: Vec = Vec::new(); // `const` items can't have destructors const fn sample(_v: Vec) -> usize { 0 // Discards the input vector, dropping it @@ -55,6 +57,8 @@ const fn sample(_v: Vec) -> usize { 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. +Destructors _will_ run on `const` items at runtime, which can lead to unexpected behavior when a type's destructor has effects outside the program. + # Alternatives [alternatives]: #alternatives From db54fa900b413086edb06ac796dd702fb137a6c2 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 19 Dec 2016 16:58:17 -0800 Subject: [PATCH 2/3] Clarify which constant expression are still disallowed. --- text/1440-drop-types-in-const.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/1440-drop-types-in-const.md b/text/1440-drop-types-in-const.md index ba0cd0b1b87..d11e2049bfe 100644 --- a/text/1440-drop-types-in-const.md +++ b/text/1440-drop-types-in-const.md @@ -26,7 +26,7 @@ runtime-initialisation for global variables. - (Optionally adding a lint that warn about the possibility of resource leak) - Alloc instantiating structures with destructors in constant expressions, - Allow `const fn` to return types with destructors. -- Disallow constant expressions resulting in destructors being called at runtime (i.e: a `drop(foo)` in a `const fn`). +- Disallow constant expressions that require destructors to run during compile-time constant evaluation (i.e: a `drop(foo)` in a `const fn`). ## Examples Assuming that `RwLock` and `Vec` have `const fn new` methods, the following example is possible and avoids runtime validity checks. From 5a9a2b24ea28e62b852342b8e94c91e01d0d9303 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 24 Jan 2017 12:53:21 -0800 Subject: [PATCH 3/3] Clarify drawback pertaining to dropping const items. --- text/1440-drop-types-in-const.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/text/1440-drop-types-in-const.md b/text/1440-drop-types-in-const.md index d11e2049bfe..03377c4bbef 100644 --- a/text/1440-drop-types-in-const.md +++ b/text/1440-drop-types-in-const.md @@ -11,7 +11,7 @@ Allow types with destructors to be used in `static` items, `const` items, and `c # Motivation [motivation]: #motivation -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 +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 is no longer required to be a hard error (as it is safe and accepted that these destructors may never run). 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 @@ -57,7 +57,7 @@ const fn sample(_v: Vec) -> usize { 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. -Destructors _will_ run on `const` items at runtime, which can lead to unexpected behavior when a type's destructor has effects outside the program. +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. # Alternatives [alternatives]: #alternatives