How unsafe is my reference type?

I wonder if a new annotation could be added to the language that would mark a type as not permitting safe mem::forget?

For example, something like this:

[#no_safe_forget]
pub struct ScDropper { ... }

A struct so annotated would not be permitted to be "mem::forgotten" in a "safe" context and would require unsafe. I wonder if something like this could be created without too much hassle? Perhaps a Pre-RFC to discuss this option if there is no other work-around?

EDIT: Perhaps a better solution would be the introduction of a new "Marker Trait":

pub Trait UnsafeForget;

Then, mem::forget could change to:

pub fn forget<T>( t : T ) -> () where T : !UnsafeForget {...}

The compiler would never auto-derive "UnsafeForget" for any type, but, you could make a type implement this marker trait to make it "Unforgettable" using the "safe" mem::forget as follows:

impl UnsafeForget for ScDropper;

All this solution requires is the support for negative trait bounds which I believe are already available on nightly, but, not yet stabilized.

1 Like