Skip to content

Commit aa2acb7

Browse files
tkfdlfivefifty
authored andcommitted
Fix @~ macro for case a dot call is wrapping a normal call (#55)
1 parent 71e51a0 commit aa2acb7

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/lazymacro.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ is_call(ex::Expr) =
1717
ex.head == :call && !startswith(String(ex.args[1]), ".")
1818

1919
is_dotcall(ex::Expr) =
20-
ex.head == :. || (ex.head == :call && startswith(String(ex.args[1]), "."))
20+
(ex.head == :. && ex.args[2].head === :tuple) ||
21+
(ex.head == :call && startswith(String(ex.args[1]), "."))
2122
# e.g., `f.(x, y, z)` or `x .+ y .+ z`
2223

2324
lazy_expr(x) = x
@@ -40,11 +41,17 @@ end
4041
bc_expr_impl(x) = x
4142
function bc_expr_impl(ex::Expr)
4243
# walk down chain of dot calls
43-
if is_dotcall(ex)
44-
return Expr(ex.head,
45-
lazy_expr(ex.args[1]), # function name (`f`, `.+`, etc.)
46-
bc_expr_impl.(ex.args[2:end])...) # arguments
44+
if ex.head == :. && ex.args[2].head === :tuple
45+
@assert length(ex.args) == 2 # argument is always expressed as a tuple
46+
f = ex.args[1] # function name
47+
args = ex.args[2].args
48+
return Expr(ex.head, lazy_expr(f), Expr(:tuple, bc_expr_impl.(args)...))
49+
elseif ex.head == :call && startswith(String(ex.args[1]), ".")
50+
f = ex.args[1] # function name (e.g., `.+`)
51+
args = ex.args[2:end]
52+
return Expr(ex.head, lazy_expr(f), bc_expr_impl.(args)...)
4753
else
54+
@assert !is_dotcall(ex)
4855
return lazy_expr(ex)
4956
end
5057
end

test/macrotests.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module MacroTests
22

33
using Test, LazyArrays, MacroTools
4+
using Base.Broadcast: Broadcasted
45

56
A = randn(6, 6)
67
B = BroadcastArray(+, A, 2)
@@ -28,7 +29,7 @@ testparams = [
2829
@testset "$label" for (label, ex) in testparams
2930
desired = @eval $ex
3031
lazy = @eval @~ $ex
31-
@test lazy isa Union{Broadcast.Broadcasted, LazyArrays.Applied}
32+
@test lazy isa Union{Broadcasted, Applied}
3233

3334
@testset ".= @~ $label" begin
3435
actual = zero(desired)
@@ -57,4 +58,16 @@ testparams = [
5758
end
5859
end
5960

61+
@testset "@~ laziness" begin
62+
A = ones(1, 1)
63+
x = [1]
64+
65+
bc = @~ exp.(A * x)
66+
@test bc.args isa Tuple{Applied}
67+
68+
bc = @~ exp.(A * x .+ 1)
69+
@test bc.args isa Tuple{Broadcasted}
70+
@test bc.args[1].args isa Tuple{Applied, Int}
71+
end
72+
6073
end # module

0 commit comments

Comments
 (0)