Skip to content

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

@dm3

Description

@dm3

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.

Metadata

Metadata

Assignees

Labels

macros@macrosregressionRegression in behavior compared to a previous version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions