modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
22.88k stars 2.58k forks source link

[BUG]: Hashing a `String` does not return a deterministic value #1556

Open RyanLeber opened 8 months ago

RyanLeber commented 8 months ago

Bug description

When using the builtin hash function on a String the returned hash value is not deterministic unless the lifetime of the string is extended.

fn main():
    let s1: String = "mojo"

    print("s1: ", hash(s1._as_ptr(), len(s1)))
    print("s1: ", hash(s1._as_ptr(), len(s1)))
s1:  1974758210704828359
s1:  7376120937799554262

Lifetime extended:

fn main():
    let s1: String = "mojo"

    print("s1: ", hash(s1._as_ptr(), len(s1)))
    print("s1: ", hash(s1._as_ptr(), len(s1)))

    _ = s1
s1:  1974758210704828359
s1:  1974758210704828359

Steps to reproduce

System information

- Ubuntu 22.04.3 LTS (WSL 2)
- mojo 0.6.1 (876ded2e)
- modular 0.3.1 (589ce200)
JoeLoser commented 8 months ago

FYI @bethebunny