Skip to content

Pointer equality of ZST in debug build is inconsistent with that in release build #87477

Open
@danieljl

Description

@danieljl

Consider this code:

fn main() {
    let a = ();
    let b = ();
    dbg!(std::ptr::eq(&a, &b));
}

It will output false in debug build, but true in release build.

I've tried other Zero-Sized Types (ZST), like an empty array and a struct with no field, and the result is still the same.

Interestingly, the code below will consistently output false in both debug build and release build:

fn main() {
    let a = ();
    let b = ();
    dbg!((&a as *const _) == (&b as *const _));
}

Playground

Meta

rustc --version --verbose:

rustc 1.51.0 (2fd73fabe 2021-03-23)
binary: rustc
commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
commit-date: 2021-03-23
host: x86_64-apple-darwin
release: 1.51.0
LLVM version: 11.0.1

(Rust 1.51, 1.53, and nightly (2021-07-25) have this behavior.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-raw-pointersArea: raw pointers, MaybeUninit, NonNullA-zstArea: Zero-sized types (ZSTs).C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions