Skip to content

Commit b5f796d

Browse files
authored
Try to give a better location for Cassette-like transforms (#324)
By looking at the `method_for_inference_heuristics` and obtaining the method source for the original un-overdubbed method and working with that.
1 parent 75059ca commit b5f796d

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/locationinfo.jl

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
1+
function body_for_method(current_file, current_line, meth)
2+
ret = JuliaInterpreter.whereis(meth)
3+
ret === nothing && return nothing
4+
deffile, _ = ret
5+
body_defline = CodeTracking.definition(String, meth)
6+
if body_defline === nothing
7+
return (nothing, 1, true)
8+
else
9+
body, defline = body_defline
10+
return (body, defline, deffile != current_file || defline > current_line)
11+
end
12+
end
13+
114
function locinfo(frame::Frame)
2-
if frame.framecode.scope isa Method
3-
meth = frame.framecode.scope
4-
ret = JuliaInterpreter.whereis(meth)
5-
ret === nothing && return nothing
6-
deffile, _ = ret
15+
scope = frame.framecode.scope
16+
if scope isa Method
17+
meth = scope
718
ret = JuliaInterpreter.whereis(frame)
819
ret === nothing && return nothing
920
current_file, current_line = ret
10-
body_defline = CodeTracking.definition(String, meth)
11-
local body, defline
12-
unknown_start = false
13-
if body_defline === nothing
14-
unknown_start = true
15-
else
16-
body, defline = body_defline
17-
if deffile != current_file || defline > current_line
18-
unknown_start = true
21+
unknown_start = true
22+
if JuliaInterpreter.is_generated(meth) && !frame.framecode.generator
23+
# We're inside the expansion of a generated function.
24+
# There's not much point trying to query the method.
25+
# However, me heuristically make one exception: If our
26+
# src has method_for_inference_heuristics set, it is likely
27+
# a Cassette pass, so we can use the un-overdubbed method
28+
# to retrieve source.
29+
src = frame.framecode.src
30+
if isdefined(src, :method_for_inference_heuristics)
31+
(body, defline, unknown_start) = body_for_method(current_file, current_line, src.method_for_inference_heuristics)
1932
end
33+
else
34+
(body, defline, unknown_start) = body_for_method(current_file, current_line, meth)
2035
end
2136
if unknown_start
2237
isfile(current_file) || return nothing
2338
body = read(current_file, String)
2439
defline = 1 # We are not sure where the context start in cases like these, could be improved?
2540
end
26-
return defline, deffile, current_line, body
41+
return defline, current_file, current_line, body
2742
else
2843
println("not yet implemented")
2944
end

0 commit comments

Comments
 (0)