Skip to content

Commit bbd7998

Browse files
committed
fix: port #1440 by @scolsen to new machinery
1 parent eb9576e commit bbd7998

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

examples/nested_lambdas.carp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
(defn my-curry [f] (fn [x] (fn [y] (f x y))))
2-
(defn double-curry [f] (fn [x] (fn [y] (fn [z] (f x y z)))))
1+
(defn my-curry [f] (fn [x] (fn [y] (~f x y))))
2+
(defn double-curry [f] (fn [x] (fn [y] (fn [z] (~f x y z)))))
33

44
(defn make-cb []
55
((fn []
@@ -15,4 +15,4 @@
1515
(defn main []
1616
(do ((make-cb))
1717
((make-cb2))
18-
(((my-curry (fn [x y] (Int.+ x y))) 1) 2)))
18+
(((my-curry &(fn [x y] (Int.+ x y))) 1) 2)))

src/Memory.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,13 @@ unmanage typeEnv globalEnv xobj =
524524
then Left (UsingCapturedValue xobj)
525525
else Left (UsingUnownedValue xobj)
526526
[one] ->
527-
let newDeleters = Set.delete one (memStateDeleters m)
528-
in do
529-
put $ m {memStateDeleters = newDeleters}
530-
pure (Right ())
527+
if isSymbolThatCaptures xobj
528+
then pure (Left (GivingAwayCapturedValue xobj))
529+
else
530+
let newDeleters = Set.delete one (memStateDeleters m)
531+
in do
532+
put $ m {memStateDeleters = newDeleters}
533+
pure (Right ())
531534
tooMany -> error ("Too many variables with the same name in set: " ++ show tooMany)
532535
else pure (Right ())
533536

src/TypeError.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ data TypeError
6464
| FailedToAddLambdaStructToTyEnv SymPath XObj
6565
| FailedToInstantiateGenericType Ty
6666
| InvalidStructField XObj
67+
| GivingAwayCapturedValue XObj
6768

6869
instance Show TypeError where
6970
show (SymbolMissingType xobj env) =
@@ -334,6 +335,9 @@ instance Show TypeError where
334335
++ " to the type environment."
335336
show (FailedToInstantiateGenericType ty) =
336337
"I couldn't instantiate the generic type " ++ show ty
338+
show (GivingAwayCapturedValue xobj) =
339+
"Can't give away the captured value '" ++ pretty xobj ++ "' at " ++ prettyInfoFromXObj xobj
340+
++ ". Captured values can be borrowed with `&` or copied with `@&`, not moved."
337341

338342
machineReadableErrorStrings :: FilePathPrintLength -> TypeError -> [String]
339343
machineReadableErrorStrings fppl err =
@@ -460,6 +464,8 @@ machineReadableErrorStrings fppl err =
460464
++ pretty xobj
461465
++ " to the type environment."
462466
]
467+
(GivingAwayCapturedValue xobj) ->
468+
[machineReadableInfoFromXObj fppl xobj ++ " Can't give away captured value '" ++ pretty xobj ++ "'."]
463469
_ ->
464470
[show err]
465471

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lambda_leaking_capture.carp:6:18 Can't give away captured value 's'.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(Project.config "file-path-print-length" "short")
2+
3+
;; see [issue #1040](https://github.com/carp-lang/Carp/issues/1040)
4+
(defn lambda-leak []
5+
(let [s @""
6+
f (fn [] s)]
7+
(ignore (f))))

0 commit comments

Comments
 (0)