Skip to content

Commit

Permalink
WIP: unpack TypeVars before subtyping with Union (fixes #11803, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jun 21, 2015
1 parent 462ac33 commit 41b60cc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ end

isvarargtype(t::ANY) = isa(t,DataType)&&is((t::DataType).name,Vararg.name)
isvatuple(t::DataType) = (n = length(t.parameters); n > 0 && isvarargtype(t.parameters[n]))
unwrapva(t::ANY) = isvarargtype(t) ? t.parameters[1] : t
unwrapva(t::ANY) = isa(t,TypeVar) ? unwrapva(t.ub) : (isvarargtype(t) ? t.parameters[1] : t)

@generated function tuple_type_tail{T<:Tuple}(::Type{T})
if isvatuple(T) && length(T.parameters) == 1
Expand Down
6 changes: 3 additions & 3 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1120,12 +1120,12 @@ function type_too_complex(t::ANY, d)
if d > MAX_TYPE_DEPTH
return true
end
if isa(t,Union)
if isa(t,TypeVar)
return type_too_complex(t.lb,d+1) || type_too_complex(t.ub,d+1)
elseif isa(t,Union)
p = t.types
elseif isa(t,DataType)
p = t.parameters
elseif isa(t,TypeVar)
return type_too_complex(t.lb,d+1) || type_too_complex(t.ub,d+1)
else
return false
end
Expand Down
35 changes: 18 additions & 17 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,24 @@ static int jl_subtype_le(jl_value_t *a, jl_value_t *b, int ta, int invariant)
return 1;
}

if (jl_is_typevar(a)) {
if (jl_is_typevar(b)) {
return
jl_subtype_le((jl_value_t*)((jl_tvar_t*)a)->ub,
(jl_value_t*)((jl_tvar_t*)b)->ub, 0, 0) &&
jl_subtype_le((jl_value_t*)((jl_tvar_t*)b)->lb,
(jl_value_t*)((jl_tvar_t*)a)->lb, 0, 0);
}
if (invariant) {
return 0;
}
return jl_subtype_le((jl_value_t*)((jl_tvar_t*)a)->ub, b, 0, 0);
}
if (jl_is_typevar(b)) {
return jl_subtype_le(a, (jl_value_t*)((jl_tvar_t*)b)->ub, 0, 0) &&
jl_subtype_le((jl_value_t*)((jl_tvar_t*)b)->lb, a, 0, 0);
}

size_t i;
if (!ta && jl_is_uniontype(a)) {
jl_svec_t *ap = ((jl_uniontype_t*)a)->types;
Expand Down Expand Up @@ -2477,23 +2495,6 @@ static int jl_subtype_le(jl_value_t *a, jl_value_t *b, int ta, int invariant)
return 0;
}

if (jl_is_typevar(a)) {
if (jl_is_typevar(b)) {
return
jl_subtype_le((jl_value_t*)((jl_tvar_t*)a)->ub,
(jl_value_t*)((jl_tvar_t*)b)->ub, 0, 0) &&
jl_subtype_le((jl_value_t*)((jl_tvar_t*)b)->lb,
(jl_value_t*)((jl_tvar_t*)a)->lb, 0, 0);
}
if (invariant) {
return 0;
}
return jl_subtype_le((jl_value_t*)((jl_tvar_t*)a)->ub, b, 0, 0);
}
if (jl_is_typevar(b)) {
return jl_subtype_le(a, (jl_value_t*)((jl_tvar_t*)b)->ub, 0, 0) &&
jl_subtype_le((jl_value_t*)((jl_tvar_t*)b)->lb, a, 0, 0);
}
if ((jl_datatype_t*)a == jl_any_type) return 0;

return jl_egal(a, b);
Expand Down
2 changes: 2 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ isnot(x,y) = !is(x,y)
@test !(Type{Bottom} <: Type{Int32})
@test !(Vector{Float64} <: Vector{Union{Float64,Float32}})
testintersect(Vector{Float64}, Vector{Union{Float64,Float32}}, Bottom)
@test TypeVar(:T,Int,true) <: Int
@test TypeVar(:T,Union(Int,Float64),true) <: Union(Int,Float64)

@test !isa(Array,Type{Any})
@test Type{Complex} <: DataType
Expand Down

0 comments on commit 41b60cc

Please sign in to comment.