Skip to content

Commit

Permalink
test: add tests for lambda leak cases
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
scolsen committed Oct 30, 2022
1 parent 16c58d8 commit 38932a9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions examples/nested_lambdas.carp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(defn my-curry [f] (fn [x] (fn [y] (f x y))))
(defn double-curry [f] (fn [x] (fn [y] (fn [z] (f x y z)))))
(defn my-curry [f] (fn [x] (fn [y] (~f x y))))
(defn double-curry [f] (fn [x] (fn [y] (fn [z] (~f x y z)))))

(defn make-cb []
((fn []
Expand All @@ -15,4 +15,4 @@
(defn main []
(do ((make-cb))
((make-cb2))
(((my-curry (fn [x y] (Int.+ x y))) 1) 2)))
(((my-curry &(fn [x y] (Int.+ x y))) 1) 2)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lambda_leaking_capture_ownership.carp:4:2 [ERROR] `I expected an array of valid arguments, but got: (let [string (copy "") leaks (fn [] s)])
- `defn` requires an array of arugments, but it got: (let [string (copy "") leaks (fn [] s)])
7 changes: 7 additions & 0 deletions test/test-for-errors/lambda_leaking_capture_ownership.carp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(Project.config "file-path-print-length" "short")

;; see [issue #1040](https://github.com/carp-lang/Carp/issues/1040)
(defn lambda-leak
(let [string @""
leaks (fn [] s)]) ;; disallow this
())

0 comments on commit 38932a9

Please sign in to comment.