From 38932a94f835422e86527d36382e76de685ed804 Mon Sep 17 00:00:00 2001 From: Scott Olsen Date: Sun, 30 Oct 2022 19:57:53 -0400 Subject: [PATCH] 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). --- examples/nested_lambdas.carp | 6 +++--- .../lambda_leaking_capture_ownership.carp.output.expected | 2 ++ test/test-for-errors/lambda_leaking_capture_ownership.carp | 7 +++++++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 test/output/test/test-for-errors/lambda_leaking_capture_ownership.carp.output.expected create mode 100644 test/test-for-errors/lambda_leaking_capture_ownership.carp diff --git a/examples/nested_lambdas.carp b/examples/nested_lambdas.carp index f0f15b20b..2ea7ff1ca 100644 --- a/examples/nested_lambdas.carp +++ b/examples/nested_lambdas.carp @@ -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 [] @@ -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))) diff --git a/test/output/test/test-for-errors/lambda_leaking_capture_ownership.carp.output.expected b/test/output/test/test-for-errors/lambda_leaking_capture_ownership.carp.output.expected new file mode 100644 index 000000000..62d27c86a --- /dev/null +++ b/test/output/test/test-for-errors/lambda_leaking_capture_ownership.carp.output.expected @@ -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)]) diff --git a/test/test-for-errors/lambda_leaking_capture_ownership.carp b/test/test-for-errors/lambda_leaking_capture_ownership.carp new file mode 100644 index 000000000..9a0ba8ae2 --- /dev/null +++ b/test/test-for-errors/lambda_leaking_capture_ownership.carp @@ -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 + ())