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

Serialization of lambdas with kwargs fails #56815

Open
pablosanjose opened this issue Dec 12, 2024 · 1 comment
Open

Serialization of lambdas with kwargs fails #56815

pablosanjose opened this issue Dec 12, 2024 · 1 comment
Labels
bug Indicates an unexpected problem or unintended behavior keyword arguments f(x; keyword=arguments) stdlib Julia's standard library

Comments

@pablosanjose
Copy link
Contributor

pablosanjose commented Dec 12, 2024

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

@exaexa
Copy link
Contributor

exaexa commented Dec 12, 2024

Long story short about what we think is the core reason (see the other issue for details):

  • serialize on a lambda with keyword arguments actually tries to serialize the Core.kwcall wrapper method
  • it only sees symbols "pointing" to the actual function body (that one is represented by the var"#24#25" etc. in the code above)
  • it serializes the symbols as-is and cares no longer

No real idea on how to fix this tho.

@nsajko nsajko added keyword arguments f(x; keyword=arguments) stdlib Julia's standard library bug Indicates an unexpected problem or unintended behavior labels Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior keyword arguments f(x; keyword=arguments) stdlib Julia's standard library
Projects
None yet
Development

No branches or pull requests

3 participants