-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Description
Comparing raw pointers in constants is forbidden (hard error) in const contexts.
The const_compare_raw_pointers
feature gate enables the guaranteed_eq
and guaranteed_ne
methods on raw pointers. The root problem with pointer comparisons at compile time is that in many cases we can't know for sure whether two pointers are equal or inequal. While it's fairly obvious that a pointer to a static
and a local variable won't be equal, we can't ever tell whether two local variables are equal (their memory may alias at runtime) or whether any pointer is equal to a specific integer. In order to make it obvious that there is a discrepancy between runtime and compile-time, the methods are called guaranteed_*
and furthermore they return an Option
where None
represents "unknown". At runtime these methods just return the result of the actual pointer comparison (i.e., they will always return Some
then).
This permits const fn
to do performance optimization tricks like the one done in slice comparison (if length is equal and pointer is equal, don't compare the slice contents, just return that it's equal).
Since we aren't sure yet what the semantics of functions that return different values at runtime and at compile-time are, we'll keep this function unstable until we've had that discussion.
Unresolved questions
- What exact logic for determining "guaranteed eq/ne cases" are we willing to stably commit to?