Skip to content

Pointer to static is not guaranteed_eq to itself. #144584

Closed

Description

@theemathas

I tried this code:

#![feature(const_raw_ptr_comparison)]

static X: u64 = 1;
const B: Option<bool> = (&raw const X).guaranteed_eq(&raw const X);

fn main() {
    println!("{B:?}");
}

I expected to the code to output Some(true), but it output None.

guaranteed_eq is documented to sometimes output None on equal pointers though, so I'm not sure if this is a bug.

@rustbot labels +A-const-eval

Tracking issue: #53020

Meta

Reproducible on the playground with version 1.90.0-nightly (2025-07-27 f8e355c230c6eb7b78ff)

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    on Jul 28, 2025
  2. zachs18 commented on Jul 28, 2025

    @zachs18
    Contributor

    Seems like there's a FIXME in the compiler for this:

    // FIXME: return a `1` for when both sides are the same pointer, *except* that
    // some things (like functions and vtables) do not have stable addresses
    // so we need to be careful around them (see e.g. #73722).
    // FIXME: return `0` for at least some comparisons where we can reliably
    // determine the result of runtime inequality tests at compile-time.
    // Examples include comparison of addresses in different static items.
    (Scalar::Ptr(..), Scalar::Ptr(..)) => 2,

  3. xonx4l commented on Jul 29, 2025

    @xonx4l
    Contributor

    This is not a bug but expected behavior based on how Rust evaluates const expressions. The compiler cannot guarantee that two pointers to the same static are equal at compile time, so guaranteed_eq correctly returns None.

  4. asquared31415 commented on Jul 29, 2025

    @asquared31415
    Contributor

    by definition, taking the address of a static is guaranteed to produce the same pointer every time. the current behavior is just a limitation of the current implementation, not an inherent property of constant evaluation.

    However, properly tracking this is likely very hard and the function must be conservative, so right now only comparisons with null can possibly return a value in const eval, and even then only sometimes.

  5. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    and removed
    C-bugCategory: This is a bug.
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jul 29, 2025
  6. added a commit that references this issue on Aug 23, 2025
  7. added 2 commits that reference this issue on Aug 24, 2025
  8. added a commit that references this issue on Aug 25, 2025
  9. added 2 commits that reference this issue on Aug 26, 2025
  10. added a commit that references this issue on Aug 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions