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

TypeError in getproperty on container types #32

Open
akirakyle opened this issue Nov 18, 2024 · 1 comment
Open

TypeError in getproperty on container types #32

akirakyle opened this issue Nov 18, 2024 · 1 comment

Comments

@akirakyle
Copy link

I think I may have stumbled upon a bug with getproperty on accessing container types, although I suspect it's actually a missing convert in the constructor's definition. For example

using Moshi.Data: @data

@data OptionVec{T} begin
    None
    struct Some
        n::T
        xs::Vector{Any}
    end
end

A = OptionVec.Some{String}("hi", [1, 2])
A.xs

gives the error

ERROR: TypeError: in typeassert, expected Vector{Any}, got a value of type Vector{Int64}
Stacktrace:
 [1] getproperty(value::Main.OptionVec.var"typeof(OptionVec)"{String}, name::Symbol)
   @ Main.OptionVec ~/.julia/packages/Moshi/SEGHC/src/data/emit/getproperty.jl:21
 [2] top-level scope
   @ REPL[5]:1

This can obviously be fixed by just converting before the constructor OptionVec.Some{String}("hi", Any[1, 2]) but I would expect the constructor to automatically handle the conversion to the specified variant's type?

@visr
Copy link
Contributor

visr commented Jan 25, 2025

After doing a macroexpand on a simple data type with Vector{Any}, I saw that the variant storage struct had an Any field instead of Vector{Any}, hence there was no automatic conversion from Vector{Int} to Vector{Any} on construction. The variant_fieldtypes however correctly shows Vector{Any}. This results is a mismatch in the typeassert in getproperty.

using Moshi.Data.Prelude

@data M begin
    V(Vector{Any})
end

v = M.V([5])

variant_fieldtypes(v)  # => (Vector{Any},)

dump(variant_storage(v))
Main.M.var"##Storage#V"
  ##field#230: Array{Int64}((1,)) [5]

getproperty(v, 1)  # => TypeError in typeassert

The reason for this any is that it gives up when encountering an Any type parameter like in Vector{Any}. Removing this line gets the desired behavior for this example:

Any in typevars && return Any # no need to specialize further

I don't quite understand what goes wrong though, the typeassert now is sometimes wrong in a different way, causing tests to fail. Probably related to self referencing data types?

julia> using Moshi

julia> r = Moshi.Match.Pattern.Row([]);

julia> r.xs
ERROR: TypeError: in typeassert, expected Vector{Moshi.Match.Pattern.var"typeof(Pattern)"}, got a value of type Vector{Any}
Stacktrace:
 [1] getproperty(value::Moshi.Match.Pattern.var"typeof(Pattern)", name::Symbol)
   @ Moshi.Match.Pattern a:\.julia\dev\Moshi\src\data\emit\getproperty.jl:21

julia> variant_fieldtypes(r)
(Vector{Moshi.Match.Pattern.var"typeof(Pattern)"},)

julia> dump(variant_storage(r))
Moshi.Match.Pattern.var"##Storage#Row"
  xs: Array{Any}((0,))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants