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

Inlined dispatch uses incorrect (type-equivalent, but not egal) type #53445

Open
topolarity opened this issue Feb 23, 2024 · 2 comments
Open

Comments

@topolarity
Copy link
Member

topolarity commented Feb 23, 2024

is_union(::Type{T}) where T = "not Union!"
is_union(T::Union) = "Union!"

@inline f(i, nm, T) = (println(T); is_union(T))

applyfield(f, ::Type{T}, i) where T = f(i, fieldname(T, i)::Symbol, fieldtype(T, i))

struct Foo{I<:Union{Nothing,<:Number}} value::I end
struct Bar{I<:Union{Nothing,Number}} value::I end

The dispatch in f ends up "sticky":

julia> applyfield(f, Foo, 1)
Union{Nothing, var"#s1"} where var"#s1"<:Number
"not Union!"

julia> applyfield(f, Bar, 1)
Union{Nothing, Number}
"not Union!"

The dispatch in f ends up evaluated with a different (type-equivalent, but not egal) T.

The opposite results happen if you do Bar first then Foo.

@topolarity
Copy link
Member Author

Reduced from a JSON3.jl example that exhibits similar "stickiness":

using JSON3

struct Foo{I<:Union{Nothing,<:Number}}
  name::String
  value::I
end

serialized_ = JSON3.write(Foo("test", nothing))
deserialized_ = JSON3.read(serialized_, Foo) # if this runs first, both `read`s fail

struct Bar{I<:Union{Nothing,Number}}
  name::String
  value::I
end

serialized_ = JSON3.write(Bar("test", nothing))
deserialized_ = JSON3.read(serialized_, Bar) # if this runs first, both `read`s succeed

@mikmoore
Copy link
Contributor

A recent comment and the surrounding discussion seems somewhat related to the complicated and brittle handling of Union. I didn't really understand the problem or explanation, however, so I can't say anything smart here.

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