Apparently this has been used by lazy-static for a while now as a "nightly feature":
use std::cell::UnsafeCell;
struct SyncCell(UnsafeCell<Option<$T>>);
unsafe impl Sync for SyncCell {}
static DATA: SyncCell = SyncCell(UnsafeCell::new(None));
This happens to work even when $T is Vec<X> or String, which should not end up in a static, by the current rules.
A potential fix would involve adding another constant qualification flag for "contains UnsafeCell<D> where D has a destructor" and deny that in a static.
I would like to avoid reusing a combination of the existing flags, as that can result in false positives.
Another possible plan (requiring a new RFC) would be to:
- allow destructors in statics
- optionally warn about the "potential leak"
- allow instantiating structures that impl
Drop in constant expressions
- prevent
const items from holding values with destructors, but allow const fn to return them
- disallow constant expressions which would result in the
Drop impl getting called, where they not in a constant context
cc @nikomatsakis @thepowersgang
Apparently this has been used by
lazy-staticfor a while now as a "nightly feature":This happens to work even when
$TisVec<X>orString, which should not end up in astatic, by the current rules.A potential fix would involve adding another constant qualification flag for "contains
UnsafeCell<D>whereDhas a destructor" and deny that in astatic.I would like to avoid reusing a combination of the existing flags, as that can result in false positives.
Another possible plan (requiring a new RFC) would be to:
Dropin constant expressionsconstitems from holding values with destructors, but allowconst fnto return themDropimpl getting called, where they not in a constant contextcc @nikomatsakis @thepowersgang