modularml / mojo

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

[BUG] Accessing aliased list of lists gives nonsense #3451

Open helehex opened 2 weeks ago

helehex commented 2 weeks ago

Bug description

I also tried with VariadicList, and InlineArray. Both of those work fine. If you use var entry = ll[1][1] instead of alias entry = ll[1][1], it works. (see example)

Steps to reproduce

fn main():
    alias ll = List(List(1, 2, 3), List(4, 5, 6), List(7, 8, 9))
    alias entry = ll[1][1]
    # var entry = ll[1][1]
    print(entry)
    # prints nonsense

System information

Host Information
  ================

  Target Triple: x86_64-unknown-linux
  CPU: skylake
  CPU Features: adx, aes, avx, avx2, bmi, bmi2, clflushopt, cmov, crc32, cx16, cx8, f16c, fma, fsgsbase, fxsr, invpcid, lzcnt, mmx, movbe, pclmul, popcnt, prfchw, rdrnd, rdseed, sahf, sgx, sse, sse2, sse3, sse4.1, sse4.2, ssse3, x87, xsave, xsavec, xsaveopt, xsaves

mojo 2024.9.315 (62cda08f)

modular 0.9.2 (b3079bd5)
helehex commented 2 weeks ago

More of an issue with UnsafePointer[UnsafePointer[...]] in general though:

fn init() -> UnsafePointer[UnsafePointer[Int]]:
    var ptr = UnsafePointer[Int].alloc(1)
    ptr[] = 5
    var ptrptr = UnsafePointer[UnsafePointer[Int]].alloc(1)
    ptrptr[] = ptr
    return ptrptr

fn main():
    alias p = init()
    alias entry = p[][] # prints nonsense
    # var entry = p[][] # prints 5
    print(entry)
thatstoasty commented 2 weeks ago

Seems like the same root cause of an issue I raised as well https://github.com/modularml/mojo/issues/3285