Skip to content

Serialization of lambdas with kwargs fails #56815

@pablosanjose

Description

@pablosanjose

Serialization/deserialization of anonymous functions with kwargs fails (no error if we remove the kwarg; p = 1 below, or if we define f(; p = 1) = 0 instead of with a lambda)

MWE

julia> using Serialization

julia> f = (; p = 2) -> 0;

julia> serialize("test.dat", f)

julia> ff = deserialize("test.dat")
#1 (generic function with 1 method)

julia> ff()
ERROR: MethodError: no method matching var"#1#2"(::Int64, ::Serialization.__deserialized_types__.var"##230")
The function `#1#2` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  var"#1#2"(::Any, ::var"#1#3")
   @ Main REPL[2]:1

Stacktrace:
 [1] (::Serialization.__deserialized_types__.var"##230")()
   @ Main ./REPL[2]:1
 [2] top-level scope
   @ REPL[6]:1

Interestingly, if we do

julia> genf() = (; p = 2) -> 0; f = genf();

instead of f = (; p = 2) -> 0, we get no error, and the deserialized ff() evaluates correctly

The key difference between the two definitions is apparent in lowering, which may give a clue as to a possible cause/fix

julia> f = (; p = 2) -> 0; code_lowered(f)
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1%1 = Main.:(var"#17#18")
│   %2 = (%1)(2, #self#)
└──      return %2
)

julia> genf() = (; p = 2) -> 0; f = genf(); code_lowered(f)
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1%1 = Core.getfield(#self#, Symbol("#24#25"))%2 = (%1)(2, #self#)
└──      return %2
)

Thanks to @exaexa for his help in slack isolating the core issue (which affects Distributed in particular)!

Xref: JuliaLang/Distributed.jl#104
CC: @exaexa

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviorkeyword argumentsf(x; keyword=arguments)stdlibJulia's standard library

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions