From 879a9088195d355a89a51d47a96014a2d78dc03c Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sat, 10 Jun 2023 14:50:27 -0600 Subject: [PATCH] `getproperty` bugfix (#45) * Some fixes for type constrained parameters * use `[]` for lists of similar variants in `@cases` * README fixes * compat fix * make coverage happy * avoid getproperty * bump version --- Project.toml | 2 +- src/compute_storage.jl | 4 ++-- test/runtests.jl | 10 +++++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index 55bb8d6..5c2a75c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SumTypes" uuid = "8e1ec7a9-0e02-4297-b0fe-6433085c89f2" authors = ["MasonProtter "] -version = "0.4.7" +version = "0.4.8" [deps] MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" diff --git a/src/compute_storage.jl b/src/compute_storage.jl index 9b52539..361649d 100644 --- a/src/compute_storage.jl +++ b/src/compute_storage.jl @@ -127,10 +127,10 @@ unwrap(x::ST, var) where {ST} = unwrap(x, var, variants_Tuple(ST)) bit_sigs = nt.bit_sigs quote names = ($(QuoteNode.(bit_names[i])...),) - bits = unsafe_padded_reinterpret(Variant{names, Tuple{$(bit_sigs[i]...)}}, x.bits) + bits = unsafe_padded_reinterpret(Variant{names, Tuple{$(bit_sigs[i]...)}}, getfield(x, :bits)) args = $(Expr(:tuple, (bit_names[i][j] ∈ ptr_names[i] ? let k = findfirst(x -> x == bit_names[i][j], ptr_names[i]) - :(x.ptrs[$k]:: $(ptrss[i][k][2])) + :(getfield(x, :ptrs)[$k]:: $(ptrss[i][k][2])) end : :(bits.data[$j]) for j ∈ eachindex(bit_names[i]))...)) Variant{names, $(Var.parameters[2])}(args) end diff --git a/test/runtests.jl b/test/runtests.jl index 0622702..5782663 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -23,6 +23,10 @@ function log_nothrow(x::T)::Result{T} where{T<:AbstractFloat} Success(log(x)) end +Base.getproperty(f::Foo, s::Symbol) = error("Don't do that!") +Base.getproperty(f::Either, s::Symbol) = error("Don't do that!") +Base.getproperty(f::Result, s::Symbol) = error("Don't do that!") + #------------------- @testset "Basics " begin @test SumTypes.is_sumtype(Int) == false @@ -163,6 +167,7 @@ end Cons{A}(::A, ::List) end Cons(x::A, y::List{Uninit}) where {A} = Cons(x, List{A}(y)) +Base.getproperty(f::List, s::Symbol) = error("Don't do that!") List(first, rest...) = Cons(first, List(rest...)) List() = Nil @@ -216,6 +221,7 @@ end C(common_field::Int, b::Float64, d::Bool, e::Float64, k::Complex{Real}) D(common_field::Int, b::Any) end +Base.getproperty(f::AT, s::Symbol) = error("Don't do that!") A(;common=1, a=true, b=10) = A(common, a, b) B(;common=1, a=1, b=1.0, d=1 + 1.0im) = B(common, a, b, d) @@ -350,7 +356,6 @@ foo(x::Foo) = @cases x begin D((_, x)) => x end - @sum_type Re begin Empty Class(::UInt8) @@ -367,8 +372,7 @@ count_classes(r::Re, c=0) = @cases r begin Rep(x) => c + count_classes(x) [Alt, Cat, Diff, And](x, y) => c + count_classes(x) + count_classes(y) end; - - + @testset "Collection of variants" begin @test foo(A(1, 1)) == 2 @test foo(B(1, 1.5)) == 2.5