FluxML / MacroTools.jl

MacroTools provides a library of tools for working with Julia code and expressions.
https://fluxml.ai/MacroTools.jl/stable/
Other
308 stars 77 forks source link

`isdef()` seems to return true in any case #172

Closed omlins closed 6 months ago

omlins commented 2 years ago
help?> MacroTools.isdef
  Test for function definition expressions.

julia> MacroTools.isdef(:(f() = 3))
true

julia> MacroTools.isdef(:(f()))
true

julia> MacroTools.isdef(:ix)
true
omlins commented 2 years ago

The function isdef is defined as follows (copied from here):

isdef(ex) = isshortdef(ex) || longdef1(ex) !== nothing

The problem is that longdef1(ex) !== nothing does not at all check if ex is a function: if ex is not matched as short function definitions within longdef1, it is returned unchanged (see here). As result, isdef(ex) only returns false in one single case: when ex is nothing.

In MacroTools.splitdef, the check if an expression is a function is done differently - and hopefully correctly - using longdef1 (copied from here):

@capture(longdef1(fdef), function (fcall_ | fcall_) body_ end)

I do not quite follow what the or-statement here ((fcall_ | fcall_)) is supposed to do though.

fingolfin commented 2 years ago

Duplicate of issue #154 ?