-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Currently as far as i understand it the unsafe keyword only applys to potentialy breaking memory safety guarantees.
Some people argued that other types of guarantees like leak-safety, safety from panics, ... would be useful and violating those should be considered unsafe too.
The most important argument against this seems to be that this would weaken memory safety because it would lead to an inflationary use of unsafe.
My ideal solution to this would be not having just one unsafe keyword but at least three, maybe many:
- unsafe_memory_access
- unsafe_potential_leaks
- unsafe_could_panic
- ...
Code that can not accept one or more of those types of unsafety could opt out of each specifically.
Since 1.0 is out this scenario is not realistic any more.
But maybe we could have something like:
- "nopanic" which would raise an error when the compiler can not prove, that the code in question does not panic.
- "noleak" which would raise an error when the compiler can not prove freedom of leaks
- ...
Since a practical compiler has to run in finite time it can on it's own only ever prove the most trivial cases. A mechanism would be needed to assist the compiler.
The most simple form would be corresponding attributes wontpanic and wontleak.
For example i want to call a function that could theoretically panic inside a nopanic block, then i would make sure that i call this function only in ways that are guaranteed not to panic and wrap the call inside a wontpanic block.
Use of reference counting inside a noleak block could be done the same way.
This could be added in a backward compatible way and nopanic and noleak... should be added where ever possible.