Skip to content

Commit

Permalink
Merge pull request #199 from FluxML/fix-isdef
Browse files Browse the repository at this point in the history
Fix isdef
  • Loading branch information
cstjean committed Dec 19, 2023
2 parents a266470 + 28bf092 commit 6b3034a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "MacroTools"
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
version = "0.5.11"
version = "0.5.12"

[deps]
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Expand Down
6 changes: 4 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ macro expand(ex)
end


"Test for function definition expressions."
isdef(ex) = isshortdef(ex) || longdef1(ex) !== nothing
"Test for function definition expressions. `function f end` and anonymous functions are considered
as function definitions and return true."
isdef(ex::Expr) = isshortdef(ex) || ex.head == :function || ex.head == :->
isdef(ex) = false

isshortdef(ex) = (@capture(ex, (fcall_ = body_)) &&
(@capture(gatherwheres(fcall)[1],
Expand Down
8 changes: 7 additions & 1 deletion test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ using MacroTools: isdef, flatten, striplines

ex6 = :(f(a) = a)
@test isdef(ex6)
ex7 = :(f(a)::Int == 1)
ex7 = :(f(a)::Int = 1)
@test isdef(ex7)
ex8 = :(f(a::T) where T = a)
@test isdef(ex8)
ex9 = :(f(a::T)::Int where T = 1)
@test isdef(ex9)
ex10 = :(f(a::S, b::T)::Union{S,T} where {S,T} = rand() < 0.5 ? a : b)
@test isdef(ex10)
@test !isdef(:(f()))
@test !isdef(:ix)
@test isdef(:(function f end)) # This is an arbitrary decision. Arguably it could be called a
# function declaration, and have `isdef` return false.
@test isdef(:(x -> x+2))
@test isdef(:(function (y) y - 4 end))
end

@testset "flatten" begin
Expand Down

2 comments on commit 6b3034a

@cstjean
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/97424

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.12 -m "<description of version>" 6b3034a5bbef280ab8b927cada9dd9c8a665d5d4
git push origin v0.5.12

Please sign in to comment.