Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Accessors"
uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
authors = ["Takafumi Arakaki <[email protected]>", "Jan Weidner <[email protected]> and contributors"]
version = "0.1.4"
version = "0.1.5"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
15 changes: 3 additions & 12 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ See also [`opticmacro`](@ref), [`setmacro`](@ref).
function modifymacro(optictransform, f, obj_optic)
f = esc(f)
obj, optic = parse_obj_optic(obj_optic)
:(let
optic = $(optictransform)($optic)
($modify)($f, $obj, optic)
end)
:(($modify)($f, $obj, $(optictransform)($optic)))
end

foldtree(op, init, x) = op(init, x)
Expand Down Expand Up @@ -228,17 +225,11 @@ function setmacro(optictransform, ex::Expr; overwrite::Bool=false)
dst = overwrite ? obj : gensym("_")
val = esc(val)
ret = if ex.head == :(=)
quote
optic = ($optictransform)($optic)
$dst = $set($obj, optic, $val)
end
:($dst = $set($obj, ($optictransform)($optic), $val))
else
op = get_update_op(ex.head)
f = :($_UpdateOp($op,$val))
quote
optic = ($optictransform)($optic)
$dst = $modify($f, $obj, optic)
end
:($dst = $modify($f, $obj, ($optictransform)($optic)))
end
ret
end
Expand Down
31 changes: 31 additions & 0 deletions test/test_setmacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,35 @@ using StaticNumbers
@test m3 === @SMatrix[1 0; 0 0]
end

# inference

macro setfield156(expr)
# Example of macro that caused inference issues,
# see https://github.com/jw3126/Setfield.jl/pull/156
quote
function f($(esc(:x)))
$(Accessors.setmacro(identity, expr, overwrite=true))
$(Accessors.setmacro(identity, expr, overwrite=true))
$(Accessors.setmacro(identity, expr, overwrite=true))
$(Accessors.setmacro(identity, expr, overwrite=true))
$(Accessors.setmacro(identity, expr, overwrite=true))
return $(esc(:x))
end
end
end

function test_all_inferrable(f, argtypes)
# hacky, maybe JETTest can help?
# * JETTest.@test_nodispatch does not detect the problem
typed = first(code_typed(f, argtypes))
code = typed.first
@test all(T -> !(T isa UnionAll || T === Any), code.slottypes)
end

@testset "setmacro multiple usage" begin
let f = @setfield156(x[end] = 1)
test_all_inferrable(f, (Vector{Float64}, ))
end
end

end#module