Repository navigation
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?
Activity
RalfJung commentedon May 14, 2020
oli-obk commentedon May 15, 2020
Rollup merge of rust-lang#73398 - oli-obk:const_raw_ptr_cmp, r=varkorβ¦
Rollup merge of rust-lang#73398 - oli-obk:const_raw_ptr_cmp, r=varkorβ¦
Rollup merge of rust-lang#73398 - oli-obk:const_raw_ptr_cmp, r=varkorβ¦
Rollup merge of rust-lang#73398 - oli-obk:const_raw_ptr_cmp, r=varkorβ¦
19 remaining items
Rollup merge of rust-lang#144885 - zachs18:ptr_guaranteed_cmp_more, rβ¦
Rollup merge of rust-lang#144885 - zachs18:ptr_guaranteed_cmp_more, rβ¦
Rollup merge of rust-lang#144885 - zachs18:ptr_guaranteed_cmp_more, rβ¦
Rollup merge of rust-lang#144885 - zachs18:ptr_guaranteed_cmp_more, rβ¦
Unrolled build for #144885
Rollup merge of #144885 - zachs18:ptr_guaranteed_cmp_more, r=RalfJung
Auto merge of #144847 - Randl:const-ord, r=oli-obk
Auto merge of #144847 - Randl:const-ord, r=oli-obk
Auto merge of #144847 - Randl:const-ord, r=oli-obk
Auto merge of rust-lang#144847 - Randl:const-ord, r=oli-obk
Rollup merge of #144885 - zachs18:ptr_guaranteed_cmp_more, r=RalfJung
Auto merge of #101483 - oli-obk:guaranteed_opt, r=fee1-dead
Auto merge of #101483 - oli-obk:guaranteed_opt, r=fee1-dead
MadProbe commentedon Oct 12, 2026
RalfJung commentedon Oct 13, 2026
That's not possible as we cannot know the address that the static/constant will get at runtime. Please follow-up on Zulip if you don't understand, to avoid spamming this issue.