Skip to content

Commit 2b374c0

Browse files
authored
Don't assume that a method body's file is always the same as the def (#100)
* Don't assume that a method body's file is always the same as the method def `CodeTracking.whereis` currently assumes that the only thing a LineNumberNode can change is the line within the function, but that the file is always the same as the file that the method was defined in (unless there's a macro expansion). That is true for methods that come from the parser, but not necessarily for those that were generated programatically, causing `whereis` to return an incorrect file with the correct line number, causing dowstream issues like JuliaDebug/CassetteOverlay.jl#18.
1 parent 9996990 commit 2b374c0

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/CodeTracking.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,29 @@ Return the file and line number associated with a specific statement in `method`
108108
`lineinfo.line` should contain the line number of the statement at the time `method`
109109
was compiled. The current location is returned.
110110
"""
111-
function whereis(lineinfo, method::Method)
111+
whereis(lineinfo, method::Method) = whereis((lineinfo.file, lineinfo.line), method)
112+
function whereis((curfile, curline)::NTuple{2, Any}, method::Method)
112113
file, line1 = whereis(method)
113114
# We could be in an expanded macro. Apply the correction only if the filename checks out.
114115
# (We're not super-fastidious here because of symlinks and other path ambiguities)
115-
samefile = basename(file) == basename(String(lineinfo.file))
116+
samefile = basename(file) == basename(String(curfile))
116117
if !samefile
117-
return maybe_fix_path(String(lineinfo.file)), lineinfo.line
118+
return maybe_fix_path(String(curfile)), curline
118119
end
119-
return file, lineinfo.line - method.line + line1
120+
return file, curline - method.line + line1
120121
end
121122
function whereis(lineinfo::Core.LineInfoNode, method::Method)
122-
# With LineInfoNode we have certainty about whether we're in a macro expansion
123+
# With LineInfoNode we have certainty about whether we're in a macro expansion, but
124+
# we're still not guaranteed that the lineinfo points into the method otherwise
125+
# (e.g. from generated or programmatically defined methods)
123126
meth = lineinfo.method
124127
if isa(meth, WeakRef)
125128
meth = meth.value
126129
end
127130
if meth === Symbol("macro expansion")
128131
return maybe_fix_path(String(lineinfo.file)), lineinfo.line
129132
end
130-
file, line1 = whereis(method)
131-
return file, lineinfo.line - method.line + line1
133+
return whereis((lineinfo.file, lineinfo.line), method)
132134
end
133135

134136
"""

0 commit comments

Comments
 (0)