Skip to content

Commit 1e12a48

Browse files
committed
add type checks to UnionAll and TypeVar constructors
1 parent df01d34 commit 1e12a48

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

base/inference.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ immutable Const
6363
Const(v::ANY) = new(v)
6464
end
6565

66+
function rewrap(t::ANY, u::ANY)
67+
isa(t, Const) && return t
68+
rewrap_unionall(t, u)
69+
end
70+
6671
type InferenceState
6772
sp::SimpleVector # static parameters
6873
label_counter::Int # index of the current highest label for this function
@@ -149,9 +154,9 @@ type InferenceState
149154
end
150155
s_types[1][la] = VarState(Tuple, false)
151156
else
152-
s_types[1][la] = VarState(rewrap_unionall(tuple_tfunc(limit_tuple_depth(params,
153-
tupletype_tail(atypes, la))),
154-
linfo.specTypes),
157+
s_types[1][la] = VarState(rewrap(tuple_tfunc(limit_tuple_depth(params,
158+
tupletype_tail(atypes, la))),
159+
linfo.specTypes),
155160
false)
156161
end
157162
la -= 1
@@ -572,8 +577,8 @@ function getfield_tfunc(s00::ANY, name)
572577
end
573578
s = DataType # typeof(p1)
574579
elseif isa(s,Union)
575-
return rewrap_unionall(tmerge(getfield_tfunc(s.a, name), getfield_tfunc(s.b, name)),
576-
s00)
580+
return rewrap(tmerge(getfield_tfunc(s.a, name), getfield_tfunc(s.b, name)),
581+
s00)
577582
elseif isa(s,Const)
578583
sv = s.val
579584
if isa(name, Const)

src/jltypes.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ JL_DLLEXPORT jl_value_t *jl_type_union(jl_value_t **ts, size_t n)
309309

310310
JL_DLLEXPORT jl_tvar_t *jl_new_typevar(jl_sym_t *name, jl_value_t *lb, jl_value_t *ub)
311311
{
312+
if (lb != jl_bottom_type && !jl_is_type(lb) && !jl_is_typevar(lb))
313+
jl_type_error_rt("TypeVar", "lower bound", (jl_value_t*)jl_type_type, lb);
314+
if (ub != (jl_value_t*)jl_any_type && !jl_is_type(ub) && !jl_is_typevar(ub))
315+
jl_type_error_rt("TypeVar", "upper bound", (jl_value_t*)jl_type_type, ub);
312316
jl_ptls_t ptls = jl_get_ptls_states();
313317
jl_tvar_t *tv = (jl_tvar_t*)jl_gc_alloc(ptls, sizeof(jl_tvar_t), jl_tvar_type);
314318
tv->name = name;
@@ -319,6 +323,8 @@ JL_DLLEXPORT jl_tvar_t *jl_new_typevar(jl_sym_t *name, jl_value_t *lb, jl_value_
319323

320324
JL_DLLEXPORT jl_value_t *jl_type_unionall(jl_tvar_t *v, jl_value_t *body)
321325
{
326+
if (!jl_is_type(body) && !jl_is_typevar(body))
327+
jl_type_error_rt("UnionAll", "", (jl_value_t*)jl_type_type, body);
322328
// normalize `T where T<:S` => S
323329
if (body == (jl_value_t*)v)
324330
return v->ub;

test/core.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ f47{T}(x::Vector{Vector{T}}) = 0
1010
@test_throws MethodError f47(Array{Vector}(0))
1111
@test f47(Array{Vector{Int}}(0)) == 0
1212

13+
# checking unionall and typevar components
14+
@test_throws TypeError ([] where T)
15+
@test_throws TypeError ([T] where T)
16+
@test_throws TypeError (Array{T} where T<:[])
17+
@test_throws TypeError (Array{T} where T>:[])
18+
1319
# issue #8652
1420
args_morespecific(a, b) = ccall(:jl_type_morespecific, Cint, (Any,Any), a, b) != 0
1521
let

0 commit comments

Comments
 (0)