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

Use ismutabletype #322

Merged
merged 2 commits into from
Jun 3, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:
matrix:
version:
- '1.0'
- '1.5'
- '1'
- 'nightly'
os: [ubuntu-latest, windows-latest, macos-latest] # adjust according to need, e.g. os: [ubuntu-latest] if testing only on linux
arch:
- x64
Expand Down
13 changes: 13 additions & 0 deletions src/backwards_compatibility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ function read_array(f::JLDFile, dataspace::ReadDataspace,
v = read_array(f, dataspace, rrv, data_length, filter_id, NULL_REFERENCE, attributes)
String(v)
end


@static if VERSION < v"1.7"
# Location of `mutable` flag is moved from datatype to typename in julia v1.7
# Switch to using accessor function added in v1.7

# Borrowed from julia base
function ismutabletype(@nospecialize(t::Type))
t = unwrap_unionall(t)
# TODO: what to do for `Union`?
return isa(t, DataType) && t.mutable
end
end
14 changes: 7 additions & 7 deletions src/data/reconstructing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ jlconvert(::ReadRepresentation{Core.TypeofBottom,nothing}, f::JLDFile, ptr::Ptr,
# For bits types, we should always inline, because otherwise we'll just
# pass a lot of crap around in registers
push!(args, Expr(:meta, :inline))
elseif T.mutable
elseif ismutabletype(T)
push!(args, quote
obj = $(Expr(:new, T))
track_weakref!(f, header_offset, obj)
Expand All @@ -527,7 +527,7 @@ jlconvert(::ReadRepresentation{Core.TypeofBottom,nothing}, f::JLDFile, ptr::Ptr,
rtype = types[i]
odr = odrs[i]

if !T.mutable
if !ismutabletype(T)
fsym = Symbol("field_", fn[i])
push!(fsyms, fsym)
end
Expand All @@ -538,12 +538,12 @@ jlconvert(::ReadRepresentation{Core.TypeofBottom,nothing}, f::JLDFile, ptr::Ptr,
# Type is not stored or single instance
if T.types[i] == Union{}
# This cannot be defined
@assert !T.mutable
@assert !ismutabletype(T)
push!(args, Expr(:return, Expr(:new, T, fsyms[1:i-1]...)))
return blk
else
newi = Expr(:new, T.types[i])
if T.mutable
if ismutabletype(T)
fni = QuoteNode(fn[i])
push!(args, :(setfield!(obj, $fni, $newi)))
else
Expand All @@ -557,7 +557,7 @@ jlconvert(::ReadRepresentation{Core.TypeofBottom,nothing}, f::JLDFile, ptr::Ptr,
$(if T <: Tuple || i <= T.ninitialized
# Reference must always be initialized
:(throw(UndefinedFieldException(T,$(QuoteNode(fn[i])))))
elseif T.mutable
elseif ismutabletype(T)
# Reference could be uninitialized
:(return obj)
else
Expand All @@ -574,7 +574,7 @@ jlconvert(::ReadRepresentation{Core.TypeofBottom,nothing}, f::JLDFile, ptr::Ptr,
else
ttype = T.types[i]
fni = QuoteNode(fn[i])
if T.mutable
if ismutabletype(T)
push!(args, :(setfield!(obj, $fni,
convert($ttype, jlconvert($rr, f, ptr+$offset, NULL_REFERENCE)::$rtype)::$ttype)))
else
Expand All @@ -584,7 +584,7 @@ jlconvert(::ReadRepresentation{Core.TypeofBottom,nothing}, f::JLDFile, ptr::Ptr,
end
end

push!(args, T.mutable ? (:obj) : T <: Tuple ? Expr(:tuple, fsyms...) : Expr(:new, T, fsyms...))
push!(args, ismutabletype(T) ? (:obj) : T <: Tuple ? Expr(:tuple, fsyms...) : Expr(:new, T, fsyms...))

blk
end
8 changes: 4 additions & 4 deletions src/data/writing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ odr_sizeof(::ReadRepresentation{T,S}) where {T,S} = odr_sizeof(S)
T = T::DataType
T in encounteredtypes && return true
push!(encounteredtypes, T)
(T.mutable || T <: Type) && return true
(ismutabletype(T) || T <: Type) && return true
hasdata(T, encounteredtypes)
end

Expand Down Expand Up @@ -68,7 +68,7 @@ fieldnames(@nospecialize x) = collect(Base.fieldnames(x))
elseif isa(T, DataType)
if isbitstype(T)
return :(odr(T))
elseif !T.mutable
elseif !ismutabletype(T)
return :(initialized ? odr(T) : RelOffset)
end
end
Expand All @@ -83,7 +83,7 @@ end
if isconcretetype(T)
if !hasfielddata(T)
return nothing
elseif isbitstype(T) || (isa(initialized, Type{Type{Val{true}}}) && !T.mutable)
elseif isbitstype(T) || (isa(initialized, Type{Type{Val{true}}}) && !ismutabletype(T))
return quote
@lookup_committed f T
$(if isempty(T.types)
Expand Down Expand Up @@ -511,7 +511,7 @@ end
newstruct(T) = ccall(:jl_new_struct_uninit, Any, (Any,), T)

function newstruct(T, fields)
if !T.mutable
if !ismutabletype(T)
return ccall(:jl_new_structv, Any, (Any,Ptr{Cvoid},UInt32), T, fields, length(fields))
else
# Manual inline of newstruct! to work around bug
Expand Down