Skip to content

Commit 6cb4da0

Browse files
authored
Merge pull request #20092 from JuliaLang/jb/check_unionall
fixes for WeakKeyDict, and some other error checks
2 parents a6cde76 + 1e12a48 commit 6cb4da0

File tree

5 files changed

+28
-7
lines changed

5 files changed

+28
-7
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)

base/weakkeydict.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ function WeakKeyDict(kv)
5555
try
5656
Base.associative_with_eltype((K, V) -> WeakKeyDict{K, V}, kv, eltype(kv))
5757
catch e
58-
if any(x->isempty(methods(x, (typeof(kv),))), [start, next, done]) ||
59-
!all(x->isa(x,Union{Tuple,Pair}),kv)
58+
if !applicable(start, kv) || !all(x->isa(x,Union{Tuple,Pair}),kv)
6059
throw(ArgumentError("WeakKeyDict(kv): kv needs to be an iterator of tuples or pairs"))
6160
else
6261
rethrow(e)

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

test/dict.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ d = Dict(:a=>"a")
443443
@test_throws ArgumentError Dict([1])
444444
@test_throws ArgumentError Dict([(1,2),0])
445445

446+
# test Dict constructor's argument checking (for an iterable of pairs or tuples)
447+
# make sure other errors can propagate when the nature of the iterator is not the problem
448+
@test_throws InexactError Dict(convert(Int,1.5) for i=1:1)
449+
@test_throws InexactError WeakKeyDict(convert(Int,1.5) for i=1:1)
450+
446451
# ImmutableDict
447452
import Base.ImmutableDict
448453
let d = ImmutableDict{String, String}(),

0 commit comments

Comments
 (0)