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

only warn for anon functions #597

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/data/writing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,11 @@

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",

Check warning on line 450 in src/data/writing_datatypes.jl

View check run for this annotation

Codecov / codecov/patch

src/data/writing_datatypes.jl#L450

Added line #L450 was not covered by tests
"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})
Expand Down Expand Up @@ -627,9 +632,6 @@
# 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.
Expand Down
6 changes: 6 additions & 0 deletions src/julia_compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 1 addition & 8 deletions test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading