Skip to content

What are the exact semantics of pointer comparison? #239

Closed
@RalfJung

Description

@RalfJung

Pointer comparison is complicated. However, for better or worse, Rust lets you safely compare any raw pointers, so we need to assign this some reasonable semantics. I wanted a place to centrally track all questions related to this, so here we go. (I never know whether to open such issues here in the UCG repo, or over in the rustc repo.)

There are two ways in which pointer comparison is "interesting": provenance, and objects without a guaranteed stable address.

Provenance

Does provenance affect whether pointers are equal? One major concern here is figuring out the semantics of pointer comparison in LLVM, and then likely we have no choice but to inherit that. Some data points:

  • Eli Friedman writes

    icmp is defined to just take the raw pointer bits as an integer. This is described in LangRef. If the text of LangRef describing icmp isn't sufficiently clear, suggestions welcome. If some transform isn't consistent with this, please file a bug.

  • In our LLVM semantics paper, we made pointer comparison non-deterministic under some conditions (when the addresses are equal but provenance differs), so that pointers derived from different calls to malloc can always compare inequal even if they are physically equal. This directly contradicts Eli's statement above.

I am trying to figure out if LLVM optimizations clearly indicate one or the other choice; so far that has been inconclusive.

Unstable objects

Functions, vtables, and consts do not have a unique stable address, also leading to interesting problems.

function address equality

vtable equality

const address equality

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-provenanceTopic: Related to when which values have which provenance (but not which alias restrictions follow)A-ptr-eqTopic: pointer equalityC-open-questionCategory: An open question that we should revisit

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions