Skip to content

Commit 8596dbe

Browse files
authored
Add one-argument argtypes methods to source reflection functions (#58925)
Follow-up to #58891 (comment), extending the feature to `which`, `functionloc`, `edit` and `less`.
1 parent a355630 commit 8596dbe

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

base/methodshow.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ end
181181
Return a tuple `(filename,line)` giving the location of a generic `Function` definition.
182182
"""
183183
functionloc(@nospecialize(f), @nospecialize(types)) = functionloc(which(f,types))
184+
functionloc(@nospecialize(argtypes::Union{Tuple, Type{<:Tuple}})) = functionloc(which(argtypes))
184185

185186
function functionloc(@nospecialize(f))
186187
mt = methods(f)

base/reflection.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ Returns the method that would be called by the given type signature (as a tuple
960960
function which(@nospecialize(tt#=::Type=#))
961961
return _which(tt).method
962962
end
963+
which(@nospecialize(argtypes::Tuple)) = which(to_tuple_type(argtypes))
963964

964965
"""
965966
which(module, symbol)

stdlib/InteractiveUtils/src/editless.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ function edit(@nospecialize f)
269269
end
270270
edit(m::Method) = edit(functionloc(m)...)
271271
edit(@nospecialize(f), idx::Integer) = edit(methods(f).ms[idx])
272-
edit(f, t) = (@nospecialize; edit(functionloc(f, t)...))
272+
edit(f, t) = (@nospecialize; edit(functionloc(f, t)...))
273+
edit(@nospecialize argtypes::Union{Tuple, Type{<:Tuple}}) = edit(functionloc(argtypes)...)
273274
edit(file::Nothing, line::Integer) = error("could not find source file for function")
274275
edit(m::Module) = edit(pathof(m))
275276

stdlib/InteractiveUtils/test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,18 @@ catch err13464
329329
@test startswith(err13464.msg, "expression is not a function call")
330330
end
331331

332+
@testset "Single-argument forms" begin
333+
a = which(+, (Int, Int))
334+
b = which((typeof(+), Int, Int))
335+
c = which(Tuple{typeof(+), Int, Int})
336+
@test a == b == c
337+
338+
a = functionloc(+, (Int, Int))
339+
b = functionloc((typeof(+), Int, Int))
340+
c = functionloc(Tuple{typeof(+), Int, Int})
341+
@test a == b == c
342+
end
343+
332344
# PR 57909
333345
@testset "Support for type annotations as arguments" begin
334346
@test (@which (::Vector{Int})[::Int]).name === :getindex

0 commit comments

Comments
 (0)