modularml / mojo

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

[BUG]: Crash when adding items to `DynamicVector` while modifying it in the same loop. #1382

Open omerbenamram opened 10 months ago

omerbenamram commented 10 months ago

Bug description

The following code:

fn main():
    var count = DynamicVector[Int]()

    for i in range(0, 100):
        count.append(1)

        for idx in range(i + 1, i + 3):
            count[idx] += 1 * 1

produces:

double free or corruption (!prev)
[1179796:1179796:20231204,233857.559000:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[1179796:1179796:20231204,233857.559076:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0.      Program arguments: mojo bug.mojo

Coming from some background in rust, we cannot have both a mutable and an immutable reference living at the same time to the same variable in the loop.

This does not crash:

fn main():
    var count = DynamicVector[Int]()
    for i in range(0, 100):
        count.append(1)

    for i in range(0, 100):
        # count.append(1)

        for idx in range(i + 1, i + 3):
            count[idx] += 1 * 1

I'm not sure if mojo has any notion of a borrow checker, but AFAIK this should be a compile error.

Steps to reproduce

Running the code always produces a crash.

System information

- What OS did you do install Mojo on ? Linux
- mojo version: mojo 0.6.0 (d55c0025)
- modular version: modular 0.2.0 (355ea9c0)
rarebreed commented 10 months ago

Mojo has plans for lifetimes and references, but it's not ready yet. Check out their proposals on lifetime tracking and provenance, and also on their Roadmap section

What I am interested in though, because I have seen no documentation of it anywhere, is whether mojo will ultimately allow programmers to use raw Pointers (like now) just as easily as references. If there's no equivalent of rust's unsafe, I think that will be a big problem.

omerbenamram commented 10 months ago

I see - still i suppose it shouldn't segfault 😅