From 180ebde4bebc26ab905929cb5da600b420cd3e24 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Sun, 1 Aug 2021 09:42:36 +0200 Subject: [PATCH 1/2] make set more hygienic, see https://github.com/jw3126/Setfield.jl/pull/156 --- src/sugar.jl | 15 +++------------ test/test_setmacro.jl | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/sugar.jl b/src/sugar.jl index a7c36185..3d5494dd 100644 --- a/src/sugar.jl +++ b/src/sugar.jl @@ -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) @@ -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 diff --git a/test/test_setmacro.jl b/test/test_setmacro.jl index 4b1ebbbd..fae162c2 100644 --- a/test/test_setmacro.jl +++ b/test/test_setmacro.jl @@ -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 From 3392e761512d86fc13db74f4cf395178ea4ee1d2 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Sun, 1 Aug 2021 10:06:57 +0200 Subject: [PATCH 2/2] v0.1.5 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 58b05c04..3e844484 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Accessors" uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" authors = ["Takafumi Arakaki ", "Jan Weidner and contributors"] -version = "0.1.4" +version = "0.1.5" [deps] Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"