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

fix: don't allow lambdas to leak captures #1440

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Commits on Oct 30, 2022

  1. fix: don't allow lambdas to leak captures

    When lambdas close over some external environment variable, if that
    variable is a linear value, their environment becomes the owner of the
    captured value and the value will be freed with the environment. If the
    lambda moves this variable out of its own scope, however, e.g. by
    returning it in its body, both the caller and the lambda environment
    will wind up owning the value, resulting in double frees.
    
    For now, we prevent this scenario by simply disallowing functions to
    leak variables captured from another scope. Doing so is now an error. Of
    course, one can still copy these values without issue.
    
    We achieve this through set operations. If the memory state loses the
    fake deleters for the set of a lambdas captured bindings after its body
    is evaluated, we've encountered an ownership leak, and report an error.
    
    ```clojure
    (defn example []
      (let [capture @""
            lambda (fn [] capture)])) ;; this is now an error
    
    (defn example []
      (let [capture @""
            lambda (fn [] @&capture)])) ;; this is still OK
    ```
    
    fixes carp-lang#1040
    scolsen committed Oct 30, 2022
    Configuration menu
    Copy the full SHA
    16c58d8 View commit details
    Browse the repository at this point in the history
  2. test: add tests for lambda leak cases

    Adds tests to ensure lambdas do not leak ownership of their captured
    variables. In addition, the nested lambda test now needs to accept a
    reference to a function instead of a function value (otherwise an
    ownership leak occurs).
    scolsen committed Oct 30, 2022
    Configuration menu
    Copy the full SHA
    38932a9 View commit details
    Browse the repository at this point in the history

Commits on Nov 1, 2022

  1. Configuration menu
    Copy the full SHA
    96f4736 View commit details
    Browse the repository at this point in the history
  2. chore: disable unused-but-set-variable for clang (carp-lang#1441)

    At some point (I believe version 13.0.0?) clang added a warning that
    catches variables that were assigned but unused. This version of clang
    (or later) is now bundled w/ github's macos images and is causing our
    tests to fail in continuous integration. We can currently generate C
    code that trips this warning, so for now I've disabled it as we do some
    other warnings related to variable usages.
    scolsen committed Nov 1, 2022
    Configuration menu
    Copy the full SHA
    dbceaab View commit details
    Browse the repository at this point in the history