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

References must keep track of their origin #470

Open
hellerve opened this issue May 29, 2019 · 4 comments
Open

References must keep track of their origin #470

hellerve opened this issue May 29, 2019 · 4 comments
Assignees
Labels
bug difficult haskell memory Borrow checker and lifetimes

Comments

@hellerve
Copy link
Member

Currently, references can be invalidated by invalidating their origin inside their scope.

(defn use-up [x] ())

(defn main []
  (let-do [a [@"hi"]
           n (Array.nth &a 0)]
    (use-up a)
    (println* n))) ; we used a already! this is bad!

It is also possible to set references to point to values in a more short-lived scope:

(defn main []
  (let-do [x ""]
    (let [a [@"hi"]]
      (set! x (Array.nth &a 0)))
    (println* x))) ; a was already dropped!

Both of these can be prevented by checking the origin of a reference and whether the origin scope still exists; an error could be emitted if it doesn’t.

Cheers

@eriksvedang eriksvedang self-assigned this May 30, 2019
@eriksvedang eriksvedang added this to the Version 0.3.0 milestone Jun 13, 2019
@eriksvedang
Copy link
Collaborator

The compiler now cathes this error properly.

@eriksvedang
Copy link
Collaborator

Or actually only the first one is caught (missed that there were a second one). So this will have to be fixed still...

@eriksvedang eriksvedang removed this from the Version 0.4.0 milestone May 5, 2020
@eriksvedang
Copy link
Collaborator

I think the second one requires the fixes I'm working on in the sets-of-lifetimes branch.

@redbar0n
Copy link

maybe data structures like Array should be able to take ownership (or have ownership automatically transferred to it when it has a reference to a variable that goes out of scope and would otherwise be deallocated) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug difficult haskell memory Borrow checker and lifetimes
Projects
None yet
Development

No branches or pull requests

3 participants