What do we do with "unconst" operations? #14
Description
Spawned off rust-lang/rust#55009 (comment) (a PR making raw pointer to usize casts unsafe and making raw pointer operations unsafe.
These unconst operations are explained in https://github.com/rust-rfcs/const-eval/blob/master/const_safety.md
There is the question of whether we should overload unsafe to also mean unconst in const environments or whether we should create a new keyword like unconst, const unsafe or similar. The PR linked above uses the unsafe keyword (but all operations still require feature gates).
My view is that unsafe allows you to do things that the compiler can't prove as sound but are still sound. If you mess that up, your code is unsound. Adding another keyword like unconst which means essentially the same thing (but for a different set of operations), feels like it does not add any gains.
Activity
oli-obk commentedon Nov 25, 2018
Further datapoint: we could use unconst in the future to mark functions explicitly not meant to be const. then, in a future edition we can remove the need to mark things as const.
oli-obk commentedon Dec 14, 2018
The counterargument is that ppl might be confused why something is
unsafein const functions but not in regular functions. This would further complicate the unsafe coding guidelines.Would it make sense to also make raw pointer to
usizecasts unsafe in regular code? What about raw pointer operations like<? The result is unspecified but not undefined per se. So no nasal demons if the optimizer figures out that you are using<on two pointers that are from different allocations. It's still very funky thatlet x = 42; let y = 55; let a = &x as *const i32; let b = &y as *const i32; assert!(a < b || a > b);can theoretically panic.RalfJung commentedon Aug 7, 2020
In the const lang-team meeting with @rust-lang/wg-const-eval we did make some plans towards unconst operations, but nothing has happened since then I think. See the meeting notes. The first step would be an RFC specifying what "UB during CTFE" means.
[-][Tracking issue] Reuse the `unsafe` keyword to also mean `unconst`?[/-][+]What do we do with "unconst" operations?[/+]carbotaniuman commentedon Aug 22, 2020
Would it be possible for unsafe blocks to be a superset of unconst blocks, so if you don't need unsafe you can just use unconst?
oli-obk commentedon Jan 3, 2021
There is now an RFC with a way to permit arbitrary
unsafe(if there is an impl for it): rust-lang/rfcs#3016I'm going ahead and close this issue
RalfJung commentedon Jan 3, 2021
Well, that RFC is mostly about
unsafe, notunconst. Using this RFC to enableunconstoperations is listed as a "future possibility":