Skip to content

Commit 09507d7

Browse files
committed
Fix JuliaLang#3377. It allows for writing @methods @time 1+1, `@functionloc @time
1+1` plus the equivalent for `@which`, `@edit` and `@less`. The macros check for method specialization of the macro and return the dispatched macro. The `@functionloc` macro is exported and the documentation is updated.
1 parent b3b1411 commit 09507d7

File tree

5 files changed

+116
-77
lines changed

5 files changed

+116
-77
lines changed

base/docs/helpdb/Base.jl

-65
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,6 @@ Bessel function of the first kind of order `nu`, ``J_\\nu(x)``.
218218
"""
219219
besselj
220220

221-
"""
222-
@code_lowered
223-
224-
Evaluates the arguments to the function call, determines their types, and calls
225-
[`code_lowered`](:func:`code_lowered`) on the resulting expression.
226-
"""
227-
:@code_lowered
228-
229221
"""
230222
//(num, den)
231223
@@ -723,14 +715,6 @@ keyword arguments `addprocs` was called with.
723715
"""
724716
launch
725717

726-
"""
727-
@code_typed
728-
729-
Evaluates the arguments to the function call, determines their types, and calls
730-
[`code_typed`](:func:`code_typed`) on the resulting expression.
731-
"""
732-
:@code_typed
733-
734718
"""
735719
invdigamma(x)
736720
@@ -3507,13 +3491,6 @@ Returns the lower triangle of `M` starting from the `k`th superdiagonal.
35073491
"""
35083492
tril(M,k)
35093493

3510-
"""
3511-
@edit
3512-
3513-
Evaluates the arguments to the function call, determines their types, and calls the `edit`
3514-
function on the resulting expression.
3515-
"""
3516-
:@edit
35173494

35183495
"""
35193496
subtypes(T::DataType)
@@ -3628,14 +3605,6 @@ not representable.
36283605
"""
36293606
trunc
36303607

3631-
"""
3632-
@less
3633-
3634-
Evaluates the arguments to the function call, determines their types, and calls the `less`
3635-
function on the resulting expression.
3636-
"""
3637-
:@less
3638-
36393608
"""
36403609
broadcast_function(f)
36413610
@@ -5583,14 +5552,6 @@ Returns `string` with the first character converted to lowercase.
55835552
"""
55845553
lcfirst
55855554

5586-
"""
5587-
@code_native
5588-
5589-
Evaluates the arguments to the function call, determines their types, and calls
5590-
[`code_native`](:func:`code_native`) on the resulting expression.
5591-
"""
5592-
:@code_native
5593-
55945555
"""
55955556
flipbits!(B::BitArray{N}) -> BitArray{N}
55965557
@@ -5605,14 +5566,6 @@ Returns the value of a symbolic link `path`.
56055566
"""
56065567
readlink
56075568

5608-
"""
5609-
@code_warntype
5610-
5611-
Evaluates the arguments to the function call, determines their types, and calls
5612-
[`code_warntype`](:func:`code_warntype`) on the resulting expression.
5613-
"""
5614-
:@code_warntype
5615-
56165569
"""
56175570
deg2rad(x)
56185571
@@ -8299,16 +8252,6 @@ Converts a packed boolean array to an array of booleans.
82998252
"""
83008253
bitunpack
83018254

8302-
"""
8303-
@which
8304-
8305-
Applied to a function call, it evaluates the arguments to the specified function call, and
8306-
returns the `Method` object for the method that would be called for those arguments. Applied
8307-
to a variable, it returns the module in which the variable was bound. It calls out to the
8308-
`which` function.
8309-
"""
8310-
:@which
8311-
83128255
"""
83138256
size(A, [dim...])
83148257
@@ -8496,14 +8439,6 @@ address will be either IPv4 or IPv6 as appropriate.
84968439
"""
84978440
recvfrom
84988441

8499-
"""
8500-
@code_llvm
8501-
8502-
Evaluates the arguments to the function call, determines their types, and calls
8503-
[`code_llvm`](:func:`code_llvm`) on the resulting expression.
8504-
"""
8505-
:@code_llvm
8506-
85078442
"""
85088443
nextfloat(f)
85098444

base/exports.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,7 @@ export
13861386
# reflection
13871387
@which,
13881388
@edit,
1389+
@functionloc,
13891390
@less,
13901391
@code_typed,
13911392
@code_warntype,

base/interactiveutil.jl

+82-4
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,12 @@ function gen_call_with_extracted_types(fcn, ex0)
254254
Expr(:call, typesof, map(esc, args[2:end])...))
255255
end
256256
exret = Expr(:none)
257+
is_macro = false
257258
ex = expand(ex0)
258-
if !isa(ex, Expr)
259+
if isa(ex0, Expr) && ex0.head == :macrocall # Make @edit @time 1+2 edit the macro
260+
is_macro = true
261+
exret = Expr(:call, fcn, esc(ex0.args[1]), typesof(ex0.args[2:end]...))
262+
elseif !isa(ex, Expr)
259263
exret = Expr(:call, :error, "expression is not a function call or symbol")
260264
elseif ex.head == :call
261265
if any(e->(isa(e, Expr) && e.head==:(...)), ex0.args) &&
@@ -278,15 +282,15 @@ function gen_call_with_extracted_types(fcn, ex0)
278282
end
279283
end
280284
end
281-
if ex.head == :thunk || exret.head == :none
285+
if (!is_macro && ex.head == :thunk) || exret.head == :none
282286
exret = Expr(:call, :error, "expression is not a function call, "
283287
* "or is too complex for @$fcn to analyze; "
284288
* "break it down to simpler parts if possible")
285289
end
286290
exret
287291
end
288292

289-
for fname in [:which, :less, :edit, :code_typed, :code_warntype,
293+
for fname in [:which, :less, :edit, :functionloc, :code_typed, :code_warntype,
290294
:code_lowered, :code_llvm, :code_llvm_raw, :code_native]
291295
@eval begin
292296
macro ($fname)(ex0)
@@ -295,7 +299,80 @@ for fname in [:which, :less, :edit, :code_typed, :code_warntype,
295299
end
296300
end
297301

298-
# `methodswith` -- shows a list of methods using the type given
302+
"""
303+
@which
304+
305+
Applied to a function or macro call, it evaluates the arguments to the specified call, and
306+
returns the `Method` object for the method that would be called for those arguments. Applied
307+
to a variable, it returns the module in which the variable was bound. It calls out to the
308+
`which` function.
309+
"""
310+
:@which
311+
312+
"""
313+
@less
314+
315+
Evaluates the arguments to the function or macro call, determines their types, and calls the `less`
316+
function on the resulting expression.
317+
"""
318+
:@less
319+
320+
"""
321+
@edit
322+
323+
Evaluates the arguments to the function or macro call, determines their types, and calls the `edit`
324+
function on the resulting expression.
325+
"""
326+
:@edit
327+
328+
"""
329+
@functionloc
330+
331+
Applied to a function or macro call, it evaluates the arguments to the specified call, and
332+
returns a tuple `(filename,line)` giving the location for the method that would be called for those arguments.
333+
It calls out to the `functionloc` function.
334+
"""
335+
:@functionloc
336+
337+
"""
338+
@code_typed
339+
340+
Evaluates the arguments to the function or macro call, determines their types, and calls
341+
[`code_typed`](:func:`code_typed`) on the resulting expression.
342+
"""
343+
:@code_typed
344+
345+
"""
346+
@code_warntype
347+
348+
Evaluates the arguments to the function or macro call, determines their types, and calls
349+
[`code_warntype`](:func:`code_warntype`) on the resulting expression.
350+
"""
351+
:@code_warntype
352+
353+
"""
354+
@code_lowered
355+
356+
Evaluates the arguments to the function or macro call, determines their types, and calls
357+
[`code_lowered`](:func:`code_lowered`) on the resulting expression.
358+
"""
359+
:@code_lowered
360+
361+
"""
362+
@code_llvm
363+
364+
Evaluates the arguments to the function or macro call, determines their types, and calls
365+
[`code_llvm`](:func:`code_llvm`) on the resulting expression.
366+
"""
367+
:@code_llvm
368+
369+
"""
370+
@code_native
371+
372+
Evaluates the arguments to the function or macro call, determines their types, and calls
373+
[`code_native`](:func:`code_native`) on the resulting expression.
374+
"""
375+
:@code_native
299376

300377
function type_close_enough(x::ANY, t::ANY)
301378
x == t && return true
@@ -304,6 +381,7 @@ function type_close_enough(x::ANY, t::ANY)
304381
(isa(x,Union) && isa(t,DataType) && any(u -> is(u,t), x.types))
305382
end
306383

384+
# `methodswith` -- shows a list of methods using the type given
307385
function methodswith(t::Type, f::Function, showparents::Bool=false, meths = Method[])
308386
mt = typeof(f).name.mt
309387
d = mt.defs

doc/stdlib/base.rst

+14-8
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Getting Around
8080

8181
.. Docstring generated from Julia source
8282
83-
Evaluates the arguments to the function call, determines their types, and calls the ``edit`` function on the resulting expression.
83+
Evaluates the arguments to the function or macro call, determines their types, and calls the ``edit`` function on the resulting expression.
8484

8585
.. function:: less(file::AbstractString, [line])
8686

@@ -98,7 +98,7 @@ Getting Around
9898

9999
.. Docstring generated from Julia source
100100
101-
Evaluates the arguments to the function call, determines their types, and calls the ``less`` function on the resulting expression.
101+
Evaluates the arguments to the function or macro call, determines their types, and calls the ``less`` function on the resulting expression.
102102

103103
.. function:: clipboard(x)
104104

@@ -188,7 +188,7 @@ Getting Around
188188

189189
.. Docstring generated from Julia source
190190
191-
Applied to a function call, it evaluates the arguments to the specified function call, and returns the ``Method`` object for the method that would be called for those arguments. Applied to a variable, it returns the module in which the variable was bound. It calls out to the ``which`` function.
191+
Applied to a function or macro call, it evaluates the arguments to the specified call, and returns the ``Method`` object for the method that would be called for those arguments. Applied to a variable, it returns the module in which the variable was bound. It calls out to the ``which`` function.
192192

193193
.. function:: methods(f, [types])
194194

@@ -1314,6 +1314,12 @@ Reflection
13141314
13151315
Returns a tuple ``(filename,line)`` giving the location of a ``Method`` definition.
13161316

1317+
.. function:: @functionloc
1318+
1319+
.. Docstring generated from Julia source
1320+
1321+
Applied to a function or macro call, it evaluates the arguments to the specified call, and returns a tuple ``(filename,line)`` giving the location for the method that would be called for those arguments. It calls out to the ``functionloc`` function.
1322+
13171323
Internals
13181324
---------
13191325

@@ -1351,7 +1357,7 @@ Internals
13511357

13521358
.. Docstring generated from Julia source
13531359
1354-
Evaluates the arguments to the function call, determines their types, and calls :func:`code_lowered` on the resulting expression.
1360+
Evaluates the arguments to the function or macro call, determines their types, and calls :func:`code_lowered` on the resulting expression.
13551361

13561362
.. function:: code_typed(f, types; optimize=true)
13571363

@@ -1363,7 +1369,7 @@ Internals
13631369

13641370
.. Docstring generated from Julia source
13651371
1366-
Evaluates the arguments to the function call, determines their types, and calls :func:`code_typed` on the resulting expression.
1372+
Evaluates the arguments to the function or macro call, determines their types, and calls :func:`code_typed` on the resulting expression.
13671373

13681374
.. function:: code_warntype(f, types)
13691375

@@ -1375,7 +1381,7 @@ Internals
13751381

13761382
.. Docstring generated from Julia source
13771383
1378-
Evaluates the arguments to the function call, determines their types, and calls :func:`code_warntype` on the resulting expression.
1384+
Evaluates the arguments to the function or macro call, determines their types, and calls :func:`code_warntype` on the resulting expression.
13791385

13801386
.. function:: code_llvm(f, types)
13811387

@@ -1389,7 +1395,7 @@ Internals
13891395

13901396
.. Docstring generated from Julia source
13911397
1392-
Evaluates the arguments to the function call, determines their types, and calls :func:`code_llvm` on the resulting expression.
1398+
Evaluates the arguments to the function or macro call, determines their types, and calls :func:`code_llvm` on the resulting expression.
13931399

13941400
.. function:: code_native(f, types)
13951401

@@ -1401,7 +1407,7 @@ Internals
14011407

14021408
.. Docstring generated from Julia source
14031409
1404-
Evaluates the arguments to the function call, determines their types, and calls :func:`code_native` on the resulting expression.
1410+
Evaluates the arguments to the function or macro call, determines their types, and calls :func:`code_native` on the resulting expression.
14051411

14061412
.. function:: precompile(f,args::Tuple{Vararg{Any}})
14071413

test/reflection.jl

+19
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,22 @@ end
248248
end
249249
end
250250
@test functionloc(f15447)[2] > 0
251+
252+
module MacroTest
253+
export @macrotest
254+
macro macrotest(x::Int, y::Symbol) end
255+
macro macrotest(x::Int, y::Int)
256+
nothing #This is here because of #15280
257+
end
258+
end
259+
260+
let
261+
using MacroTest
262+
a = 1
263+
m = getfield(current_module(), Symbol("@macrotest"))
264+
@test which(m, Tuple{Int,Symbol})==@which @macrotest 1 a
265+
@test which(m, Tuple{Int,Int})==@which @macrotest 1 1
266+
267+
@test methods(m,Tuple{Int, Int})[1]==@which MacroTest.@macrotest 1 1
268+
@test functionloc(@which @macrotest 1 1) == @functionloc @macrotest 1 1
269+
end

0 commit comments

Comments
 (0)