Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mojo-nightly] struct lifetime issue #2429

Closed
dorjeduck opened this issue Apr 28, 2024 · 4 comments
Closed

[mojo-nightly] struct lifetime issue #2429

dorjeduck opened this issue Apr 28, 2024 · 4 comments
Assignees
Labels
bug Something isn't working mojo Issues that are related to mojo mojo-repo Tag all issues with this label

Comments

@dorjeduck
Copy link

dorjeduck commented Apr 28, 2024

Bug description

In the following test demo. It seems the destructor is called on the filehandle instead of move.
The demo runs without problems with stable but i get the following with nightly:

file handle address: 0x0
error reading: invalid file handle
struct FileManager:
    var fh:FileHandle

    fn __init__(inout self):
        self.fh = FileHandle()

    fn open(inout self,file_name:String):
        try:
            self.fh = open(file_name,"r")^
            print("file handle address:",self.fh.handle.address)
        except e:
            print("error opening:",e)
        
    fn read(self)  -> String:
        try:
            return self.fh.read()
        except e:
            print("error reading:",e)
            return ""
    
    fn close(inout self):
        try:
            self.fh.close()
        except e:
            print("error closing:",e)

fn main() raises:

    var file_name="./my_file.txt"
    var fh = FileManager()
    fh.open(file_name)
    print(fh.read())
    fh.close()

Changing the open command line to

self.fh = open(…)^

results in the following warning

.../demo.mojo:9:42: warning: transfer from an owned value has no effect and can be removed

and the programs reads the file content as expected

System information

MacOS 14.4.1 (23E224)
modular 0.7.2 (d0adc668)
mojo-nightly 2024.4.2621 (6dff3016)
@dorjeduck dorjeduck added bug Something isn't working mojo Issues that are related to mojo labels Apr 28, 2024
@ematejska ematejska added the mojo-repo Tag all issues with this label label Apr 29, 2024
@dorjeduck
Copy link
Author

This is now in the stable mojo 24.3.0 (9882e19d).

Is there any workaround (without a warning) for now?

@stumpOS
Copy link
Contributor

stumpOS commented May 2, 2024

A fix is currently in review; for now there is not a work around the warning

@lattner
Copy link
Collaborator

lattner commented May 6, 2024

I verified this, it still happens, I haven't dug in all the way though. @stumpOS do you have a patch, if so can you ping me?

@stumpOS
Copy link
Contributor

stumpOS commented May 6, 2024

yea I have a patch open

@linear linear bot closed this as completed May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo Issues that are related to mojo mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

4 participants