Skip to content

Commit ceb2ea3

Browse files
committed
fix var scope of thunk macro
1 parent 2aad117 commit ceb2ea3

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/tangent_types/thunks.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,18 @@ Define a [`Thunk`](@ref) wrapping the `expr`, to lazily defer its evaluation.
144144
macro thunk(body)
145145
# Basically `:(Thunk(() -> $(esc(body))))` but use the location where it is defined.
146146
# so we get useful stack traces if it errors.
147-
func = Expr(:->, Expr(:tuple), Expr(:block, __source__, body))
147+
letargs = Base._lift_one_interp!(body)
148+
func = Base.replace_linenums!(:(()->($(esc(body)))), __source__)
148149
return quote
149-
_usethunks() ?
150-
Thunk($(esc(func))) :
151-
$(esc(body))
150+
if _usethunks()
151+
let $(letargs...)
152+
Thunk($func)
153+
end
154+
else
155+
let $(letargs...)
156+
$(esc(body))
157+
end
158+
end
152159
end
153160
end
154161

0 commit comments

Comments
 (0)