Julia: Bad codegen when trying to unify on Type{Union{...}}

Created on 15 Aug 2018  ·  3Comments  ·  Source: JuliaLang/julia

I'm not sure if this should work or not, but I'm surprised by the result:

julia> f(::Type{Union{X,Y}}) where {X,Y} = (X,Y)
f (generic function with 1 method)

julia> f(Union{Int64, Float64})
ERROR: UndefVarError: Y not defined
Stacktrace:
 [1] f(::Type{Union{Float64, Int64}}) at ./REPL[2]:1
 [2] top-level scope at none:0

julia> @code_warntype f(Union{Int64, Float64})
Body::Tuple{Type,Any}
1 1 ─ %1 = (Core.tuple)($(Expr(:static_parameter, 1)), $(Expr(:static_parameter, 2)))::Tuple{Type,Any}
  └──      return %1 

Most helpful comment

Yes. Actually, prior to v0.7 we did make methods uncallable if we couldn't determine type variable values, but we found that too complicated since applicability had an extra condition aside from just subtyping. You can also get undefined type variables in a case like:

f(x, y::Vararg{T}) where {T} = ...

If this function is called with 1 argument, T is undefined.

All 3 comments

All of those answers and results look correct to me

Could you expand on that? I would expect that either the type variables are bound or the method is uncallable. Are there other situations that can produce undef type variables?

Yes. Actually, prior to v0.7 we did make methods uncallable if we couldn't determine type variable values, but we found that too complicated since applicability had an extra condition aside from just subtyping. You can also get undefined type variables in a case like:

f(x, y::Vararg{T}) where {T} = ...

If this function is called with 1 argument, T is undefined.

Was this page helpful?
0 / 5 - 0 ratings