Skip to content

Commit 6b3034a

Browse files
authored
Merge pull request #199 from FluxML/fix-isdef
Fix isdef
2 parents a266470 + 28bf092 commit 6b3034a

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "MacroTools"
22
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
3-
version = "0.5.11"
3+
version = "0.5.12"
44

55
[deps]
66
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"

src/utils.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,10 @@ macro expand(ex)
224224
end
225225

226226

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

230232
isshortdef(ex) = (@capture(ex, (fcall_ = body_)) &&
231233
(@capture(gatherwheres(fcall)[1],

test/utils.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ using MacroTools: isdef, flatten, striplines
1414

1515
ex6 = :(f(a) = a)
1616
@test isdef(ex6)
17-
ex7 = :(f(a)::Int == 1)
17+
ex7 = :(f(a)::Int = 1)
1818
@test isdef(ex7)
1919
ex8 = :(f(a::T) where T = a)
2020
@test isdef(ex8)
2121
ex9 = :(f(a::T)::Int where T = 1)
2222
@test isdef(ex9)
2323
ex10 = :(f(a::S, b::T)::Union{S,T} where {S,T} = rand() < 0.5 ? a : b)
2424
@test isdef(ex10)
25+
@test !isdef(:(f()))
26+
@test !isdef(:ix)
27+
@test isdef(:(function f end)) # This is an arbitrary decision. Arguably it could be called a
28+
# function declaration, and have `isdef` return false.
29+
@test isdef(:(x -> x+2))
30+
@test isdef(:(function (y) y - 4 end))
2531
end
2632

2733
@testset "flatten" begin

0 commit comments

Comments
 (0)