Skip to content

Commit

Permalink
Adapt to upstream changes wrt. generated function world age handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Feb 23, 2023
1 parent 4d661f8 commit bdb7807
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/ConstructionBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,46 @@ getfields(x::NamedTuple) = x
getproperties(o::NamedTuple) = o
getproperties(o::Tuple) = o

function is_propertynames_overloaded(T::Type)::Bool
which(propertynames, Tuple{T}).sig !== Tuple{typeof(propertynames), Any}
@static if fieldcount(Core.GeneratedFunctionStub) == 3

# after JuliaLang/julia#48611, generated functions should respect world ages

function is_propertynames_overloaded_generator(world::UInt, source, self, obj)
sig = Base.signature_type(propertynames, Tuple{obj})
m = Base._which(sig; world)
has_overload = m.method.sig !== Tuple{typeof(propertynames), Any}
stub = Core.GeneratedFunctionStub(identity, Core.svec(:is_propertynames_overloaded, :obj), Core.svec())
stub(world, source, :(return $has_overload))
end

@eval function is_propertynames_overloaded(obj)
$(Expr(:meta, :generated_only))
$(Expr(:meta, :generated, is_propertynames_overloaded_generator))
end

else

@generated function is_propertynames_overloaded(obj)
sig = Base.signature_type(propertynames, Tuple{obj})
m = Base._which(sig)
has_overload = m.method.sig !== Tuple{typeof(propertynames), Any}
:(return $has_overload)
end

end

@generated function check_properties_are_fields(obj)

function check_properties_are_fields(obj::T) where T
if is_propertynames_overloaded(obj)
return quote
T = typeof(obj)
msg = """
error("""
The function `Base.propertynames` was overloaded for type `$T`.
Please make sure the following methods are also overloaded for this type:
```julia
ConstructionBase.setproperties
ConstructionBase.getproperties # optional in VERSION >= julia v1.7
```
"""
error(msg)
end
else
:(nothing)
```""")
end
return
end

# names are consecutive integers: return tuple
Expand Down

0 comments on commit bdb7807

Please sign in to comment.