diff --git a/src/ConstructionBase.jl b/src/ConstructionBase.jl index 546eae5..1ec59bb 100644 --- a/src/ConstructionBase.jl +++ b/src/ConstructionBase.jl @@ -48,26 +48,34 @@ getfields(x::NamedTuple) = x getproperties(o::NamedTuple) = o getproperties(o::Tuple) = o +const CAN_CHECK_PROPERTIES_ARE_FIELDS = VERSION < v"1.10-" + function is_propertynames_overloaded(T::Type)::Bool which(propertynames, Tuple{T}).sig !== Tuple{typeof(propertynames), Any} end -@generated function check_properties_are_fields(obj) - if is_propertynames_overloaded(obj) - return quote - T = typeof(obj) - msg = """ - 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) +if CAN_CHECK_PROPERTIES_ARE_FIELDS + @generated function check_properties_are_fields(obj) + if is_propertynames_overloaded(obj) + return quote + T = typeof(obj) + msg = """ + 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 - else - :(nothing) + end +else + function check_properties_are_fields(obj) + nothing end end diff --git a/test/runtests.jl b/test/runtests.jl index fcc236a..7e59425 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -271,25 +271,27 @@ Base.getproperty(obj::FieldProps, name::Symbol) = getproperty(getfield(obj, :com x = FieldProps((a=1, b=:b)) @test constructorof(typeof(x)) === FieldProps @test getfields(x) === (components=(a=1, b=:b),) - res = @test_throws ErrorException setproperties(x, c=0) - msg = sprint(showerror, res.value) - @test occursin("overload", msg) - @test occursin("setproperties", msg) - @test occursin("FieldProps", msg) - @test_throws ErrorException setproperties(x, components=(a=1,b=:b)) - msg = sprint(showerror, res.value) - @test occursin("overload", msg) - @test occursin("setproperties", msg) - @test occursin("FieldProps", msg) - @test_throws ErrorException setproperties(x, a="aaa") - msg = sprint(showerror, res.value) - @test occursin("overload", msg) - @test occursin("setproperties", msg) - @test occursin("FieldProps", msg) + if ConstructionBase.CAN_CHECK_PROPERTIES_ARE_FIELDS + res = @test_throws ErrorException setproperties(x, c=0) + msg = sprint(showerror, res.value) + @test occursin("overload", msg) + @test occursin("setproperties", msg) + @test occursin("FieldProps", msg) + @test_throws ErrorException setproperties(x, components=(a=1,b=:b)) + msg = sprint(showerror, res.value) + @test occursin("overload", msg) + @test occursin("setproperties", msg) + @test occursin("FieldProps", msg) + @test_throws ErrorException setproperties(x, a="aaa") + msg = sprint(showerror, res.value) + @test occursin("overload", msg) + @test occursin("setproperties", msg) + @test occursin("FieldProps", msg) + end # == FieldProps((a="aaa", b=:b) if VERSION >= v"1.7" @test getproperties(x) == (a=1, b=:b) - else + elseif ConstructionBase.CAN_CHECK_PROPERTIES_ARE_FIELDS res = @test_throws ErrorException getproperties(x) msg = sprint(showerror, res.value) @test occursin("overload", msg)