diff --git a/src/data/writing_datatypes.jl b/src/data/writing_datatypes.jl index 68570202..91c9f9f4 100644 --- a/src/data/writing_datatypes.jl +++ b/src/data/writing_datatypes.jl @@ -446,6 +446,11 @@ end function h5convert!(out::Pointers, ::DataTypeODR, f::JLDFile, T::DataType, wsession::JLDWriteSession) t = typename(T) + if T <: Function && isgensym(nameof(T.instance)) + @warn LazyString("Attempting to store ", T, ".\n", + "JLD2 only stores functions by name.\n", + " This may not be useful for anonymous functions.") + end store_vlen!(out, UInt8, f, unsafe_wrap(Vector{UInt8}, t), f.datatype_wsession) if isempty(T.parameters) h5convert_uninitialized!(out+odr_sizeof(Vlen{UInt8}), Vlen{UInt8}) @@ -627,9 +632,6 @@ end # fieldodr, but actually encoding the data for things that odr stores # as references @nospecializeinfer function odr(@nospecialize(T::Type)) - if T <: Function - @warn LazyString("Attempting to store ", T, ".\n Function types cannot be propertly stored in JLD2 files.\n Loading may yield unexpected results.") - end if !hasdata(T) # A pointer singleton or ghost. We need to write something, but we'll # just write a single byte. diff --git a/src/julia_compat.jl b/src/julia_compat.jl index f17e348e..a577bebb 100644 --- a/src/julia_compat.jl +++ b/src/julia_compat.jl @@ -28,4 +28,10 @@ end end else using Base: @nospecializeinfer +end + +@static if VERSION < v"1.7.0" + isgensym(s::Symbol) = '#' in string(s) +else + using Base: isgensym end \ No newline at end of file diff --git a/test/loadsave.jl b/test/loadsave.jl index ec87d90b..943ebc3c 100644 --- a/test/loadsave.jl +++ b/test/loadsave.jl @@ -764,14 +764,7 @@ end eval(:(module ModuleWithFunction fun(x) = x+1 end)) - if VERSION ≥ v"1.7" - @test_warn( - contains("Function types cannot"), - eval(:(save_object("test.jld2", (1, ModuleWithFunction.fun, 2)))) - ) - else - eval(:(save_object("test.jld2", (1, ModuleWithFunction.fun, 2)))) - end + eval(:(save_object("test.jld2", (1, ModuleWithFunction.fun, 2)))) obj = load_object("test.jld2") @test length(obj) == 3 @test obj[1] == 1