Pointer to `usize` Bug

I stumbled upon something a while ago and thought it might be interesting to share. What do you think the output of this code is?

fn () {
    let :  = {
        let  = 0u8;
        & as *const _ as 
    };

    let :  = {
        let  = 0u8;
        & as *const _ as 
    };

    if  !=  {
        !("{a:?} != {b:?}");
    }

    if  ==  {
        !("{a:?} == {b:?}");
    }
}

Drumroll please…

140736004887288 != 140736004887288
140736004887288 == 140736004887288

Surprise! Weird things happen in Rust when dealing with pointer-casted integers.

This is actually a bug in LLVM, with details that go over my head. From the looks of things, the issue isn’t even going away anytime soon. How fun!

Sources

src
fn main()
usize

The pointer-sized unsigned integer type.

The size of this primitive is how many bytes it takes to reference any location in memory. For example, on a 32 bit target, this is 4 bytes and on a 64 bit target, this is 8 bytes.

let a: usize
std::macros
macro_rules! println

Prints to the standard output, with a newline.

On all platforms, the newline is the LINE FEED character (\n/U+000A) alone (no additional CARRIAGE RETURN (\r/U+000D)).

This macro uses the same syntax as format, but writes to the standard output instead. See [std::fmt] for more information.

The println! macro will lock the standard output on each call. If you call println! within a hot loop, this behavior may be the bottleneck of the loop. To avoid this, lock stdout with io::stdout().lock:

use std::io::{stdout, Write};

let mut lock = stdout().lock();
writeln!(lock, "hello world").unwrap();

Use println! only for the primary output of your program. Use [eprintln] instead to print error and progress messages.

See the formatting documentation in std::fmt for details of the macro argument syntax.

Panics

Panics if writing to [io::stdout] fails.

Writing to non-blocking stdout can cause an error, which will lead this macro to panic.

Examples

println!(); // prints just a newline
println!("hello there!");
println!("format {} arguments", "some");
let local_variable = "some";
println!("format {local_variable} arguments");
let b: usize
let v: u8