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

[BUG]: Consumption of struct works only on definition of __del__ #734

Closed
ivellapillil opened this issue Sep 11, 2023 · 4 comments
Closed
Assignees
Labels
bug Something isn't working mojo Issues that are related to mojo mojo-lang Tag for all issues related to language.

Comments

@ivellapillil
Copy link

ivellapillil commented Sep 11, 2023

Bug description

I expect the following code to raise compile time error:

struct Mo:
    var x: Int

    fn __init__(inout self, a: Int):
        self.x = a

    #fn __del__(owned self):
    #    pass

def take(owned x: Mo):
    print(x.x) 

def main():
    var l: Mo = Mo(100)
    take(l^)
    l.x = 10
    print("Again " + String(l.x))

However, it compiles successfully.

If I uncomment the __del__ method, then it shows:

error: value 'l' cannot be consumed, because 'l.x' is used later
    take(l^)

I would have expected moving a location should not be dependent on definition of a destructor.

Steps to reproduce

Compile with the above code.

System information

x86_64
Ubuntu 22.04.2 LTS
Linux-5.19.0-38-generic-x86_64-with-glibc2.35
mojo 0.2.1 (64d14e85)

modular 0.1.4 (6b54d308)
@ivellapillil ivellapillil added bug Something isn't working mojo Issues that are related to mojo labels Sep 11, 2023
@ematejska ematejska added the mojo-lang Tag for all issues related to language. label Sep 11, 2023
@Mogball
Copy link
Collaborator

Mogball commented Sep 21, 2023

This is super weird. I'll take a look.

@Mogball Mogball self-assigned this Sep 21, 2023
@Mogball Mogball removed their assignment Oct 19, 2023
@Mogball
Copy link
Collaborator

Mogball commented Oct 30, 2023

@stumpOS when you have some free cycles, could you take a look at this?

@lattner
Copy link
Collaborator

lattner commented Nov 6, 2023

This is likely a checklifetimes problem, I'll investigate.

@lattner lattner assigned lattner and unassigned stumpOS Nov 6, 2023
@lattner
Copy link
Collaborator

lattner commented Nov 6, 2023

Indeed good catch, CheckLifetimes is currently ignoring types that don't have a destructor for liveness analysis. The rationale is that there is no codegen need to know whether something is alive, and the parser does various simplifications for trivial types. As you mention, codegen isn't all that matters though.

The solution here is to tighten up checklifetimes thing to work on actually-trivial types and not just destructor-less things. The parser should also reject the transfer operator on trivial types.

@lattner lattner closed this as completed Nov 6, 2023
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-lang Tag for all issues related to language.
Projects
None yet
Development

No branches or pull requests

5 participants