modularml / mojo

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

[BUG]: @parameter bound objects are incorrectly discarded #1404

Open p88h opened 9 months ago

p88h commented 9 months ago

Bug description

I suspect this may be a deeper issue. but the easy way to manifest that something is amiss is to create a 'memory-only' object, and then have a @parameter-bound callable use that. If the object is not referenced anywhere after the function call, you may 'get lucky' and Mojo decides to 'clean up' the object prior to actual use. A smallest repro case is included below:

fn main():
    let simpler = String("I want you to hold this")

    @parameter
    fn doit():
        for i in range(len(simpler)-5):
            print(simpler[i:i+5])   

    doit()
    # If uncommented, this will actually work correctly
    #print(len(simpler))

Steps to reproduce

System information

- What OS did you do install Mojo on ?
Win 11 / WSL2 / Ubuntu 23.04
- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 0.6.0 (d55c0025)
- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.2.2 (95d42445)
p88h commented 1 day ago

just to check in from the future, it seems parametric closures capturing lifetime is still broken in mojo 24.5.0