Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GC.@preserve does not work within a macro in Julia 1.4 #35391

Closed
dm3 opened this issue Apr 7, 2020 · 3 comments · Fixed by #35394
Closed

GC.@preserve does not work within a macro in Julia 1.4 #35391

dm3 opened this issue Apr 7, 2020 · 3 comments · Fixed by #35394
Assignees
Labels
macros @macros regression Regression in behavior compared to a previous version

Comments

@dm3
Copy link

dm3 commented Apr 7, 2020

As noted on Julia Slack #internals - https://julialang.slack.com/archives/C688QKS7Q/p1586239098050400.

Macro which used to work pre Julia-1.4 stopped working. Setting aside the useless invocation of GC.@preserve:

julia> macro transmute(tp, ptr, off)
            :(begin
                     v = $(esc(ptr))
                     GC.@preserve v unsafe_load(convert(Ptr{$(esc(tp))}, v+$(esc(off))), 1)
             end)
         end
julia> @transmute(Int32, pointer(xs), 0)
ERROR: syntax: invalid syntax (escape (outerref Int32))

@macroexpand result is different to 1.3 - doesn’t expand the GC.@preserve call which “loses” v
1.4:

julia> @macroexpand @transmute(Int32, pointer(xs), 0)
quote
    #= REPL[344]:3 =#
    var"#2452#v" = pointer(xs)
    #= REPL[344]:4 =#
    $(Expr(:gc_preserve, :(unsafe_load(convert(Ptr{$(Expr(:escape, :Int32))}, v + $(Expr(:escape, 0))), 1)), :v))
end

1.3:

julia> @macroexpand @transmute(Int32, pointer(xs), 0)
quote
    #= REPL[1]:3 =#
    var"#1#v" = pointer(xs)
    #= REPL[1]:4 =#
    begin
        #= gcutils.jl:90 =#
        var"#2###422" = $(Expr(:gc_preserve_begin, Symbol("#1#v")))
        #= gcutils.jl:91 =#
        var"#3###423" = Main.unsafe_load(Main.convert(Main.Ptr{Int32}, var"#1#v" + 0), 1)
        #= gcutils.jl:92 =#
        $(Expr(:gc_preserve_end, Symbol("##422")))
        #= gcutils.jl:93 =#
        var"#3###423"
    end
end

Haven't dug deeper but seems like this shouldn't have broken.

@maleadt
Copy link
Member

maleadt commented Apr 7, 2020

Reduced a little:

macro a(b)
    :(GC.@preserve ($(esc(b)),) )
end
@a 0

@yuyichao
Copy link
Contributor

yuyichao commented Apr 7, 2020

Unrelated to this issue but for the record the use of go preserve here is wrong. You never need to preserve a pointer, you preserve the owner of it.

@yuyichao
Copy link
Contributor

yuyichao commented Apr 7, 2020

Also the 1.3 output certainly looks wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
macros @macros regression Regression in behavior compared to a previous version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants