Skip to content

Commit 0aafa91

Browse files
authored
fix(emit): skip move-out memset when nested closure captures a borrow (#1556)
1 parent 905f945 commit 0aafa91

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

src/Emit.hs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ toC toCMode emitLines mutualGroup (Binder meta root) = renderEmitterState (execS
336336
srcExpr = case lookupMode of
337337
LookupLocal (Capture _) -> "_env->" ++ pathToC path
338338
_ -> pathToC path
339+
-- Refs/pointers don't own their pointee, so the outer
340+
-- env's delete is already a no-op for them. Zeroing
341+
-- the source would also break repeat invocations of
342+
-- the outer closure (second call would see NULL).
343+
isBorrow = case forceTy xobj of
344+
RefTy _ _ -> True
345+
PointerTy _ -> True
346+
_ -> False
339347
in do
340348
appendToSrc
341349
(addIndent indent ++ dstField ++ " = " ++ srcExpr ++ ";\n")
@@ -345,13 +353,14 @@ toC toCMode emitLines mutualGroup (Binder meta root) = renderEmitterState (execS
345353
-- delete is a no-op for this field — otherwise both envs
346354
-- would try to free the same heap data on teardown.
347355
case lookupMode of
348-
LookupLocal (Capture _) ->
349-
appendToSrc
350-
( addIndent indent ++ "memset(&" ++ srcExpr
351-
++ ", 0, sizeof("
352-
++ srcExpr
353-
++ "));\n"
354-
)
356+
LookupLocal (Capture _)
357+
| not isBorrow ->
358+
appendToSrc
359+
( addIndent indent ++ "memset(&" ++ srcExpr
360+
++ ", 0, sizeof("
361+
++ srcExpr
362+
++ "));\n"
363+
)
355364
_ -> pure ()
356365
_ -> appendToSrc ""
357366
)

0 commit comments

Comments
 (0)