# Locating a method defined through a macro

**URL:** <https://discourse.julialang.org/t/locating-a-method-defined-through-a-macro/51764>\
**Category:** General Usage\
**Created:** [December 13, 2020, 3:33pm UTC](https://discourse.julialang.org/t/locating-a-method-defined-through-a-macro/51764 "2020-12-13T15:33:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [December 13, 2020, 3:33pm UTC](https://discourse.julialang.org/t/locating-a-method-defined-through-a-macro/51764/1 "2020-12-13T15:33:29Z")

</div>

I’ve often encountered a problem figuring out where a method is defined when it’s definition happens through a macro. For example:

```julia
macro foo()
    :(bar() = x)
end

@foo()

bar()

```

This gives an `UndefVarError` with a stacktrace pointing to line 2 (inside the `foo` macro), but not showing line 5, where the definition was expanded from (usually, of course, it won’t be in the same file).  
How can I find this information? Is it even stored anywhere? It doesn’t seem to be anywhere in the `Method` object (`@which bar()`).

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [December 13, 2020, 5:45pm UTC](https://discourse.julialang.org/t/locating-a-method-defined-through-a-macro/51764/2 "2020-12-13T17:45:14Z")

</div>

you can make it work to do that, but it is not easy.  
And the user can’t do it, it needs to be written into the macro, to tell it that the location of code defined inside it is the location the macro was called from.

Here is how it is done in ChainRulesCore’s `@thunk` macro  
[https://github.com/JuliaDiff/ChainRulesCore.jl/blob/a1a8da5e5d8a3772d725a0a0292dd84b36c2e9b6/src/differentials/thunks.jl#L21-L26](https://github.com/JuliaDiff/ChainRulesCore.jl/blob/a1a8da5e5d8a3772d725a0a0292dd84b36c2e9b6/src/differentials/thunks.jl#L21-L26)  
the important bit is the insertion of ` __source__ ` into the output.

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [January 11, 2021, 10:51am UTC](https://discourse.julialang.org/t/locating-a-method-defined-through-a-macro/51764/3 "2021-01-11T10:51:13Z")

</div>

I just found an old-ish issue about this problem, which contains one useful hint: a `Method`’s `module` field is the macro’s caller, so at least that’s some starting point in looking for it.

[https://github.com/JuliaLang/julia/issues/31197](https://github.com/JuliaLang/julia/issues/31197)

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [January 12, 2021, 12:19pm UTC](https://discourse.julialang.org/t/locating-a-method-defined-through-a-macro/51764/4 "2021-01-12T12:19:45Z")

</div>

I’ve now created a package ([https://github.com/yha/FindDefinition.jl](https://github.com/yha/FindDefinition.jl)) for solving this problem, using hints from the above issue:

```julia
julia> using StatsPlots

julia> using FindDefinition

julia> methods(StatsPlots.qqplot)
# 1 method for generic function "qqplot":
[1] qqplot(args...; kw...) in StatsPlots at [...]\.julia\packages\RecipesBase\92zOw\src\RecipesBase.jl:357

julia> finddefs(StatsPlots.qqplot)
1-element Array{LineNumberNode,1}:
 :(#= [...]\.julia\packages\StatsPlots\IMY3F\src\distributions.jl:96 =#)

```
