Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix var scope of thunk macro #686

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/tangent_types/thunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,31 @@ function LinearAlgebra.LAPACK.trsyl!(transa, transb, A, B, C::AbstractThunk, isg
return throw(MutateThunkException())
end


@static if VERSION > v"1.10"
using Base: replace_linenums!
else
replace_linenums!(ex, ln::LineNumberNode) = ex
function replace_linenums!(ex::Expr, ln::LineNumberNode)
if ex.head === :block || ex.head === :quote
# replace line number expressions from metadata (not argument literal or inert) position
map!(ex.args, ex.args) do @nospecialize(x)
isa(x, Expr) && x.head === :line && length(x.args) == 1 && return Expr(:line, ln.line)
isa(x, Expr) && x.head === :line && length(x.args) == 2 && return Expr(:line, ln.line, ln.file)
isa(x, LineNumberNode) && return ln
return x
end
end
# preserve any linenums inside `esc(...)` guards
if ex.head !== :escape
for subex in ex.args
subex isa Expr && replace_linenums!(subex, ln)
end
end
return ex
end
end

"""
@thunk expr

Expand All @@ -144,11 +169,18 @@ Define a [`Thunk`](@ref) wrapping the `expr`, to lazily defer its evaluation.
macro thunk(body)
# Basically `:(Thunk(() -> $(esc(body))))` but use the location where it is defined.
# so we get useful stack traces if it errors.
func = Expr(:->, Expr(:tuple), Expr(:block, __source__, body))
letargs = Base._lift_one_interp!(body)
func = replace_linenums!(:(()->($(esc(body)))), __source__)
return quote
_usethunks() ?
Thunk($(esc(func))) :
$(esc(body))
if _usethunks()
let $(letargs...)
Thunk($func)
end
else
let $(letargs...)
$(esc(body))
end
end
end
end

Expand Down
Loading