Skip to content

Commit 1d86d42

Browse files
committed
copy replace_linenums func for older julia
1 parent ceb2ea3 commit 1d86d42

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/tangent_types/thunks.jl

+26-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,31 @@ function LinearAlgebra.LAPACK.trsyl!(transa, transb, A, B, C::AbstractThunk, isg
136136
return throw(MutateThunkException())
137137
end
138138

139+
140+
@static if VERSION > v"1.10"
141+
using Base: replace_linenums!
142+
else
143+
replace_linenums!(ex, ln::LineNumberNode) = ex
144+
function replace_linenums!(ex::Expr, ln::LineNumberNode)
145+
if ex.head === :block || ex.head === :quote
146+
# replace line number expressions from metadata (not argument literal or inert) position
147+
map!(ex.args, ex.args) do @nospecialize(x)
148+
isa(x, Expr) && x.head === :line && length(x.args) == 1 && return Expr(:line, ln.line)
149+
isa(x, Expr) && x.head === :line && length(x.args) == 2 && return Expr(:line, ln.line, ln.file)
150+
isa(x, LineNumberNode) && return ln
151+
return x
152+
end
153+
end
154+
# preserve any linenums inside `esc(...)` guards
155+
if ex.head !== :escape
156+
for subex in ex.args
157+
subex isa Expr && replace_linenums!(subex, ln)
158+
end
159+
end
160+
return ex
161+
end
162+
end
163+
139164
"""
140165
@thunk expr
141166
@@ -145,7 +170,7 @@ macro thunk(body)
145170
# Basically `:(Thunk(() -> $(esc(body))))` but use the location where it is defined.
146171
# so we get useful stack traces if it errors.
147172
letargs = Base._lift_one_interp!(body)
148-
func = Base.replace_linenums!(:(()->($(esc(body)))), __source__)
173+
func = replace_linenums!(:(()->($(esc(body)))), __source__)
149174
return quote
150175
if _usethunks()
151176
let $(letargs...)

0 commit comments

Comments
 (0)